var Selection = new Object({

	add:
		function(id)
		{	
			Tooltip.ajax();
			$.get("/ajax/pl/ajax/selection", { data: id, act: "add" }, function(response){
				$("#detail #actions .active").hide();
    			$("#detail #actions .inactive").show();
    			Tooltip.hide();
    			if(response == 'OK') Selection.newRow(id);
			});
		},
	
	erase:
		function()
		{		
			$.weeboxs.open(_translate.selection.erase_selection_confirm, {type: 'box', cancelBtnName: _translate.weebox.cancel, title: _translate.weebox.please_choose, onok:
				function(){
					Tooltip.ajax();
					$.get("/ajax/pl/ajax/selection", { act: "erase" },
						function(data){
							$("#mySelectContent tr").filter("[id!='myselection_pattern']").remove();
    						$("#myselection .count").text("0");
    						$("#myselection .empty").hide();
    						$("#mySelectionPin").attr("src","/images/blocs/punaise_fermer.gif");
    						Tooltip.hide();
  					}, 'text');
  					$.weeboxs.close();
				}
			});
		},
		
	remove:
		function(id)
		{		
			Tooltip.ajax();
			$.get("/ajax/pl/ajax/selection", { act: "del", id: id },function(data){
				$("#detail #actions .active").show();
    			$("#detail #actions .inactive").hide();
    			Tooltip.hide();
			});			
		},
		
	del:
		function(id,class_parent)
		{		
			Tooltip.ajax();
			$(class_parent).css({"opacity": 0.5});
			$.get("/ajax/pl/ajax/selection", { act: "del", id: id },function(data){
				$(class_parent).remove();
    			var count = $("#myselection .count").text();
    			count--;
    			$("#myselection .count").text(count);
    			if (count == 0)
    			{
    				$("#myselection .empty").hide();
    				$("#mySelectionPin").attr("src","/images/blocs/punaise_fermer.gif");
    			}
    			Tooltip.hide();
  			}, 'text');
		},
	newRow:
		function(id)
		{
			var data = id.split('_'); 
			var $cell = $("#"+id).parent();
			var $row = $("#myselection_pattern").clone(true);
			var $img = $cell.find("div.NormalCross img");
			var $a = $row.find(".selection a");
			var ahref = $a.attr('href');
			ahref = ahref.replace("url_pattern",data[1]).replace("controller_pattern", _translate.seo[data[0]]);
			$a.attr('href', ahref);

			$rowimg = $(".selection img",$row);
			$rowimg.attr('src',$img.attr('src'));
			$rowimg.attr('alt', $img.attr('alt'));
			 
			$row.attr("id", id);
			
			$(".selection .price",$row).html($cell.find("div.price").attr('title') + ' - ' + $cell.find("div.price").html());
			$(".selection .location",$row).html($cell.find("div.location").html());
			$(".selection .info",$row).html($cell.find(".NormalAdText span.title").html());
			$("#myselection_pattern").before($row.show());
			Selection.count();
		},
	count:
		function()
		{
			var count = $("#mySelectContent tr").length - 1;
			if(count<=0) 
			{
				count = '0';
				$("#mySelectionTxt .empty").hide();
			}
			else
			{
				$("#mySelectionTxt .empty").show();
			}
			$("#mySelectionTxt .count").text(""+count+"");
		}
});

$(document).ready(function(){
	//Add to selection cart
	$("#detail #actions .active").click(function(){
		var child = $(this).children("span");
		var id = $(child).attr("id");
		Selection.add(id);	
	});
	
	//Delete ad from selection cart 
	$("#detail #actions .inactive").click(function(){
		var child = $(this).children("span");
		var id = $(child).attr("id");
		
		Selection.remove(id);
	});
	
	//Erase selection Cart
	$("#myselection .empty").click(function(){	
		Selection.erase();
	});
	
	//Delete ad from selection cart 
	$("#myselectioncontent .close").click(function(){
		var class_parent = $(this).parents("tr.id_ad");
		var id = $(class_parent).attr("id");

		Selection.del(id, class_parent);
	});
	
	//Toggle arrow
	if ($("#myselection .count").text() != "0")
	{
		$("#mySelectionTws").toggle(
			function(){
				$("#myselectioncontent").hide("slow");
				$(this).attr("src","/images/blocs/ouvrir_ma_selection.gif");
				$("#mySelectionPin").attr("src","/images/blocs/punaise_fermer.gif");
			}, 
			function(){
				$("#myselectioncontent").show("slow");
				$(this).attr("src","/images/blocs/fermer_ma_selection.gif");
				$("#mySelectionPin").attr("src","/images/blocs/punaise_ouvert.gif");
			}
		);
	}
	
	//Select from ad list
	$(".SelectAd img").each(function(i, el){
		$this = $(this);
		var pid = $this.parent().attr('id');
		$this.click(function(e){
			e.preventDefault();
			Selection.add(pid);
			return false;
		});
	});
});