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]
		}
	}
	thumbnailnum = imggallery.length;
	current_imgid = first_id;
	currentwidth=startwidth;
	currentheight=startheight;
	window.onload=function(){loadfirstimage()}
}
initGallery( tempgallery, tempgallery.length, tempgallery[0][8], tempgallery[0][1], tempgallery[0][2] );

function getstarted(width, height, loadarea, imgindex, img_id, current_imgid, cwidth, cheight,ClientClick){
        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);	
		fadeout=0;
}

function loadfirstimage(){
	var photonum=document.getElementById('photocount')
	photonum.innerHTML=(Number(0)+1)+" of "+imggallery.length+" Photos"; 
	
					var phototitle=document.getElementById('imgtitle')
   phototitle.innerHTML="<strong>"+ imggallery[0][4]+"</strong>" + ' ' + "<br />" + imggallery[0][7] + '<br />';
   
	var firstimageload = new Image();
	firstimageload.src = null;
	firstimageload.src = imggallery[0][0];
	var fadefirst = new Fx.Style('imgloader','opacity', {duration:fadespeed });
	fadefirst.set(0);
	var setfirstheight = new Fx.Style('main_image_wrapper','height',{duration:transspeed});
	var setfirstwidth =  new Fx.Style ('main_image_wrapper','width',{duration:transspeed});
	setfirstwidth.start(currentwidth,imggallery[0][1]);
	setfirstheight.start(currentheight,imggallery[0][2]);
	firstimageload.onload=function(){
		var firstimg=document.getElementById('imgloader');
		firstimg.innerHTML=returnimgcode(imggallery[0]);
        fadefirst.start(0,1);
		currentheight=imggallery[0][2];
		currentwidth=imggallery[0][1];
		
		
		//pre load next image
		var myimage=new Image()
			myimage.src=imggallery[1][0]
	}
	firstimageload.src = imggallery[0][0];
	
	Start();
}

function nextimage(current_imgid){
	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),'false')

}
 var timerID = 0;
 var tStart  = null;
var secounds =5000;
function UpdateTimer() {
if (currentpos != imggallery.length -1) {
 nextimage(currentpos);
currentpos+=1

//pre load next image
for (x=currentpos; x<currentpos + 2; x++){
			if (imggallery.length -1 > currentpos) {
			var myimage=new Image()
			myimage.src=imggallery[currentpos+1][0]
			
			
			
			}
			}
	
			
 
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart)
      tStart   = new Date();
   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();
  tDate.setTime(tDiff);
   timerID = setTimeout("UpdateTimer()", 8000);   
    var myimaget=new Image()
			myimaget.src=imggallery[currentpos][0]
			
			
		
		
}
else {
getstarted(630,635, 'imgloader',0,'0', current_imgid,null,null,'true');
Start()
currentpos=0;
currentthumbpos=0;
}
}
function Start() {
 tStart   = new Date();
   timerID  = setTimeout("UpdateTimer()", 8000);
   
   var myimaget=new Image()
			myimaget.src=imggallery[1][0]


 }
 
 
 
 function Stop() {


   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   
     // currentpos = currentpos-1;
   }
     document.getElementById('imgStop').src="inc/stop-grey.gif";
   document.getElementById('imgPlay').src="inc/play.gif";
//    
    

   
   tStart = null;
   
}


function Start() {

   tStart   = new Date();
   timerID  = setTimeout("UpdateTimer()", 5000);
   
// 	var imgStop=document.getElementById('imgStop')
// 		var imgPlay=document.getElementById('imgPlay')
// document.imgStop.src ="inc/StopWhite.gif";
//     document.imgPlay.src= "inc/grayPlay.gif";
      document.getElementById('imgStop').src="inc/stop.gif";
   document.getElementById('imgPlay').src="inc/play-grey.gif";
}
