/* msvPreloadImg plugin */
$.fn.msvPreloadImg = function() { // preload Image
	return this.each(function(){
				
		var aImg = $(this),
			aSrc = $(this).attr('src'),
			oLoading = $(this).parent().find('.load'),
			timedDid = false,
			timed = 0;

		var showImg = function(){
			oLoading.css({'display':'none'});
			if(app.ie){
				aImg.css({'display':'block'});
			} else {
				aImg.fadeIn(function(){
					aImg.fadeTo(10,1).show();
				});
			}
		}
		var imgsLoaded = function (domObject){
			if(!timedDid) {
				clearTimeout(timed);
				showImg();
			}
		}
		var selLoaded = function (domObject){showImg();}		
		var timed = setTimeout(function(){
			timedDid = true;
			clearTimeout(timed);
			showImg();
		}, 10000);
		
		aImg.attr('src',aSrc);

		aImg.onImagesLoad({
			itemCallback: imgsLoaded,
			selectorCallback: selLoaded,
			callbackIfNoImagesExist: true
		});
		if(app.ie) { // ie double check
			aImg.onImagesLoad({
				itemCallback: imgsLoaded,
				selectorCallback: selLoaded,
				callbackIfNoImagesExist: true
			});
		}
	});
}
/* msvSlider1 plugin */
$.msvSlider1 = function(elem, options) {
	var defaults = {
		autoAnim:	true,
		nav:		true,
		interval:	9000,
		easeDur:	800,
		liWidth:	300,
		wait:		0,
		easeFunc:	'easeInOutExpo'
	}
	var plugin = this;
	plugin.opcoes = {}
	var elem = $(elem), cont, count, contW, index, offset, btnEsq, btnDir, sto;
	plugin.init = function() {
		plugin.opcoes = $.extend({}, defaults, options);
		/* init */
		if(plugin.opcoes.nav){
			btnEsq = elem.find('.btnLeft');
			btnDir = elem.find('.btnRight');		
		}
		cont = elem.find('ul');
		count =	cont.find('li').size();
		contW = plugin.opcoes.liWidth*count;
		cont.css({width: contW});		
		index = 1;
		if(count>1){
			setTimeout(function(){	
				if (plugin.opcoes.autoAnim) plugin.initTimer();
				if(plugin.opcoes.nav){		
					btnEsq.show();
					btnDir.show();
					bindBtns();
				}				
			},plugin.opcoes.wait);
		}
	}
	var bindBtns = function() {
		btnEsq.bind('click',function(){
			unbindBtns();
			plugin.delTimer();
			if (index == 1) {
				offset = - Number(plugin.opcoes.liWidth*(count - 1));
				index = count;
			} else {
				index -= 1;
				offset = - Number(plugin.opcoes.liWidth*(index - 1));
			};
			anima();
			return false;
		});
		btnDir.bind('click',function(){
			unbindBtns();			
			plugin.delTimer();
			if (index == count) {
				offset = 0;
				index = 1;
			} else {
				offset = - Number(plugin.opcoes.liWidth*index);
				index += 1;
			};
			anima();
			return false;
		});		
	}
	var unbindBtns = function() {
		btnEsq.unbind('click');
		btnDir.unbind('click');
	};
	var anima = function() {
		if(plugin.opcoes.nav) unbindBtns();
		cont.stop().animate({ marginLeft: offset }, plugin.opcoes.easeDur, plugin.opcoes.easeFunc);					
		setTimeout(function(){
			if(plugin.opcoes.nav) bindBtns();
			plugin.initTimer();
		},plugin.opcoes.easeDur);
	}	
	var autoSlide = function() {
		if (index == count) {
			offset = 0;
			index = 1;
		} else {
			offset = - Number(plugin.opcoes.liWidth*index);
			index += 1;
		};
		anima();
	}
	plugin.initTimer = function() { // public
		sto = setTimeout(autoSlide,plugin.opcoes.interval);
	}	
	plugin.delTimer = function() { // public
		clearTimeout(sto);
	}
	plugin.init(); // init	
}
$.fn.msvSlider1 = function(options) {
	return this.each(function() {
		if (undefined == $(this).data('msvSlider1')) {
			var plugin = new $.msvSlider1(this, options);
			$(this).data('msvSlider1', plugin);
		}
	});
}
/* msvSlider2 plugin */
$.msvSlider2 = function(elem, options) {
	var defaults = {
		interval:	6000,
		easeDur:	800,
		liHeight:	40,
		wait:		0,
		easeFunc:	'easeInOutExpo'
	}
	var plugin = this;
	plugin.opcoes = {}
	var cont = $(elem), count, contH, index, offset, sto;
	plugin.init = function() {
		plugin.opcoes = $.extend({}, defaults, options);
		/* init */
		count =	cont.find('li').size();			
		contH = plugin.opcoes.liHeight*count;
		cont.css({height: contH});
		index = 1;
		if(count>1)	{
			var li1 = cont.find('li:first');
			cont.append(li1.clone());
			setTimeout(function(){
				cont.find('li:last').removeAttr('id');
				plugin.initTimer();				
			},100);
		}
	}
	var anima = function() {
		cont.stop().animate({ top: offset }, plugin.opcoes.easeDur, plugin.opcoes.easeFunc);					
		setTimeout(function(){
			if (index > count) {
				index = 1;
				cont.stop().css({top:0});
			}			
			plugin.initTimer();
		},plugin.opcoes.easeDur);
	}	
	var autoSlide = function() {
		offset = - Number(plugin.opcoes.liHeight*index);
		index += 1;
		anima();
	}
	plugin.initTimer = function() { // public
		sto = setTimeout(autoSlide,plugin.opcoes.interval);
	}	
	plugin.delTimer = function() { // public
		clearTimeout(sto);
	}
	plugin.init(); // init	
}
$.fn.msvSlider2 = function(options) {
	return this.each(function() {
		if (undefined == $(this).data('msvSlider2')) {
			var plugin = new $.msvSlider2(this, options);
			$(this).data('msvSlider2', plugin);
		}
	});
}
/* msvSlider3 plugin */
$.msvSlider3 = function(elem, options) {
	var defaults = {
		easeDur:	300,
		liHeight:	97,
		easeFunc:	'easeInOutExpo'
	}
	var plugin = this;
	plugin.opcoes = {}
	var elem = $(elem), cont, count, contH, index, offset, btnUp, btnDown;
	plugin.init = function() {
		plugin.opcoes = $.extend({}, defaults, options);
		/* init */
		btnUp = elem.parent().find('.btnUp');
		btnDown = elem.parent().find('.btnDown');		
		cont = elem.find('ul');
		count =	cont.find('li').size();
		contH = plugin.opcoes.liHeight*count;
		cont.css({height: contH});
 		if(count>5){
			index = 1;
			verif();    
		} else {
			btnUp.addClass('off');
			btnDown.addClass('off');
		}
	}
	var bindBtns = function() {
		btnUp.bind('click',function(){
			if(!btnUp.hasClass('off')){
				unbindBtns();
				index -= 1;
				anima();
			}
			return false;
		});
		btnDown.bind('click',function(){
			if(!btnDown.hasClass('off')){
				unbindBtns();
				index += 1;
				anima();
			}
			return false;
		});		
	}
	var unbindBtns = function() {
		btnUp.unbind('click');
		btnDown.unbind('click');
	};
 	var verif = function() {
          if(index==1) {
            btnUp.addClass('off');
          } else {
            btnUp.removeClass('off');
          }
          if(index==Number(count-4)) {
            btnDown.addClass('off');
          } else {
            btnDown.removeClass('off');
          }
		  bindBtns();       
	}
	var anima = function() {
		offset = - Number(plugin.opcoes.liHeight*(index - 1));
		cont.stop().animate({ top: offset }, plugin.opcoes.easeDur, plugin.opcoes.easeFunc);					
		setTimeout(function(){
			verif();
		},plugin.opcoes.easeDur);
	}	
	plugin.init(); // init	
}
$.fn.msvSlider3 = function(options) {
	return this.each(function() {
		if (undefined == $(this).data('msvSlider3')) {
			var plugin = new $.msvSlider3(this, options);
			$(this).data('msvSlider3', plugin);
		}
	});
}
