var GA = {
	// Versao 5.0
	// By Carlao :) rsrs
	'qtdItensTotal': 0,
	'qtdItensPagina': 4,
	'qtdItensMoved': 0,
	'masc': '',
	'content': '',
	'btAnt': '',
	'btProx': '',
	'hitAnt': '',
	'hitProx': '',
	'btHitAnt': '',
	'btHitProx': '',
	'galeriaID': 0,
	'xmlRet': null,
	'thumb': {
		'w':0,
		'h':0
	},
	'arrImagemG': [],
	'arrLegenda': [],
	'twCreated': false,
	'opened': false,
	'create': function(){
		//alert(this.xmlRet.getCountItens());
		this.qtdItensTotal = this.xmlRet.getCountItens();
		
		if (this.qtdItensTotal <= 0) return;

		new tween('galeria');

		var contThumbs = ""		

		for (var i = 0; i < this.qtdItensTotal; i++){
			this.arrImagemG[i] = this.xmlRet.getAttByName('imagem',i);
			this.arrLegenda[i] = this.xmlRet.getAttByName('legenda',i);
			contThumbs += '<div id="gaThumb'+i+'" class="linkThumbGaleria"><img src="media/'+this.xmlRet.getAttByName('thumb',i)+'" width="96" onclick="GA.loadImg('+i+');" /></div>';
			if(i==0)this.loadImg(0);
		}
		

		//$(this.content).innerHTML = contThumbs;
		//alert(this.content);
		document.getElementById(this.content).innerHTML = contThumbs;
		//document.getElementById('content_thumbs').innerHTML = contThumbs;


		if (this.btAnt){
			document.getElementById(this.btAnt).style.cursor = 'default';
			setAlpha(this.btAnt,30);
			document.getElementById(this.btAnt).onclick = function(){
				GA.backPg();
			}
		}
		
		if (this.btProx){
			document.getElementById(this.btProx).style.cursor = 'default';
			setAlpha(this.btProx,30);
			document.getElementById(this.btProx).onclick = function(){
				GA.nextPg();
			}
		}

		if (this.qtdItensTotal > this.qtdItensPagina){
			if (this.btProx){
				document.getElementById(this.btProx).style.cursor = 'pointer';
				document.getElementById(this.btProx).onclick = function(){
					GA.nextPg();
				}
			}
		}
		
		if(!this.twCreated){
			new tween(this.content);
			this.twCreated = true;
		}

	},
	'nextPg': function(){
		var qtdFora = this.qtdItensTotal - this.qtdItensPagina;
		
		if (qtdFora < 0) return;
		
		if (this.qtdItensMoved < qtdFora){
			document.getElementById(this.btAnt).style.cursor = 'pointer';
			setAlpha(this.btAnt,100);
			
			this.qtdItensMoved += 1;
				
			var maxLeft = -(this.qtdItensMoved * this.thumb.w)	
			
			//$(this.content).tweenToRelative(this.masc, 0, maxLeft, "easeoutexpo", 0.3);
			document.getElementById(this.content).tweenToRelative(this.masc, 0, maxLeft, "easeoutexpo", 0.3);
			
			
			if(this.qtdItensMoved == qtdFora){
				document.getElementById(this.btProx).style.cursor = 'default';
				setAlpha(this.btProx,30);
			}
		}
	},
	'backPg': function(){
		if (this.qtdItensMoved > 0){
			
			document.getElementById(this.btProx).style.cursor = 'pointer';
			setAlpha(this.btProx,100);
			
			var maxAtual = -(this.qtdItensMoved * this.thumb.w);
			
			var maxLeft = maxAtual+(1 * this.thumb.w);
			
			this.qtdItensMoved -= 1;		
		
			document.getElementById(this.content).tweenToRelative(this.masc, 0, maxLeft, "easeoutexpo", 0.3);
			
			if(this.qtdItensMoved == 0){
				document.getElementById(this.btAnt).style.cursor = 'default';
				setAlpha(this.btAnt,30);
			}
		}		
	},

	'tImg':null,
	'loadedImg':false,
	'imgLoad':null,
	'checkLoadImg': function(){
		clearTimeout(this.tImg);
		if(this.loadedImg){
			document.getElementById('fotoG').src = this.imgLoad.src;
			document.getElementById('fotoG').style.display = 'block';
			document.getElementById('fotoLoading').style.display = 'none';
		}else{
			this.tImg = setTimeout("GA.checkLoadImg()",400);
		}
	},
	'loadImg': function(img){
		document.getElementById('fotoG').style.display = 'none';
		document.getElementById('fotoLoading').style.display = 'block';

		this.imgLoad = new Image(96,66);
		this.imgLoad.src = 'media/'+this.arrImagemG[img];
		this.imgLoad.onload = function(){
			GA.loadedImg = true;
		}
		this.checkLoadImg();

		document.getElementById('txtGaleria').innerHTML = this.arrLegenda[img];
	},

	'showBtHitAnt': function(e){
		document.getElementById(this.btHitAnt).style.display='block';
	},
	'hideBtHitAnt': function(e){
		document.getElementById(this.btHitAnt).style.display='none';
	},
	'showBtHitProx': function(e){
		document.getElementById(this.btHitProx).style.display='block';
	},
	'hideBtHitProx': function(e){
		document.getElementById(this.btHitProx).style.display='none';
	},
	
	'show': function(){
		bgSite.show();	
		document.getElementById('galeria').style.display = "block";
		this.open();
	},
	'open': function(){
		if(this.opened) return;
		this.opened = true;
		mensure.load();

		var SC = {x:mensure.posScrollX(), y:mensure.posScrollY()};
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:document.getElementById('galeria').offsetWidth, y:document.getElementById('galeria').offsetHeight};
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-(SZ.y))/2;

		document.getElementById('galeria').style.top = (newY+SC.y)+"px";
		document.getElementById('galeria').style.left = (newX+SC.x)+"px";

		bgSite.alinha();
	},
	'refresh':function(){
		if(!this.opened) return;
		mensure.load();

		var SC = {x:mensure.posScrollX(), y:mensure.posScrollY()};
		var VS = {x:mensure.widthVisible(), y:mensure.heightVisible()};
		var SZ = {x:document.getElementById('galeria').offsetWidth, y:document.getElementById('galeria').offsetHeight};
		newX = (VS.x-SZ.x)/2;
		newY = (VS.y-(SZ.y))/2;

		document.getElementById('galeria').tweenTo((newY+SC.y),(newX+SC.x),'easeoutexpo',0.35,null);

		bgSite.alinha();
	},
	'hide': function(){
		this.opened = false;
		document.getElementById('galeria').style.top = "-1000px";
		document.getElementById('galeria').style.left = "-1000px";
		document.getElementById('galeria').style.display = "none";
		bgSite.hide();
	}
}


/* config galeria */ 
GA.masc = 'masc_thumbs';
GA.content = 'content_thumbs';
GA.btAnt = "btAnt";
GA.btProx = "btProx";
GA.hitAnt = "hitAnt";
GA.hitProx = "hitProx";
GA.btHitAnt = "btHitAnt";
GA.btHitProx = "btHitProx";
GA.thumb.w = 102;
GA.thumb.h = 69;


/*******************************************/
var bgSite = {
	'show': function() {
		setAlpha('bgSite', 80);
		document.getElementById('bgSite').style.height = mensure.heightTotal()+13+'px';
		document.getElementById('bgSite').style.display = 'block';
		hideSelect();
	},
	'hide': function() {
		document.getElementById('bgSite').style.display = 'none';
		showSelect();
	},
	'alinha': function(){
		mensure.load();

		var newY;
		if(mensure.heightVisible() > mensure.heightTotal()){
			newY = mensure.heightVisible();
		}else{
			newY = mensure.heightTotal()+44;
		}
		document.getElementById('bgSite').style.width = mensure.widthVisible()+'px';
		document.getElementById('bgSite').style.height = newY+'px';
	}
};



