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){
  checknext(img_id)
  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.Height('main_image_wrapper',{duration:transspeed,onComplete: function(){areweready(loadarea,imgindex,img_id);currentheight=height;}})
      var resizeDivWidth=new Fx.Width('main_image_wrapper',{duration:transspeed,onComplete: function(){areweready(loadarea,imgindex,img_id);currentwidth=width;}})
      var fader=new Fx.Opacity('imgloader',{duration:fadespeed,onComplete: function(){resizeDivWidth.custom(currentwidth,width);resizeDivHeight.custom(currentheight,height);}})
      fader.toggle()

      var fadeiptc=new Fx.Opacity('iptc_btn',{duration:transspeed})
      fadeiptc.toggle()
      
      var titlefade=new Fx.Opacity('imgtitle',{duration:transspeed})
      titlefade.toggle()
      
      if(fadeout==0){
        var fademe=new Fx.Opacity('iptc_info',{duration:transspeed})
        fademe.hide()
        fadeout=0
      } else{
        var fademe=new Fx.Opacity('iptc_info',{duration:transspeed})
        fademe.toggle()
        fadeout=0
      }
    }
  }

function loadfirstimage(){
  var firstimageload=new Image()
  firstimageload.src=null
  firstimageload.src=imggallery[0][0]
  var fadefirst=new Fx.Opacity('imgloader',{duration:fadespeed})
  fadefirst.hide()
  
  var firsttitlefade=new Fx.Opacity('imgtitle',{duration:transspeed})
  firsttitlefade.hide()
  
  var setfirstheight=new Fx.Height('main_image_wrapper',{duration:transspeed})
  var setfirstwidth=new Fx.Width('main_image_wrapper',{duration:transspeed})
  
  setfirstwidth.custom(currentwidth,imggallery[0][1])
  setfirstheight.custom(currentheight,imggallery[0][2])
  
  firstimageload.onload=function(){
    var firstimg=document.getElementById('imgloader')
    
    firstimg.innerHTML=returnimgcode(imggallery[0])   
    
    var firsttitle=document.getElementById('imgtitle')
    
    firsttitle.innerHTML="<strong>"+tempgallery[0][4]+"</strong> "+tempgallery[0][7]
    fadefirst.toggle()
    firsttitlefade.toggle()
    currentheight=imggallery[0][2]
    currentwidth=imggallery[0][1]
  }
    
}

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))
}

function previmage(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))
}
