var Limit=0;
var FinishedEffect=true;
var pepito;

function FreshGalleryShowLoadedThumb(ThumbID,Duration){

	new Effect.Appear(ThumbID, {duration:Duration,beforeStart: function(){FinishedEffect=false},afterFinish: function (){FinishedEffect=true} });
	return false;

}


function highlightThumb(ThumbID,Duration){
	Effect.Shake(ThumbID, { distance: 3, duration: 0.5 });
}


function FreshGalleryShowScrollButtons(GalleryName){//GalleryName,Rows,Height,ItemSeparation){
	
	if(document.getElementById("FreshBoard_"+GalleryName).style.top==""){
	
		Limit=document.getElementById("FreshBoard_"+GalleryName).offsetHeight;
		Height=parseInt(document.getElementById("Fresh_"+GalleryName).style.height.substr(0,document.getElementById("Fresh_"+GalleryName).style.height.length-2));
		if(Limit>Height){
			Limit=Limit-Height;
		
			new Control.Slider('FreshHandle_'+GalleryName, 'FreshSlider_'+GalleryName, {
			  axis:'vertical',
			  increment:1,
		  	minimum: 0,
		  	maximum: Limit,
		  	alignX: -28,
		  	alignY: 0,
		  	range: $R(0,Limit),
		  	disabled: false, 
		  	onSlide: function(value) {
	      	  document.getElementById("FreshBoard_"+GalleryName).style.top=Math.round(value*-1)+'px';
	     	 },
	      	onChange: function(value){
	      	  document.getElementById("FreshBoard_"+GalleryName).style.top=Math.round(value*-1)+'px';
	     	 }
			});
		}else{
		//	document.getElementById('FreshGalleryScroll_'+GalleryName).style.opacity="50";

		}
	}

}


function FreshGalleryCheckScrollBar(GalleryName){

	
	
	if(document.getElementById("FreshBoard_"+GalleryName).style.top==""){
		if(!checkLoadedImages(GalleryName)){
			//showtime=setInterval("alerta()",1000);
			window.setTimeout("FreshGalleryCheckScrollBar('"+GalleryName+"')",800);
		}else{

			FreshGalleryShowScrollbar(GalleryName);
		}
	}
}


function FreshGalleryShowScrollbar(GalleryName) {
	
/*
		if(!checkLoadedImages(GalleryName)){
			FreshGalleryCheckScrollBar(GalleryName)		
		}
*/

	
		Limit=document.getElementById("FreshBoard_"+GalleryName).offsetHeight;
		Height=parseInt(document.getElementById("Fresh_"+GalleryName).style.height.substr(0,document.getElementById("Fresh_"+GalleryName).style.height.length-2));
/* 		alert(Height+"-"+Limit); */
		if(Limit>Height){
		//	alert('omg');

			new Effect.Appear('FreshGalleryScroll_'+GalleryName, {duration:1,beforeStart: function(){FinishedEffect=false},afterFinish: function (){FinishedEffect=true} });


		}

}




function FreshGalleryShowItem(DivId,ChildID,ImgFile){

	Center=true;
	

	img=new Image();

	img.src = ImgFile;
	imgWidth=img.width;
	imgHeight=img.height;
	

	
	topPos=0;

 	//	alert(window.outerWidth);

	Left=(document.documentElement.clientWidth/2)-(imgWidth/2);
	Left=Left;
	Left=Left+"px";



	topPos=(document.documentElement.clientHeight/2)-(imgHeight/2); 


	var scrollPos = window.pageYOffset || document.documentElement.scrollTop || 0;

/* 	alert(scrollPos); */
	topPos=topPos+scrollPos+"px";


	newWindow=document.createElement("div");
	newWindow.id=DivId;

	if(document.getElementById(ChildID)){
		document.getElementById(ChildID).appendChild(newWindow);
	}else{
		document.body.appendChild(newWindow);
	}
	
	newWindow.style.display="none";
	newWindow.style.position="absolute";
	//newWindow.style.top=top+"px";
	newWindow.style.top=topPos;
	newWindow.style.left=Left;
	newWindow.style.height=imgHeight+"px";//window.innerHeight+"px";
	newWindow.style.width=imgWidth+"px";
	newWindow.style.backgroundColor="#DDDDDD";
	newWindow.style.backgroundAttachment="fixed";

	new Effect.Appear(newWindow.id, { delay:0.5,duration:0.8, from:0, to:0.8});
	new Effect.Grow(newWindow.id, { delay:0.5,duration:0.8});

		window.document.onscroll = function()
	{

	};

}

function FreshGalleryFade(DivId,ChildID){


	Center=true;
	

	newWindow=document.createElement("div");
	newWindow.id=DivId;

	if(document.getElementById(ChildID)){
		document.getElementById(ChildID).appendChild(newWindow);
	}else{
		document.body.appendChild(newWindow);
	}

	if(window.pageYOffset){
		topPos=window.pageYOffset;
	}else{
		topPos=document.body.scrollTop;
	}
	

	newWindow.style.display="none";
	newWindow.style.position="absolute";
	//newWindow.style.top=top+"px";
	newWindow.style.top=0;
	newWindow.style.left=0;
	newWindow.style.height=document.body.offsetHeight+"px";//window.innerHeight+"px";
	newWindow.style.width="100%";
	newWindow.style.backgroundColor="#000000";
	newWindow.style.backgroundAttachment="fixed";

	new Effect.Appear(newWindow.id, { duration:0.3, from:0, to:0.8});
	window.document.onscroll = function()
	{

	};
}


function checkLoadedImages(GalleryName){

	var element=document.getElementById("FreshBoard_"+GalleryName);
	var images=element.getElementsByTagName("IMG");
	var imagesArray;
	var imagesLoaded=1;
	
	for (var i = 0;i<images.length;i++){
		if(images[i].complete==false){
			imagesLoaded=0;
		}	
		
		if(images[i].style.display=="none"){
			imagesLoaded=0
		}
    }
    if (imagesLoaded!=0){
    //	alert("cargadas!");
    }
    return imagesLoaded;
	

}
function allImagesLoaded(GalleryName) {

    // return variable
    var imagesloaded = 1;

    // All images are saved in an array called document.images. Very usefull
    var images = document.images;
    // Loop through all the images
    for (var i = 0;i<images.length;i++)
    {
            // If the image isnt loaded we set the return varible to 0
            if(images[i].complete == false) {
                    imagesloaded = 0;
            }
    }

    // This will return 0 if one or more images are not loaded and 1 if all images are loaded.
    return imagesloaded;
}


