/*var ImageExists = function( URL )
{
	var TemporaryImage = new Image();
		TemporaryImage.src = URL;

	if ( TemporaryImage.width > 0 )
		return true;
	else
		return false;
};*/
var Right = function( Variable, Size )
{
	return Variable.substring( Variable.length - Size );
};

var Left = function( Variable, Size )
{
	return Variable.substring( 0, Size );
};

var ClassProductPhotos = function( ProductReference, LimitOfPhotos )
{
	this.ProductReference	= Left( ProductReference, 12 );
	this.LimitOfPhotos		= LimitOfPhotos;
	this.PhotosPath			= this.getPhotosPath();


	this.PhotosDetail		= new Array( this.LimitOfPhotos );
	this.PhotosThumb		= new Array( this.LimitOfPhotos );
	this.PhotosExpansion	= new Array( this.LimitOfPhotos );
	this.PhotosZoom			= new Array( this.LimitOfPhotos );

	this.Showcase			= document.getElementById( "Thumbs" );
};

ClassProductPhotos.prototype.getPhotosPath = function()
{
	return "FILES/_FOTOS/imagens/" + Right( this.ProductReference, 2 ) + "/" + this.ProductReference + "/";
};

ClassProductPhotos.prototype.getPhotosPathAndName = function( Type, Count )
{
	return this.PhotosPath + this.ProductReference + "_" + Type + Count + ".jpg";
};

ClassProductPhotos.prototype.VerifyPhotos = function()
{
	for ( var Count = 1; Count <= this.LimitOfPhotos; Count++ )
	{
		//if ( ImageExists( this.getPhotosPathAndName( "detalhe", Count ) ) )
		//{
			this.PhotosDetail[ Count ] = this.getPhotosPathAndName( "detalhe", Count );
		//}

		//if ( ImageExists( this.getPhotosPathAndName( "thumb", Count ) ) )
		//{
			this.PhotosThumb[ Count ] = this.getPhotosPathAndName( "thumb", Count );
		//}

		//if ( ImageExists( this.getPhotosPathAndName( "ampliacao", Count ) ) )
		//{
			this.PhotosExpansion[ Count ] = this.getPhotosPathAndName( "ampliacao", Count );
		//}

		//if ( ImageExists( this.getPhotosPathAndName( "zoom", Count ) ) )
		//{
			this.PhotosZoom[ Count ] = this.getPhotosPathAndName( "zoom", Count );
		//}
	}
};

ClassProductPhotos.prototype.ClearShowcase = function()
{
	if ( this.Showcase )
	{
		while ( this.Showcase.firstChild )
		{
			this.Showcase.removeChild( this.Showcase.firstChild );
		}
	}
};

ClassProductPhotos.prototype.ConfigureShowcase = function()
{
	this.ClearShowcase();
	var HTML = "";
	var HTMLAmpliacao = "";
	
	if(document.getElementById( "ZoomImg" )){
	
		var ZoomLink = document.getElementById( "ZoomImg" );
			ZoomLink.href = this.PhotosZoom[ 1 ];
	
		var ZoomImg = document.getElementById( "FotoPrincipal" );
			ZoomImg.src = this.PhotosDetail[ 1 ];
	
		for ( var Count = 1; Count <= this.LimitOfPhotos; Count++ )
		{
			if ( this.PhotosDetail[ Count ] != null && this.PhotosThumb[ Count ] != null )
			{
				HTML += '<a href="' + this.PhotosZoom[ Count ] + '" id="ThumbLink' + Count + '" rel="ZoomImg" rev="' + this.PhotosDetail[ Count ] + '"><img src="' + this.PhotosThumb[ Count ] + '" style="margin-right:10px; border:1px solid ' + ( Count == 1 ? "#333333" : "#E1E1E1" ) + ';display:none;" onload="this.style.display=\'inline-block\';" /></a>';
			}
	
			if ( this.PhotosExpansion[ Count ] != null )
			{
				HTMLAmpliacao += '<div style="position:relative; padding:2px; display:none;"><img src="' + this.PhotosExpansion[ Count ] + '" onload="this.parentNode.style.display=\'block\';" /></div>';
			}
		}
	
		//document.getElementById( "GerarAmpliacaoFotos" ).innerHTML = HTMLAmpliacao;
		this.Showcase.innerHTML = HTML;
	}
};

var ChangeProductPhotos = function( ProductReference, LimitOfPhotos )
{
	EzZoom_stopZooms();

	if ( !LimitOfPhotos ) LimitOfPhotos = 10;

	var ProductPhotos = new ClassProductPhotos( ProductReference, LimitOfPhotos );
		ProductPhotos.VerifyPhotos();
		ProductPhotos.ConfigureShowcase();

	EzZoom_findZooms();
};

var arrFotosSrc = "|";
var openPhoto = function(){
	if(document.getElementById("globalfotos")){
		
		var src		= document.getElementById('FotoPrincipal').src.replace('detalhe','ampliacao');
		if(arrFotosSrc.indexOf(src)!=-1){
			document.getElementById('FotoAmpliada').src = src;
		}else{
			document.getElementById('FotoAmpliada').src = 'FILES/_IMG/loading.gif';
			var img		= new Image();
			img.src		= src;
			img.onload	= function(){
				document.getElementById('FotoAmpliada').src = this.src;
				arrFotosSrc = arrFotosSrc + src + '|';
			}
		}
		
		var Selects = document.getElementsByTagName("SELECT");
		for(var i = 0; i<Selects.length; i++){
			Selects[i].style.visibility="hidden";
			Selects[i].disabled=true;
		}
		
		if(document.getElementById("swf_video")){
			document.getElementById("swf_video").style.visibility = "hidden";
		}
		
		var div = window.document.getElementById("bg");
		div.style.display = "block";
		
		var fotos = document.getElementById("globalfotos");	
		fotos.style.display = "block";
		
		window.bgOpened = true;
	}else{
		return;
	}
}