$(document).ready(function($) {
  
  $('.slideshow').cycle();
  
  setTimeout(function () { $('.empire-slideshow').cycle() }, 1000);
  
  setTimeout(function () { $('.victorian-slideshow').cycle() }, 2000);
      
  // Add Accept:text/javascript header to jQuery ajax requests
	// Add authenticity token to ajax requests
	$.ajaxSetup({
	  'beforeSend': function(xhr) {
	    xhr.setRequestHeader("Accept", "text/javascript")
	  },
	  data: {
	    authenticity_token : AUTH_TOKEN
	  }
	})
	
  // //Set validation defaults  
  // if(typeof($.validator) != "undefined") {
  //   $.validator.setDefaults({
  //      debug: true,
  //      errorElement: "span",
  //      success: function(span) {
  //        span.text("ok!").addClass("success");
  //      }
  //    }); 
  // }
  
  // Thumbnail Switcher
  $('a.thumbnail').live('click', function() {
    var self = this;
    $('#product-images').prepend('<a href="' + $(this).attr('href') + '" class="thickbox"><img src="' + $(this).attr('href').replace(/_x?large/, "_large") + '" style="display: none" /></a>');
    $('#product-images img:nth(1)').fadeOut("slow", function() {
      $(this).parents('#product-images').find('img').fadeIn("slow");
      $(this).remove();
    });
    return false;
  });
  
  // Product Options Quantity
  $('select#product_option').change(function() {
    $.getJSON(
      location + "/options/" + $(this).val(),
      function(json) {
        $('select#product_quantity').children().remove();
        for(var i=1; i<=json; i++) {
          $('<option>').attr({value : i, text : i }).appendTo('select#product_quantity');
        }
      }
    );
  });
  
  // Contact Form
  $('form#contact-form').submit(function() {
    $(this).validate({
      rules: {
        'contact[name]' : 'required',
        'contact[email]' : {
          required : true,
          email : true
        },
        'contact[subject]' : 'required',
        'contact[message]' : 'required'
      },
      submitHandler: function(form) {
        form.submit();
      }
    });
    return false;
  });
  
  // Currency Select
  $('#currency_select').change(function() {
    $.ajax({
      type: "POST",
      url: "/currency",
      data : { "_method" : "put", "currency" : $(this).val() },
      success : function() {
        location = location;
      }
    });
  });
  
  $('.thickbox').live("click", function() {
    $.fn.colorbox({ href : this.href });
    return false;
  });
  
  
});