var catalogId ;
$(document).ready(function(){
	
    //set link corrente
    $(".product-list").block({
        message: '<h2>Attendere..</h2>'
    });
    $("#linkPremi1").attr("class", "alpha");
    $("#linkPremi2").attr("class", "current");
    
    //set nome catalogo e validita
    var pdvCode = getUrlParameter('pdvCode');
    catalogId = getUrlParameter('catalogId');
    
    if (pdvCode == null || pdvCode == '') {
        pdvCode = 'no';
    }
    if(catalogId == null || catalogId == ''){
		catalogId = '';
	} else {
		$.getJSON("/AjaxRpc/c4UserValidationWeb.isCatalogueNotExpired?catalogId="+catalogId, null, function(data2, textstatus2){
		       if (!data2.result) {
		       	alert('La versione HTML di questo catalogo non è ancora disponibile!');
		    	   window.location = "/catalogo-premi/catalogo-flash.html?catalogId="+catalogId;
		        }
			});
	}
    $.getJSON("/AjaxRpc/c4UserValidationWeb.getGiftCatalogInfo?pdvCode=" + pdvCode, null, function(data, textstatus){
        if (!data.result) {
            $("#giftCatalogValido").text("");
        }
        else {
            eval('res=' + data.data);
            $("#giftCatalogValido").text(res.valido);
            catalogId = res.catalogId;
        }
        $.getJSON("/AjaxRpc/c4UserValidationWeb.isCatalogueNotExpired?catalogId="+catalogId, null, function(data, textstatus){
 	       if (!data.result) {
 	    	   alert('La versione HTML di questo catalogo non è ancora disponibile!');
		    	   window.location = "/catalogo-premi/catalogo-flash.html";
 	        }
 		});
        
        $("#linkPremi1 a").attr("href", "/catalogo-premi/catalogo-flash.html?pdvCode=" + pdvCode + "&catalogId=" + catalogId);
        $("#linkPremi2 a").attr("href", "/catalogo-premi/catalogo.html?pdvCode=" + pdvCode + "&catalogId=" + catalogId);
        
        //remove product of other catalog
        $("input[id^='catalogo-']").each(function(i){
            var catId = $(this).attr('id').split('-');
            var found = 'N';
            
            for (var t = 2; t < catId.length; t++) {
                if (parseInt(catId[t]) == parseInt(catalogId)) {
                	
                    found = 'S';
                }
            }
            if (found == 'N') {
                $(this).parent().remove();
            }
        });
        
    });
    
    
    //set punti
    $.getJSON("/AjaxRpc/c4UserValidationWeb.retrieveCarrefourClientPoints?", null, function(data, textstatus){

        var ck = $.cookie("jcmUserCookie");
        if (!data.result) {
            myPoint = 0;
            var ck = $.cookie("jcmUserCookie");
            if (ck != null && ck != '' && ck != 'undefined') {
            	$("#myPoint").html("<a href='/index-profile.html?myc4'>Associa</a> la tua SpesAmica al tuo profilo MyCarrefour.");
            } else {
            	$("#myPoint").text("");
            }
            
        } else {
		        eval('res=' + data.data);
		        if(res['statoCarta'] == 'WAIT' ){
					$("#myPoint").text("Associazione in corso, il processo può durare dalle 24 alle 48 ore.");
				}	else if(res['statoCarta'] == 'FAILED' ){
					$("#myPoint").html("I dati del profilo non corrispondono a quelli della tua SpesAmica. <a href='/index-profile.html?myc4'>Verifica.</a>");
				}    else {
		            myPoint = 0;
		            if (res['points'] != null && res['points'] != 'undefined') {
		                $("#myPoint").text("Il tuo saldo punti è " + res['points']);
		                myPoint = parseInt(res['points']);
		            }
				}
        }
        
        //set labels
        $("p[id^='msgContr-']").each(function(i){
        
            var val = jQuery(this).attr('id').split('-');
            var labels = checkStatus(val[2], val[3], val[4],val[5]);
            var classLabel = labels[0];
            var textLabel = labels[1];
            var activeP = labels[2];
            if(val[3] == 0){
            	jQuery(this).html("&nbsp;");
            }
            //if(activeP == 'N'){
            //	$("#stdSelection-" + val[1]).hide();
            //}
            //set element values
            $("#class-" + val[1]).attr("class", classLabel);
            $("#statuspts-" + val[1]).text(textLabel);
        });
        
        //hide product from 1000 to higher
        $("li[class^='filter1Selector']").each(function(i){
            jQuery(this).attr("style", "z-index: 97; display: none;");
        });
        
        //view only product from 0 to 1000 points
        $("li[class='filter1Selector1 filter2Selector1']").each(function(i){
            if (i < 6) {
                jQuery(this).attr("style", "z-index: 97; display: block;");
            }
            else {
                jQuery(this).attr("style", "z-index: 97; display: none;");
            }
        });
        $(".product-list").unblock();
        $("a#filter1Selector1").click();
        
    });
    
});

