var tLiveSearchTime = 0,
    tLiveSearchTimeOut = null,
    iLiveSearchLanguage = 0,
    sLiveSearchLinkUrl = '',
    sLiveSearchOfferUrl = '',
    sLiveSearchOutletUrl = '',
    sLiveSearchImageUrl = '',
    sLiveSearchNoResults = '',
    oLiveSearchAjax = null,
    oLiveSearchPosition = { left: 0, top: 0 },
    sLiveSearchKeyword = '',
    iLiveSearchProductId = null,
    sLiveResultsDiv = '',
    sLiveResultsProductLink = '',
    fLiveResultsPrice = 0,
    fLiveResultsDiscountPrice = 0,
    fPriceFormatWhole = 0,
    fPriceFormatCents = 0,
    sPriceFormatFormatted = 0,
	sLiveSearchSearchingMessage = '';

$(document).ready(function() {
	$("#content").css("position", "relative");
	$("#side-panel .search input[type=text]").attr("autocomplete", "off");
	
	oLiveSearchPosition = $("#side-panel .search input[type=text]").position();
	oLiveSearchPosition.top += $("#side-panel .search input[type=text]").outerHeight() + 5;
	
	$("#side-panel .search input[type=text]").bind("focus keyup", function(e) {
		if (e.type == "keyup" && e.which == 40) {
			var oDivs = $(".livesearchresult");
			
			for (var i=0; i<oDivs.length; i++) {
				$(oDivs[i]).find('a').removeClass('bg-combination-3').addClass('bg-body');
			}
			
			for (var i=0; i<oDivs.length; i++) {
				if ($(oDivs[i]).attr("rel") == "keyfocus:true") {
					window.clearTimeout(tLiveSearchTimeOut);
					$(oDivs[i]).attr("rel", "keyfocus:false");
					if (i+2 > oDivs.length) i = -1;
					
					$(oDivs[i+1]).find('a').removeClass('bg-body').addClass('bg-combination-3');
					$(oDivs[i+1]).attr("rel", "keyfocus:true");
					tLiveSearchTimeOut = window.setTimeout('clearLiveResults()', 15000);
					return true;
				}
			}
			
			if (oDivs.length > 0) {
				window.clearTimeout(tLiveSearchTimeOut);
				$(oDivs[0]).find('a').removeClass('bg-body').addClass('bg-combination-3');
				$(oDivs[0]).attr("rel", "keyfocus:true");
				tLiveSearchTimeOut = window.setTimeout('clearLiveResults()', 15000);
				return true;
			}
		}
		
		if (e.type == "keyup" && e.which == 38) {
			var oDivs = $(".livesearchresult");
			
			for (var i=0; i<oDivs.length; i++) {
				$(oDivs[i]).find('a').removeClass('bg-combination-3').addClass('bg-body');
			}
			
			for (var i=0; i<oDivs.length; i++) {
				if ($(oDivs[i]).attr("rel") == "keyfocus:true") {
					window.clearTimeout(tLiveSearchTimeOut);
					$(oDivs[i]).attr("rel", "keyfocus:false");
					if (i-1 < 0) i = oDivs.length;
					
					$(oDivs[i-1]).find('a').removeClass('bg-body').addClass('bg-combination-3');
					$(oDivs[i-1]).attr("rel", "keyfocus:true");
					tLiveSearchTimeOut = window.setTimeout('clearLiveResults()', 15000);
					return true;
				}
			}
			
			if (oDivs.length > 0) {
				window.clearTimeout(tLiveSearchTimeOut);
				$(oDivs[oDivs.length-1]).find('a').removeClass('bg-body').addClass('bg-combination-3');
				$(oDivs[oDivs.length-1]).attr("rel", "keyfocus:true");
				tLiveSearchTimeOut = window.setTimeout('clearLiveResults()', 15000);
				return true;
			}
		}
		
		if (e.type == "keyup" && e.which == 13) {
			var oDivs = $(".livesearchresult");
			
			for (var i=0; i<oDivs.length; i++) {
				if ($(oDivs[i]).attr("rel") == "keyfocus:true") {
					window.location = $(oDivs[i]).find('a').attr("href");
					return false;
				}
			}
		}
		
		if (e.type == "keyup" && e.which == 27) {
			window.clearTimeout(tLiveSearchTimeOut);
			var oDivs = $(".livesearchresult");
			
			for (var i=0; i<oDivs.length; i++) {
				$(oDivs[i]).find('a').removeClass('bg-combination-3').addClass('bg-body');
				if ($(oDivs[i]).attr("rel") == "keyfocus:true") {
					$(oDivs[i]).attr('rel', "keyfocus:false");
					return false;
				}
			}
			
			clearLiveResults();
			return true;
		}
		
		if (e.type == "keyup") {
			window.clearTimeout(tLiveSearchTimeOut);
			var oDivs = $(".livesearchresult");
			
			for (var i=0; i<oDivs.length; i++) {
				$(oDivs[i]).find('a').removeClass('bg-combination-3').addClass('bg-body');
				if ($(oDivs[i]).attr("rel") == "keyfocus:true") {
					$(oDivs[i]).attr('rel', "keyfocus:false");
					return false;
				}
			}
			
			tLiveSearchTimeOut = window.setTimeout('clearLiveResults()', 15000);
		}
		
		if ($(this).val() == sLiveSearchKeyword && $("#livesearchresults").length > 0) {
			window.clearTimeout(tLiveSearchTimeOut);
			tLiveSearchTimeOut = window.setTimeout("clearLiveResults()", 15000);
			return true;
		}
		
		sLiveSearchKeyword = $(this).val();
		window.clearTimeout(tLiveSearchTimeOut);
		
		if (sLiveSearchKeyword == '') {
			window.clearTimeout(tLiveSearchTimeOut);
			tLiveSearchTimeOut = window.setTimeout("clearLiveResults()", 200);
			return true;
		}
		
		iLiveSearchTime = (new Date()).getTime();
		
		if ($("#livesearchresults").length > 0 && $("#livesearchbusy").length == 0 && $("#livesearchresults").height() > 164) {
			window.clearTimeout(tLiveSearchTimeOut);
			$("#livesearchresults").after('<div id="livesearchbusy" style="width: ' + $("#livesearchresults").width() + 'px; height: ' + $("#livesearchresults").height() + 'px; background-color: #fff; opacity: .8; text-align: center; position: absolute; top: ' + (oLiveSearchPosition.top + 1) + 'px; left: ' + (oLiveSearchPosition.left + 1) + 'px; font-size: 14px; font-weight: bold; color: #000; z-index: 150;"><br /><br /><img src="/img/ajaxLoader.gif" width="43" height="43" alt="" /><br /><br />' + sLiveSearchSearchingMessage + '<br /><br /></div>');
		} else if ($("#livesearchbusy").length == 0) {
			$("#livesearchresults").remove();
			$("#livesearchbusy").remove();
		}
		
		if (!(oLiveSearchAjax === null) && oLiveSearchAjax.abort) {
			oLiveSearchAjax.abort();
		}
		
		oLiveSearchAjax = $.getJSON('/search_helper.php?time=' + iLiveSearchTime + '&lang=' + iLiveSearchLanguage + '&text=' + escape(sLiveSearchKeyword), function(oResult) {
			if (!oResult) return true;
			if (!oResult.success) return true;
			if (oResult.time < tLiveSearchTime) return true;
			tLiveSearchTime = oResult.time;
			
			if ($("#livesearchresults").length > 0) {
				$("#livesearchresults").remove();
			}
			
			if ($("#livesearchbusy").length > 0) {
				$("#livesearchbusy").remove();
			}
			
			tLiveSearchTimeOut = window.setTimeout("clearLiveResults()", 15000);
			
			sLiveResultsDiv = '';
			
			if (oResult.results > 0) {
				sLiveResultsDiv += '<div id="livesearchresults" class="bg-body" style="top: ' + oLiveSearchPosition.top + 'px; left: ' + oLiveSearchPosition.left + 'px">';
				iLiveSearchProductId = null;
				
				for (iLiveSearchProductId in oResult.products) {
					sLiveResultsDiv += '<div class="livesearchresult" rel="keyfocus:false">';
					
					sLiveResultsProductLink = '#';
					if (oResult.products[iLiveSearchProductId].offer == 'Y') {
						sLiveResultsProductLink = sLiveSearchOfferUrl.replace('/2/', '/' + iLiveSearchProductId + '/').replace('product-name', oResult.products[iLiveSearchProductId].name_slug);
					} else if (oResult.products[iLiveSearchProductId].outlet == 'Y') {
						sLiveResultsProductLink = sLiveSearchOutletUrl.replace('/2/', '/' + iLiveSearchProductId + '/').replace('product-name', oResult.products[iLiveSearchProductId].name_slug);
					} else {
						sLiveResultsProductLink = sLiveSearchLinkUrl.replace('/1/', '/' + oResult.products[iLiveSearchProductId].category_id + '/').replace('category-name', oResult.products[iLiveSearchProductId].category_slug).replace('/2/', '/' + iLiveSearchProductId + '/').replace('product-name', oResult.products[iLiveSearchProductId].name_slug);
					}
					
					sLiveResultsDiv += '<a href="' + sLiveResultsProductLink + '" class="link-nohover color-combination-3 bg-body" onmouseout="$(this).removeClass(\'bg-combination-3\').addClass(\'bg-body\'); tLiveSearchTimeOut = window.setTimeout(\'clearLiveResults()\', 15000);" onmouseover="$(this).removeClass(\'bg-body\').addClass(\'bg-combination-3\'); window.clearTimeout(tLiveSearchTimeOut);">';
					sLiveResultsDiv += '<img src="' + (oResult.products[iLiveSearchProductId].image.indexOf('/') > -1 ? oResult.products[iLiveSearchProductId].image : sLiveSearchImageUrl.replace('product-image', oResult.products[iLiveSearchProductId].image)) + '" width="36" height="36" />';
					sLiveResultsDiv += oResult.products[iLiveSearchProductId].name + '<br />';
					
					fLiveResultsPrice = oResult.products[iLiveSearchProductId].price;
					fLiveResultsDiscountPrice = oResult.products[iLiveSearchProductId].discountprice;
					
					if (oResult.products[iLiveSearchProductId].price_alternative != null && oResult.products[iLiveSearchProductId].price_alternative > 0) fLiveResultsPrice = oResult.products[iLiveSearchProductId].price_alternative;
					if (oResult.products[iLiveSearchProductId].discount_alternative != null && oResult.products[iLiveSearchProductId].discount_alternative > -1) fLiveResultsDiscountPrice = oResult.products[iLiveSearchProductId].discount_alternative;
					
					if (fLiveResultsDiscountPrice > 0) {
						sLiveResultsDiv += '<span class="old-price">' + sLiveSearchPriceString.replace('%s', price_format(fLiveResultsPrice)) + '</span> &nbsp;&nbsp;&nbsp; ' + sLiveSearchPriceString.replace('%s', price_format(fLiveResultsDiscountPrice));
					} else {
						sLiveResultsDiv += sLiveSearchPriceString.replace('%s', price_format(fLiveResultsPrice));
					}
					
					sLiveResultsDiv += '</a>';
					sLiveResultsDiv += '</div>';
					oResult.results--;
				}
				
				sLiveResultsDiv += '</div>';
			} else {
				sLiveResultsDiv += '<div id="livesearchresults" class="bg-body" style="top: ' + oLiveSearchPosition.top + 'px; left: ' + oLiveSearchPosition.left + 'px">';
				sLiveResultsDiv += sLiveSearchNoResults;
				sLiveResultsDiv += '</div>';
			}
			
			$("#livesearchresults").remove();
			$("#content").append(sLiveResultsDiv);
		});
		
		return true;
	});
	
	$("#side-panel .search input[type=text]").bind("blur", function() {
		window.setTimeout("clearLiveResults()", 200);
		return true;
	});
});

function price_format(fPrice) {
	fPrice /= 100;
	fPriceFormatWhole = Math.floor(fPrice);
	fPriceFormatCents = Math.round(100 * (fPrice - fPriceFormatWhole));
	if (fPriceFormatCents == 100) {
		fPriceFormatCents = 0;
		fPriceFormatWhole++;
	}
	
	sPriceFormatFormatted = '' + fPriceFormatWhole + ',';
	if (fPriceFormatCents < 10) sPriceFormatFormatted += '0';
	sPriceFormatFormatted += fPriceFormatCents;
	
	return sPriceFormatFormatted;
}

function clearLiveResults() {
	$("#livesearchresults").remove();
	$("#livesearchbusy").remove();
}
