//getAbsoluteScreensize necessária, Ajax necessária
document.write("<link rel = 'stylesheet' type = 'text/css' href = 'source/script/DVPhotoGalery/DVPhotoGalery.css' />");

//criar classe Screen, com dimensões e posicionamentos
function DVPhotoGalery(_this)
{	
	this._idgal = _this;
	this._area = 0;
	if(arguments.length>1)
		this._area = arguments[1];
	arguments[0] = this;
	this.obj = null;
	this.screen = new getAbsoluteScreenSize();
	this.photos = {idg:null,qt:0,photos:[]};
	this.speed ={interval:6,time:10};
	
	this.constructor = function()
	{
		this.mask = new DVDarkMask();
		this.mask.setOpacity(0.9);

		this.obj = document.createElement('div'); // container box
		this.obj.className = 'DVPhotoGalery';
		this.setSize(905,550);
		this.setOpacity(this.obj,0.94);
		document.body.appendChild(this.obj);
		this.obj.style.left = (this.screen.getWidth()/2 - this.obj.clientWidth/2)+'px';
		this.obj.style.top = (this.screen.getHeight()/2 - this.obj.clientHeight/2)+'px';



		var a = new Ajax("source/script/DVPhotoGalery/DVPhotoGalery.php");
		a.execInside = function()
		{
			var x = (document.PG._idgal != null) ? document.PG._idgal : gE('pg_cats').getElementsByTagName('li')[0].getAttribute('_id');
			
			document.PG.filme = document.getElementById('filme');
			document.PG.rolo = document.getElementById('rolo');
			document.PG.loadGalery.selected = {old:null,act:null};
			document.PG.changePhoto.selected = {old:null,act:null,id:0};
			document.PG.loadGaleryEffect(x);
			document.PG.slideShow.timer = 3;
			document.getElementById('stimercount').firstChild.nodeValue = '0'+document.PG.slideShow.timer+'s';
		}
		a.vars.add(new Array('area',this._area));
		a.ajaxRetriev(this.obj);
		
		this.obj.style.visibility='visible';
	}
	
	this.setOpacity = function(obj,val)
	{
		var o = document.ISGECKO ? val : (val*100);
		document.ISGECKO ? obj.style.MozOpacity = o : obj.style.filter = 'alpha(opacity='+o+')';
	}

	this.setSize = function(w,h)
	{
		this.obj.style.width = w+'px';
		this.obj.style.height = h+'px';
	}
	//-----------------------------------------------------------------------------------------------
	this.choosePhotoEffect = function(id)
	{
		var func = function(){_this.changePhoto(id)}
		this.fadeEffectReverse(document.getElementById('_photo'),func);
	}
	//-----------------------------------------------------------------------------------------------
	this.changePhoto = function(id)
	{
		//criar método p/ opacity geral (obj,grau)
		if(document.PG.changePhoto.selected.act==null)
			document.PG.changePhoto.selected = {old:document.getElementById('filme').getElementsByTagName('a')[0],act:document.getElementById('filme').getElementsByTagName('a')[0],id:id};
		
		document.PG.changePhoto.selected.old.className = '';
		document.PG.changePhoto.selected.act.className = 'mselected';//.className = 'selected';
		document.getElementById('slideshow').firstChild.nodeValue = ((document.PG.changePhoto.selected.id+1 < 10) ? ('0'+(document.PG.changePhoto.selected.id+1)) : document.PG.changePhoto.selected.id+1) + '/'+((document.PG.photos.qt+1 < 10) ? ('0'+(document.PG.photos.qt+1)) : document.PG.photos.qt);
		this.setOpacity(document.getElementById('_photo'),0);
		document.getElementById('credits').firstChild.nodeValue = document.PG.photos.photo[id]._CREDITO;
		document.getElementById('legenda').firstChild.nodeValue = document.PG.photos.photo[id]._LEGENDA;
		document.getElementById('_photo').src=document.PG.photos.photo[id]._PHOTO.src;
		this.fadeEffect(document.getElementById('_photo'));
	}
	//-----------------------------------------------------------------------------------------------
	this.fadeEffect = function(obj)
	{
		//a imagem deve ser carregada por completo antes do fadeeffect
		try{this.fadeEffect.stop()}catch(err){}
		
		this.setOpacity(obj,0);
		this.fadeEffect.counter = 0;
		var timer = function()
		{
			if(_this.fadeEffect.counter < 1)
				_this.setOpacity(_this.fadeEffect.obj,_this.fadeEffect.counter+=0.09);
			else	
				_this.fadeEffect.stop();
		}
		this.fadeEffect.obj = obj;
		this.fadeEffect.stop = function(){clearInterval(_this.fadeEffect.start)}
		this.fadeEffect.start = setInterval(timer,10);
	}
	//-----------------------------------------------------------------------------------------------
	//load galery deve estar dentro de fadeeffect
	this.fadeEffectReverse = function(obj,func)
	{
		//a imagem deve ser carregada por completo antes do fadeeffect
		try{this.fadeEffectReverse.stop()}catch(err){}
		
		this.fadeEffectReverse.counter = 1;
		
		this.fadeEffectReverse.execInside = func;
		
		var timer = function()
		{
			if(_this.fadeEffectReverse.counter > 0)
				_this.setOpacity(_this.fadeEffectReverse.obj,_this.fadeEffectReverse.counter-=0.12);
			else	
			{
				_this.fadeEffectReverse.execInside();
				_this.fadeEffectReverse.stop();
				//_this.fadeEffect(_this.fadeEffectReverse.obj);		
			}
		}
		this.fadeEffectReverse.obj = obj;
		this.fadeEffectReverse.stop = function(){clearInterval(_this.fadeEffectReverse.start)}
		this.fadeEffectReverse.start = setInterval(timer,10);
	}
	//-----------------------------------------------------------------------------------------------
	this.loadGaleryEffect = function(id)
	{
		if(document.PG.loadGalery.selected.act==null)
			document.PG.loadGalery.selected = {old:gE('cat'+id),act:gE('cat'+id)};

		document.PG.loadGalery.selected.old.className = '';
		document.PG.loadGalery.selected.act.className = 'selected';

		var func = function()
		{
			_this.loadGalery(id);
		}
		this.fadeEffectReverse(document.getElementById('_photo'),func);
	}
	//-----------------------------------------------------------------------------------------------
	this.loadGalery = function(id)
	{
		try
		{
			this.slideShow.stop();
			this.fadeEffect.stop();
			this.fadeEffectReverse.stop();
		}
		catch(err)
		{
			//document.title = "loadgal -"+err;
		}

		document.getElementById('_photo').src = "source/script/DVPhotoGalery/images/loading.gif";
		document.PG.filme.style.visibility='hidden';
		var a = new Ajax("source/script/DVPhotoGalery/loadgalery.php");
		a.execInside = function()
		{
			if(wr.getElementsByTagName('XML')[0].getAttribute('data'))
			{
				var photos = wr.getElementsByTagName('PHOTO');
				document.PG.photos.idg = photos[0].getAttribute('gal');
				document.PG.photos.photo = [];
				document.PG.photos.qt = photos.length-1;
				for(var i = 0; i< photos.length; i++)
				{
					
					document.PG.filme.innerHTML = (!i) ? "<li><a  title = '"+photos[i].getElementsByTagName('XLEGENDA')[0].firstChild.nodeValue+"' onmouseover = \"this.href = '#'\" onclick = \"document.PG.changePhoto.selected = {old:document.PG.changePhoto.selected.act,act:this,id:"+0+"}; document.PG.choosePhotoEffect("+(i)+")\" style = 'background:transparent url(intranet/source/calls/mods/cads/fots/photos/"+document.PG.photos.idg+"/thumbs/"+(photos[i].getElementsByTagName('XPHOTO')[0].firstChild.nodeValue)+")'>"+(i+1)+"</a></li>" : document.PG.filme.innerHTML+"<li><a title = '"+photos[i].getElementsByTagName('XLEGENDA')[0].firstChild.nodeValue+"' onmouseover = \"this.href = '#'\" onclick = \"document.PG.changePhoto.selected = {old:document.PG.changePhoto.selected.act,act:this,id:"+(i)+"}; document.PG.choosePhotoEffect("+(i)+")\" style = 'background:transparent url(intranet/source/calls/mods/cads/fots/photos/"+document.PG.photos.idg+"/thumbs/"+(photos[i].getElementsByTagName('XPHOTO')[0].firstChild.nodeValue)+")'>"+(i+1)+"</a></li>";
					//colocar um loading neste ponto
					document.PG.photos.photo[i] = 
					{
						_ID: photos[i].getElementsByTagName('XID')[0].firstChild.nodeValue,
						_LEGENDA: photos[i].getElementsByTagName('XLEGENDA')[0].firstChild.nodeValue.replace("$$",""),
						_CREDITO: photos[i].getElementsByTagName('XCREDITO')[0].firstChild.nodeValue.replace("$$",""),
						_PHOTO:new Image(),
						_PATH:photos[i].getElementsByTagName('XPHOTO')[0].firstChild.nodeValue,
						_DTCAD: photos[i].getElementsByTagName('XDTCAD')[0].firstChild.nodeValue
					}
					document.PG.photos.photo[i]._PHOTO.src = "intranet/source/calls/mods/cads/fots/photos/"+document.PG.photos.idg+"/"+document.PG.photos.photo[i]._PATH;
					//alert(document.PG.photos.photo[i]._PHOTO.src);
				}
				
				document.PG.filme.style.width = (photos.length*76)+(2)+'px';
				document.PG.filme.style.marginLeft=0;
				var timer = function()
				{
					//fica até a última ser carregada;
					//if(document.PG.photos.photo[document.PG.photos.qt]._PHOTO.complete)
					//{
						document.PG.changePhoto.selected = {old:document.getElementById('filme').getElementsByTagName('a')[0],act:document.getElementById('filme').getElementsByTagName('a')[0],id:0};
						document.PG.changePhoto(0);
						document.PG.loadGalery.stop();
						document.PG.filme.style.visibility='visible';
					//}
					//else
					//{
						//document.PG.setOpacity(document.getElementById('_photo'),0.94);
						//document.PG.filme.style.visibility='visible';
					//}
				}
				document.PG.loadGalery.start = setInterval(timer,50);
				document.PG.loadGalery.stop = function()
				{
					clearInterval(document.PG.loadGalery.start)
				}

				
			}
		}
		a.vars.add(new Array('idg',id));
		a.ajaxRetrievXML();
	}
	//Controle
	//-------------------------------------------------------------------------------------------------
	this.moveLeft = function()
	{
		if(!this.moveLeft.status)
		{
			var timer = function()
			{
				_this.moveLeft.status = true;
				
				if(_this.filme.clientWidth - (_this.filme.style.marginLeft.substr(0,_this.filme.style.marginLeft.length-2)*-1) > _this.rolo.clientWidth)
					_this.filme.style.marginLeft = (_this.filme.style.marginLeft.substr(0,_this.filme.style.marginLeft.length-2)) - _this.speed.interval+'px';
			}
			this.moveLeft.start = setInterval(timer,this.speed.time); 

			this.moveLeft.stop = function()
			{
				_this.moveLeft.status = false;
				clearInterval(_this.moveLeft.start);
			}
		}
	}

	this.moveRight = function()
	{
		if(!this.moveRight.status)
		{
			var timer = function()
			{
				_this.moveRight.status = true;
				
				if((_this.filme.style.marginLeft.substr(0,_this.filme.style.marginLeft.length-2)) < 0)
				_this.filme.style.marginLeft = parseInt((_this.filme.style.marginLeft.substr(0,_this.filme.style.marginLeft.length-2))) + _this.speed.interval+'px';
			}
			this.moveRight.start = setInterval(timer,this.speed.time); 

			this.moveRight.stop = function()
			{
				_this.moveRight.status = false;
				clearInterval(_this.moveRight.start);
			}
		}
	}


	this.nextPhoto = function()
	{
		if(document.PG.changePhoto.selected.id < document.PG.photos.qt)
		{
			document.PG.changePhoto.selected = {old:document.PG.changePhoto.selected.act,act:_this.filme.getElementsByTagName('a')[document.PG.changePhoto.selected.id+1],id:document.PG.changePhoto.selected.id+1};
			document.PG.choosePhotoEffect(document.PG.changePhoto.selected.id);
		}
	}
	//-------------------------------------------------------------------------------------------------
	this.previousPhoto = function()
	{
		if(document.PG.changePhoto.selected.id > 0)
		{
			document.PG.changePhoto.selected = {old:document.PG.changePhoto.selected.act,act:_this.filme.getElementsByTagName('a')[document.PG.changePhoto.selected.id-1],id:document.PG.changePhoto.selected.id-1};
			document.PG.choosePhotoEffect(document.PG.changePhoto.selected.id);
		}
	}

	//-------------------------------------------------------------------------------------------------
	this.slideShow = function()
	{
		document.getElementById('spsp').className = 'spause';
		document.getElementById('spsp').onclick = function(){_this.slideShow.stop()}
		document.getElementById('spsp').setAttribute('title','pausar apresentação');
		document.getElementById('stimercount').firstChild.nodeValue = '0'+this.slideShow.timer+'s';

		var timer = function()
		{
			if(document.PG.changePhoto.selected.id < document.PG.photos.qt)
			{
				//_this.filme.style.marginLeft = '-'+(document.PG.changePhoto.selected.id_1*40)+'px';
				document.PG.changePhoto.selected = {old:document.PG.changePhoto.selected.act,act:_this.filme.getElementsByTagName('a')[document.PG.changePhoto.selected.id+1],id:document.PG.changePhoto.selected.id+1};
				document.PG.choosePhotoEffect(document.PG.changePhoto.selected.id);
			}
			else	
				_this.slideShow.stop();
		}
		this.slideShow.start = setInterval(timer,this.slideShow.timer*1000); 

		this.slideShow.stop = function()
		{
			clearInterval(_this.slideShow.start);
			document.getElementById('spsp').className = 'splay';
			document.getElementById('spsp').onclick = function(){_this.slideShow()}
			document.getElementById('spsp').setAttribute('title','iniciar apresentação');
		}
	}
	
	this.changeTimeUp = function()
	{
		if(this.slideShow.timer<9)
		{
			this.slideShow.timer++;
			document.getElementById('stimercount').firstChild.nodeValue = '0'+this.slideShow.timer+'s';
			this.slideShow.stop();
			this.slideShow();
		}
		else
			alert("o tempo máximo entre slides é de 9 segundos")
	}
	
	this.changeTimeDown = function()
	{
		if(this.slideShow.timer>2)
		{
			this.slideShow.timer--;
			document.getElementById('stimercount').firstChild.nodeValue = '0'+this.slideShow.timer+'s';
		}
		else
			alert("o tempo mínimo entre slides é de 2 segundos")
	}

	this.closeGallery = function()
	{
		try
		{
			this.slideShow.stop();
			this.fadeEffect.stop();
			this.fadeEffectReverse.stop();
		}
		catch(err)
		{
			//document.title = "closegal -"+err;
		
		}
		finally
		{
			document.body.removeChild(this.obj);
			this.mask.removeMask();
		}
	}

	this.constructor();
}
