// 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]
		}
	}
	
	current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
	
	window.addEvent('load', function() {
		var setloadersize = new Fx.Styles('main_image_wrapper',{duration:transspeed,onComplete: function(){loadfirstimage(currentwidth,currentheight)}});
		
		setloadersize.start({
			'width':imggallery[0][1],
			'height':imggallery[0][2]	
		});
	});
	
	
}

window.addEvent('domready', function() {
	if (tempgallery.length > 0) { initGallery( tempgallery, tempgallery.length, tempgallery[0][8], tempgallery[0][1], tempgallery[0][2], 0 ); }
});

function getstarted(width, height, loadarea, imgindex, img_id, current_imgid){
	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);	
	}
}
function loadfirstimage(currentwidth,currentheight){
	var fadefirst = new Fx.Style('imgloader','opacity', {duration:fadespeed });
	fadefirst.set(0);
	function setfirstimage(){
		var newHTML = "<a href='"+tempgallery[0][7]+"' target='_self'><img src='"+tempgallery[0][0]+"' border='0' /></a>";
		$('imgloader').setHTML(newHTML);
	
		var firsttitle=document.getElementById('imgtitle');
		var phtitini ="";
		if (tempgallery[0][5].length > 0) {
			phtitini = phtitini + "<br /><strong>";
			for (var i = 0; i < tempgallery[0][5].length; i++) {
				phtitini = phtitini + tempgallery[0][5][i];
				if (i < tempgallery[0][5].length - 1) phtitini = phtitini + ", ";
			}
		phtitini = phtitini + "</strong>";
		}
		fadefirst.start(0,1);
		currentheight=imggallery[0][2];
		currentwidth=imggallery[0][1];
	}
	new Asset.image(imggallery[0][0], {onload: setfirstimage});
}
function nextimage(current_imgid){
    var next = Number(current_imgid)+1;
    if (next >= tempgallery.length) next = 0; 
	hoverNum(document.getElementById('numeros_'+ next));
	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]
	nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
	
}

function previmage(current_imgid){
	var prev = Number(current_imgid)-1;
    if (prev < 0) prev = tempgallery.length-1; 
	hoverNum(document.getElementById('numeros_'+ prev));
	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]
	nextorprev=1;
	getstarted(Number(newwidth), Number(newheight), 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
}

function hoverNum(obj) {
	for (i=0;i<tempgallery.length;i++) {
		document.getElementById('numeros_'+i).style.backgroundColor='#000';
		document.getElementById('numeros_'+i).style.color='#727d84';
	}
	obj.style.backgroundColor='#41637e';
	obj.style.color='#fff';
}

