/**
 * @author Peter Hoose (C) 2007
 */

var isRunning;

function hoverEffect(id, colorType, Loc) {
  var liId = id + Loc;
  var idNav = document.getElementById(liId);  
  if (Loc == "OldIE") {
    idNav = document.getElementById(id);		
  }
  var idNavStyle = idNav.style;
  if (Loc == "TNav" || Loc == "OldIE") {
    if (colorType == "Hover") {
  	  if (!(curMgThumbPos == 0 && (id == "mgMinus" || id == "mgMinusTNav"))) {
	    //if ((id == "mgPlus" || id == "mgPlusTNav") && (curMgThumbPos == -(moveAmt * (totalMgThumbs - visMgThumbs)))) {
      if ((id == "mgPlus" || id == "mgPlusTNav") && (curMgThumbPos <= -(moveAmt * (totalMgThumbs - visMgThumbs)))) {
        
  	    }
  	    else {
	  	  idNavStyle.backgroundColor = "#D90B0B";
		  idNavStyle.cursor = "pointer";
	    }
	  }
    }
    else if (colorType == "Norm") {  	
	  if (!(curMgThumbPos == 0 && (id == "mgMinus" || id == "mgMinusTNav"))) {
		if (id == "mgMinus" || id == "mgMinusTNav") {
	      idNavStyle.backgroundColor = "#000000";			
		  idNavStyle.cursor = "pointer";
		}
	    //if ((id == "mgPlus" || id == "mgPlusTNav") && (curMgThumbPos == -(moveAmt * (totalMgThumbs - visMgThumbs)))) {
      if ((id == "mgPlus" || id == "mgPlusTNav") && (curMgThumbPos <= -(moveAmt * (totalMgThumbs - visMgThumbs)))) {
          idNavStyle.backgroundColor = "#808080";
		  idNavStyle.cursor = "default";
  	    }
  	    else {
	  	  idNavStyle.backgroundColor = "#000000";
		  idNavStyle.cursor = "pointer";
	    }
	  }	  
	  else {
	  	idNavStyle.backgroundColor = "#808080";
		idNavStyle.cursor = "default";
	  }
    }
  }
  if (Loc == "navBar") {
  	idNav.style.backgroundColor = colorType;
   }
}

function buildMainNav() {

  if (!document.getElementById("navBar")) return false;
  var navBar = document.getElementById("navBar");
  var navBarLinks = navBar.getElementsByTagName("a");
  for (var i=0;i<navBarLinks.length;i ++) {
  	var bID = document.getElementsByTagName("body")[0].id;
  	if (navBarLinks[i].id.indexOf(bID) >-1) {
      // Don't highlight the current link
	  navBarLinks[i].style.cursor = "default";
	}
	else {	
  	  navBarLinks[i].onmouseover = function() {
	     hoverEffect(this.id, "#D90B0B", "navBar");
      }
	  navBarLinks[i].onmouseout = function() {
	     hoverEffect(this.id, "#000000", "navBar");
      }
	}
  }
}

function buildGallery() {
  if (!document.getElementById("gCategories")) return false;
  var gCats = document.getElementById("gCategories");
  var links = gCats.getElementsByTagName("a");
  for (var i=0;i<links.length;i++) {
     links[i].onclick = function() {       
        return smoothImageSwap(this.href);
     }
  }
}
function showImgDetails() {
    if ($("mainImgDes")) {
      $("mainImgDes").setStyle('opacity', 0);  
    }
    var efx = new Fx.Style("mainImg", "opacity", {duration:1000}).start(0,1).chain(function() {     
      setTimeout('isRunning = 0', 800);     
      var efx2 = new Fx.Style("imgDetails", "opacity", {duration:800}).start(0,1).chain(function() {        
        if ($("mainImgDes")) {
          var efx3 = new Fx.Style("mainImgDes", "opacity", {duration:500}).start(0,0.6);                    
        }
        else {          
        }
      });      
    });    
}
function updateImgData(imgSrc, dataUrl) {
  new Asset.image(imgSrc, {id: "mainImg", onload: function() {    
    this.setStyle('opacity', 0);                  
    var myAjax = new Ajax(dataUrl, {method: 'get', update: 'imgDetails', onComplete: function(response) {showImgDetails();}});    
    myAjax.request();
    this.injectBefore($('imgDetails')); 
    var newMar = ((($('mainImgWrap').getCoordinates().height / 2) - (this.height / 2)) -50);
    var marChange = new Fx.Style(this, 'margin-top');                             
    marChange.set(newMar);   
  }});
}
function smoothImageSwap(newImg) {

  if (isRunning == 1) {
    return false;
  }
  else {
    isRunning = 1;

    if (!$("mainImg")) {
      return true;
    }
    var imgUrl = newImg + "&o=iPath";
    var dataUrl = newImg + "&o=iData"; 
    var imgSrc;    
    var efx4 = new Fx.Style("imgDetails", "opacity", {duration:200}).start(1,0).chain(function() {
    var efx5 = new Fx.Style("mainImg", "opacity", {duration:500}).start(1,0).chain(function() {            
      $("mainImg").remove();
      if (!document.getElementById('mainImg')) {       
        //var myID = newImg.split("=");
        //location.href = "/cgi-bin/gallery/gallery.pl#?i=" + myID[1];      
        var myAjax = new Ajax(imgUrl, {method: 'get', onComplete: function(response) {updateImgData(response, dataUrl);}});
        myAjax.request();      
      }     
    })
    });  
  }
  return false;  
}

// Trying to figure out how to save state.

//if (location.href.match(/.*\#\?i=\d*/)) {
//  alert("hello");
//  var myID = location.href.split("=");
//  location.href = "gallery.pl?i=" + myID[1];
//}


window.addEvent('domready', function(){      
  buildMainNav();
  buildGallery();  
});


