function querySt(ji) {
    gy = document.location.toString().split('#')[1];
	gy = gy.split('&');
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split('=');
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function replaceAll( str, replacement ) {
        var idx = str.indexOf( replacement );
        while ( idx > -1 ) {
            str = str.replace( replacement, '' );
            idx = str.indexOf( replacement );
        }
    return str;
}

$(document).ready(function(){
	var url = document.location.toString();
	url = url.replace(/{/gi,'(').replace(/}/gi,')');
	if (!url.match('#!') && !url.match('_escaped_fragment_') ) {
		// no parameters sent in, check for cookie
		if ($.cookie('lasthash')) {
			window.location = $.cookie('lasthash');
			var url = document.location.toString();
		}
	}

	// see if url has a hash
	if (url.match('#!') || url.match('_escaped_fragment_') ) {
		if ($.cookie('position') == null) {
			$.cookie('position',1);
		}

		// check for keywords url data
		if (url.indexOf('kw') > 0) {
			document.getElementById('kw').value = unescape(url.substring( url.indexOf('kw')+3 , url.lastIndexOf(')') ));
			Refresh();
		}

		// check all links to see if they are in the url data, stop at first match
		var bMatch = false;
		var sMatch = false;
		$('.filterlink').each( function() {
			if (!bMatch) {
				var test1 = this.id;
				var test2 = this.id.replace(/\(/i,"%28").replace(/\)/i,"%29");
				var test3 = this.id.replace(/\(/i,"%7B").replace(/\)/i,"%7D");
				
				if (url.indexOf(test1) >0 || url.indexOf(test2) >0 || url.indexOf(test3) >0 ) {
					$(this).addClass('filter-on');
					// load the subcat div
					var tmp = (this.id.replace(/\(/gi,"\\(").replace(/\)/gi,"\\)"));
					var qs = this.id.replace(/\(/gi,'=').replace(/\)/gi,'&');
					if ($('#s' + tmp).length) {
						$('#s' + tmp).load("/ajax/browse-subcat.asp?" + qs, function() {
							$('.subcat-link').each(function() {
								if (url.indexOf(this.id) > 0) {
									$(this).addClass('subcat-link-on');
								}
							})
							$('.tl-link').each(function() {
								if (url.indexOf(this.id)>0) {
									$(this).addClass('tl-link-on');	
								}
							});
							Refresh();
						});
					} else {
						Refresh();	
					}
					bMatch = !bMatch;
				}		
			}
		});
	}
						 
	$('.filterlink').click(function(e){
		$.cookie('position',1);
		$('.subcat-link').removeClass('subcat-link-on');
		$('.filterlink').removeClass('filter-on');
		$(this).toggleClass('filter-on');
		$('.subcat').html("");
		var tmp = (this.id.replace(/\(/gi,"\\(").replace(/\)/gi,"\\)"));
		var qs = this.id.replace(/\(/gi,'=').replace(/\)/gi,'&');
		$('#s' + tmp).load("/ajax/browse-subcat.asp?" + qs);
		//alert("/ajax/browse-subcat.asp?" + qs);
		Refresh();
				
		return false;
	});
	
	$('#list-cat').jScrollPane();
	$('#list-des').jScrollPane();
	$('#list-man').jScrollPane();


	$("#shop-online").accordion({
	   change: function(event, ui) { 
		$('#list-cat').jScrollPane();
		$('#list-des').jScrollPane();
		$('#list-man').jScrollPane();	   
	   }
	});
	//Refresh();
	
});

function PageNext() {
	if ($.cookie('position') == null) {
		$.cookie('position',1);
	}
	
	if ($('#resultpage' + (parseInt($.cookie('position'))+1)).length > 0) {
		$.cookie('position' ,parseInt($.cookie('position'))+1 );
		$('#results').scrollTo($('#resultpage'+$.cookie('position')), 300);	
		$('#currentpage').html($.cookie('position'));
	}
}

function PagePrev() {
	if (parseInt($.cookie('position')) > 1) {
		$.cookie('position', parseInt($.cookie('position'))-1 );
	}
	$('#results').scrollTo($('#resultpage'+$.cookie('position')), 300);
	$('#currentpage').html($.cookie('position'));
}

function Refresh() {
	// get the text after # 
	var sQuery = '';
	var url = '';
	var sURL = document.location.toString();
	var Active = "#h3-category";
	var Pane = "#list-cat";
	// check for links set to 'on'
	$('.filter-on:first').each(function() {
		// check for subcategories set to 'on'
		$('.subcat-link-on:first').each(function() {
			url=url + this.id;										 
		});
		// check for third level links 'on'
		$('.tl-link-on:first').each(function() {
			url=this.id + url;
		});
		url=url+this.id;
		if (this.id.indexOf("c")>-1) {Active = "#h3-category";Pane = "#list-cat";}
		if (this.id.indexOf("m")>-1) {Active = "#h3-manufacturer";Pane = "#list-man";}
		if (this.id.indexOf("d")>-1) {Active = "#h3-designer";Pane = "#list-des";}
	});

	// check for keywords
	var kw = '';
	kw = document.getElementById('kw').value;
	if (kw.length > 0) {
		url=url + 'kw(' + escape(kw) + ')';	
	} 

	sQuery = url;
	
	$("#results").load('/ajax/loading.htm');

	if (url.length == 0) {
		$.cookie('lasthash','');
		$("#results").load("/ajax/product-browser-empty.htm");
	} else {
		$.cookie('lasthash','#!'+url);
		$.cookie('lastsearch','#!'+url);
		
		document.location = '#!' + url;	
		sQuery = sQuery.replace(/\(/gi,'=').replace(/\)/gi,'&');	
		$("#results").load("/ajax/product-browser.asp?" + sQuery, function() {
			// scroll to position when the ajax load is complete
			if ($('#resultpage'+$.cookie('position')).length > 0) {
				$('#results').scrollTo($('#resultpage'+$.cookie('position')), 100);
				$('#currentpage').html($.cookie('position'));				
			} else {
				$.cookie('position',1);	
			}
			// open up accordion page
			$("#shop-online").accordion('activate',Active);
		});		
	}
	
	$('#list-cat').jScrollPane();
	$('#list-des').jScrollPane();
	$('#list-man').jScrollPane();

	return false;
}
