$.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

$(document).ready(function() { 
  // auto focus on any field with a focus class
  $(".focus").focus();
  
  $(".product_link").click(function() {
    $.post($(this).attr("href"), null, $(this).parent().toggleClass("selected_item"));
    return false;
  });
    
  $('a[rel*=facebox]').facebox();
  
  $("#subscription_submit").live("click", function() {
    $(this).attr("disabled", "1");
    $(this).attr("value", "Submitting...");
    var div = $(this).parents("#newsletter-form");
    $.ajax({
      type: 'post', 
      data: $(this).parents("form").serialize(), 
      dataType: 'html', 
      url: $("#new_subscription").attr("action") + ".js",
      success: function(data) { div.html(data) }
    });
    return false;
  });

  $("#contact_submit").click(function() {
    $("#contact_submit").attr("disabled", true);
    pageTracker._trackPageview('/goal/Contact-Form-Enquiry');
    jQuery.ajax({
      data: $("#new_contact").serialize(),
      url: "/contacts",
      type: "POST",
      dataType: "text",
      success: function(data) {
        $("#contact_submit").attr("disabled", false);
        $(".flash-success").html(data);
        return false;
      }
    });
    return false;
  });
  
  var htmlStr;
  $(".read-more").toggle( 
    function(){
      htmlStr = $(this).html()
      $(this).html("Collapse &#0060;")
      $(".additional-information").show("blind",null,500,null);
    }, function(){
      $(this).html(htmlStr)      
      $(".additional-information").hide("blind",null,500,null);
  });
  
  // for creative licence page
  if ( $.browser.msie ) {
    var $nav_li_elements = $(".creative_licences_nav li");
    $nav_li_elements.last().addClass("last-child")
    $nav_li_elements.first().addClass("first-child")
  }
  
  //set_content_height(".current div.content");
  $('.current div.content').equalizeHeights();
  
  $(".creative_licences_nav li").click(function(){
    var $this = $(this),
        clicked_element = $this.attr("id").match(/([\w]+)_nav/)[1]
    $(".current").removeClass("current");
    $this.addClass("current");
    $("#"+clicked_element+"_content").addClass("current");
    $('.current div.content').equalizeHeights();
  });
  
  $("div.enquire a").bind('click', function(){
    var $level = $(this).parents("li.level");
        level_name = $level.find("span.level_name span.name").text();
    $("#creative_licence_enquiry_level").val($level.attr("id").match(/\d+/));
    $("#enquire-form").find("span.level").text(level_name);
  }).facebox();
  $(".creative_licence_enquiry_submit").live("click", function(){
    var $this = $(this).parents("form");
    $.facebox.loading();
    $.post($this.attr("action"), $this.serialize(), function(data){
      
      $.facebox(data);
    }, "text");
    return false;
  });
});

function set_content_height(context) {
  var max_height = 0
  $(context).each(function(){
    max_height = $(this).height() > max_height ? $(this).height() : max_height;
  });
  $(context).height(max_height);
}


$.fn.equalizeHeights = function(){
  return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
}