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_response_hp(str) {
//	var level = 0;
	var data;
	var obj; var obj1; var obj2; var obj3; var obj4; var obj5; var obj6; 
	var i; var j; var l; var id;
//alert(str);	
	eval(str);

//alert(level);	
//alert(price_order);	
//	if(level == 0) { return false; }
	obj1 = document.getElementById('horizontalFilterBrands'); // brands drop-down list
	id1  = obj1.options[obj1.selectedIndex].value;
	l    = obj1.options.length;
	for(i = 0; i < l; ++ i) { obj1.remove(0); }
	if(data == null || data[0] == null) { return false; }
	j = 0;
	for(i = 0; i < data[0].length; i += 2) {
		obj1.options[j] = new Option(data[0][i+1]);
		obj1.options[j].value = data[0][i];
		if(data[0][i] == id1) {
			obj1.options[j].selected = true;
		}
		++ j;
	}

	obj2 = document.getElementById('horizontalFilterCategories'); // categories drop-down list
	id2  = obj2.options[obj2.selectedIndex].value;
	l    = obj2.options.length;
	for(i = 0; i < l; ++ i) { obj2.remove(0); }
	if(data == null || data[1] == null) { return false; }
	j = 0;
	for(i = 0; i < data[1].length; i += 2) {
		obj2.options[j] = new Option(data[1][i+1]);
		obj2.options[j].value = data[1][i];
		if(data[1][i] == id2) {
			obj2.options[j].selected = true;
		}
		++ j;
	}

	obj3 = document.getElementById('horizontalFilterColours'); // colours drop-down list
	id3 = obj3.options[obj3.selectedIndex].value;
	l = obj3.options.length;
	for(i = 0; i < l; ++ i) { obj3.remove(0); }
	if(data == null || data[2] == null) { return false; }
	j = 0;
	for(i = 0; i < data[2].length; i += 2) {
		obj3.options[j] = new Option(data[2][i+1]);
		obj3.options[j].value = data[2][i];
		if(data[2][i] == id3) {
			obj3.options[j].selected = true;
		}
		++ j;
	}

	obj4 = document.getElementById('horizontalFilterSizes'); // sizes drop-down list
	id4  = obj4.options[obj4.selectedIndex].value;
	l    = obj4.options.length;
	for(i = 0; i < l; ++ i) { obj4.remove(0); }
	if(data == null || data[3] == null) { return false; }
	j = 0;
	for(i = 0; i < data[3].length; i += 2) {
		obj4.options[j] = new Option(data[3][i+1]);
		obj4.options[j].value = data[3][i];
		if(data[3][i] == id4) {
			obj4.options[j].selected = true;
		}
		++ j;
	}

	obj5 = document.getElementById('horizontalFilterOrderPriceAsc'); // order by price asc button
//	alert(obj5.className);
	//obj6 = document.getElementById('horizontalFilterOrderPriceDesc'); // order by price desc button

	return false;
}


function ajax_refresh_horizontal_filter(level) {
	var ajax_str  = 'ajax-actions.php?use_ajax=true&action=ajax_hf_dropdowns&level=' +level;
	var brand_id  = "";
	var cat_id    = "";
	var colour_id = "";
	var size_id   = "";
//alert(level);

	var obj;
	obj = document.getElementById('horizontalFilterBrands');
	brand_id = obj.options[obj.selectedIndex].value;
	obj = document.getElementById('horizontalFilterCategories');
	cat_id = obj.options[obj.selectedIndex].value;
	obj = document.getElementById('horizontalFilterColours');
	colour_id = obj.options[obj.selectedIndex].value;
	obj = document.getElementById('horizontalFilterSizes');
	size_id = obj.options[obj.selectedIndex].value;

	ajax_str += "&horizontalFilterBrands=" + brand_id + "&horizontalFilterCategories=" + cat_id + "&horizontalFilterColours=" + colour_id + "&horizontalFilterSizes=" + size_id;

	price_order = "";
	if(level==="asc" || level==="desc") {
		ajax_str += "&horizontalFilterOrderPrice=" + level;
		price_order = level;
	}
//	alert(ajax_str);
//	alert(price_order);
//	return false;
//	ajaxBShow();
	xmlhttp.open("GET", ajax_str, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//ajaxBHide();
			ajax_response_hp(xmlhttp.responseText);
		}
	}
	xmlhttp.send(null)

	reloadCarouselContent(brand_id, cat_id, colour_id, size_id, price_order);

	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;
}

// CAROUSEL BACKGROUND FUNCTIONALITY
function reloadCarouselContent(brand_id, cat_id, colour_id, size_id, price_order) {
	$.ajax({
		type: 'GET',
		url: 'ajax-fragments?action=carousel_content&brand_id=' +brand_id+ '&cat_id=' +cat_id+ '&colour_id=' +colour_id+ '&size_id=' +size_id+ '&price_order=' +price_order,
		success: function(cc_msg){
			$('#carouselWrapper').html(cc_msg);
			instanceOne = new ImageFlow();
			instanceOne.init({ ImageFlowID:'dropShopCarousel' });
		}
	});

	return false;
}
