var currentpos=0;
var moveamount=105;
var thumbnailnum = 3; //Define the number of image thumbnail that there will be.  This will effect showing the more>> link or not.  Should be dynamic according to the size of imggallery.
var maxthumbvisible = 6; //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 current_imgid="";
var moving = false;
var finishedsizing=0;

var preloadimg="yes"//Preload images ("yes" or "no"):

var currentwidth=250;
var currentheight=250;
var imggallery=new Array()

function initGallery ( tempgallery, count, first_id, startwidth, startheight ) {
	imggallery = tempgallery;
	if (preloadimg=="yes"){
		for (var x=0; x<imggallery.length; x++){
			var myimage=new Image()
			myimage.src=imggallery[x][0]
		}
	}
	thumbnailnum = count;
	current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
}

function getstarted(width, height, loadarea, imgindex, img_id, current_imgid){
	if(current_imgid!=img_id){
		var resizeDivHeight = new fx.Height('main_image_wrapper',{duration:200, onComplete: function(){areweready(loadarea, imgindex, img_id);currentheight=height;} });
		var resizeDivWidth =  new fx.Width ('main_image_wrapper',{duration:200, onComplete: function(){areweready(loadarea, imgindex, img_id);currentwidth=width;} });
		var fader = new fx.Opacity('imgloader', {duration: 50, onComplete: function(){resizeDivWidth.custom(currentwidth,width);resizeDivHeight.custom(currentheight,height);} });
		fader.toggle();
	}
}

function areweready(loadarea, imgindex, img_id){
	if ( finishedsizing++ ) { 
		modifyimage(loadarea, imgindex, img_id);
		finishedsizing = 0;
	}
}

function modifyimage(loadarea, imgindex, img_id ){
	if (document.getElementById) {
		if(current_imgid!=img_id){
			var imgobj=document.getElementById(loadarea);
			imgobj.innerHTML=returnimgcode(imggallery[imgindex]);
			initImage(loadarea);
			current_imgid=img_id;
		}
	}
	return false
}

function returnimgcode(theimg){
	var imghtml=""
	if (theimg[1]!="")
		imghtml='<a href="'+theimg[1]+'">'
	imghtml+='<img src="'+theimg[0]+'" border="0" id="'+theimg[2]+'" />'
	if (theimg[1]!="")
		imghtml+='</a>'
	return imghtml
}

function initImage(imageId) {
	var fader = new fx.Opacity(imageId, {duration: 150});
	fader.hide();
	fader.toggle();
}

function checkbutton(mynum){
	if ( mynum == 0 ) {
		mm_shl('back','hidden');
		mm_shl('more','visible');
	} else if ( mynum < thumbnailnum - maxthumbvisible ) {
		mm_shl('back','visible');
		mm_shl('more','visible');
	} else {
		mm_shl('back','visible');
		mm_shl('more','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 animagic(el, x, y, a, b, c, s) { //v2.5 PVII
 var g,elo=el,f="",m=false,d="";x=parseInt(x);y=parseInt(y);
 var t = 'g.p7Magic = setTimeout("animagic(\''+elo+'\','; 
 if ((g=MM_findObj(el))!=null) {d=(document.layers)?g:g.style;}else{return;}
 if (parseInt(s)>0) {eval(t+x+','+y+','+a+','+b+','+c+',0)",' + s+')');return;}
 var xx=(parseInt(d.left))?parseInt(d.left):0;
 var yy=(parseInt(d.top))?parseInt(d.top):0;
 if(parseInt(c)==1) {x+=xx;y+=yy;m=true;c=0;}
 else if (c==2) {m=false;clearTimeout(g.p7Magic);}
 else {var i=parseInt(a);
  if (eval(g.moved)){clearTimeout(g.p7Magic);}
  if (xx<x){xx+=i;m=true;if(xx>x){xx=x;}}
  if (xx>x){xx-=i;m=true;if(xx<x){xx=x;}}
  if (yy<y){yy+=i;m=true;if(yy>y){yy=y;}}
  if (yy>y){yy-=i;m=true;if(yy<y){yy=y;}}}
moving = m; // stop quick button pushing
 if (m) {if((navigator.appName=="Netscape") && parseInt(navigator.appVersion)>4){
    xx+="px";yy+="px";}
  d.left=xx;d.top=yy;g.moved=true;eval(t+x+','+y+','+a+','+b+','+c+',0)",'+b+')');
  }else {g.moved=false;}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
