// JavaScript Document
function initGallery ( tempgallery, count, first_id, startwidth, startheight ) {

  document.body.setAttribute("onkeydown","KeyDown(event);");
  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;
  width=imggallery[0][9];
  height=imggallery[0][10];
  if(400*(width/height)>600)
    {
    cwidth=600;
    }
  else
    {
    cwidth=400*(width/height);
    }

  var setloadersize = new Fx.Styles('main_image_wrapper',{duration:transspeed,onComplete: function(){loadfirstimage(currentwidth,currentheight)}});
     var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
    fademe.set(1,0);

    setloadersize.start({
      'width':[400,cwidth],
      'height':[399,400]
    });
}


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




function KeyDown(e)
{
  if (!e) e=window.event;
  var selectName;
  switch(e.keyCode)
  {
  case 37:
    // Key left.
    previmage(current_imgid);
    break;
  case 39:
    // Key right.
    nextimage(current_imgid);
    break;
  }
}


function getstarted(width, height, loadarea, imgindex, img_id, current_imgid){

  // Check if we have a video or not
  if(tempgallery[img_id][12])
    {
    // Set the vimeo parameters
    var movieembedparam=document.getElementById('movieembedparam');
    var source = 'http://player.vimeo.com/video/'+tempgallery[img_id][12]+'?title=0&amp;byline=0&amp;portrait=0';
    movieembedparam.setAttribute('src',source);

    // Set the youtube parameters
    /*var movieembedparam=document.getElementById('movieembedparam');
	movieembedparam.setAttribute('src',tempgallery[img_id][12]);
	var movieparam=document.getElementById('movieparam');
	movieparam.setAttribute('value',tempgallery[img_id][12]);
	*/
   
	var imgloader=document.getElementById('imgloader');
	imgloader.setAttribute('style','display:none');
    var video=document.getElementById('video');
    video.removeAttribute('style');
    }
  else
    {
	var video=document.getElementById('video');
	video.setAttribute('style','display:none');
	var imgloader=document.getElementById('imgloader');
	imgloader.removeAttribute('style');
    }

  var height_custom='';
  var width_custom='';
  if(400*(width/height)>600)
    {
    width=600;
    }
  else
    {
    width=400*(width/height);
    }
  height=400;
  minheight=document.getElementById('left_text').offsetHeight;
  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.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': [400,400],'width': [currentwidth,width]});} });
   fader.start(1,0);
   var fadeiptc = new Fx.Style('iptc_btn','opacity', {duration:transspeed });
   fadeiptc.set(0);
   var titlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
   titlefade.set(0);
   fadeout=0

   var fademe = new Fx.Style('iptc_info','opacity', {duration:transspeed });
   fademe.set(1,0);
  }
}

/** Load the first image */
function loadfirstimage(currentwidth,currentheight){

  // Check if we have a video or not
  if(tempgallery[0][12])
    {
    // Set the vimeo parameters
    var movieembedparam=document.getElementById('movieembedparam');
    var source = 'http://player.vimeo.com/video/'+tempgallery[img_id][12]+'?title=0&amp;byline=0&amp;portrait=0';
    movieembedparam.setAttribute('src',source);

    /*/ Set the youtube parameters
	var movieembedparam=document.getElementById('movieembedparam');
	movieembedparam.setAttribute('src',tempgallery[0][12]);
	var movieparam=document.getElementById('movieparam');
	movieparam.setAttribute('value',tempgallery[0][12]);
	*/

	// Hide the imgloader div and show the video div
	var imgloader=document.getElementById('imgloader');
	imgloader.setAttribute('style','display:none');
	var video=document.getElementById('video');
	video.removeAttribute('style');
    }

  var fadefirst = new Fx.Style('imgloader','opacity', {duration:fadespeed });
  fadefirst.set(0);
  var firsttitlefade = new Fx.Style('imgtitle','opacity', {duration:transspeed });
  firsttitlefade.set(0);
  function setfirstimage(){
    var height_custom='';
    var width_custom='';
    if(400*(tempgallery[0][9]/tempgallery[0][10])>600)
      {
      tmp=tempgallery[0][10]*(600/tempgallery[0][9]);
      tmp=200-tmp/2;
      width_custom='width=600 style="position:relative;top:'+tmp+'px;';
      }
    else
      {
      width_custom='height=400';
      }
    var newHTML = "<img src='"+tempgallery[0][0]+"' "+height_custom+" "+width_custom+" style=\"cursor:pointer;\" onclick=\"javascript:fullres(current_imgid);\" />";
    $('imgloader').setHTML(newHTML);

    var firsttitle=document.getElementById('imgtitle');

    var strChUserAgent = navigator.userAgent;
     var intSplitStart = strChUserAgent.indexOf("(",0);
     var intSplitEnd = strChUserAgent.indexOf(")",0);
     var strChStart = strChUserAgent.substring(0,intSplitStart);
     var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
     var strChEnd = strChUserAgent.substring(strChEnd);

     if(strChMid.indexOf("MSIE") == -1)
      {
       var main_image_wrapper=document.getElementById('main_image_wrapper');
    main_image_wrapper.setAttribute('onclick','javascript:fullres('+current_imgid+')');
      }
    firsttitle.innerHTML="<strong>"+tempgallery[0][4]+"</strong> "+tempgallery[0][7];
    fadefirst.start(0,1);
    currentheight=imggallery[0][2];
    currentwidth=imggallery[0][1];
  }
  new Asset.image(imggallery[0][0], {onload: setfirstimage});
}

var fullrest_allowed=true;
function nextimage(current_imgid){
  fullrest_allowed=false;
  newimgid = Number(current_imgid)+1;
  if(imggallery[newimgid]==undefined)
    {
    return false;
    }
  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;
  height=Number(newheight);
  getstarted(Number(newwidth), height, 'imgloader',Number(newimgindex) ,Number(newimgid) , Number(current_imgid), Number(cwidth), Number(cheight))
  loadinfo();
}

var current_imgid_global=0;

function fullres(current_imgid){
  current_imgid_global=current_imgid;
  setTimeout("fullres2()",100);
}

function fullres2()
  {
  if (fullrest_allowed)
    {
    window.open(imggallery[current_imgid_global][11], '_blank', config = 'height=' + imggallery[current_imgid_global][10] + ',  width=' + imggallery[current_imgid_global][9] + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
    }
  fullrest_allowed=true;
  }

function previmage(current_imgid){

  fullrest_allowed=false;
  newimgid = Number(current_imgid)-1;
  if(imggallery[newimgid]==undefined)
    {
    return false;
    }
  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))
}



