function ajaxBShow() {
	 if(typeof is_homepage != 'undefined' 
			&& is_homepage) {
				homepageDropdownEvent(0);
	 } else { leftDropdownEvent(0); }
}

function ajaxBHide() {
	if(typeof is_homepage != 'undefined' 
		&& is_homepage) {
			homepageDropdownEvent(1);
	} else { leftDropdownEvent(1); }
}
$(document).ready(function() {	
	$("#delivery-select li input").click(function() {
		$("#delivery-select li").removeClass("selected");
		$(this).parents("li").addClass("selected");
	});

	$("#shipping-select li input").click(function() {
		$("#shipping-select li").removeClass("selected");
		$(this).parents("li").addClass("selected");
	});
	
	// TT - toggle tabs when on dearch results page
	$(".productListing-header-tab").click(function(){
		ref_id = this.id;
		$(".productListing-rows").each(function(){
			$(this).hide();
		});
		$("#sr-"+ref_id).show();
		$(".productListing-header-tab").each(function(){
			$(this).removeClass("productListing-header-active-tab");
		});
		$(this).addClass("productListing-header-active-tab");
	});

});

function showDefaultTabSearchResults() {
	$(".productListing-rows").each(function(){
		$(this).hide();
	});
	$(".productListing-header-active-tab").each(function() {
		ref_id = this.id;
		$("#sr-"+ref_id).show();
	});
}
// AJAX
//


function ajax_response_hp(str) {
	var level = 0;
	var data; var data;
	var obj; var obj2; var i; var j; var l;
	var id;
	eval(str); 
	// alert(str);
	
	var q = "";

	obj = document.getElementById('search_phrase');
	if(obj != null && obj) {
		q = ("&q=" + obj.value);
	}
	

	if(level == 0) { return false; }
	switch(level) {
		case 1:

			obj2 = document.getElementById('search_product');
			id = obj2.options[obj2.selectedIndex].value;

			if(false && data[level-1].length <= 2 && id != null && id != 0) {
				window.location.href = "browse?filter=truk_rax_hp_search&cat_id=" + id + q;
				return false;
			}

			obj = document.getElementById('search_sub_product');
			l = obj.options.length;
			for(i = 0; i < l; ++ i) { obj.remove(0); }
			if(data == null || data[level-1] == null) { break; }
			j = 0;
			for(i = 0; i < data[level-1].length; i += 2) {
				obj.options[j] = new Option(data[level-1][i+1]);
				obj.options[j].value = data[level-1][i];
				++ j;
			}
			++ level;

		case 2:
			obj2 = document.getElementById('search_sub_product');
			id = obj2.options[obj2.selectedIndex].value;
			if(data[level-2].length <= 2 && data[level-2][0] != ''
				&& data[level-2][0] != 0) { ajax_refresh_hp_dd(2); return false; }
			if(false && data[level-1].length <= 2 && id != null && id != 0) {
				window.location.href = "browse?filter=truk_rax_hp_search&cat_id=" + id + q;
				return false;
			}

			obj = document.getElementById('search_make');
			l = obj.options.length;
			for(i = 0; i < l; ++ i) { obj.remove(0); }
			if(data == null || data[level-1] == null) { break; }
			j = 0;
			for(i = 0; i < data[level-1].length; i += 2) {
				obj.options[j] = new Option(data[level-1][i+1]);
				obj.options[j].value = data[level-1][i];
				++ j;
			}
			++ level;

		case 3:
			obj2 = document.getElementById('search_sub_product');
			id = obj2.options[obj2.selectedIndex].value;
			obj2 = document.getElementById('search_make');
			id2 = obj2.options[obj2.selectedIndex].value;
			if(data[level-1].length <= 2 && id != null && id != 0 && id2 != null && id2 != 0) {
				window.location.href = "browse?filter=truk_rax_hp_search&cat_id=" + id + "&brand_id=" + id2 + q;
				return false;
			}

			obj = document.getElementById('search_model');
			l = obj.options.length;
			for(i = 0; i < l; ++ i) { obj.remove(0); }
			if(data == null || data[level-1] == null) { break; }
			j = 0;
			for(i = 0; i < data[level-1].length; i += 2) {
				obj.options[j] = new Option(data[level-1][i+1]);
				obj.options[j].value = data[level-1][i];
				++ j;
			}
			++ level;

		case 4:

			obj2 = document.getElementById('search_sub_product');
			id = obj2.options[obj2.selectedIndex].value;
			obj2 = document.getElementById('search_model');
			id2 = obj2.options[obj2.selectedIndex].value;
			if(data[level-1].length <= 2 && data[level].level <= 2
					&& id != null && id != 0 && id2 != null && id2 != 0) {
				window.location.href = "browse?filter=truk_rax_hp_search&cat_id=" + id + "&brand_id=" + id2 + q;
				return false;
			}

			obj = document.getElementById('search_wheelbase');
			l = obj.options.length;
			for(i = 0; i < l; ++ i) { obj.remove(0); }
			if(data == null || data[level-1] == null) { break; }
			j = 0;
			for(i = 0; i < data[level-1].length; i += 2) {
				obj.options[j] = new Option(data[level-1][i+1]);
				obj.options[j].value = data[level-1][i];
				++ j;
			}
			++ level;

		case 5:

			if (false) {
				obj2 = document.getElementById('search_sub_product');
				id = obj2.options[obj2.selectedIndex].value;
				obj2 = document.getElementById('search_model');
				id2 = obj2.options[obj2.selectedIndex].value;
				if(data[level-1].length <= 2 && data[level].level <= 2
						&& id != null && id != 0 && id2 != null && id2 != 0) {
					window.location.href = "browse?filter=truk_rax_hp_search&cat_id=" + id + "&brand_id=" + id2 + q;
					return false;
				}
			}

			obj = document.getElementById('search_roofheight');
			l = obj.options.length;
			for(i = 0; i < l; ++ i) { obj.remove(0); }
			if(data == null || data[level-1] == null) { break; }
			j = 0;
			for(i = 0; i < data[level-1].length; i += 2) {
				obj.options[j] = new Option(data[level-1][i+1]);
				obj.options[j].value = data[level-1][i];
				++ j;
			}
			++ level;

			break;
		default:
			break;
	}	

	return false;
}

