// JavaScript Document
function initGallery ( Gallery, count, first_id, startwidth, startheight ) {
	imggallery = Gallery;

	thumbnailnum = imggallery.length;
	current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
	
}
if(Gallery.length>0){
	initGallery( Gallery, Gallery.length, -1, Gallery[0][1], Gallery[0][2], 0 );
}

function getstarted(width, height, loadarea, imgindex, img_id, current_imgid,clicked){
	if(clicked){
		autoRotation=0;	
	}
	checknext(img_id);
	if(current_imgid!=img_id){
		if(firstimagestart==1){
			currentwidth=firstimagewidth;
			currentheight=firstimageheight;
			firstimagestart=0;		
		}
		if(nextorprev==1){
			currentwidth=cwidth;
			currentheight=cheight;
			nextorprev=0;		
		}

		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': [currentheight,height],'width': [currentwidth,width]});} });
		fader.start(1,0);	
		if(document.getElementById('iptc_btn')){
			var fadeiptc = new Fx.Style('iptc_btn','opacity', {duration:transspeed });
			fadeiptc.set(0);
		}
		if(document.getElementById('imgtitle')){
			var titlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
			titlefade.set(0);
		}
		fadeout=0
		if(document.getElementById('iptc_info')){
			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);
	if(document.getElementById('imgtitle')){
		var firsttitlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
		firsttitlefade.set(0);
	}
	function setfirstimage(){
		var newHTML = "<img src='"+Gallery[0][0]+"' />";
		$('imgloader').setHTML(newHTML);
	
		if(document.getElementById('imgtitle')){
			var firsttitle=document.getElementById('imgtitle');
			firsttitle.innerHTML=""+Gallery[0][4]+" "+Gallery[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){
	autoRotation=0;
	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))
}

function previmage(current_imgid){
	autoRotation=0;
	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))
}

var nextImageId=-1;

function rotate(){
	if(autoRotation){
		if(Gallery.length>0){
			if(nextImageId<0){
				nextImageId=0;
			}
			if(nextImageId >= (imggallery.length-1)){
				nextImageId=-1;	
			}
			nextImageId=nextImageId+1;
	
			getstarted(imggallery[nextImageId][1], imggallery[nextImageId][2], 'imgloader', nextImageId, nextImageId, current_imgid);
		}
	}
}