var HTTP_AJAX_ROOT="http://www.esploro.it/ajax/";
var action=new Array();
action[20]=1; //brand

var added_filter_array=new Array();
var action_suggested=1; //action flag for suggested category

var category_vars=new Object;
var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";

var temp_obj=new Object;

temp_obj.afa_name='';
temp_obj.afa_desc='';
temp_obj.afa_type='';
temp_obj.afa_suffix='';
temp_obj.afa_range='';
temp_obj.afa_crypt='';
temp_obj.afa_pers='';

added_filter_array[0]=temp_obj;

jQuery(document).ready(function(){
	for (var i=0;i<10;i++)
		set_href_stop(i);
		
	set_href_stop('b');
	
	jQuery("#price_max_cat").keydown(function(event) {
		
		// Allow only backspace and delete
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40 || event.keyCode == 9) {
			// let it happen, don't do anything
		} else if (event.keyCode == 13) { //on enter keystroke
			cat_set_price_filter('');
		
		} else {
			// Ensure that it is a number and stop the keypress
			if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
				event.preventDefault(); 
			}   
		}
	});
	
	jQuery("#price_min_cat").keydown(function(event) {
		
		// Allow only backspace and delete
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40 || event.keyCode == 9 ) {
			// let it happen, don't do anything
		} else if (event.keyCode == 13) { //on enter keystroke
			cat_set_price_filter('');
		
		} else {
			// Ensure that it is a number and stop the keypress

			if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
				event.preventDefault(); 
			}   
		}
	});
});


function set_added_filter_vars( filter_id,afa_name,afa_desc,afa_type,afa_suffix,afa_range,afa_crypt,afa_pers) {
	var temp_obj_b=new Object;
	temp_obj_b.afa_name=afa_name;
	temp_obj_b.afa_desc=afa_desc;
	temp_obj_b.afa_type=afa_type;
	temp_obj_b.afa_suffix=afa_suffix;
	temp_obj_b.afa_range=afa_range;
	temp_obj_b.afa_crypt=afa_crypt;
	temp_obj_b.afa_pers=afa_pers;
	
	added_filter_array[filter_id]=temp_obj_b;
	
	action[filter_id]=1; //setting starting action
}

function set_category_vars(id_category,price_min,price_max,drilldown_clause,drilldown_get,drilldown_table,more_filter,page_name_cat,url_order,current_total_link) {
	
	category_vars.id_category=id_category;
	category_vars.price_min=price_min;
	category_vars.price_max=price_max;
	category_vars.drilldown_clause=drilldown_clause;
	category_vars.drilldown_table=drilldown_table;
	category_vars.drilldown_get=drilldown_get;
	category_vars.page_name_cat=page_name_cat;
	category_vars.url_order=url_order;
	category_vars.current_total_link='http://'+decode64(current_total_link);
	
	//if (category_vars.drilldown!="") category_vars.drilldown_text="-"+category_vars.drilldown;
	//else category_vars.drilldown_text="";
}

function set_href_stop(id){
	if (jQuery("#showlistf1_j_"+String(id)).length>0) {
		jQuery("#showlistf1_j_"+String(id)).unbind();
		
		jQuery("#showlistf1_j_"+String(id)).click(function(e) {
			
			e.preventDefault();
			
			var temp_id=String(jQuery(this).attr('id')).substring(13);
			
			if (temp_id=='b') {
				var real_id='b';
				temp_id=0;
				var action_index=20;
			} else {
				var real_id=temp_id;
				var action_index=temp_id;
			}
			
			//alert (HTTP_AJAX_ROOT + "ajax_pc_filter_expand.php?"+"filter_id="+real_id+"&action="+action[action_index]+"&id_category="+category_vars.id_category+"&drilldown_clause="+category_vars.drilldown_clause+"&drilldown_table="+category_vars.drilldown_table+"&price_min="+category_vars.price_min+"&price_max="+category_vars.price_max+"&drilldown_get="+category_vars.drilldown_get+"&page_name_cat="+category_vars.page_name_cat+"&url_order="+category_vars.url_order+"&afa_name="+added_filter_array[temp_id].afa_name+"&afa_desc="+added_filter_array[temp_id].afa_desc+"&afa_type="+added_filter_array[temp_id].afa_type+"&afa_suffix="+added_filter_array[temp_id].afa_suffix+"&afa_range="+added_filter_array[temp_id].afa_range+"&afa_crypt="+added_filter_array[temp_id].afa_crypt+"&afa_pers="+added_filter_array[temp_id].afa_pers);
			
			jQuery.ajax({
				url: HTTP_AJAX_ROOT + "ajax_pc_filter_expand.php",
				data: "filter_id="+real_id+"&action="+action[action_index]+"&id_category="+category_vars.id_category+"&drilldown_clause="+category_vars.drilldown_clause+"&drilldown_table="+category_vars.drilldown_table+"&price_min="+category_vars.price_min+"&price_max="+category_vars.price_max+"&drilldown_get="+category_vars.drilldown_get+"&page_name_cat="+category_vars.page_name_cat+"&url_order="+category_vars.url_order+"&afa_name="+added_filter_array[temp_id].afa_name+"&afa_desc="+added_filter_array[temp_id].afa_desc+"&afa_type="+added_filter_array[temp_id].afa_type+"&afa_suffix="+added_filter_array[temp_id].afa_suffix+"&afa_range="+added_filter_array[temp_id].afa_range+"&afa_crypt="+added_filter_array[temp_id].afa_crypt+"&afa_pers="+added_filter_array[temp_id].afa_pers,
				dataType: 'jsonp',
				jsonp: 'callback',
				jsonpCallback: 'CB_expand_filter'
			});
		});
		
	}
}

