$(window).load(function() {
  $("div#slider").slideView({ 
		easeFunc: "easeInOutBack", 
		easeTime: 1000,
		toolTip: true 
	});
});

$(function() {
  $('#brands').change(function() {
    if ($(this).val() != "")
      window.location = $(this).val();
  });
  
  $("#slider_marque").easySlider();
  
	$("#first").click(function(){
		$("div#produitMainPic > ul").animate({marginLeft:"0px"}, 800)					
		$("#firstBall").css({ color:'#41423f'});
		return false; 
		});	
	
	$("#second").click(function(){
		$("div#produitMainPic > ul").animate({marginLeft:"-300px"}, 800)
		$("#firstBall").css({ color:'#c0c0c0'}); 
		return false;
		});	
	
	$("#third").click(function(){
		$("div#produitMainPic > ul").animate({marginLeft:"-600px"}, 800)
		$("#firstBall").css({ color:'#c0c0c0'});
		return false;
		});	
		
	$("#fourth").click(function(){
		$("div#produitMainPic > ul").animate({marginLeft:"-900px"}, 800)
		return false;
		});
		
	$("#produitThumb a").click(function(event){
    event.preventDefault();
	  $("#produitMainPic li").hide('fast');
	  $($("#produitMainPic li")[$("#produitThumb a").index(this)]).show('fast');
	});
	
	$('#productFilters select').change(function(event){
	  form = $(event.target).closest('form');
	  $('#nouveautesContainer').animate({opacity: 0.4});
	  $.getJSON($(form).attr('action'),{format: 'js', sort: $(form).find('select').eq(0).val(), filter: $(form).find('select').eq(1).val()},function(data){
	    overall = $('<div>').attr('id','nouveautesContainer');
      if(data.length == 0){
        $('<span>').text('Il n\'y a pas de produits répondant à ces critères').appendTo(overall);
      }
      else{
  	    $(data).each(function(){
  	      container = $('<div>').attr('class','unit size1of4');
  	      div_item = $('<div>').attr('class','newItem');
  	      if(this.product.novelty){
  	        $('<div>').attr('class','newarrival').appendTo(div_item);
          }
          
          a = $('<a>').attr('href','/products/'+this.product.id)
          $('<img>').attr('src',this.product.first_thumb_path).appendTo(a);
          $(a).appendTo(div_item);
          p = $('<p>');
          $('<span>').attr('class','newItemTitle').text(this.product.brand.name).append('<br>').appendTo(p);
          $('<span>').text(this.product.name).append('<br>').appendTo(p);
          
          price = $('<span>').attr('class','price')
          if(this.product.promotion_activated) {
            price.text(this.product.price_promotion+'€').append('<br>');
            $('<span>').attr('class','old-price').text(this.product.price_ttc+'€').appendTo(price);
          } else {
            price.text(this.product.price_ttc+'€');
          }
          
          price.appendTo(p);
          $(p).appendTo(div_item);
          $(div_item).appendTo(container);
          $(container).appendTo(overall);
  	    })
      }
	    $('#nouveautesContainer').html(overall);
	    $('#nouveautesContainer').animate({opacity: 1});
	  })
	  return false;
	});

  //newsletter
  jQuery('form[name="newsletter"] #email').click(function(e){
    var element = e.target;
    var form = $(element).closest('form');
    var token = form.find('input[name="authenticity_token"]').val();
    $.post(form.attr('action'),{checked: element.checked, authenticity_token: token, format: 'js'}, function(data){
      console.log($(element).siblings('label'));
      $(element).siblings('label').animate({opacity:0.4},300).animate({opacity:1},300);
    }, 'json');
  });
});