/**
 * Init
 */

$(document).ready( function () {
	
	preset_search_values ();
});

/**
 * Preset search values
 */

function preset_search_values () {
	
	var start_with_search = false;
	var current_url_hash_value = unescape(self.document.location.hash.substring(1));
	
	// Split url hash in components
	var current_url_hash_parts = current_url_hash_value.split("/");
	
	if (current_url_hash_parts.length > 0) {
		
		for (var i in current_url_hash_parts) {
		
			var component = current_url_hash_parts[i];
			
			// Determine component type
			var component_parts = component.split("=");
			
			if (component_parts.length == 2) {
				
				switch (component_parts[0]) {
					
					// Spec value id
					case "spec_value_id":

						// Start with a search, because we have some values to filter on
						if (start_with_search === false)
							start_with_search = true;
					
						// Determine spec value ids
						var selected_spec_value_ids = component_parts[1].split("_");
						
						for (var i in selected_spec_value_ids) {
							
							if (selected_spec_value_ids[i] != "") {
							
								$("#spec_value_"+selected_spec_value_ids[i]).attr("checked", true);
							}
						}
					
						break;
				}
			}
		}
	}
	
	if (start_with_search === true) {
		
		set_call_to_search_action (true);
		
	} else {
		
		set_call_to_search_action ();
		update_search_filter ();
	}
}

/**
 * Set search action
 */

var current_possible_choices = new Array();
var start_filter_block_index = 100;

function set_call_to_search_action (start_search) {
	
	if ( typeof search_result_settings !== "undefined" && search_result_settings) {

		var current_block_choice = '';
		
		// Toggle search items specs
		$(".filter_select").click(function() {
			
			// First hide current choice (if any)
			if (current_block_choice != "") {
				
				current_block_choice.css("display","none");
			}
			
			// Open new choice
			start_filter_block_index++;
			$(this).parent(".filter_block").css("zIndex", start_filter_block_index);
			current_block_choice = $(this).next("div.filter_block_choices");
			current_block_choice.slideToggle("fast", function() {});
			
			// Determine current possible choices
			current_possible_choices = new Array();
			
			$.each(current_block_choice.find("input:checkbox"), function(i,v) {
			
				var element = $(v);
				var id = element.attr("id");				
				
				current_possible_choices.push(id);
			});		
		});
		
		// Hide choices on mouse leave
		var allow_go_search = false;
		
		$(".filter_block").mouseleave(function() {
			
			var block_choice = $(this).find("div.filter_block_choices");
			var current_block_display = block_choice.css("display");
			
			// Go search on this mouse leave action
			if (current_block_display == "block") {
				
				block_choice.css("display", "none");
			
				if (allow_go_search === true) {
				
					go_search (search_result_settings);
				
					allow_go_search = false;
				}
			}
		});

		// Go search when filter checkbox is clicked
		$(".search_filter input:checkbox").click(function(){
	
			allow_go_search = true;
			
			//go_search (search_result_settings);
			
			//$(this).closest("div.filter_block_choices").css("display", "none");
		});
		
		// Go search on request
		if (start_search === true) {
			
			allow_go_search = true;
			
			go_search (search_result_settings);
			
			$(this).closest("div.filter_block_choices").css("display", "none");
		}
	}
}

/**
 * Go search
 */

var search_result_html = '';

function go_search (search_result_settings) {
	
	// Settings
	var search_url_hash = '#';
	var current_value_filter = false;
	
	// Go through each checkbox to check if it is selected
	var spec_value_id = '';
	
	$.each($(".search_filter input:checkbox"), function(i,v) {
		
		var tag = v.tagName;
		var element = $(v);
		var checked = element.attr("checked");
		var name = element.attr("name");
		var id = element.attr("id");
		var value = element.val();
		
		// When it is checked, we can do some action
		if (checked === true) {

			// Spec value ID
			spec_value_id += value+"_";
		}
	});

	// Clean up hash result vars
	spec_value_id = spec_value_id.replace(/_$/, "");
	
	// New search url hash
	if (spec_value_id != '') {
		
		search_url_hash += "spec_value_id="+spec_value_id;
	}
	
	// Add spec value IDs to search request settings
	search_result_settings['spec_value_id'] = spec_value_id;
	
	// Change url with new hash
	var current_url = new String(document.location);
	var current_url_parts = current_url.split("#");
	
	if (current_url_parts.length > 0) {
		
		var url_base = current_url_parts[0];
		var new_url = url_base + search_url_hash;
		document.location = new_url;
	}
	
	// Create search request url
	var search_request_url = "include/search_result.php?";
	
	for (var i in search_result_settings) {
	
		search_request_url += i+"="+search_result_settings[i]+"&";
	}
	
	search_request_url = search_request_url.replace(/&$/, "");
	
	// Replace content with loader
	$("#search_result").empty().html('<div class="loader"><img src="gfx/ajax-loader.gif" alt="" /></div>');
	
	// Load search result
	$("#search_result").load(search_request_url, function() {
		
		update_search_filter ();
	});
}

