$(document).ready(function() {
  
  // open links with class 'popup' in new windows
  $('a.popup').click(function() {
    window.open($(this).attr('href'));
    return false;
  });  
  
  $('.basket-add').click(function(){
    $('#item-qty').val(parseInt($('#item-qty').val()) + 1);
    return false;
  });
  
  $('.basket-minus').click(function(){
    tmp_val = $('#item-qty').val();
    
    if (parseInt(tmp_val) > 1)
      $('#item-qty').val(parseInt($('#item-qty').val()) - 1);   
      
    return false;
  });  
  
  $('.search-text').focus(function(){
    $(this).val('');
  
  });  
  
  $('.home-rotating').cycle({
		fx: 'scrollRight', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 6000
	});
  
  $('.option').change(function(){
    var all_set = true;
    // make sure we have selected an option from all of the drop downs
    $(this).parent().siblings().find('.option').each(function () { if ($(this).val() == 0) all_set = false; });
    
    if ($(this).val() > 0 && all_set == true)
    {
      /*$('#product-form').addClass('hide');*/
      
      var result = $.ajax({ url: '/catalogue/sku',
                            data: $('#product-option-form').serializeArray(),
                            type: 'POST',
                            success: function(data, textStatus, XMLHttpRequest){
                              $('#sku').html(data);
                              
                              $('#product-form').find('#basket-sku-id').val($('#sku').find('input[name=sku_id]').val());
                              $('#product-form #submit-button').removeClass('hide');
                              $('#product-form #submit-button-disabled').addClass('hide');
                              $('#product-form-error').addClass('hide');
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown){
                              $('#product-form #submit-button').addClass('hide');
                              $('#product-form #submit-button-disabled').removeClass('hide');
                              $('#product-form-error').removeClass('hide');
                            }});
    }
  });
  
  $('.product-image-thumb').click(function(){
    img = this;
    new_src = $(img).find('img').attr('src').replace('_thumb', '_large');
    $(img.parentNode.parentNode.parentNode).find('.large-img').attr('src', new_src);  
    return false;  
  });
  
  $('.signup-text').focus(function(){
    $(this).val('');
  
  });  
  
  $('#product-option-form').submit(function(){
    frm = this;
    /*console.log($(this).serializeArray());
    console.log(window.location);*/
    /*$('.mybasket').load('/basket/add', $(this).serializeArray(), function(){
      bskt = this;
      $(frm.parentNode.parentNode).find('.images img').each(function(i){
        img = $(this).clone();
        img_offset = $(this).offset();
        bskt_offset = $(bskt).offset();
        img.css("position", "absolute");
        img.css('left', img_offset.left + 'px');
        img.css('top', img_offset.top + 'px');
        $('body').append(img);
        img.animate({left: bskt_offset.left, top: bskt_offset.top + $(bskt).height(), opacity: 0}, 'normal', 'swing', function(){ $(this).remove() });
      });
    });*/
    return false;
  });
  
  $('#product-form').submit(function(){
    frm = this;
    /*console.log($(this).serializeArray());*/
    $('.mybasket').load('/basket/add', $(this).serializeArray(), function(){
      bskt = this;
      $(frm.parentNode.parentNode).find('.images img').each(function(i){
        img = $(this).clone();
        img_offset = $(this).offset();
        bskt_offset = $(bskt).offset();
        img.css("position", "absolute");
        img.css('left', img_offset.left + 'px');
        img.css('top', img_offset.top + 'px');
        $('body').append(img);
        img.animate({left: bskt_offset.left, top: bskt_offset.top + $(bskt).height(), opacity: 0}, 'normal', 'swing', function(){ $(this).remove() });
      });
    });
    return false;
  });

  $('#product-area select').click(function() {
     $(this).css("width","auto");
  });
  $('#product-area select').blur(function() {
     $('#product-area select').css("width","135px");
  });
  $('#product-area select option').click(function() {
     $(this).parent().css("width","135px");
  });  
});
