$(document).ready(function(){
  $.ajaxSetup({
		  cache: false
  });

  setupCarousel();
  
  $('#panier .item a.delete').live('click',function(){
    $.ajax({
      dataType: 'html',
      url: this.href,
      type: 'POST',
      success: updatePanier,
      beforeSend: displayLoader
    });
  });
  
  $('.list-products a.ajout-panier').live('click',function(){
     
    $.ajax({
      dataType: 'html',
      url: this.href,//'/panier/ajouter/produit_id/'+this.id.substr('produit_a_'.length),
      type: 'POST',
      success: updatePanier,
      beforeSend: displayLoader
    });
    
  });
  
  //$('.list-products div.container-tooltip').simpletip({content: 'D&eacute;posez ce produit dans votre panier',fixed: true, position: [10, 10]});

  //$('.list-products a.tooltip-link').simpletip({content: 'Voir le d&eacute;tail du produit',fixed: true, position: [-5, -35]});
  
  //$('.list-products .drag, .fiche-product .drag').draggable({helper: 'clone', appendTo: '.wrapper'});
  
  /*$('#panier_container').droppable({
      accept: '.drag',
      hoverClass: 'panier-state-active',
      drop: function(event,ui){
        var id = ui.draggable.attr('id').substr('produit_'.length);
        $.ajax({
            dataType: 'html',
            url: '/panier/ajouter/produit_id/'+id,
            type: 'POST',
            success: updatePanier,
            beforeSend: displayLoader
          });
      }
  })*/
  
  
  setupBlur();
});

function setupBlur(){
  $('input.panier_qte').change(function(event){
    var id = $(this).attr('id').substr('panier_qte_'.length);
    if(!(this.value.match(/^\d+$/) && this.value > 0))
    {
    	this.value = 1;
    }
    if(this.value.match(/^\d+$/) && this.value > 0){
    	var self = this;
      $.get('/panier/modifier',{produit_id: id, qte: this.value},function(data){
    	  d = data.split(';');
    	  if($(self).val() != d[1]){
    		 if(d[1] != 0){
    			 $(self).val(d[1]);
    		 }
    		 else{
    			 $(self).closest('li').remove();
    			 if($('#panier li').length == 0){
    				 $('#panier').html('<p class="empty">Déposez vos produits ici.</p>');
    			 }
    		 }
    		 showStockModal(d[1]);
    	  }
    	  $('#panier_container p.total span').html(d[0]);
      })
    }
  })
}

function setupCarousel(){
  $('#mycarousel').jcarousel({
    vertical: true,
    scroll: 5,
    start: $('#mycarousel li').length,
    initCallback: function(carousel, stat){
	  setTimeout(function(){carousel.next()},100);
	}
  })  
  $('#panier_container').removeClass('panier-state-active');
}
function updatePanier(data){
  $('#panier_container').html(data);
  setupCarousel();
  setupBlur();
}

function displayLoader(){
  $('#panier_container').addClass('panier-state-active');
  $('#panier_loader').show();
}

function initScrollPane(){
  //$('.scroll-pane').jScrollPane({showArrows:true});
}

$(window).bind('load',function() {
  setTimeout(function(){initScrollPane();},1);
});


$(document).ready(function(){
  var autocomplete_click = false;
  $('#autocomplete').click(function(){
    if(autocomplete_click == false){
      autocomplete_click = true;
      $('#autocomplete').val('');
    }
  });
  
  $('#autocomplete').autocomplete('/boutique/search',{
    resultsClass: 'search-result',
    width: 203
  });
  
  $('#autocomplete').result(function(a,b){
    a.preventDefault();
    document.location.href = b[1];
  });
})

$(document).ready(function(){
	$('.list-products td div .thumb').hover(function(){$(this).addClass('thumb-hover')},function(){$(this).removeClass('thumb-hover')})
});

function showStockModal(qte){
	$('#modal').dialog('destroy');
	$('#modal').remove();
	$('body').append('<p id="modal"></p>')
	if(qte > 0){
		$('#modal').html('Nous ne pouvons satisfaire votre demande.<br/> Quantité maximum commandable pour ce produit: <strong>'+qte+'</strong>');
	}
	else{
		$('#modal').html('Nous ne pouvons satisfaire votre demande.<br/> <strong>Ce produit n\'est plus disponible</strong>');
	}
	$('#modal').dialog({
		modal: true, 
		resizable: false, 
		draggable: false, 
		title: "Stock insuffisants",
		width: 350,
		buttons: {
			"Ok": function(){
				$(this).dialog('close');
			}
		}
	});
}

function showViderModal(){
  $('#modal').dialog('destroy');
  $('#modal').remove();
  $('body').append('<p id="modal"></p>')
  
  $('#modal').html('Êtes-vous sûr de vouloir vider votre panier ?');
  
  $('#modal').dialog({
    modal: true, 
    resizable: false, 
    draggable: false, 
    title: "Vider mon panier",
    width: 350,
    buttons: {
      "Ok": function(){
        $.ajax({
          dataType: 'html',
          url: '/panier/vider',
          type: 'POST',
          success: updatePanier,
          beforeSend: displayLoader
        });
        $(this).dialog('close');
      },
      "Annuler": function(){
        $(this).dialog('close');
      }
    }
  });
}
