var catalogId ;
$(document).ready(function(){
	var cookie = $.cookie("jcmUserCookie");
    /*if (cookie != null && cookie.search('1071061') != -1){
        window.location = "/home.html";
        return true;
    }*/
	
    //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 = '';
	}
    $.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;
        }
        if(catalogId == null || catalogId == ''){
	    	   window.location = "/myspesamica.html";
	      }
	      if(catalogId == 1 || catalogId == '1'){
	    	   window.location = "/landing/110527-raddoppiaerisparmia.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' && cookie.search('1071061') == -1) {
            	$("#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['activeCards'] == 'true' ){
	                    myPoint = 0;
	                    if (res['points'] != null && res['points'] != 'undefined') {
	                        $("#myPoint").text("Il tuo saldo punti è " + res['points']);
	                        myPoint = parseInt(res['points']);
	                    }
	                } else 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>");
	                }   
        }
        
        //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;");
            }
            //set element values
            $("#class-" + val[1]).attr("class", classLabel);
            $("#statuspts-" + val[1]).text(textLabel);
            if(textLabel == labelGet){
            	$("#statuspts-" + val[1]).attr("style","cursor: pointer;");        
            	$("#statuspts-" + val[1]).click(function(){
	        		window.location.href = val[6]+'?giftId='+ val[1] +'&giftName='+ val[7] +'&giftPoint='+ val[1] +'-'+ val[2] +'-'+ val[3] +'-'+ val[4] +'&myPoint=' + myPoint;
	        	});
	        }
        });
        //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();
        
    });
    
});

