/*
AdGaleriaImagem - Galeria de imagens
AdSwapImage - Componente AdGaleriaImagem
Autor: Bruno Possidonio Oliveira
*/
var AdGaleriaImagem = function(el,carouselInstance) {
	if (!el){el="AdSwapImgComponent";}
	if (!carouselInstance || carouselInstance == "undefined"){carouselInstance=false;}
	this.init(el,carouselInstance);
};
AdGaleriaImagem.prototype = {
	component: {
		main:false,
		thumbnailContainer:'AdSwapImgThumbnailContainer',
		thumbnailContainerItem:'AdSwapImgThumbnailContainerItem',
		thumbnailObj:'AdSwapImgThumbnailObj',
		controlNext:'AdSwapControlNext',
		controlPrev:'AdSwapControlPrevious'
	},

	/**
     * Construtor
     * @method init
     * @param el
     */
     init: function(el,carouselInstance){
     	this.component.main = YAHOO.util.Dom.get(el);
     	if(!this.component) {
     		return false;
     	}
     	try {
	     	this.component.thumbnailContainer = YAHOO.util.Dom.get(this.component.thumbnailContainer);
	     	this.component.thumbnailObj = YAHOO.util.Dom.getElementsByClassName(this.component.thumbnailObj,'a',this.component.thumbnailContainer);
	     	
	     	this.component.controlNext = YAHOO.util.Dom.get(this.component.controlNext);
	     	this.component.controlPrev = YAHOO.util.Dom.get(this.component.controlPrev);
	     	
	     	AdSwapControl.carouselComponent=carouselInstance;
     	} catch(e){alert(e);return false;}
     	
     	this.setThumbnailBehaviors();
     	this.setControlBehaviors();
     	
     	var AdSwapImage = new AdSwap(AdSwapControl.firstIndex());
     },
    
    /**
     * Define comportamento dos thumbnails
     * @method setThumbnailBehaviors
     */
     setThumbnailBehaviors: function() {
     	for (i in this.component.thumbnailObj) {
   			t = this.component.thumbnailObj[i];
   			AdSwapControl.addIndex(i,t);
   			
   			t.onclick=function(){return false;}; // PARA NAVEGACAO
   			YAHOO.util.Event.addListener(t.firstChild, 'click', function(event){var AdSwapImage = new AdSwap(YAHOO.util.Event.getTarget(event));});
   		}
   		
     },
    
    /**
     * Define comportamento dos controles da galeria
     * @method setControlBehaviors
     */
     setControlBehaviors: function() {
     	if (!YAHOO.lang.isUndefined(this.component.controlNext)) {
     		this.component.controlNext.onclick=function(){return false;}
     		YAHOO.util.Event.addListener(this.component.controlNext, 'click', function(event){
     			var AdSwapImage = new AdSwap(AdSwapControl.nextIndex());
     		});
     	}

		if (!YAHOO.lang.isUndefined(this.component.controlPrev)) {
     		this.component.controlPrev.onclick=function(){return false;}
     		YAHOO.util.Event.addListener(this.component.controlPrev, 'click', function(event){
     			var AdSwapImage = new AdSwap(AdSwapControl.previousIndex());
     		});
     	}
     }
};

