/**
 * @author Rodrigo de Mac�do Ferreira <wrodrigo.macedo@msn.com>
 * @since <2010-11-18>
 */

//add to jQuery

var wrgalleryFunction = {
	
	wrgWait: false,
	wrgSetInterval: null,
	
	strWrgEvent: 'click', //default
	intWrgSpeedTransition: 1000, //default
	intWrgChangeTime: 4000, //default
	
	//events
	evtOnStartChange: null,
	
	initialize: function(objConfig){
		//create a new object if its not defined
		if(objConfig == undefined){
			objConfig = {};
		}
		
		
		//
		//VALIDATE THE CONFIG
		//
		
		
		//validating the event attribute
		if(objConfig.event != undefined){
			var arrValidEvents = ['click','mouseover','mouseout','release'];
			for(var i = 0 ; i < arrValidEvents.length ; i++){
				if(arrValidEvents[i] == objConfig.event){
					this.strWrgEvent = objConfig.event;
					break;
				}
			}
		}
		
		if(objConfig.speedTransition != undefined){
			this.intWrgSpeedTransition = objConfig.speedTransition;
		}
		
		
		if(objConfig.changeTime != undefined){
			this.intWrgChangeTime = objConfig.changeTime;
		}
		
		if(typeof objConfig.onStartChange == 'function'){
			this.evtOnStartChange = objConfig.onStartChange;
		}
		
		
		//
		// ADDING THE EVENTS
		//
		
		
		//get the number of imgs 
		var qtdImgs = $('ul.wrgallery_images li').length;
		
		for(var i = 0 ; i < qtdImgs ; i++){
			//add the events to buttons
			eval("" + 
				"$('.wrgallery_" + i + "_btn').live('" + this.strWrgEvent + "',function(){"+
					"if(($('ul.wrgallery_images li.wrgallery_" + i + "_img:last').index() != " + qtdImgs + " -1) && (wrgalleryFunction.wrgWait == false)){"+
						"wrgalleryFunction.wrgWait = true;"+
						
						//verify if exist any event associated
						"if(wrgalleryFunction.evtOnStartChange != null){"+
							"wrgalleryFunction.evtOnStartChange(" + i + ", $('ul.wrgallery_images li:last').attr('className').substr(10,1));"+
						"}"+
						
						"$('li.wrgallery_" + i + "_img').insertBefore($('ul.wrgallery_images li:last'));"+
						"$('ul.wrgallery_images li:last').animate({opacity: 0}," + this.intWrgSpeedTransition + ",'linear',function(){" +
							"$('ul.wrgallery_images li:last').insertBefore($('li.wrgallery_" + i + "_img'));" +
							"$('ul.wrgallery_images li:last').prev().css({opacity:1});" +
							"wrgalleryFunction.wrgWait = false;"+
						"});"+
					"}"+
					"clearTimeout(wrgalleryFunction.wrgSetInterval);"+
					"wrgalleryFunction.wrRunGallery(true);"+
				"});"
			);
		}
		
		//add event to start the gallery
		$('.wrgallery_images, #cortina').live('mouseover',function(){
			wrgalleryFunction.wrRunGallery(false);
		});
		
		//add event to stop the gallery
		$('.wrgallery_images, #cortina').live('mouseout',function(){
			wrgalleryFunction.wrRunGallery(true);
		});
		
		this.wrRunGallery(true);
	},
	
	
	wrGallery_nextImg: function(){
		if(this.wrgWait == false){
			this.wrgWait = true;
			
			//verify if exist any event associated
			if(this.evtOnStartChange != null){
				this.evtOnStartChange($('ul.wrgallery_images li:last').prev().attr('className').substr(10,1), $('ul.wrgallery_images li:last').attr('className').substr(10,1));
			}
			
			$('ul.wrgallery_images li:last').animate({opacity: 0}, this.intWrgSpeedTransition,'linear',function(){
				$('ul.wrgallery_images li:last').insertBefore($('ul.wrgallery_images li:first'));
				$('ul.wrgallery_images li:first').css({opacity:1});
				clearTimeout(wrgalleryFunction.wrgSetInterval);
				wrgalleryFunction.wrgWait = false;
				wrgalleryFunction.wrRunGallery(true);
			});
		}
		
	},
	
	wrGallery_prevImg: function(){
		if(this.wrgWait == false){
			this.wrgWait = true;
			
			//verify if exist any event associated
			if(this.evtOnStartChange != null){
				this.evtOnStartChange($('ul.wrgallery_images li:last').prev().attr('className').substr(10,1), $('ul.wrgallery_images li:last').attr('className').substr(10,1));
			}
			
			$('ul.wrgallery_images li:first').insertBefore($('ul.wrgallery_images li:last'));
			$('ul.wrgallery_images li:last').animate({opacity: 0}, this.intWrgSpeedTransition,'linear',function(){
				$('ul.wrgallery_images li:last').insertBefore($('ul.wrgallery_images li:last').prev());
				$('ul.wrgallery_images li:last').prev().css({opacity:1});
				clearTimeout(wrgalleryFunction.wrgSetInterval);
				wrgalleryFunction.wrgWait = false;
				wrgalleryFunction.wrRunGallery(true);
			});
		}
		
	},
	
	
	wrRunGallery: function(bolRun){
		if(bolRun){
			//wrgSetInterval = setInterval("this.wrGallery_nextImg();",this.intWrgChangeTime);
			this.wrgSetInterval = setInterval("wrgalleryFunction.wrGallery_nextImg();",this.intWrgChangeTime);
		}else{
			clearTimeout(this.wrgSetInterval);
		}
	}
};

//add to jQuery
$.fn.wrgallery = function(objConfig){
	//initialize process
	wrgalleryFunction.initialize(objConfig);
};
