// JavaScript for the home page only
var timer;
$(document).ready(function() {
  //Just run once if it is safari or chrome
  if (jQuery.browser.safari) 
      setTimeout('grayscale($("#homeImageNav").find("a"));', 500);  
  
  $("#homeImageNav").find("a").each(function() {
    $(this).css("backgroundImage", 'url(' + $(this).find("img")[0].src + ')');
    //This is an attribute used to reset the grayscaled image
    //I have to delay safari and chrome since they have issues with the grayscaling
    if (!jQuery.browser.safari)
      grayscale($(this));
    
    $(this).click(function () {
      //Remove the active class from the last selected image
      $("#homeImageNav a").removeClass("active");
      //Reset all images back to grayscale
      grayscale.reset($("#homeImageNav a"));
      grayscale($("#homeImageNav a"));
            
      //Resets the image to the colored version
      grayscale.reset(this);
      $(this).addClass("active"); //Adds the border around the image
      
      //Sets the main background image and background color
      if($("#homeBG1").css("display") == "none") {
        $("#homeBG1").css("backgroundImage", 'url(' + $(this).attr("href") + ')');
        $("#homeBG1").css("backgroundColor", $(this).children()[0].style.borderColor);
        $("body, html").css("backgroundColor", $(this).children()[0].style.borderColor);
        $("#homeBG1").fadeIn(800);
        $("#homeBG2").fadeOut(800, function() { $("#homeBG2").css("display", "none"); } );
      }
      else {
        $("#homeBG2").css("backgroundImage", 'url(' + $(this).attr("href") + ')');
        $("#homeBG2").css("backgroundColor", $(this).children()[0].style.borderColor);
        $("body, html").css("backgroundColor", $(this).children()[0].style.borderColor);
        $("#homeBG2").fadeIn(800);
        $("#homeBG1").fadeOut(800, function() { $("#homeBG1").css("display", "none"); } );
      }
      
      //Set the link
      if($(this).find(":first-child")[0].alt != "" ) {
        $("#homeBillboardLink").attr("href",  $(this).find(":first-child")[0].alt);
        $("#homeBillboardLink").css("display", "block");
      } else
        $("#homeBillboardLink").css("display", "none");
      clearInterval(timer);
      return false;
    });
  });
  //Does the grayscaling
  $("#homeImageNav a").hover(function () { //Make it color
    grayscale.reset($(this));
  }, function () { //Make it gray
    if(!$(this).hasClass("active"))
      grayscale($(this));
  });
  
  //Colorize and set the first image to active
  $("#homeImageNav a")[0].className = "active";
  if (jQuery.browser.safari) 
    setTimeout('grayscale.reset($("#homeImageNav a")[0]);', 500);  
  else
    grayscale.reset($("#homeImageNav a")[0]);
  
  //Select the first image as the active background
  $("#homeBG1").css("backgroundImage", 'url(' + $("#homeImageNav a")[0].href + ')');
  $("#homeBG1").css("background-color", $("#homeImageNav a img")[0].style.borderColor);
  $("body, html").css("background-color", $("#homeImageNav a img")[0].style.borderColor);
  //Set the min height of the div
  $("#homeBG1, #homeBG2").css("height", ($(document).height() - 42) + 'px');
  
  if($("#homeImageNav a img")[0].alt != "" ) {
    $("#homeBillboardLink").attr("href", $("#homeImageNav a img")[0].alt);
    $("#homeBillboardLink").css("display", "block");
  } else
    $("#homeBillboardLink").css("display", "none");
  
  //Preload the images
  $("#homeImageNav a").each(function() {
    var pic1= new Image(1484,684); 
    pic1.src=$(this).attr("href");;
  });
  
  timer = setTimeout('backgroundAnimation()', 6000);
	
});

function backgroundAnimation() {
  cur=$('#homeImageNav a.active');
  next=cur.next();
  if (next.attr('id')==undefined) {
    next=$('#homeImageNav a').children(':first');
	}
  next.click();
  timer = setTimeout('backgroundAnimation()', 6000);
}

