
var imgCount;
var currentImg = 1;
var divName = 'image_';
var boxImagesHidden = 1;

function setImgCount(count)
{
   if (count != null)
   {
      imgCount = count;
      return true;
   }
   return false;
}
function setCurrentImg(img)
{

   if (boxImagesHidden == 1) {

       var myRegExp = /nopreload/;
       var divContents = document.getElementById('box_imgs').innerHTML;
       var matchPos1 = divContents.search(myRegExp);

       if (matchPos1 != -1) {
           var newContents = divContents.replace(/<!-- nopreload /g, "<");
           newContents = newContents.replace(/ nopreload -->/g, ">");
           document.getElementById('box_imgs').innerHTML = newContents;
       }

       boxImagesHidden = 0;
   }

   if (img != null)
   {
      if (img <= imgCount)
      {
         document.getElementById(divName + currentImg).style.display = 'none';
         currentImg = img;
         document.getElementById(divName + currentImg).style.display = '';
         
         
         if ((img == imgCount) && (img == 1))
         {
            document.getElementById('next_arrow').className = 'next_arrow_disabled';
            document.getElementById('pre_arrow').className = 'pre_arrow_disabled';

         }else if (img == imgCount)
         {
            document.getElementById('next_arrow').className = 'next_arrow_disabled';
            document.getElementById('pre_arrow').className = 'pre_arrow';

         }else if (img == 1)
         {
            document.getElementById('pre_arrow').className = 'pre_arrow_disabled';
            document.getElementById('next_arrow').className = 'next_arrow';
         }
         
         else
         {
            document.getElementById('pre_arrow').className = 'pre_arrow';
            document.getElementById('next_arrow').className = 'next_arrow';
         }
         document.getElementById('img_count').innerHTML = 'Showing ' + currentImg + ' of ' + imgCount;
      }

   }
   return false;
}
function nextImg()
{
   if (currentImg < imgCount)
   {
      counter = currentImg + 1;
      setCurrentImg(counter);
   }
}
function previousImg()
{
   if (currentImg > 1)
   {
      counter = currentImg - 1;
      setCurrentImg(counter);
   }
}
function showBox(box1){

    $('overlay').show();
    $(box1).show();
    center(box1);

    return false;
}

function hideBox(box1, box2, box3, box4){
    $(box1).hide();
    $(box2).hide();
    $(box3).hide();
    $(box4).hide();
    $('overlay').hide();
    return false;
}

function center(element){
    try{
        element = $(element);
    }catch(e){
        return;
    }

    var my_width  = 0;
    var my_height = 0;

    if ( typeof( window.innerWidth ) == 'number' ){
        my_width  = window.innerWidth;
        my_height = window.innerHeight;
    }else if ( document.documentElement && 
             ( document.documentElement.clientWidth ||
               document.documentElement.clientHeight ) ){
        my_width  = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if ( document.body && 
            ( document.body.clientWidth || document.body.clientHeight ) ){
        my_width  = document.body.clientWidth;
        my_height = document.body.clientHeight;
    }

    element.style.position = 'absolute';
    element.style.zIndex   = 4000002;

    var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}

function overlayHeight(){

   function GetHeight()
      {
            var y = 0;
            if (self.innerHeight)
            {
                  y = self.innerHeight;
            }
            else if (document.documentElement && document.documentElement.clientHeight)
            {
                  y = document.documentElement.clientHeight;
            }
            else if (document.body)
            {
                  y = document.body.clientHeight;
            }
            return y;
      }

   var bdimensions = $('middle_body').getDimensions();
   var bheight = (bdimensions.height + 250);
   var winHeight = GetHeight();

   if (bheight > winHeight) 
      {
         $('overlay').setStyle({ height: bheight + 'px' });
      }
   else {
      $('overlay').setStyle({ height: winHeight + 'px' });
   }
}