jQuery(document).ready(function(){
	jQuery(".productcontent").hide();
    $(".PrImg").ready(function(o) {
        jQuery(".mimg").show();
    });	
	jQuery(".productheading").click(function(){
		jQuery(this).next(".productcontent").toggle(0);
		jQuery(".productheading").hide();
	});
	jQuery(".Paging_Spinner").click(function(){
		jQuery(".SpinnerImg").show();
	});
	jQuery(".productfooter").click(function(){
		jQuery(".productcontent").hide();
		jQuery(".productheading").show();
	});
	$('#ProductSearch').submit(function() {
		var FormId = '#'+$('#ProductSearch').attr('id');
		if($("#ProductSearch input[name=q]").val() == '' ){
			$("#ProductSearch input[name=q]").focus();
			alert(EmptyQuery);
			return false;
		}
		if($("#ProductSearch input[name=PriceMin]").val()) {
			var value = $("#ProductSearch input[name=PriceMin]").val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			var intRegex = /^\d+$/;
			if(!intRegex.test(value)) {
				alert( "Field must be numeric");
				$("#ProductSearch input[name=PriceMin]").focus();
				return false;
			}
		}
		if($("#ProductSearch input[name=PriceMax]").val()) {
			var value = $("#ProductSearch input[name=PriceMax]").val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			var intRegex = /^\d+$/;
			if(!intRegex.test(value)) {
				alert( "Field must be numeric");
				$("#ProductSearch input[name=PriceMax]").focus();
				return false;
			}
		}
		if($("#ProductSearch input[name=PriceMax]").val() && $("#ProductSearch input[name=PriceMin]").val()) {
			if(parseInt($("#ProductSearch input[name=PriceMin]").val()) >= parseInt($("#ProductSearch input[name=PriceMax]").val())){
				$("#ProductSearch input[name=PriceMin]").select();
				alert('Field must be lower');
				return false;	
			}
		}
		send_ajax($(FormId).serialize(),this);
		return false;
	});
	$('#srcBtn').click(function() {
		$('#searchform').submit();
    });
	$('#searchform').submit(function() {
		var FormId = '#'+$('#searchform').attr('id');
		//$('#'+$('#searchform')).delay(2000).submit();
		
		if($("#searchform input[name=q]").val() == '' ){
			alert(EmptyQuery);
			$("#searchform input[name=q]").focus();
			return false;
		}
		send_ajax($(FormId).serialize(),this);
		return false;
	});
	function send_ajax(query,obj) {
		if($("#SearchInCategoryId").is(':checked')===true){
			obj.submit();
			return;
		}
		$("#shoutbox").centerInClient();
		$('#shoutbox').show();
		$("#progressbar").progressBar(1, {showText: true} );
		$('#ProductArea').css({'background-color':'#fff','filter':'alpha(opacity=50)','opacity':'0.5','-moz-opacity':'0.5','z-index':'1'});
		$.ajax({
			url: MainURL+'index.php',
			cache: false,
			data: query+'&jqrequest='+uniq,
			success: function(response) {
				clearInterval(get_remote);
				$("#progressbar").progressBar(100, {showText: true} );
				obj.submit();
			}
		});
		$("#progressbar").progressBar(0, {showText: true} );
		setInterval("get_remote();", 1000);
	}
});

function ucFirst(string) {
	return string.substring(0,1).toUpperCase() + string.substring(1).toLowerCase();
}
uniq = 'id' + (new Date()).getTime();
uniq.toString();
function get_remote() {
	$.ajax({
		url: MainURL+'tmp/'+uniq+'.xml',
		dataType: 'xml',
		ifModified: true,
		cache: false,
		success: function(xml) {
			var webservice = $(xml).find('webservice').text();
			var count = $(xml).find('count').text();
			$("#progressbar").progressBar(count, {showText: true} );
			if(webservice != 'done') $("#webservice").text(Searching + ucFirst(webservice));
		}
	});
	return false;
}
var res = 0;
var fn = Math.random(0, 999)+"";
fn = fn.substr(5, 10);
sfHover = function() {
	var sfEls = document.getElementById("navbar").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" hover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

