$("#go").hover(function(){
		$("#search-wrapper-right").addClass("hover");
	},
	function(){
		$("#search-wrapper-right").removeClass("hover");
});
$("#search").focus(function(){
	if($(this).val() == "Search by Printer Model or Cartridge Model Number") $(this).val("");
});
$("#search").blur(function(){
	if($(this).val() == "") $(this).val("Search by Printer Model or Cartridge Model Number");
});
$("#header-navegation-wrapper li:gt(0)").hoverIntent(function(){
		$(this).addClass("active");
		if($(this).find('div').length > 0)
			$(this).find('div:first:hidden').slideDown();
	},
	function(){
		$(this).removeClass("active");
		if($(this).find('div').length > 0)
			$(this).find('div:first:not(:hidden)').slideUp("fast");
});
$("#header-navegation-wrapper li:first a").click(function(){
	if($(this).parent().find("div:first").is(":hidden"))	{
		if($("#advanced-search-button").css("top") == "8px")	{
			$(this).parent().find("div:first").slideDown();
			$("#advanced-search-button").animate({"top": "+=140px"}, "linear",function(){
				$(this).css("backgroundPosition","0px 0px");
			});
		}
	}
	else	{
		if($("#advanced-search-button").css("top") == "148px")	{
			$(this).parent().find("div:first").slideUp("fast");
			$("#advanced-search-button").animate({"top": "-=140px"}, "fast", function(){
				$(this).css("backgroundPosition","0px -21px");
			});
		}
	}
	return false;
});
/* Code by MS for Advanced Search */
// Category URL
var caturl = '/s.nl/category.[[catid]]/.f';
// Brands url
var brandsurl = '/ink-cartridges'; 

/* return ajax object */
function AJAXObject() {
	var httpobj = null;
	if(window.ActiveXObject) {
		httpobj = new window.ActiveXObject('Microsoft.XMLHTTP');
	} else if(window.XMLHttpRequest) {
		httpobj = new XMLHttpRequest();
	}
	return httpobj;
}
/* Execute ajax request */
function callUrl(url, callback) {
	var req = AJAXObject();
	
	req.onreadystatechange = function() {
		if(req.readyState  == 4) {
			callback(req.responseText);
		} else {
			// loading process
		}
	}
	req.open('GET',url,true);
	req.send(null);
}
/* Reset combo */
function resetCombo(comboid) {
	document.getElementById(comboid).options.length = 1;
}
/////////////////////////////////////////////////////////////////////////
function loadCombo(response, comoboId, parentValue, currentCombo) {
	resetCombo(comoboId);
	var brands_names = response.split('<PRINTER_' + 'NAME>');
	var brands_ids = response.split('<PRINTER_' + 'ID>');
	
	var optionsList = [];
	var optionsListToSort = [];
    for(var i = 1; (i < brands_names.length && i < brands_ids.length); i++) {
		optionsListToSort[optionsListToSort.length] = brands_names[i].split('</PRINTER_' + 'NAME>')[0].toUpperCase() + 'HeHeHe' + brands_ids[i].split('</PRINTER_' + 'ID>')[0].toUpperCase();
		optionsList[optionsList.length] = brands_names[i].split('</PRINTER_' + 'NAME>')[0] + 'HeHeHe' + brands_ids[i].split('</PRINTER_' + 'ID>')[0];
	}

	optionsListToSort = optionsListToSort.sort();
	var sortedOptions = [];
	for(var i = 0; i < optionsListToSort.length; i++) {
		var printRep = optionsListToSort[i];
		for (var j = 0; j < optionsList.length; j++) {
			if (optionsList[j].toUpperCase() == printRep.toUpperCase()) {
				sortedOptions[sortedOptions.length] = optionsList[j];
			}
		}
	}
	optionsList = sortedOptions;
	var comboBrands = document.getElementById(comoboId);
	for(var i = 1; i <= optionsList.length; i++) {
		var currentBrand_name = optionsList[i-1].split('HeHeHe')[0];
		var currentBrand_id = optionsList[i-1].split('HeHeHe')[1];
		
		var optionText = currentBrand_name; // .replace(parentValue,'').replace(currentCombo,'');
		comboBrands.options[i] = new Option(optionText, currentBrand_id);
	}
}

function loadBrand(response) {
	loadCombo(response, 'finder-brand', '', '');
	var comboBrand = document.getElementById('finder-brand');
	comboBrand.removeAttribute('disabled');
	comboBrand.options[0].text = 'Choose your Printer\'s Brand';
}
function changeBrand() {
	document.getElementById('finder-model').disabled = true;
	var comboSeries = document.getElementById('finder-serie');
	comboSeries.setAttribute('disabled','true');
	comboSeries.options[0].text = 'Loading...';
	
	var selectedBrand = document.getElementById('finder-brand').value;
	callUrl(caturl.replace('[[catid]]',selectedBrand), loadSeriesAndModels);
}
function loadSeriesAndModels(response) {
	try {
		loadSeries(response);
		loadModel('');
		document.getElementById('finder-model').disabled = true;
	} catch (E) { }
}
function loadSeries(response) {
	loadCombo(response, 'finder-serie', new RegExp(document.getElementById('finder-brand').options[document.getElementById('finder-brand').selectedIndex].text.split(' ')[0], "i"), 'Serie');
	var comboBrand = document.getElementById('finder-serie');
	comboBrand.removeAttribute('disabled');
	
	comboBrand.options[0].text = 'Choose your Printer\'s Serie';
}
function changeSerie() {
	var comboModel = document.getElementById('finder-model');
	comboModel.setAttribute('disabled','true');
	comboModel.options[0].text = 'Loading...';
	
	var selectedBrand = document.getElementById('finder-serie').value;
	callUrl(caturl.replace('[[catid]]',selectedBrand), loadModel);
}
function loadModel(response) {
	loadCombo(response, 'finder-model', new RegExp(document.getElementById('finder-serie').options[document.getElementById('finder-serie').selectedIndex].text.split(' ')[0], "i"), 'Model');
	var comboBrand = document.getElementById('finder-model');
	comboBrand.removeAttribute('disabled');
	comboBrand.options[0].text = 'Choose your Printer\'s Model';
}
function goFast() {
	var brandSelect = document.getElementById('finder-brand');
	var serieSelect = document.getElementById('finder-serie');
	var modelSelect = document.getElementById('finder-model');
	
	if (brandSelect.selectedIndex == 0) {
    	alert('Please select a brand.');
		return ;
	}
	if (serieSelect.selectedIndex == 0) {
		document.location = '/s.nl/category.' + brandSelect.value + '/.f';
		return ;
	}
	if (modelSelect.selectedIndex == 0) {
		document.location = '/s.nl/category.' + serieSelect.value + '/.f';
		return ;
	}
	document.location = '/s.nl/category.' + modelSelect.value + '/.f';
}
try {
	var comboBrand = document.getElementById('finder-brand');
	comboBrand.setAttribute('disabled','true');
	comboBrand.options[0].text = 'Loading...';
	callUrl(brandsurl, loadBrand);
} catch(e) {}
