//Determine Broswer Type
var isIE=(document.all)? true:false;
var isW3C=((document.getElementById)&&(isIE==false))? true:false;
var isNS4=((document.layers)&&(isW3C==false))? true:false;
var isMac=(navigator.appVersion.indexOf('Mac') != -1)? true:false;

//Declare persistent variables

var galleryIndex=0; //Current gallery section
var galleryImage=0; //Current gallery image
var prevGalleryImage=0; //previous gallery image
var tempvar=1; //temporary variable to be removed
var logotop=-40 //temp
var logoleft=-40 //temp
var buttonvar=""//"<div class='back'><img src='images/home_grey.png' width='25' height='25' border='0' alt='' id='back1'></div><div class='arrowleft'><img src='images/left_grey.png' width='20' height='25' border='0' alt='' id='back1'></div><div class='arrowright'><img src='images/right_grey.png' width='20' height='25' border='0' alt='' id='back1'></div>" //supplementary content for all pages

var home1="<div class='back' id='backon'><img src='images/home_on.png' width='25' height='25' border='0' alt=''></div><div class='back' id='backoff'><img src='images/home_off.png' width='25' height='25' border='0' alt=''></div><div class='back' onclick="
var home2=" onmouseover='divvis(\"backoff\",\"hidden\")' onmouseout='divvis(\"backoff\",\"visible\")'><a href='#'><img src='images/trans.gif' width='25' height='25' border='0' alt=''></a></div>";
var home2alt="><a href='#'><img src='images/trans.gif' width='25' height='25' border='0' alt=''></a></div>"
var leftarrow1="<div class='arrowleft' id='arrowlefton'><img src='images/left_on.png' width='20' height='25' border='0' alt='next'></div><div class='arrowleft' id='arrowleftoff'><img src='images/left_off.png' width='20' height='25' border='0' alt='next'></div><div class='arrowleft' id='arrowleftblank' onclick="
var leftarrow2=" onmouseover='divvis(\"arrowleftoff\",\"hidden\")' onmouseout='divvis(\"arrowleftoff\",\"visible\")'><a href='#'><img src='images/trans.gif' width='20' height='25' border='0' alt=''></a></div>";
var leftarrow2alt="><a href='#'><img src='images/trans.gif' width='20' height='25' border='0' alt=''></a></div>"
var rightarrow1="<div class='arrowright' id='arrowrighton'><img src='images/right_on.png' width='20' height='25' border='0' alt='next'></div><div class='arrowright' id='arrowrightoff'><img src='images/right_off.png' width='20' height='25' border='0' alt='next'></div><div class='arrowright' id='arrowrightblank' onclick="
var rightarrow2=" onmouseover='divvis(\"arrowrightoff\",\"hidden\")' onmouseout='divvis(\"arrowrightoff\",\"visible\")'><a href='#'><img src='images/trans.gif' width='20' height='25' border='0' alt=''></a></div>";
var rightarrow2alt="><a href='#'><img src='images/trans.gif' width='20' height='25' border='0' alt=''></a></div>"

//write text to div
function filldiv (id,content) {
  if (isW3C){
    x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = content;
  }
  else if (isIE) {
		x = document.all[id];
		x.innerHTML = '';
		x.innerHTML = content;
  }
  else if (isNS4) {
		x = document.layers[id];
		x.document.open();
		x.document.write(content);
		x.document.close();
  }
  correctPNG();
}

function imageswap(x,y) {
  var img=document.getElementById('ribbon')
filldiv (x,"<img src='"+y+"'>");
correctPNG();
}


//Show/Hide divs
function divvis(id,state) {
	if (isW3C)	{
		document.getElementById(id).style.visibility = state;
	}
	else if (isIE)	{
		eval(id+'.style.visibility =\"'+state+'\"');
		}
  else if (isNS4)  {
		document.layers[id].visibility = state;
	}
}

//move div
function movediv(id,direction,y) {

  if (direction=='top'){
    if (isW3C)	{
      document.getElementById(id).style.top = y+'px';
    }
    else if (isIE)	{
      eval(id+'.style.top =\"'+y+'\"');
      }
    else if (isNS4)  {
      document.layers[id].top = y;
    }
  }
  else if (direction=='left'){
    if (isW3C)	{
      document.getElementById(id).style.left = y+'px';
    }
    else if (isIE)	{
      eval(id+'.style.left =\"'+y+'\"');
      }
    else if (isNS4)  {
      document.layers[id].left = y;
    }
  }
}

//Set Div Color
function setDivColor(id,state) {
	if (isW3C)	{
		document.getElementById(id).style.backgroundColor = state;
	}
	else if (isIE)	{
		eval(id+'.style.backgroundColor =\"'+state+'\"');
		}
  else if (isNS4)  {
		document.layers[id].backgroundColor = state;
	}
}

//Change div content to mimic pages
function openpage (pagename) {
  content=buttonvar+pageArray[pagename].pageContent;
  filldiv("contentdiv",content);
  if (pageArray[pagename].pageImage!=""){
    filldiv("graphicdiv",pageArray[pagename].pageImage);
  }
  divvis("galleryborder","hidden");
  //divvis("ribbondiv","visible");
  //divvis("graphicdiv","visible");
}