function ajax_response(str) {
	var level = 0;
	var data; var data1; var data2;
	var obj; var i; var j; var l;
	eval(str); 
	if(level == 0) { return false; }
	if(level == 1) { 
		obj = document.getElementById('search_model');
		l = obj.options.length;
		for(i = 0; i < l; ++ i) { obj.remove(0); }

		var obj2 = document.getElementById('search_wheelbase');
		l = obj.options.length;
		for(i = 0; i < l; ++ i) { obj2.remove(0); }

		obj2 = document.getElementById('search_roofheight');
		l = obj.options.length;
		for(i = 0; i < l; ++ i) { obj2.remove(0); }

		if(data) {
			j = 0;
			for(i = 0; i < data.length; i += 2) {
				obj.options[j] = new Option(data[i+1]);
				obj.options[j].value = data[i];
				++ j;
			}
		}
	} else if(level == 2) {
		obj = document.getElementById('search_wheelbase');
		l = obj.options.length;
		for(i = 0; i < l; ++ i) { obj.remove(0); }

		if(data1) {
			j = 0;
			for(i = 0; i < data1.length; i += 2) {
				obj.options[j] = new Option(data1[i+1]);
				obj.options[j].value = data1[i];
				++ j;
			}
		}

		obj = document.getElementById('search_roofheight');
		l = obj.options.length;
		for(i = 0; i < l; ++ i) { obj.remove(0); }

		if(data2) {
			j = 0;
			for(i = 0; i < data2.length; i += 2) {
				obj.options[j] = new Option(data2[i+1]);
				obj.options[j].value = data2[i];
				++ j;
			}
		}
	}
	return false;
}

var xmlhttp=false;

if (!xmlhttp && typeof XMLHttpRequest !='undefined' )  {
     xmlhttp = new XMLHttpRequest();
} else if(!xmlhttp) {
	xmlhttp = navigator.userAgent.indexOf("MSIE 5") >= 0 ?
			"Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
}

