$("document").ready(function(){
	
	$("#ads>.ad").hover(function(){
		$(this).css("border","1px solid #38548C");
		$(this).children(".operations").css("visibility","visible");
	},
	function(){
		$(this).css("border","1px solid #e0e0e0");
		$(this).children(".operations").css("visibility","hidden");
	});
	
	$(".ad .add").click(function(){

		
		id_part=$(this).parents(".ad").attr("id").split("-");
		var div=$(this);
		var data="ad_id="+id_part[1];		
		$.ajax({
		type: "GET",
		url: "ajax-panier",
		data: data,
		success: function(msg){
			//alert(msg);
			v=div.parents("tr").children(".remove");
			
			try
			{
				v.css("display","table-cell");
			}
			catch(err)
			{
				v.css("display","block");
			}
			
			div.parents("tr").children(".add").css("display","none");
			}	
		});
	});
	
	$(".ad .remove").click(function(){

	var id_part=$(this).parents(".ad").attr("id").split("-");

	var data="ad_id="+id_part[1]+"&delete";
	var div=$(this);
	$.ajax({
	type: "GET",
	url: "ajax-panier",
	data: data,
	success: function(msg){
		div.parents("tr").children(".remove").css("display","none");		
		v=div.parents("tr").children(".add");
		try
		{
			v.css("display","table-cell");
		}
		catch(err)
		{
			v.css("display","block");
		}
		}	
	});
	});
	
	
	$(".ad .show-details").click(function(){
		
		adShowDetails($(this));
	});
	
	$(".ad .hide-details").click(function(){
		
		adHideDetails($(this));
	});
	
	function adShowDetails(div)
	{
		
		div.parents(".ad").children(".padding").children(".desc").children(".details").css("display","block");		
		
		div.parents("tr").children(".show-details").css("display","none");
		
		v=div.parents("tr").children(".hide-details");
		
		
		try
		{
			v.css("display","table-cell");
		}
		catch(err)
		{
			v.css("display","block");
		}
		
		
		
		id_part=div.parents(".ad").attr("id").split("-");
		adUpdatePhotoSize(id_part[1], "max");
		
	}
	
	function adHideDetails(div)
	{
		div.parents(".ad").children(".padding").children(".desc").children(".details").css("display","none");		
		//div.parents("tr").children(".show-details").css("display","table-cell");
		div.parents("tr").children(".hide-details").css("display","none");
		
		v=div.parents("tr").children(".show-details");
				
		try
		{
			v.css("display","table-cell");
		}
		catch(err)
		{
			v.css("display","block");
		}

		id_part=div.parents(".ad").attr("id").split("-");
		adUpdatePhotoSize(id_part[1], "min");
	}
	
	function adUpdatePhotoSize(id, state)
	{
		if(id==null)
		{
			$(".ad .photo").each(function(){
				height=$(this).parent().children(".desc").height();
				$(this).css("height",height);
			});		
		}
		else
		{
			$("#ad-"+id+" .photo").each(function(){
				height=$(this).parent().children(".desc").height();
				$(this).css("height",height);
				if(state=="max")
				{
					$(this).css("width",250);
					$(this).parent().children(".desc").css("margin-left","270px");
				}
				else if(state=="min")
				{
					$(this).css("width",175);
					$(this).parent().children(".desc").css("margin-left","190px");
				}
					
			});		
		}
		
	}
	
	adUpdatePhotoSize();
	

});
