var loaded = 0;
$(document).ready(function() {
	var thispage = "" + window.location;
	var splitted = thispage.split("/");
	var loc = "/" + splitted[splitted.length - 2] + "/"+ splitted[splitted.length - 1];
	var urlTail = splitted[splitted.length - 1];
	var nametemp = "";
	var nomecut = $('a[href$="'+loc+'"]').text();
	nametemp = nomecut.toLowerCase();
	nametemp = nametemp.substring(0,nametemp.length-21);
	nametemp = nametemp.replace("\n"," ");
	nametemp = nametemp.replace(".","-");
	nametemp = nametemp.replace("/","-");
	var nm = nametemp.split(" ");
	loc = urlTail.split(nm[0]);
	var name = "";
	for(var j=1 ;j< loc.length ; j++){
		name += nm[0] + loc[j];
	}
	//var name = nm[0] +  loc[loc.length - 1];
	var recentprod = $.cookie("recentProductsCookie");
	splitted = name.split("_");
	if (thispage.search("index-myc4") == -1) {
		// getting product name - it could be BRAND-PRODUCTNAME or simply -PRODUCTNAME if brand is null
	        //var productname = name[0].substring(name[0].indexOf("-") + 1);
	        var nomeCompleto = "";
	        nomeCompleto = splitted[0];
	        var iniz = "";
	        iniz = splitted[0].substring(0, 1);
	        var finalUrl = iniz + "/";
		finalUrl += nomeCompleto + "_detail_slim_" + splitted[1];
		
		if (recentprod == null) {
			recentprod = "";
		}
		if (recentprod.search(finalUrl) == -1) {
		
			var products = recentprod.split(":");
			if(products.length >= 13){
				products.pop();
				recentprod = "";
				for ( var i = 0; i < products.length - 1; i++) {
					recentprod += products[i] + ":";
				}
			}
			recentprod = finalUrl + ":"+recentprod;
			var date = new Date();
			date.setTime(date.getTime() + 157783320000);
			$.cookie("recentProductsCookie", recentprod, {
				path : '/',
				expires : date
			});
		}
	}
});

function getRecenti() {
	var thispage = "" + window.location;
	var splitted = thispage.split("/");
	var loc = splitted[splitted.length - 1];
	loc = loc.substring(7, loc.length);
	var name = loc.split("_");
	var recentprod = $.cookie("recentProductsCookie");
	if (thispage.search("index-myc4") != -1) {
		if (recentprod != null && recentprod != '' && recentprod != 'undefined') {
			$.ajaxSetup( {
				async : false
			});
			$("#recentProducts").addClass("box productType_1");
			$("#recentProducts").load("/dyncomponent/template/box_recenti.html");
			$("#recentProducts").show();
			//$("#recent_ul li").hide();
			var products = recentprod.split(":");
			/*var remoteUrl = "/AjaxRpc/recentiManager.retrieveRecentProducts?prodotti=" + recentprod;
			alert(remoteUrl);
			$.getJSON(	remoteUrl, null,function(data, textstatus){
								$("#recent_ul").html(data);	
					});*/
			var rec = 0;
			for ( var i = 0; i < products.length - 1 && rec < 12; i++) {
				$("#recent" + (i + 1)).load("/dyncomponent/product/" + products[i], function() {
					rec++;
				});
				//$("#recent" + (i + 1)).show();
				
			}
			$.ajaxSetup( {
				async : true
			});
			// ! Fires Carrousels
			setCarrousel();
			initPdtCarrousel();
			$(document).trigger("loadPrices");
		} else {
			$("#recentProducts").hide();
		}
	}
}