function ajax_refresh_dd(level) {
		 var ajax_str = 'ajax_request.php?use_ajax=true&level=' + level;
		 var make_id = 0;
		 var model_id = 0;
		 var obj;
		 obj = document.getElementById('search_make');
		 make_id = obj.options[obj.selectedIndex].value;
		 obj = document.getElementById('search_model');
		 model_id = obj.options[obj.selectedIndex].value;

		 if(level == 1) {
		 	ajax_str += "&search_make=" + make_id;
		 } else if(level == 2) {
		 	ajax_str += "&search_model=" + model_id;
		 }

		 ajaxBShow();
     xmlhttp.open("GET", ajax_str, true);

     xmlhttp.onreadystatechange=function() {

			 if (xmlhttp.readyState==4) {
			 ajaxBHide();
									 ajax_response(xmlhttp.responseText);
			 }
     }
		xmlhttp.send(null);
		return false;

	}

	function ajax_refresh_hp_dd(level) {
		 var ajax_str = 'ajax_request.php?use_ajax=true&hp=true&level=' + level;
		 var make_id = 0;
		 var model_id = 0;
		 var wheelbase = 0;

		 var obj;
		 obj = document.getElementById('search_product');
		 cat_id = obj.value;
		 if(isNaN(cat_id)) { cat_id = obj.options[obj.selectedIndex].value; }
		 obj = document.getElementById('search_sub_product');
		 sub_cat_id = obj.options[obj.selectedIndex].value;
		 obj = document.getElementById('search_make');
		 make_id = obj.options[obj.selectedIndex].value;
		 obj = document.getElementById('search_model');
		 model_id = obj.options[obj.selectedIndex].value;
		 obj = document.getElementById('search_wheelbase');
		 wheelbase = obj.options[obj.selectedIndex].value;

		 if(level == 1) {
		 	ajax_str += "&cat_id=" + cat_id;
		 } else if(level == 2) {
		 	ajax_str += "&sub_cat_id=" + sub_cat_id;
		 } else if(level == 3) {
			ajax_str += "&sub_cat_id=" + sub_cat_id + "&make=" + make_id;
		 } else if(level == 4) {
			ajax_str += "&sub_cat_id=" + sub_cat_id + "&make=" + make_id + "&model=" + model_id;
		 } else if(level == 5) {
			ajax_str += "&sub_cat_id=" + sub_cat_id + "&make=" + make_id + "&model=" + model_id + "&wheelbase="
				+ wheelbase;
		 }


		 ajaxBShow();
		 //alert(ajax_str);
     xmlhttp.open("GET", ajax_str, true);

     xmlhttp.onreadystatechange=function() {
           if (xmlhttp.readyState==4) {
					 ajaxBHide();
						 		 ajax_response_hp(xmlhttp.responseText);
           }
     }
		xmlhttp.send(null);
		reloadProductsList();
		return false;

	}

// TT - additional function to reload products list without reloading page when filtering within category (horizontal filter)
	function reloadProductsList() {
		reloadBrandImagesContainer();
		m_cat_id   = document.getElementById("search_product").value;
		cat_id     = document.getElementById("search_sub_product").value;
		m_brand_id = document.getElementById("search_make").value;
		brand_id   = document.getElementById("search_model").value;
		wheelbase  = document.getElementById("search_wheelbase").value;
		roofheight = document.getElementById("search_roofheight").value;
		filter     = document.getElementById("search_filter").value;
		$.ajax({
			type: 'GET',
			url: 'ajax-fragments?action=reload_products_list&m_cat_id=' +m_cat_id+ '&cat_id=' +cat_id+ '&m_brand_id=' +m_brand_id+ '&brand_id=' +brand_id+ '&wheelbase=' +wheelbase+ '&roofheight=' +roofheight+ '&filter=' +filter,
			success: function(rpl_msg){
				$('#tt_prod_list').html(rpl_msg);
			}
		});
	
		return false;
	}

	function reloadBrandImagesContainer() {
		m_brand_id = document.getElementById("search_make").value;
		brand_id   = document.getElementById("search_model").value;
		$.ajax({
			type: 'GET',
			url: 'ajax-fragments?action=reload_brand_images_container&m_brand_id=' +m_brand_id+ '&brand_id=' +brand_id,
			success: function(rbic_msg){
				$('#brandImagesContainer').html(rbic_msg);
			}
		});
	
		return false;
	}
//

function getElement(elementName) {
// thanks to http://www.netlobo.com/div_hiding.html
if (window.document.getElementById) {
	// this is the way the standards work
	thiselement = window.document.getElementById(elementName);
	return true;
	} else if (window.document.all) {
	// this is the way old msie versions work
	thiselement = window.document.all[elementName];
	return true;
	} else if (window.document.layers) {
	// this is the way nn4 works
	thiselement = window.document.layers[elementName];
	return true;
	}
	return false;
}

function showAlert() {
if (getElement("hoverOverBrowse")) {
  if(thiselement) {
  thiselement.style.display = 'block';
  }
}
}

function hideAlert() {
if (getElement("hoverOverBrowse")) {
  if(thiselement) {
  thiselement.style.display = 'none';
  }
}
}

