//infos: http://www.drweb.de/magazin/ajax-mit-dem-jquery-framework/
//http://docs.jquery.com/

var rowTemplate = '<div class="row compare###UID###"><span><a href="###LINK###">###TITLE###</a></span><a onclick="deleteFromCompareList(\'###UID###\'); return false;" href="index.php?id=255&amp;no_cache=1&amp;tx_commerce_pi1[showUid]=0&amp;tx_commerce_pi1[compareUid]=###UID###&amp;tx_commerce_pi1[compareAction]=deleteProduct"><img src="typo3conf/ext/tmpl_gala/res/img/comparison/featurecol/comp-remove.png"/></a><div class="clear"/></div>';

function addToCompareList(uid){
	if ($(".row").length >= 3) {
		alert("Sie können nicht mehr als 3 Produkte vergleichen.");
		return false;
	}
	
	$.get("/index.php?eID=tx_comcomparelist_pi1&no_cache=1&tx_commerce_pi1[ajaxcontroller]=addProduct&tx_commerce_pi1[compareUid]="+uid, 
		  function(result){
			if (result == "addSuccess") {
				
				//could be done better with real dom manipulation/creation of div, span, a, attributes
				theRow = rowTemplate;
				theRow = theRow.replace(/###LINK###/g, $("a.product"+uid)[0].href);
				theRow = theRow.replace(/###TITLE###/g, $("a.product"+uid).html());
				theRow = theRow.replace(/###UID###/g, uid);

				$("#rowlist").append(theRow);
				
				$("#listempty").css("display", "none");
				$("#listfilled").css("display", "inline");
			} else {
				alert("Dieses Produkt haben Sie bereits zur Vergleichsliste hinzugefügt.");
			}
		  }
	);
}

function deleteFromCompareList(uid) {
	$.get("/index.php?eID=tx_comcomparelist_pi1&no_cache=1&tx_commerce_pi1[ajaxcontroller]=deleteProduct&tx_commerce_pi1[compareUid]="+uid, 
		  function(result){
			if (result == "deleteSuccess") {
				$("div.compare"+uid).remove();
				
				if ($("#rowlist").children().length == 0) {
					$("#listempty").css("display", "inline");
					$("#listfilled").css("display", "none");
				}
				
			} else {
				alert("Dieses Produkt wurde bereits aus der Vergleichsliste entfernt.");
			}
		  }
	);
}

