jQuery(document).ready(function(){
$("#screen-meta-links").hide();
	c = $(".no_account").size();
	if(c == 0){
	$(".info_location").hide();
	$(".confirmation_adresse_modify").click(function(){
			$(this).prev(".info_location").slideToggle();
	});
	};
	$('.alert').slideUp(1);
	$(".error_message#hide1").hide();
	$("#confirm_adress").submit(function(){
		choice_valid = $('input[type=radio][name=delivery_method_choice]:checked').length;
		if(choice_valid != 1){
			$(".error_message#hide1").slideDown();		
			return false;
			
		}
	});
	$('.post').mouseover(function(){
		$(this).children('h2').children('a').children('.nfocc_statu').hide();
		$(this).children('h2').children('a').children('.nfocc_statu').slideDown();
	});
	$('.entry-multiimages img').click(function(){
		url_img = $(this).attr('alt');
		$('.product_image_big').attr('src', url_img);
		$('.product_image_big').attr('alt', url_img);
	});
	$(".product_image_big").jqueryzoom({
				xzoom: 200, //zooming div default width(default width value is 200)
				yzoom: 200, //zooming div default width(default height value is 200)
				offset: 10 //zooming div default offset(default offset value is 10)
				//position: "right" //zooming div position(default position value is "right")
			});
	// Product add or remove button in cart page
	$(".more_product").click(function(){
		siteurl = $('#headerimg h1 a').attr('href');
		rel = $(this).prev("input[type=hidden]").val();
		current = $(this).parent("form").parent("td").parent("tr").attr("id");
		$.ajax({
			type: "POST",
			url: siteurl + "modifi-product-cart-ajax",
			data: "mptcid="+rel,
			cache:false,
			success:function(html){
				affich(html, current, rel);
			},
			error:function(XMLHttpRequest,textStatus, errorThrown){
				alert('Erreur Ajax : '+textStatus+', '+errorThrown);
			}
		})
		return false;
	});
	$(".less_product").click(function(){
		siteurl = $('#headerimg h1 a').attr('href');
		rel = $(this).prev("input[type=hidden]").val();
		current = $(this).parent("form").parent("td").parent("tr").attr("id");
		$.ajax({
			type: "POST",
			url: siteurl + "modifi-product-cart-ajax",
			data: "lptcid="+rel,
			cache:false,
			success:function(html){
				affich(html, current, rel);
			},
			error:function(XMLHttpRequest,textStatus, errorThrown){
				alert('Votre demande à été prise en compte, veuillez attendre la mise à jours du total de votre commande avant de continuer');
			}
		})
		return false;
	});
	// End Product add or remove button
	$(".account_modify_button").next("form").hide();
	$(".account_modify_button").click(function(){
		$(this).prev("ul").slideToggle();
		$(this).next("form").slideToggle();
	});
	$('.trash_cart').click(function(){
		alert('Produits supprimé');
	});
	// Click on button 'add to cart'
	$('.add_product_button').click(function(){
		pos = $(this).parent('form').parent('div').parent('div').parent('.master_post').findPos();
		width = $(this).parent('form').parent('div').parent('div').parent('.master_post').css('width');
		height = $(this).parent('form').parent('div').parent('div').parent('.master_post').css('height');
		pos_cart = $('.shopping_cart_box').findPos();
		width_cart = $('.shopping_cart_box').css('width');
		height_cart = $('.shopping_cart_box').css('height');
		$('<div class="post_ghost" style="width:' + width + ';height:' + height + ';top:' + pos.y + 'px;left:' + pos.x + 'px;"></div>').prependTo('body');
		$('.post_ghost').animate({
			top:pos_cart.y,
			left:pos_cart.x,
			height:height_cart,
			width:width_cart
			},500);
		product_id = $(this).prev('input').attr('value');
		site_url = $('#headerimg h1 a').attr('href');
		$.ajax({
		   type: "POST",
		   url: site_url + "add_product_to_cart_ajax",
		   data: "product_id=post-" + product_id,
		   cache:false,
		   success: function(msg){
				
			   $('.shopping_cart_box_wait').attr('class', 'shopping_cart_box');
			   $('.shopping_cart_box').empty();
			   $(msg).appendTo('.shopping_cart_box');
			   $('.post_ghost').remove();
			},
			error:function(XMLHttpRequest,textStatus, errorThrown){
				alert('Erreur Ajax : '+textStatus+', '+errorThrown);
			}
		});
		return false;
	});
	$('.short_cgv').slideUp(1);
	$('.short_cgv_link').click(function(){
		$('.short_cgv').slideToggle();
		return false;
	});
	
	
	comp = 0;
	vign_courant = 0;
	nbr_vign = 10;
	vignwidth = 170;
	number = $(".carousel_content div").size();
	gal_width = vignwidth*number;
	// Width of list
	$(".carousel_content").css({ width: gal_width+250});
	// Carrousel Effect
	$(".index_arrow_left").click(function() { 
		if(comp != 0){
			comp++;
			vign_courant--;
			$(".carousel_content").animate({ 
				marginLeft: (vignwidth * comp + 20)
			},200); 
		}
    }); 
	$(".index_arrow_right").click(function() { 
		if(vign_courant < nbr_vign-5){
			comp--;
			vign_courant++;
			$(".carousel_content").animate({ 
			marginLeft: (vignwidth * comp + 20)
			},200); 
		}
	}); 	
	$('.widget_categories ul ul').prev('a').click(function(){
		if($(this).children('b').hasClass('upper')){
			$(this).parent('li').children('ul').slideDown();
			$(this).children('.upper').removeClass('upper').addClass('lesser');
			return false;
		}else{
			$(this).parent('li').children('ul').slideUp();
			$(this).children('.lesser').removeClass('lesser').addClass('upper');
			return false;
		}
	});
	$('<b class="upper"></b>').appendTo($('.widget_categories ul ul').prev('a'));
	$('.widget_categories ul ul').hide();
	$('.widget_categories .current-cat-parent').children('ul').show();
	$('.widget_categories .current-cat-parent').children('a').children('.upper').addClass('lesser');
	$('.widget_categories .current-cat-parent').parent('ul').show();
	$('.widget_categories .current-cat-parent').parent('ul').parent('li').children('a').children('.upper').addClass('lesser');
	$('.widget_categories a').click(function(){
		
	});

});

