// JavaScript Document
function initGallery ( tempgallery, count, first_id, startwidth, startheight ) {
	imggallery = tempgallery;
	if (preloadimg=="yes"){
		for (x=0; x<imggallery.length; x++){
			var myimage=new Image()
			myimage.src=imggallery[x][0]
		}
	}
	thumbnailnum = projectlen;
	//alert(thumbnailnum);
	piccount=picnum;
	//alert(thumbnailnum+"a"+maxthumbvisible);
	current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
	window.addEvent('load', function() {
		if(thumbnailnum>maxthumbvisible){
			lefthtml = "<div id='leftmore'><ul><li><a href=\"javascript:checkbutton(addposition('minus'));movethumbs('plus');\"><img src='images/previous.png' width='20' height='20' border='0' /></a></li></ul></div>";
			righthtml = "<div id='rightmore'><ul><li><a href=\"javascript:checkbutton(addposition('plus'));movethumbs('minus');\"><img src='images/next.png' width='20' height='20' border='0' /></a></li></ul></div>";
			$('back').setHTML(lefthtml);
			$('more').setHTML(righthtml);
		}
		var setloadersize = new Fx.Styles('main_image_wrapper',{duration:transspeed,onComplete: function(){loadfirstimage(currentwidth,currentheight)}});
		setloadersize.start({
			'width':650,
			'height':425	
		});
	});
	
	
}
window.addEvent('domready', function() {
	initGallery( tempgallery, tempgallery.length, tempgallery[0][8], tempgallery[0][1], tempgallery[0][2], 0 );
});

function getstarted(width, height, loadarea, imgindex, img_id, current_imgid){
	checknext(img_id);
	//alert(imgindex+"    "+tempgallery.length)
	if(current_imgid!=img_id){
		if(firstimagestart==1){
			currentwidth=firstimagewidth;
			currentheight=firstimageheight;
			firstimagestart=0;		
		}
		if(nextorprev==1){
			currentwidth=cwidth;
			currentheight=cheight;
			nextorprev=0;		
		}
		//alert(current_imgid);
		var resizeDivHeight = new Fx.Styles('main_image_wrapper',{duration:transspeed, onComplete: function(){modifyimage(loadarea, imgindex, img_id);currentheight=height;currentwidth=width;} });
		var fader = new Fx.Style('imgloader','opacity', {duration:fadespeed, onComplete: function(){	resizeDivHeight.start({'height': [425,425],'width': [650,650]});} });
		fader.start(1,0);	
		var fadeiptc = new Fx.Style('iptc_btn','opacity', {duration:transspeed });
		fadeiptc.set(0);
		var titlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
		titlefade.set(0);
		fadeout=0
		if(fadeout==0){
		var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
		fademe.set(0);
		fadeout=0;
		}else{
			var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
			fademe.start(1,0);
			fadeout=0
		}
	}
	
}
function loadfirstimage(currentwidth,currentheight){
	var fadefirst = new Fx.Style('imgloader','opacity', {duration:fadespeed });
	fadefirst.set(0);
	var firsttitlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
	firsttitlefade.set(0);
	function setfirstimage(){
		if(currentwidth>=650 || currentheight>=425)
		{
			if (currentwidth != 650|| currentheight != 425)
			{
				if (currentwidth >= currentheight)
				{
					width = 650;
					height = (650 / currentwidth * currentheight);
					do
					{
						while (height >= 425)
						{
							width = width - 5;
							height = (width / currentwidth * currentheight);
						}
						while (width >= 650)
						{
							height = height - 5;
							width = (height / currentheight * currentwidth);
						}
					} while (height >= 425 || width >= 650);
				}
				else
				{
					height = 425;
					width = (425 / currentheight * currentwidth);
					do
					{
						while (width >= 650)
						{
							height = height - 5;
						width = (height / currentheight * currentwidth);
						}
						while (height >= 425)
						{
							width = width - 5;
							height = (width / currentwidth * currentheight );
						}
					} while (height >= 425 || width >= 650);
				}
			}
			else
			{
				width = 650;
				height = 425;
			}
			
			if(browserType=="MSIE")
			{
				var proportion = (425-height)/2;
				
				var newHTML = " <div class=\"photoholder\" style=\"text-align:center; vertical-align:middle;padding-top:"+proportion+"px;\"><div><p style=\"text-align:center; vertical-align:middle;\"><img src='phpthumb/phpThumb.php?src=../"+tempgallery[0][0]+"&w=650&h=425&aoe=1' /></p></div></div>";
			}
			else
			{
				var proportion = (650-width)/2;
				
				proportion=0;
				//alert("a"+width+"a");
				var newHTML = " <div class=\"photoholder\" style=\"padding-left:"+proportion+"px;text-align:center; vertical-align:middle;\"><div><p style=\"text-align:center; vertical-align:middle;\"><img src='phpthumb/phpThumb.php?src=../"+tempgallery[0][0]+"&w=650&h=425&aoe=1' /></p></div></div>";
		
			}
		}
		else
		{
			if(browserType=="MSIE")
			{
				var proportion = (425-currentheight)/2;
				var newHTML = " <div class=\"photoholder\" style=\"text-align:center; vertical-align:middle;padding-top:"+proportion+"px;\"><div><p style=\"text-align:center; vertical-align:middle;\"><img src='"+tempgallery[0][0]+"' /></p></div></div>";
			}
			else
			{
				var proportion = (650-currentwidth)/2;
				if(proportion<1)
				proportion=0;
				var newHTML = " <div class=\"photoholder\" style=\"text-align:center; vertical-align:middle;padding-left:"+proportion+"px;\"><div><p style=\"text-align:center; vertical-align:middle;\"><img src='"+tempgallery[0][0]+"' /></p></div></div>";
			}
		}
		$('imgloader').setHTML(newHTML);
		//alert(currentwidth+'bb'+currentheight);
	//alert(tempgallery[0][0]);
		var firsttitle=document.getElementById('imgtitle');
		firsttitle.innerHTML="<strong>"+tempgallery[0][4]+"</strong> "+tempgallery[0][7];
		fadefirst.start(0,1);
		//firsttitlefade.start(0,1);
		currentheight=imggallery[0][2];
		currentwidth=imggallery[0][1];
	}
	new Asset.image(imggallery[0][0], {onload: setfirstimage});
}
function nextimage(current_imgid){
	//alert(current_imgid);
	//alert(tempgallery.length-1+"  "+ current_imgid)
	
	if(tempgallery.length-1 == current_imgid)
	 newimgid = 0;
	else
	 newimgid = Number(current_imgid)+1;
	//alert("aa"+newimgid+"aa");
	newwidth =imggallery[newimgid][1];
	newheight =imggallery[newimgid][2]
	newimgindex =imggallery[newimgid][8];;
	newimgid = imggallery[newimgid][8];
	cwidth=imggallery[current_imgid][1];
	cheight=imggallery[current_imgid][2];
	
	//checknext(newimgid);
	
	nextorprev=1;
	//alert(newwidth+'aa'+newheight);
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}

function previmage(current_imgid){
	//alert(current_imgid);
	
	if(current_imgid == 0)
	 newimgid = tempgallery.length-1;
	else
	 newimgid = Number(current_imgid)-1;
	 
	newwidth =imggallery[newimgid][1]
	newheight =imggallery[newimgid][2]
	newimgindex =imggallery[newimgid][8]
	newimgid = imggallery[newimgid][8]
	cwidth=imggallery[current_imgid][1]
	cheight=imggallery[current_imgid][2]
	checknext(newimgid);
	nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}