//Array Builder
function makepageArray(pageImage, pageContent) {
 this.pageImage=pageImage;
 this.pageContent=pageContent;
}

//gallerybuilder
function gallerybuilder() {

  var x=7;
  var y=83;
  var galleryType=galleryArray[0][0];
  var incrementer=0;
  var contents=buttonvar+"<span class='gallery'><h2>"+galleryArray[0][0]+"</h2></span><div id='thumbholder'>";

  divvis("galleryborder","hidden");
  //divvis("ribbondiv","visible");
  //divvis("graphicdiv","visible");

  //cycle through galleryArray entries and write thumbnail for each entry
  for (var i=0;i<galleryArray.length;i++ ) {

    //check for new gallery type.  Write title and start new line.
 
    if (galleryType!=galleryArray[i][0]){
      galleryType=galleryArray[i][0];
      incrementer=i;
      contents=contents+"<div class='galleryText' style='top:"+(y+40)+"px;left:0px;'><h2>"+galleryArray[i][0]+"</h2></div>";
      y=y+27;
    }

    //check line length, start new line if needed
    var a=(i-incrementer)/3;

    if (i==0){
      y=y+27;
      x=7;
    }

    else if (a==parseInt(a)){
      y=y+49;
      x=7;
    }

    else {
      x=x+49;
    }

    //write thumbnail
    contents=contents+"<div class='thumbframe' style='position: absolute;left: "+x+"px;top: "+y+"px;'><div class='thumbnail'><a href='javascript:viewGallery("+i+")'><img src='"+"images/gallery/"+galleryArray[i][1]+"/thumb/"+galleryArray[i][2]+"' width='40' height='40' border='0'></a></div></div>";
      
  }
  contents=contents+"</div>";
  filldiv("contentdiv",contents);
  filldiv("graphicdiv",pageArray["gallery"].pageImage);
}

function viewGallery(gindex) {

  galleryIndex=gindex;
  galleryImage=2;
  prevGalleryImage=2;
  var x=7;
  var y=110;
  var tempthumbnail="";
  var tempgraphic="";
  var contents=buttonvar+"<span class='galleryText'><h1>&nbsp;</h1></span><div id='thumbholder'><div class='galleryText' style='top:440px;left:5px;width:640px;'><h4>"+galleryArray[galleryIndex][1]+"</h4></div>";

  for (i=2;i<galleryArray[galleryIndex].length;i++ ) {

    if ((i!=2)) {

      var a=(i-2)/6;

      if (a==parseInt(a)){
        y=110;
        x=x+49;
      }

      else {
        y=y+49;
      } 
    }

    tempthumbnail="<img src='images/gallery/"+galleryArray[galleryIndex][1]+"/thumb/"+galleryArray[galleryIndex][i]+"' width='40' height='40' border='0'>";
    contents=contents+"<div id='thumb"+i+"' class='thumbframe' style='position: absolute;left: "+x+"px;top: "+y+"px;'><div class='thumbnail'><a href=\"javascript:galleryImage="+i+";showGalleryImage("+galleryIndex+","+i+")\")>"+tempthumbnail+"</a></div></div>";

  }

  contents=contents+home1+"'javascript:gallerybuilder()'"+home2;
  contents=contents+leftarrow1+"'javascript:galleryImage--;showGalleryImage(galleryIndex,galleryImage)' onmouseover='if (galleryImage!=2){divvis(\"arrowleftoff\",\"hidden\")};' onmouseout='if (galleryImage!=2){divvis(\"arrowleftoff\",\"visible\")};'"+leftarrow2alt;
  contents=contents+rightarrow1+"'javascript:galleryImage++;showGalleryImage(galleryIndex,galleryImage)' onmouseover='if (galleryImage!=(galleryArray[galleryIndex].length-1)){divvis(\"arrowrightoff\",\"hidden\");}' onmouseout='if (galleryImage!=(galleryArray[galleryIndex].length-1)){divvis(\"arrowrightoff\",\"visible\");}'"+rightarrow2alt;
  contents=contents+"</div></div>";
    
  filldiv("contentdiv",contents);
  divvis("galleryborder","visible");
  showGalleryImage(galleryIndex,"2");

}

function showGalleryImage(arrayindex1,arrayindex2) {

  setDivColor('thumb'+prevGalleryImage,'#212425');
  setDivColor('thumb'+arrayindex2,'#990000');

  filldiv("gallerydiv","<img src='images/gallery/"+galleryArray[arrayindex1][1]+"/"+galleryArray[arrayindex1][arrayindex2]+"' width='533' height='400' border='0' alt='"+galleryArray[arrayindex1][arrayindex2]+"'>");

  if (arrayindex2==(galleryArray[galleryIndex].length-1)) {
    divvis("arrowrightblank","hidden");
    divvis("arrowrighton","hidden");
    divvis("arrowrightoff","hidden");
  }
  else {
    divvis("arrowrightblank","visible");  
    divvis("arrowrighton","visible");  
    divvis("arrowrightoff","visible");  
  }
  if (arrayindex2==2) {
    divvis("arrowleftblank","hidden");
    divvis("arrowlefton","hidden");
    divvis("arrowleftoff","hidden");
  }
  else {
    divvis("arrowleftblank","visible");
    divvis("arrowlefton","visible");
    divvis("arrowleftoff","visible");

  }

  prevGalleryImage=arrayindex2;

}

