var theModelCarousel = null;

// initialisation du formulaire d'ajout aux favoris
function init_ProductAddToCartForm()
{
	$("form[id^=ProductAddToCartForm_]").submit(function() {
		s = $(this).serialize();
		obj = $(this);
		
		if($(obj).hasClass('is_bt_coeur'))
		{
			//console.log('boo');
			var pdiv = $('<div style="position: absolute; z-index: 1;" class="pt_coeur"></div>');
			var offset = $(this).find('input.add_to_lovebox_button').offset();
			
			if(offset == null)
			offset = $(this).find('input.add_to_lovebox').offset();
			
			//console.log($(pdiv).offset());
			//console.log($(pdiv).position());
			//console.log(offset);
			var offset2 = $('#content').offset();
			
			var offset3 = $('a.lovebox').offset();
			
			$(pdiv).offset({top: offset.top, left: (offset.left - offset2.left)});
			$('#content').before(pdiv);
			$(pdiv).animate({top: '0px', left: '900px'}, 1000, function(){$(this).remove();});
		}
		
		if(!$(obj).hasClass('select'))
		{
		$.ajax({
			type: "POST",
			data: s,
			url: $(this).attr("action"),
			success: function(retour){
				var obj_quant = $(obj).find("[id^=ProductQuantity_]");
				if($(obj_quant).val() == 0)
				{
					$(obj).removeClass("select");
					$(obj_quant).val('1');
					
					if($(obj).hasClass('rem_table'))
					$(obj).parent().parent().hide('slow');
				}
				else
				{
					$(obj).addClass("select");
					$(obj_quant).val('0');
				}
				
				$('.cart_total_items').html(' ('+retour+')');
			}
		});
		}
		return false;
	});
	
}

function init_ProductAddCombToCartForm()
{
	$("form[id^=ProductAddCombToCartForm_]").each(function (i) {
		//console.log($(this).attr('id'));
	$(this).submit(function() {
		s = $(this).serialize();
		obj = $(this);
		//if(!$(obj).hasClass('select'))
		//{
		$.ajax({
			type: "POST",
			data: s,
			url: $(this).attr("action"),
			success: function(retour){
				var obj_quant = $(obj).find("[id^=ProductCombQuantity_]");
				
				if($(obj_quant).val() == 0) // si c'était un retrait du panier
				{
					$(obj).removeClass("select");
					$(obj_quant).val('1');
					
					$(obj).parent().hide('slow', function() { 
						//console.log(theModelCarousel);
						//$(this).remove();
						if(theModelCarousel != null)
						{
							theModelCarousel.remove($(this).index());
							//theModelCarousel.reload();
							//console.log('bbb');
						}
						});
					init_ProductAddCombToCartForm();
					//$(obj).hide('slow');
					//$('input[type=submit]', $(obj)).attr('disabled', 'disabled');
					
					//if($(obj).hasClass('rem_table'))
					//$(obj).parent().parent().hide('slow');
				}
				else
				{
					if(!$(obj).hasClass('select'))
					$(obj).addClass("select");
					
					//$(obj_quant).val('0'); // on transforme pour retirer le produit du panier
					
					if(retour)
					{
						$('#contenu_combinaisons').html($(retour).html());
						$('input[type=submit]', $(obj)).attr('disabled', 'disabled');
						init_Comb_carousel();
						init_ProductAddCombToCartForm();
						/*if(typeof init_Comb_carousel == 'function') {
							init_Comb_carousel();
						}*/
					}
				}
			}
		});
		//}
		
		return false;
	})
	});
	
}

function modelCarousel_initCallback(carousel) {
    theModelCarousel = carousel;
    // Callback functuions if needed
}; 

function init_Comb_carousel()
{
	if($('#list_combinaisons').length > 0) {
		
	$('#list_combinaisons').jcarousel({
		initCallback: modelCarousel_initCallback
	});
	}
}

$(document).ready(function() {
	$('#flags > a').click(function(){
		if($('#flags > a').hasClass('active'))
			$('#flags > a').removeClass('active');
		else
			$('#flags > a').addClass('active');
		$("#flags div").slideToggle("slow", function(){
			
		});
		return false;
	});
	
	$('#flags > a').focusout(function(){
		$("#flags div").slideUp("slow");
		$('#flags > a').removeClass('active');
		return false;
	});
	
	if($('#listgroup').length > 0)
	{
	$('#listgroup > a').click(function(){
		if($('#listgroup > a').hasClass('active'))
			$('#listgroup > a').removeClass('active');
		else
			$('#listgroup > a').addClass('active');
		$("#listgroup div").slideToggle("slow", function(){
			
		});
		return false;
	});
	
	$('#listgroup > a').focusout(function(){
		$("#listgroup div").slideUp("slow");
		$('#listgroup > a').removeClass('active');
		return false;
	});
	}
	
	$('#nav_group').mouseover(function(){
		$("#nav_group div").stop(true, true).slideDown("fast");
		$('#nav_group').addClass('active');
		//return false;
	});
	
	$('#nav_group').mouseleave(function(){
		$("#nav_group div").stop(true, true).slideUp("fast");
		$('#nav_group').removeClass('active');
		//return false;
	});
	
	init_ProductAddToCartForm();
	init_ProductAddCombToCartForm();
	
});

$(function() {
	if($(".sliderGallery").mousewheel)
	{
		$(".sliderGallery").mousewheel(function(event, delta) {
			$(this).scrollLeft($(this).scrollLeft()-(delta * 30));
			event.preventDefault();
		});
	}
});

// initialise tout les tooltips, ou seulement les descendants d'un objet
function tooltipFunctions(object) {
	
	var tooltip = document;
	
	if(object != undefined)
	tooltip = object;
	
	$(tooltip).find('a').tooltip({ 
		track: true, 
		delay: 400, 
		showURL: false, 
		showBody: " - ", 
		fade: 200 
	});
	$(tooltip).find('button').tooltip({ 
		track: true, 
		delay: 400, 
		showURL: false, 
		showBody: " - ", 
		fade: 200 
	});
	$(tooltip).find('img').tooltip({ 
		track: true, 
		delay: 400, 
		showURL: false, 
		showBody: " - ", 
		fade: 200 
	});
	$(tooltip).find('input').tooltip({ 
		track: true, 
		delay: 400, 
		showURL: false, 
		showBody: " - ", 
		fade: 200 
	});
}

function tooltip_aide() {
	if($('#aide').length > 0)
	{
		$('#aide').hide();
		$('#bt_aide_simulateur').click(function() {
			return false;
		});
		$('#bt_aide_simulateur').tooltip({
			bodyHandler: function() { 
				var chaine = $(this).attr('href');
				return $(chaine.substring(chaine.lastIndexOf('#'))).html(); 
			}, 
			extraClass: 'tooltip_aide',
			showURL: false
		});
	}
}

function tooltip_compatibilites() {
	if($('#aide_compatibilites').length > 0)
	{
		$('#aide_compatibilites').hide();
		$('.bt_aide_compatibilites').click(function() {
			window.open($(this).attr('href'));
			return false;
		});
		$('.bt_aide_compatibilites').tooltip({
			bodyHandler: function() { 
				return $('#aide_compatibilites').html(); 
			}, 
			extraClass: 'tooltip_aide compatibilites',
			showURL: false
		});
	}
}

function init_scrollPane() {
	//$('.scroll-pane').jScrollPane();
	//$('.scroll-pane').tinyscrollbar();
}

$(document).ready(function(){
	//init_scrollPane()
	tooltipFunctions();
	tooltip_aide();
	tooltip_compatibilites();
});