function CB_expand_filter(data) {
	
	if (jQuery('#showlistf1_j_'+data.link_id).hasClass('showall_hidden_filter')) {
		var html_hide='<li class="seeall_li"><span id="showlistf1_j" class="show"><a class="grey showall_for_java" href="javascript:pc_hide_filter('+data.link_id+','+data.filter_id+');">Nascondi</a></span></li>';
	} else {
		if (jQuery('#showlistf1_j_'+data.link_id).hasClass('base_hidden_filter')) { //not showing the show all for the filter hidden from the beginning
			var html_hide="";
		} else {
			var html_hide='<li class="seeall_li"><span id="showlistf1_j" class="show"><a class="grey showall_for_java" href="'+category_vars.url_order+'.html" id="showlistf1_j_'+data.link_id+'">'+data.less_filter+'</a></span></li>';
		}
	}
	
	jQuery("#pc_filter_"+data.filter_id).empty();
	
	
	jQuery("#pc_filter_"+data.filter_id).html(data.content_html);
	
	if (data.link_id=='b') 
		var action_index=20;
	else 
		var action_index=data.link_id;
		
	if (data.action==1) {
		action[action_index]=0;
		jQuery("#pc_filter_"+data.filter_id+" td ul").append(html_hide);
	} else {
		action[action_index]=1;
	}

	set_href_stop(data.link_id);
}

function cat_set_price_filter(link) {
	if ((jQuery("#price_min_cat").val()=="" || parseInt(jQuery("#price_min_cat").val())==0) && (jQuery("#price_max_cat").val()=="" || parseInt(jQuery("#price_max_cat").val())==0)) {
		alert ('Devi impostare un prezzo per eseguire la ricerca');
	} else {
	
		if (jQuery("#price_min_cat").val()=="") jQuery("#price_min_cat").val('0');
		if (jQuery("#price_max_cat").val()=="") jQuery("#price_max_cat").val('0');
		
		if (parseInt(jQuery("#price_min_cat").val()) > parseInt(jQuery("#price_max_cat").val())) {
			alert('Il prezzo massimo deve essere superiore a quello minimo');
		} else {
		
			if (link!="") link='http://'+decode64(link)+"-prezzo_da_"+jQuery("#price_min_cat").val()+"_a_"+jQuery("#price_max_cat").val();
			else link=category_vars.current_total_link+"-prezzo_da_"+jQuery("#price_min_cat").val()+"_a_"+jQuery("#price_max_cat").val();
			
			window.location=link+".html";
		}
	}
}

function suggested_more(parent_id,id_category) {
	jQuery.ajax({
		url: HTTP_AJAX_ROOT + "ajax_suggetsted_category.php",
		data: "parent_id="+parent_id+"&action="+action_suggested+"&id_category="+id_category,
		dataType: 'jsonp',
		jsonp: 'callback',
		jsonpCallback: 'CB_suggested_category'
	});
}

function CB_suggested_category(data) {
	jQuery("#related_category_contents ul").empty();
	jQuery("#related_category_contents ul").html(data.content_html);
	
	if (data.action==1) action_suggested=0;
	else action_suggested=1;
}

function pc_hide_filter(link_id,filter_id) {
	var cat_name=jQuery('#pc_filter_'+filter_id+' td strong').html();
	
	jQuery("#pc_filter_"+filter_id).empty();
	jQuery("#pc_filter_"+filter_id).append('<td class="f_col_bb"><a href="#" class="showall_for_java showall_hidden_filter" id="showlistf1_j_'+link_id+'">'+cat_name+'</a></td>');
	
	action[link_id]=1;
	
	set_href_stop(link_id);
}

function CheckSearchPC() {
	if (jQuery.trim(jQuery("#search_string_cat").val())=="") return false;
	return true;
}
function filter_hide_menu(id) {
	jQuery("#"+id).hide();
	jQuery("#"+id+"_link").attr('href',"javascript:filter_show_menu('"+id+"');");
	jQuery("#"+id+"_link").css('backgroundImage',"url('http://www.esploro.it/images/buttons/filter-arrow-right.gif')");
}

function filter_show_menu(id) {
	jQuery("#"+id).show();
	jQuery("#"+id+"_link").attr('href',"javascript:filter_hide_menu('"+id+"');");
	jQuery("#"+id+"_link").css('backgroundImage',"url('http://www.esploro.it/images/buttons/filter-arrow-down.gif')");
}

  function encode64(input) {
     input = escape(input);
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;

     do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
           enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
           enc4 = 64;
        }

        output = output +
           keyStr.charAt(enc1) +
           keyStr.charAt(enc2) +
           keyStr.charAt(enc3) +
           keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

     } while (i < input.length);

     return output;

  }

  function decode64(input) {
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;

     // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
     var base64test = /[^A-Za-z0-9\+\/\=]/g;
     if (base64test.exec(input)) {

     }
     input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
     do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));
 
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;

        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
           output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
           output = output + String.fromCharCode(chr3);
        }
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";

     } while (i < input.length);

     return unescape(output);
  }