var AdSwap = function(objeto){this.swap(objeto);}
AdSwap.prototype = {
	options: {
		toString:function(){alert(this.el.nodeName+"\n"+this.title);},
		swapImageShowingClass:'AdSwapImgShowing',
		swapImageTitleObject:'AdSwapImgTitle',
		swapImageDescriptionObject:'AdSwapImgDescription',
		swapImageDescriptionObject:'AdSwapImgDescription',
		swapImagePortViewObject:'AdSwapImgPortView',
		swapImageIDs:['adFormSelecaoFotoConviteTurmaCdFoto','adFormSelecaoFotoConviteIndividualCdFoto'],
		carouselComponentEnabled:false
	},
	
	component:{
		swapImageTitleObject:new Object(), // OBJETO QUE RECEBER� A STRING COM O T�TULO DA IMAGEM
		swapImagePortViewObject:new Object(), // IMAGEM DE VISUALIZACAO. RECEBERA O SRC DA IMAGEM
		swapImageShowingObject:new Object(), // OBJETO QUE DISPAROU O EVENTO.
		carouselComponent:false
	},
	
	swap: function(objeto) {
		if (objeto == false)
			return false;
		
		this.component.swapImageTitleObject = YAHOO.util.Dom.get(this.options.swapImageTitleObject);
		this.component.swapImageDescriptionObject = YAHOO.util.Dom.get(this.options.swapImageDescriptionObject);
		this.component.swapImagePortViewObject = YAHOO.util.Dom.get(this.options.swapImagePortViewObject);
		this.component.swapImageShowingObject = objeto;
		
		this._swapImage();
		this._swapTitle();
		//this._swapDescription();
		this._swapShowingClass();
		this._swapObjectID();
				
		AdSwapControl.setShowing(objeto);
	},
	
	_swapImage: function(){
		this.component.swapImagePortViewObject.src="";
		this.component.swapImagePortViewObject.src=this.component.swapImageShowingObject.parentNode.href;
		
		var portViewObject = new YAHOO.util.Anim(this.component.swapImagePortViewObject,{opacity:{from:.5,to:1}},.5,YAHOO.util.Easing.easeIn);
		portViewObject.animate();
	},
	_swapTitle: function(){
		this.component.swapImageTitleObject.innerHTML=this.component.swapImageShowingObject.title;
	},
	_swapDescription: function(){
		this.component.swapImageDescriptionObject.innerHTML=this.component.swapImageShowingObject.alt;
	},
	_swapShowingClass: function(){
		this._resetShowingClass();
		this.component.swapImageShowingObject.parentNode.className+=" "+this.options.swapImageShowingClass;
	},
	_resetShowingClass: function(){
		o = YAHOO.util.Dom.getElementsByClassName(this.options.swapImageShowingClass);
		for (oi in o) {
			o[oi].className = o[oi].className.replace(this.options.swapImageShowingClass,"");
		}
	},
	_swapObjectID: function(){
		for (i in this.options.swapImageIDs) {
			try{
				document.getElementById(this.options.swapImageIDs[i]).value=this.component.swapImageShowingObject.name;
			}catch(e){}
		}
	}
};

var AdSwapControl = {
	toString:function(){var r="";for(id in AdSwapControl.index){r+="Index "+id+": "+AdSwapControl.index[id].href+"\n"}alert(r);},
	index:[],
	indexShowing:0,
	carouselComponent:false,
	setShowing:function(objeto){
		objeto=objeto.parentNode;
		i=objeto.className.replace(/.*AdSwapIndex-([0-9]+).*/g,"$1");
		
		var previuosAnim = new YAHOO.util.Anim(AdSwapControl.index[AdSwapControl.indexShowing].firstChild,{opacity:{from:1,to:.5}},.5,YAHOO.util.Easing.easeOut);
		previuosAnim.animate();
		
		var showingAnim = new YAHOO.util.Anim(objeto.firstChild,{opacity:{from:.5,to:1}},.5,YAHOO.util.Easing.easeIn);
		showingAnim.animate();
		
		if(i != "null"){
			AdSwapControl.indexShowing=i;
		}
		
		if (AdSwapControl.carouselComponent)
			AdSwapControl.scrollCarouselComponent(objeto);
		
	},
	scrollCarouselComponent: function(objeto){
		try{
			carouselItem = YAHOO.util.Dom.getAncestorByTagName(objeto, 'li');
			carouselItemNo = Math.ceil(parseInt(carouselItem.id.replace(/.*-([0-9]+)/g,"$1"))/5);
			AdSwapControl.carouselComponent.scrollTo(carouselItemNo);
		}catch(e){alert(e);}
	},
	
	addIndex:function(i,t){
		AdSwapControl.index[i]=t;
		t.className+=" AdSwapIndex-"+i
	},
	nextIndex:function() {
		if (AdSwapControl.isLastIndex()) {
			//alert("Voc� est� na �ltima foto.");
			return false;
		}
		return AdSwapControl.index[parseInt(AdSwapControl.indexShowing)+1].firstChild;
	},
	previousIndex:function() {
		if (AdSwapControl.isFirstIndex()) {
			//alert("Voc� est� na primeira foto.");
			return false;
		}
		return AdSwapControl.index[AdSwapControl.indexShowing-1].firstChild;
	},
	firstIndex: function(){return AdSwapControl.index[0].firstChild;},
	lastIndex: function(){return AdSwapControl.index[parseInt(AdSwapControl.index.length)-1].firstChild;},
	isFirstIndex:function(){return AdSwapControl.indexShowing == 0 ? true : false;},
	isLastIndex:function(){return AdSwapControl.indexShowing == AdSwapControl.index.length-1 ? true : false;}
}