/**
 * Update search filter
 */

function update_search_filter () {
	
	// Current enabled values
	var current_enabled_values = new Array();
	
	// Fetch active spec value
	var active_spec_value = $("#active_spec_value").val();
	
	if ( typeof active_spec_value !== "undefined" && active_spec_value ) {

		// Explode value to Array
		active_spec_value = active_spec_value.split(',');
		
		// Go through active current specs, and compare these with the new active specs
		$.each($(".search_filter input:checkbox"), function(i,v) {
			
			var tag = v.tagName;
			var element = $(v);
			var checked = element.attr("checked");
			var name = element.attr("name");
			var id = element.attr("id");
			var value = element.val();
			
			if (jQuery.inArray(value, active_spec_value) == -1) {

				if (jQuery.inArray(id, current_possible_choices) == -1) {
				
					$(this).parents("tr").find("label").css("color", "#cccccc");
					$(this).parents("tr").find("label").css("cursor", "default");
					$(this).attr("disabled", "disabled");
				}
				
			} else {
				
				$(this).parents("tr").find("label").css("color", "#000000");
				$(this).parents("tr").find("label").css("cursor", "pointer");
				$(this).attr("disabled", "");
			}
		});
	}
	
	// Fetch selected spec value
	var selected_spec_value = $("#selected_spec_value").val();
	
	if ( typeof selected_spec_value !== "undefined" ) {
		
		// Explode value to Array
		selected_spec_value = selected_spec_value.split(',');
		
		// Updates selected filter values
		update_selected_filter_values (selected_spec_value)
	}
}

/**
 * Update selected filter values
 */

function update_selected_filter_values (current_spec_values) {

	// Be sure all current value cons are hidden
	$(".filter_current_choices").css("display", "none");
	
	// First clean up the current values, because we are going to create a fresh list
	$(".filter_current_choices ul").html("");
		
	if (current_spec_values.length > 0) { 	
		
		// Go through current spec values
		$.each(current_spec_values, function(k, v){
	
			// Create ajax request spec value data url
			var request_url = "data/spec_value_data.php?";
			request_url += "website_id=" + search_result_settings["website_id"] + "&";
			request_url += "country_id=" + search_result_settings["country_id"] + "&";
			request_url += "spec_value_id=" + v;
			
			// Fetch spec detail data through ajax request
			$.ajax({
				
				url: request_url,
				dataType: "xml",
				encoding: "UTF-8",
				success: function(data, status){
					
					var item_tag = "spec_value";
					var spec_id_tag = "spec_id";
					var spec_value_id_tag = "spec_value_id";
					var spec_name_tag = "name";
					var spec_unit_tag = "unit";
					var spec_value_tag = "value";
					
					$(data).find(item_tag).each(function(){
						
						var spec_id = $(this).find(spec_id_tag).text();
						var spec_value_id = $(this).find(spec_value_id_tag).text();
						var spec_name = $(this).find(spec_name_tag).text();
						var spec_unit = $(this).find(spec_unit_tag).text();
						var spec_value = $(this).find(spec_value_tag).text();
						
						// Handle spec value string
						if (spec_value.length > 15) {
							
							spec_value = spec_value.substr(0,13)+"...";
						}
						
						$("#filter_select_"+spec_id+" .filter_current_choices").css("display", "block");
						
						var new_li_html = '';
						new_li_html += '<li>';
						new_li_html += spec_value+' '+spec_unit;
						new_li_html += ' <img onclick="delete_spec_value(\''+spec_value_id+'\');" class="delete_btn" src="gfx/sf_delete.gif" alt="" title="Verwijderen" />';
						new_li_html += '</li>';
						
						$("#filter_select_"+spec_id+" .filter_current_choices ul").append(new_li_html);
					});
				}
			});
		});
	}
}

/**
 * Delete spec value id
 */

function delete_spec_value (spec_value_id) {
	
	$("#spec_value_"+spec_value_id).attr("checked", false);
	go_search (search_result_settings);
}