function viewPress(pindex) {

  for (i=0;i<pressArray.length;i++ ){
    if (pindex==pressArray[i][1]){
      galleryIndex=i
      break;
    }
  }
  galleryImage=2;
  prevGalleryImage=2;
  var x=7;
  var y=110;
  var tempthumbnail="";
  var tempgraphic="";
  var contents=buttonvar+"<span class='pressText'><h1>&nbsp;</h1></span><div id='thumbholder'><div class='pressText''><h4>"+pressArray[galleryIndex][1]+"</h4></div>";

  for (i=2;i<pressArray[galleryIndex].length;i++ ) {

    if ((i!=2)) {

      var a=(i-1)/6;

      if (a==parseInt(a)){
        y=110;
        x=x+49;
      }

      else {
        y=y+49;
      } 
    }

    tempthumbnail="<img src='images/press/"+pressArray[galleryIndex][0]+"/thumb/"+pressArray[galleryIndex][i]+"' width='40' height='40' border='0'>";
    contents=contents+"<div id='thumb"+i+"' class='thumbframe' style='position: absolute;left: "+x+"px;top: "+y+"px;'><div class='thumbnail'><a href=\"javascript:galleryImage="+i+";showPressImage("+galleryIndex+","+i+")\")>"+tempthumbnail+"</a></div></div>";
  }
  contents=contents+home1+"'javascript:openpage(\"press1\")'"+home2;
  contents=contents+leftarrow1+"'javascript:galleryImage--;showPressImage(galleryIndex,galleryImage)' onmouseover='if (galleryImage!=2){divvis(\"arrowleftoff\",\"hidden\")};' onmouseout='if (galleryImage!=2){divvis(\"arrowleftoff\",\"visible\")};'"+leftarrow2alt;
  contents=contents+rightarrow1+"'javascript:galleryImage++;showPressImage(galleryIndex,galleryImage)' onmouseover='if (galleryImage!=(pressArray[galleryIndex].length-1)){divvis(\"arrowrightoff\",\"hidden\");}' onmouseout='if (galleryImage!=(pressArray[galleryIndex].length-1)){divvis(\"arrowrightoff\",\"visible\");}'"+rightarrow2alt;
  contents=contents+"</div></div>";

  filldiv("contentdiv",contents);
  divvis("galleryborder","visible");
  showPressImage(galleryIndex,"2");

}

function showPressImage(arrayindex1,arrayindex2) {

  setDivColor('thumb'+prevGalleryImage,'#212425');
  setDivColor('thumb'+arrayindex2,'#990000');

  filldiv("gallerydiv","<img src='images/press/"+pressArray[arrayindex1][0]+"/"+pressArray[arrayindex1][arrayindex2]+"' width='533' height='400' border='0' alt='"+pressArray[arrayindex1][arrayindex2]+"'>");

  if (arrayindex2==(pressArray[galleryIndex].length-1)) {
    divvis("arrowrightblank","hidden");
    divvis("arrowrighton","hidden");
    divvis("arrowrightoff","hidden");
  }
  else {
    divvis("arrowrightblank","visible");  
    divvis("arrowrighton","visible");  
    divvis("arrowrightoff","visible");  
  }
  if (arrayindex2==2) {
    divvis("arrowleftblank","hidden");
    divvis("arrowlefton","hidden");
    divvis("arrowleftoff","hidden");
  }
  else {
    divvis("arrowleftblank","visible");
    divvis("arrowlefton","visible");
    divvis("arrowleftoff","visible");
  }

  prevGalleryImage=arrayindex2;

}


function correctPNG() {
  if ((isIE)&&(isMac==false)) {
    for(var i=0; i<document.images.length; i++) {
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
          var imgID = (img.id) ? "id='" + img.id + "' " : "";
          var imgClass = (img.className) ? "class='" + img.className + "' " : "";
          var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
          var imgStyle = "display:inline-block;" + img.style.cssText;
          if (img.align == "left") {
            imgStyle = "float:left;" + imgStyle;
          }
          if (img.align == "right") {
            imgStyle = "float:right;" + imgStyle;
          }
          if (img.parentElement.href) {
            imgStyle = "cursor:hand;" + imgStyle;
          }
          var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
          img.outerHTML = strNewHTML;
          i = i-1;
      }
    }
  x = document.all['loadinganimationdiv'];
	x.innerHTML = '';
	x.innerHTML = '<img src="images/loadinganimation.gif" width="39" height="39" border="0" alt="">';
  }
}