$(document).ready(function() {

  $('.products').find('select').sbCustomSelect({showUL: true});

    $('ul.shopby').each(function() {
        var $select = $('<select />');

        $(this).find('a').each(function() {
            var $option = $('<option value="'+$(this).attr('href')+'">'+$(this).html()+'</option>');
            $select.append($option);
        });
		$select.bind("change", function() {
			document.location.href=this[this.selectedIndex].value;
		});
        $(this).replaceWith($select);

    });

  $('input, textarea').placeholder();
  
  
  $('.price-size form').submit(
    function(event){
        //alert("test");
        
        $.post($(this).attr('action') , $(this).serialize());
        $(':input',this).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
        
        $.ajax({
          url: '/ajax/cart-details',
          success: function(data) {
            var obj = $.parseJSON(data);
            //alert( obj.cart.count );
            $('.cart-items-count').html(obj.cart.count);
            //alert(data);
            //alert('Load was performed.');
          }
        });
        
        
        $(this).closest('li').addClass("added",100,function(){$(this).closest('li').removeClass("added",1000)});
        
      
        
        event.preventDefault();
      }
    );
    
    if($('.cart-items-count:contains("0")').length){
     $('.cart-checkout').hide();
      
    }
    
    $('.main form .add-to-cart').closest('form').submit(
      function(event){
          //alert("test");
          
          

          $.post($(this).attr('action') , $(this).serialize());
          $(':input',this).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');
          $.ajax({
            url: '/ajax/cart-details',
            success: function(data) {
              var obj = $.parseJSON(data);
              //alert( obj.cart.count );
              $('.cart-items-count').html(obj.cart.count);
              if($('.cart-items-count:contains("0")').length){
                $('.cart-checkout').hide();
              } else {
                $('.cart-checkout').show();
              }
              
              //alert(data);
              //alert('Load was performed.');
            }
          });


          $('.sb-dropdown .selected').addClass("added",100,function(){$(this).removeClass("added",1000)});
          $('.sb-dropdown .selected').removeClass('selected');
          
          event.preventDefault();
        }
      );
      
    $('.forced-back').show();
    
    $('.forced-back').click(function(){history.back();})

	$("form.global-search").submit(function() {
      var $search = $("#search");
      this.action = $(this).data(this.selections.value);
      this.method = $(this).data(this.selections.value+("method"));
      $search.attr("name", $search.data(this.selections.value));
    });
    
    

});
