// JavaScript Document
function initGallery ( tempgallery, count, first_id) {
	imggallery = tempgallery;
	if (preloadimg=="yes"){
		for (x=0; x<imggallery.length; x++){
			var myimage=new Image()
			myimage.src=imggallery[x][0]
		}
	}
	thumbnailnum = imggallery.length;
	current_imgid = first_id;
	window.addEvent('load', function() {
		if(thumbnailnum>maxthumbvisible){
			lefthtml = '<a id="leftmore" href="javascript:checkbutton(addposition(\'minus\'));movethumbs(\'plus\');"><img src="public/gfx/deco/boutons/buttonblank.gif" width="24" height="24" /></a>';
			righthtml = '<a id="rightmore" href="javascript:checkbutton(addposition(\'plus\'));movethumbs(\'minus\');"><img src="public/gfx/deco/boutons/buttonblank.gif" width="24" height="24" /></a>';
			$('back').set('html',lefthtml);
			$('more').set('html',righthtml);
		}
		loadfirstimage();
	});
	
	
}
window.addEvent('domready', function() {
	initGallery( tempgallery, tempgallery.length, tempgallery[0][2]);
});

function getstarted(loadarea, imgindex, img_id, current_imgid){
	checknext(img_id);
	if(current_imgid!=img_id){
		if(firstimagestart==1){
			firstimagestart=0;		
		}
		if(nextorprev==1){
			nextorprev=0;		
		}
		
		var fader = new Fx.Tween('imgloader',{property:'opacity', duration:fadespeed, onComplete: function(){modifyimage(loadarea, imgindex, img_id);} });
		fader.start(1,0);
		var titlefade = new Fx.Tween('imgtitle',{property:'opacity', duration:transspeed });
		titlefade.set(0);
		fadeout=0;
	}
}
function loadfirstimage(){
	var fadefirst = new Fx.Morph('imgloader','opacity', {duration:fadespeed });
	fadefirst.set(0);
	var firsttitlefade = new Fx.Morph('imgtitle','opacity', {duration:transspeed });
	firsttitlefade.set(0);
	function setfirstimage(){
		var newHTML = "<img src='"+tempgallery[0][0]+"' />";
		$('imgloader').set('html',newHTML);
	
		var firsttitle=document.getElementById('imgtitle');
		firsttitle.innerHTML=tempgallery[0][1];
		fadefirst.start(0,1);
		firsttitlefade.start(0,1);
	}
	new Asset.image(imggallery[0][0], {onload: setfirstimage});
}
function nextimage(current_imgid){
	
	newimgid = Number(current_imgid)+1;
	newimgindex =imggallery[newimgid][2]
	newimgid = imggallery[newimgid][2]
	checknext(newimgid);
	nextorprev=1;
	getstarted('imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid))
}

function previmage(current_imgid){
	newimgid = Number(current_imgid)-1;
	newimgindex =imggallery[newimgid][2]
	newimgid = imggallery[newimgid][2]
	checknext(newimgid);
	nextorprev=1;
	getstarted('imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid))
}

