// JavaScript Document

var currentpos=0;
var currentthumbpos=0;
var moveamount=106;
var thumbnailnum;
var maxthumbvisible = 999; //Define how many thumbnails will be visible at one time in the thumbbox--for now this should only be 3 since it's actually the css that controls the size of the viewable box
var moving = false;
var finishedsizing=0;
var preloadimg="no"//Preload images ("yes" or "no"):
var myloadedimage = new Array();
     myloadedimage[0]=1;
var imggallery=new Array()
var firstimagestart=1;
var nextorprev=0;
var fadeout=1;

function modifyimage(loadarea, imgindex, img_id, posid ){
	function loadimagenow(){
		//alert("Load Image Now Called");
		if (document.getElementById) {
				if(current_imgid[posid]!=img_id){
					
					var imgobj=document.getElementById(loadarea);
					imgobj.innerHTML=returnimgcode(imggallery[posid][imgindex]);
					initImage(loadarea);
					current_imgid[posid]=img_id;
					myloadedimage[imgindex]=1;
				}
			}
		return false
	}
	if(myloadedimage[imgindex]==null){	
		new Asset.image(imggallery[posid][imgindex][0], {onload: loadimagenow});
		
	}else{
		loadimagenow();
	}
	
}

function returnimgcode(theimg){
	var imghtml=""
	if (theimg[1]!="")
		imghtml=''
	imghtml+='<img src="'+theimg[0]+'" border="0" id="'+theimg[3]+'" />'
	if (theimg[1]!="")
		imghtml+=''
	return imghtml
}

function initImage(imageId) {
	var fader = new Fx.Style(imageId,'opacity', {duration:fadespeed});
	fader.set(0);
	fader.start(0,1);
}

function checknext(mynum, posid){
	thumbmax=(Number(thumbnailnum)-1);
	if(imggallery[posid].length == 1) {
		mm_shl('prev'+posid,'hidden');
		mm_shl('next'+posid,'hidden');
	} else {
		if ( mynum < 1 ) {
			mm_shl('prev'+posid,'hidden');
			mm_shl('next'+posid,'visible');
		} else if ( mynum <  thumbmax ) {
			mm_shl('prev'+posid,'visible');
			mm_shl('next'+posid,'visible');
		} else {
			mm_shl('prev'+posid,'visible');
			mm_shl('next'+posid,'hidden');
		}
	}
}

function mm_shl() { //v6.0
	var obj,args=arguments;
	if ((obj=MM_findObj(args[0]))!=null) {
		if (obj.style) {
			obj=obj.style;
		}
		obj.visibility=args[1];
	}
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function addposition(addwidth){
	if ( !moving ) {
		 // if animagic is still moving the image..don't update the current position till it's done
		if(addwidth=="minus"){
			currentpos-=1;
		}else if(addwidth=="plus"){
			currentpos+=1;
		}
	}
	return currentpos;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
	
// JavaScript Document
function initGallery ( tempgallery, count, first_id, startwidth, startheight, posid ) {
	imggallery[posid] = tempgallery;

	if (preloadimg=="yes"){
		for (x=0; x<imggallery[posid].length; x++){
			var myimage=new Image()
			myimage.src=imggallery[posid][x][0]
		}
	}

	thumbnailnum = imggallery[posid].length;
	current_imgid[posid] = first_id;
	currentwidth[posid]=startwidth;
	currentheight[posid]=startheight;

	checknext(0, posid)

	window.addEvent('load', function() {
		var setloadersize = new Fx.Styles('main_image_wrapper'+posid,{duration:transspeed,onComplete: function(){loadfirstimage(currentwidth,currentheight,posid)}});
		setloadersize.start({
			'width':imggallery[posid][0][1],
			'height':imggallery[posid][0][2]	
		});
	});
	
	
}

//function getstarted(width, height, loadarea, imgindex, img_id, current_imgid, chdummy, cwdummy, posid){
function getstarted(width, height, loadarea, imgindex, img_id, current_image_id, posid){
	checknext(img_id, posid);
	if(current_image_id!=img_id){
		if(firstimagestart==1){
			currentwidth[posid]=firstimagewidth[posid];
			currentheight[posid]=firstimageheight[posid];
			firstimagestart=0;		
		}
		if(nextorprev==1){
			currentwidth[posid]=cwidth;
			currentheight[posid]=cheight;
			nextorprev=0;		
		}
		
		var resizeDivHeight = new Fx.Styles('main_image_wrapper'+posid,{duration:transspeed, onComplete: function(){modifyimage(loadarea, imgindex, img_id, posid);currentheight[posid]=height;currentwidth[posid]=width;} });
		var fader = new Fx.Style('imgloader'+posid,'opacity', {duration:fadespeed, onComplete: function(){	resizeDivHeight.start({'height': [currentheight[posid],height],'width': [currentwidth[posid],width]});} });
		fader.start(1,0);
	}
}

function loadfirstimage(currentwidth,currentheight,posid){
	var fadefirst = new Fx.Style('imgloader'+posid,'opacity', {duration:fadespeed });
	fadefirst.set(0);
	function setfirstimage(){
		var newHTML = "<img src='"+tempgallery[posid][0][0]+"' />";
		
		$('imgloader'+posid).setHTML(newHTML);
	
		fadefirst.start(0,1);
		//firsttitlefade.start(0,1);
		currentheight=imggallery[posid][0][2];
		currentwidth=imggallery[posid][0][1];
	}
	new Asset.image(imggallery[posid][0][0], {onload: setfirstimage});
}

function nextimage(current_image_id, posid){
	newimgid = Number(current_image_id)+1;
	newwidth =imggallery[posid][newimgid][1]
	newheight =imggallery[posid][newimgid][2]
	newimgindex =imggallery[posid][newimgid][3]
	newimgid = imggallery[posid][newimgid][3]
	cwidth=imggallery[posid][current_image_id][1]
	cheight=imggallery[posid][current_image_id][2]
	checknext(newimgid, posid);
	nextorprev=1;
	//getstarted(Number(newwidth), Number(newheight), 'imgloader'+posid,Number(newimgindex), Number(newimgid) , Number(current_image_id), Number(cwidth), Number(cheight), posid)
	getstarted(Number(newwidth), Number(newheight), 'imgloader'+posid,Number(newimgindex), Number(newimgid) , Number(current_image_id), posid)
}

function previmage(current_image_id, posid){
	newimgid = Number(current_image_id)-1;
	newwidth =imggallery[posid][newimgid][1]
	newheight =imggallery[posid][newimgid][2]
	newimgindex =imggallery[posid][newimgid][3]
	newimgid = imggallery[posid][newimgid][3]
	cwidth=imggallery[posid][current_image_id][1]
	cheight=imggallery[posid][current_image_id][2]
	checknext(newimgid, posid);
	nextorprev=1;
	//getstarted(Number(newwidth), Number(newheight), 'imgloader'+posid,Number(newimgindex) ,Number(newimgid) , Number(current_image_id), Number(cwidth), Number(cheight), posid)
	getstarted(Number(newwidth), Number(newheight), 'imgloader'+posid,Number(newimgindex) ,Number(newimgid) , Number(current_image_id), posid)
}
