/********************** Cufon **********************/
/*    Outside document.ready for IE purposes :-)   */
Cufon.replace(".block-title");
Cufon.replace("#block-welcome-text h2");
Cufon.replace("div#votingstats h5");
Cufon.replace("div#pagetitle1 h1");
Cufon.replace("div#pagetitle2 h1");
Cufon.replace("div.latestnews h1");
Cufon.replace("div#mainmessage3 h1");
Cufon.replace("div.event_tickets h3");

var heroReady = false;
var heroSlideTime = 5000; // Time in milliseconds
var currentHeroSlide = 0;
var heroTimeoutId = null;

function animateHomepageHero(e)
{
  // Clicked? If not...
  if (!e || e.type != 'click')
  {
    // Work out which item we need to be (virtually)
    var numSlides = $("#hero-items .item").length;
    if (currentHeroSlide == numSlides-1)
    {
      var obj = $("#hero-items .item:first");
    }
    else
    {
      var obj = $("#hero-items .item:nth(" + (currentHeroSlide+1) + ")");
    }
  }
  else
  {
    var obj = $(this).closest("li.item");
  }
  
  // Disable 'active' state on links
  $(obj).closest("ul").find("li").removeClass("item-active");
  
  // Add 'active' on this link
  $(obj).addClass("item-active");

  // Get currently active slide
  var activeLink = $("#hero-items .item-active:first");
  var pos = activeLink.prevAll("li").length;
  
  // Slide appropriate slide into shot
  var height = $("#hero-items .item:first").height();
  var difference = currentHeroSlide - pos;

  $("#hero-items").animate({ 'marginTop': "+=" + (height * difference) }, 300, "linear", function () {

    // Are we on the /cloned/ last slide?
    if (currentHeroSlide == $("#hero-items .item").length-2)
    {
      // Switch between this and number 1
      $("#hero-items").css("marginTop", "0");
      pos = 0;

      obj = $("#hero-items .item:first");
      
      // Disable 'active' state on links
      $(obj).closest("ul").find(".item").removeClass("item-active");
      
      // Add 'active' on this link
      $(obj).addClass("item-active");
    }
    
    // Update current slide indicator
    currentHeroSlide = pos;
    clearTimeout(heroTimeoutId);
    
    // Reset timeout\
    heroTimeoutId = setTimeout("animateHomepageHero()", heroSlideTime);
    
  });
  
  return false;
}

$(document).ready( function() {
 
  
  /********************** Hero links **********************/
  var currentSlide = 0;
  var inTransition = false;
  
  $(".hero-nav a").click( function() {

    // Get destination, strip # off
    var destination = $(this).attr("href").replace(/^#/, "");
    
    // Get height of this slide, all the same
    var height = $(this).closest("li.item").height();
    
    // Parent container
    var container = $(this).closest("li.item").closest("ul");
    
    // Current position and destination
    var destLi = $(container).find("a[name='" + destination + "']:first").closest("li.item");
    var destPos = $(destLi).prevAll("li").length;
    var difference = Math.abs(destPos - currentHeroSlide);
    
    // Take the nav out
    inTransition = true
    var navHeight = $(this).closest("li.item").find(".hero-overlay:first").outerHeight();
    $(this).closest("li.item").find(".hero-overlay:first").hide().css("bottom", "-" + navHeight);
   
    // Slide container ul to the correct position
    currentHeroSlide = destPos;
    $(container).animate({ 'marginTop':  (height * currentHeroSlide * -1) }, 300, "linear", function()
    {
      $(container).find("li.item").removeClass("item-active");
      $(destLi).addClass("item-active");
      inTransition = false;
      $(destLi).trigger("mouseover");

      // update slide position
      //currentHeroSlide = difference-1;
      clearTimeout(heroTimeoutId);
      
      // Reset timeout
      heroTimeoutId = setTimeout("animateHomepageHero()", heroSlideTime);
    });
    
    return false;
  });

  /********************** Hero overlays **********************/
  
  $(".hero-overlay").hide();
  $("#hero-items li.item").hover(
  

 function() {
     if (!inTransition)
     
      // Hover in - animate overlay in
     var myLi = $(this).closest("li.item").find(".hero-overlay:first");
    
     var height = $(myLi).outerHeight();
     $(myLi).css("bottom", height * -1);
     $(myLi).show();
     $(myLi).animate({ 'bottom': "0" }, 300, "linear");
     },

   function () {
    if (!inTransition)
     
      // Hover out - animate overlay out
     var myLi = $(this).closest("li.item").find(".hero-overlay:first"); 
     var height = $(myLi).outerHeight();

     $(myLi).animate({ 'bottom': "+=" + (height * -1) }, 300, "linear", function ()
       {
        $(myLi).hide();
       });
    
 }



  );


  /********************** Hero auto-slide **********************/

	// Duplicate the last item.  This is so we can wrap back to number 1
	var clonedSlide = $("#hero-items .item:first").clone();
	$("#hero-items").append(clonedSlide);
	
	// Set timeout
	if (typeof heroSlideTime == 'undefined')
	{
	  var heroSlideTime = 3000;
	}
	heroTimeoutId = setTimeout("animateHomepageHero()", heroSlideTime);
  
  /********************** Form submission **********************/
  
  $(".javascript-submit").each( function() {
    
    // Remove nearest submit button
    $(this).parent().find("input:submit").remove()
    
    // Bind this link to the parent form submit
    $(this).bind("click", function() {

      $(this).closest("form").submit();
      
      return false;
    });
    
    // and show
    $(this).css("display", "inline");
  });
});