function affich(data, current, rel){
	if(data>0){
		product_price = $("#"+current).children(".product_price_unit").text();
		new_price = product_price * data;
		new_price_total = 0;
		$("#"+current).children(".product_quantity").empty();
		$("#"+current).children(".product_quantity").append('x'+data);
		siteurl = $('#headerimg h1 a').attr('href');
		$.ajax({
			   type: "POST",
			   url: siteurl + "modifi-product-cart-ajax",
			   data: "tspid=" + rel,
			   cache:false,
			   success: function(msg){
				$("#"+current).children(".product_quantity").append(msg);
			   },
			   error:function(XMLHttpRequest,textStatus, errorThrown){
					alert('Erreur Ajax : '+textStatus+', '+errorThrown);
			   }
		});
		$("#"+current).children(".product_price_total").empty();
		$("#"+current).children(".product_price_total").append(new_price);
		$(".product_price_total").each(function(){
			this_price = $(this).text();
			new_price_total = new_price_total + parseInt(this_price,10);
		});
		$(".total_cart").empty();
			$.ajax({
			   type: "POST",
			   url: siteurl + "modifi-product-cart-ajax",
			   data: "get_total=true",
			   cache:false,
			   success: function(msg){
				$(".total_cart").append(msg + " €");
			   },
			   error:function(XMLHttpRequest,textStatus, errorThrown){
					alert('Erreur Ajax : '+textStatus+', '+errorThrown);
			   }
		});
	}else{
		price = $("#"+current).children(".product_price_total").text();
		total = $("#"+current).children(".total_cart").text();
		new_price_total = price-total;
		total_price = 0;
		$("#"+current).fadeOut(100);
		$("#"+current).children(".product_price_total").empty();
		$("#"+current).children(".product_price_total").append('0');
		$(".total_cart").empty();
			$.ajax({
			   type: "POST",
			   url: siteurl + "modifi-product-cart-ajax",
			   data: "get_total=true",
			   cache:false,
			   success: function(msg){
				$(".total_cart").append(msg + " €");
			   },
			   error:function(XMLHttpRequest,textStatus, errorThrown){
					alert('Erreur Ajax : '+textStatus+', '+errorThrown);
			   }
		});
	}
}
jQuery.fn.extend({
   findPos : function() {
       obj = jQuery(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
       return {x:curleft,y:curtop};
   }
});

function toggle_3produits(){
	siteurl = $('#headerimg h1 a').attr('href');
	$.ajax({
			   type: "POST",
			   url: siteurl + "modifi-product-cart-ajax",
			   data: "toggle_triple_box=true",
			   cache:false,
			   success: function(msg){
			   },
			   error:function(XMLHttpRequest,textStatus, errorThrown){
					alert('Erreur Ajax : '+textStatus+', '+errorThrown);
			   }
		});
}
$(function(){
	clouds_speed = 20;
   $(".page_item a").mouseover(function(){
		$(this).animate({fontSize:'20px'}, 200);
	});
	$(".page_item a").mouseout(function(){
		$(this).animate({fontSize:'15px'}, 200);
	});
   $(".block_title1 a img").mouseover(function(){
		$(this).animate({width:'220px'}, 200);
	});
	$(".block_title1 a img").mouseout(function(){
		$(this).animate({width:'200px'}, 200);
	});
   $(".block_title2 a img").mouseover(function(){
		$(this).animate({width:'220px'}, 200);
	});
	$(".block_title2 a img").mouseout(function(){
		$(this).animate({width:'200px'}, 200);
	});
   $(".block_title3 a img").mouseover(function(){
		$(this).animate({width:'220px'}, 200);
	});
	$(".block_title3 a img").mouseout(function(){
		$(this).animate({width:'200px'}, 200);
	});
	setInterval('move_cloud()', clouds_speed); 
});
function move_cloud(){
	cloud_1_x = $("#logo_clouds").css('margin-left');
	if(cloud_1_x == '600px'){
		$("#logo_clouds").css('margin-left', '-300px');
		cloud_1_x = '-300px';
	}else{
		cloud_1_x = parseInt(cloud_1_x) + parseInt(1);
		$("#logo_clouds").css('margin-left', cloud_1_x );
	}
		
}

