$(document).ready(function(){
		   
  // Open external links in new tab/window
  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click(function(){
    this.target = "_blank";
  });
    
  // Return inputs to default value on blur.
  $('input[alt]').focus(function(){
    if ($(this).attr("value") === $(this).attr("defaultValue")) {
      $(this).attr("value", "");
    }
  }).blur(function(){
    if ($(this).attr("value") === "") {
     $(this).attr("value", $(this).attr("defaultValue"));
    }
  });

  // Category menus
  $('#categories.level-0 li').hover(function(){
    $('.level-1', this).show();
  }, function(){
    $('ul', this).hide();
  });
  $('#categories .level-1 li').hover(function(){
    $('.level-2', this).show();
  }, function(){
    $('.level-2', this).hide();
  });

  // Product filters
  $("#filters .bulk-controls .all").click(function() {
    $(this).parents("div").first().find("input").attr("checked", "checked");
    $(this).parents("div").first().find("input").first().change();
    return false;
  });

  $("#filters .bulk-controls .none").click(function() {
    $(this).parents("div").first().find("input").attr("checked", "");
    $(this).parents("div").first().find("input").first().change();
    return false;
  });

  $('.chzn-select').chosen();

  function get_products(page) {
    var request_data = {
      types: $('#types').val()
    }

    var query = $("#product-search input[name=q]").val();
    if( query != "Product Search" ) { request_data.q = query; }

    if( page ) { request_data.page = page; }

    jQuery.post(
      "/products",
      request_data,
      function(data) {
        if( page ) {
          $("#products").append(data);
        } else {
          $("#products").html(data);
        }
        $('li.loading').remove();
      }
    );
  }

  $('#filters').submit(function() {
    products_page = 1;
    $('#products').prepend('<li class="loading top">');
    get_products();
    return false;
  });

  $("#load-more").live('click', function() {
    products_page = products_page + 1;
    $('li.pagination').addClass('loading');
    get_products(products_page);
    return false;
  });

  $("#product-search input[name=q]").focus(function() {
    if ( $(this).val() == "Product Search" ) {
      $(this).val("");
    }
  });

  var products_page = 1;
  
  $(".carousel").cycle({ 
    fx: 'scrollLeft',
    speed: 'fast',
    timeout: 0,
    next: '#next'
  });

  if( $("#extra-links .secondary").length > 0 ) {
    $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=mercator_news&count=2&callback=?', function(json) {
      $(json).each(function(index, value) {
        $("#extra-links .secondary ul").append("<li>@mercator_news: " + value.text + "</li>");
      });
    });
  };
  
  //Accordion Show/Hide
  
  $(".accordion dl dd").hide();
  
  $(".accordion dl dt a").click(function(){
    $(this).parent("dt").next("dd").slideToggle('fast');
    $(this).toggleClass("open");
    return false;
  });
  
  //Email Wishlist
  
  $("#email-friend").colorbox({iframe: true, width:"720", height:"450"});

  $("#footer-subscribe").submit(function() {
    $('#subscriber-text').html('<img src="/images/ajax-loader.gif" alt="loading" />');
    $.post("/subscribers",
           {"subscriber[email]": $("#subscriber_email").val()})
    return false;
  });
  
  // Validation
  
  $("#footer-subscribe").validate();
  $("#footer-find-stockist").validate();
  $("#contact").validate();
  $("#send-wishlist").validate();
  

  $("#colours a").click(function() {
    $.colorbox({href: this.href, maxWidth:"80%", maxHeight:"80%"});
    return false;
  });
  
  $("#banner .wrap").cycle({
    speed: "fast",
    next: "#next",
    prev: "#prev"
  });
  
});

