var first_time=1;

window.dhtmlHistory.create({
        toJSON: function(o) {
                return Object.toJSON(o);
        }
        , fromJSON: function(s) {
                return s.evalJSON();
        }
});

function dhtmlInit(){
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
	if (isSafari==true){
		var r = window.location.href;
		var i = r.indexOf("#");
		if (i>=0){
			var ar_hash=r.split("#");
			var currentLocation= ar_hash[1];
		}
	}else{
		var currentLocation = dhtmlHistory.getCurrentLocation();
	}
	
	if (currentLocation=="" || !currentLocation){
		var r = window.location.href;
		var i = r.indexOf("?");
		if (i>=0){
			//currentLocation="scatid=2:catid=2:prodid=1";
			document.getElementById('hidebox').style.visibility="hidden";
			document.getElementById('ProductListing').style.visibility="visible";
			document.getElementById('WaitMessage').style.visibility="hidden";
		}else{
			currentLocation="home";
			dhtmlHistory.add("home",null);
			resize_topban('nodelay');
			//displayLocation(currentLocation, null);
		}
	}else{	
		if(currentLocation !="home"){ //home is already displayed
			//show_center_delayed();
			displayLocation(currentLocation);
		}else{
			document.getElementById('hidebox').style.visibility="hidden";
			document.getElementById('ProductListing').style.visibility="visible";
			document.getElementById('WaitMessage').style.visibility="hidden";
		}
	}
}

function historyChange(newLocation, historyData) {
	if (isSafari==true){
		var r = window.location.href;
		var i = r.indexOf("#");
		if (i>=0){
			var ar_hash=r.split("#");
			newLocation = ar_hash[1];
		}
	}else{
		if (newLocation=="" || !newLocation){ //shouldn't be here
			newLocation="home";
		}
	}
	
	displayLocation(newLocation, historyData);
}

function displayLocation(newLocation, historyData){
	document.getElementById('WaitMessage').style.visibility="visible";
	document.getElementById('ProductListing').style.visibility="hidden";
	document.getElementById('hidebox').style.visibility="visible";
	pageTracker._trackPageview(newLocation);
	if (newLocation=="home") {
		var r = window.location.href;
		var i = r.indexOf("?");
		if (i>=0){
			window.location.href="/index.php";
		}else{
			if(curSel!="0"){changeSel(curSel)};
			curSel="0";
			catid='home';
			
			var myAjax = new Ajax.Updater('ProductListing','index.php',
				{method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy,
					onComplete : function(){
						if(first_time==1){
							resize_topban('delay');
							first_time=0;
						}else{
							resize_topban('nodelay');
						}
						document.getElementById('search_input_id').value="";
						var myAjax = new Ajax.Updater('CategoryList',current_file,
						{method: 'get', parameters:'action=draw_leftcatmenu&catid='+catid+'&supercatid='+curSel} );
					}				
					} );
		}
	}else{
		ar_locator=newLocation.split(":");
		for(x=0; x < ar_locator.length; x++){
			var loc=ar_locator[x].indexOf("=");	
			if (ar_locator[x].substring(0,loc)=="scatid"){
				var id=ar_locator[x].substring(loc+1,ar_locator[x].length);
				var total = ((document.getElementById('scrollable').scrollWidth/165));
				
				var el= (document.getElementById('SuperCateg_'+id).parentNode.parentNode);
				var loc2 = el.id.indexOf("_");
				var num = parseInt(el.id.substring(loc2+1,el.id.length));
				
				slider4.setValue(num/(total-1));
				slidepos = Math.round( (num/(total-1)) /slider4.maximum*(document.getElementById('scrollable').scrollWidth-document.getElementById('scrollable').offsetWidth));
				
				if(curSel!="0"){changeSel(curSel)};
				
				document.getElementById('SuperCateg_'+id).style.backgroundColor = "#91E0FF";
				document.getElementById('SuperCategText_'+id).style.backgroundColor = "#91E0FF";	
				document.getElementById('SuperCategText_'+id).style.color = "#000000";
				document.getElementById('SuperCategText_'+id).style.fontWeight = "bold";
				catSel="0";
				curSel=id;
				
				if(ar_locator.length==1){
					catid='new';
					var name = document.getElementById('SuperCategText_'+id).innerHTML;
					name_adj = name.replace( /&/g,'-AND-')
					var myAjax = new Ajax.Updater('ProductListing','index.php',
								{method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy+'&supercatid='+id+'&supercategname='+name_adj,
					onComplete : function(){
											//dhtmlHistory.add("scatid="+curSel);
												if(first_time==1){
													resize_topban('delay');
													first_time=0;
												}else{
													resize_topban('nodelay');
												}
											}
											} );
					var myAjax = new Ajax.Updater('CategoryList',current_file,
						{method: 'get', parameters:'action=draw_leftcatmenu&catid='+catid+'&supercatid='+curSel+'&supercategname='+name_adj} );
				}
			}else if(ar_locator[x].substring(0,loc)=="catid"){
				var cat_id=ar_locator[x].substring(loc+1,ar_locator[x].length);
				if(ar_locator[x].substring(loc+1,ar_locator[x].length)=="search"){ 
					curSel="0";
					catid="search";
					var searchloc=ar_locator[x+1].indexOf("=");	
					var searchstring =ar_locator[x+1].substring(searchloc+1,ar_locator[x+1].length);
					if(ar_locator.length==2){
						if (searchstring!='empty'){
							var myAjax = new Ajax.Updater('ProductListing',current_file,
							{method: 'get', parameters:'sort=search&imgsize='+imgsize+'&catid=search&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy+'&searchstring='+searchstring+'&categshow='+categshow+'&prodshow='+prodshow,
							onComplete: function(){
											if(first_time==1){
												resize_topban('delay');
												first_time=0;
											}else{
												resize_topban('nodelay');
											}
											}} );
						}else{
							catid="home";
							var myAjax = new Ajax.Updater('ProductListing','index.php',
								{method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy,
									onComplete : function(){
										if(first_time==1){
											resize_topban('delay');
											first_time=0;
										}else{
											resize_topban('nodelay');
										}
										document.getElementById('search_input_id').value="";
										//var myAjax = new Ajax.Updater('CategoryList',current_file,
											//	{method: 'get', parameters:'action=draw_leftcatmenu&catid='+catid+'&supercatid='+curSel} );
									}
									
									} );
							
						}
					}else{
						var prodloc=ar_locator[x+2].indexOf("=");	
						var productid =ar_locator[x+2].substring(prodloc+1,ar_locator[x+2].length);
						var myAjax = new Ajax.Updater('ProductListing','index.php', {method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid=search&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy+'&productid='+productid,
								  onComplete:function(){
										if(first_time==1){
											resize_topban('delay');
											first_time=0;
										}else{
											resize_topban('nodelay');
										}
								  }
								  } );
					}
				}else if(ar_locator.length==2){
					document.getElementById('filterhistory').innerHTML="";
					pgnumber=1;
					document.getElementById('WaitMessage').style.visibility="visible";
					var catid=cat_id;
					var myAjax = new Ajax.Updater('ProductListing','index.php', 
								{method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy, 
								onComplete:function(){
												dhtmlHistory.add("scatid="+curSel+":"+"catid="+cat_id, cat_id+"_"+cat_id+"_"+name);
												if(first_time==1){
													resize_topban('delay');
													first_time=0;
												}else{
													resize_topban('nodelay');
												}
											}
						});	
						var myAjax = new Ajax.Updater('CategoryList',current_file,
								{method: 'get', parameters:'action=draw_leftcatmenu&supercatid='+curSel+'&catid='+catid,
								onComplete:function(){
												catSel=catid;
											}} );
				}					
			}else if(ar_locator[x].substring(0,loc)=="filters"){
				var catid=cat_id;
				var catSel=cat_id;
				
				var filters=ar_locator[x].substring(loc+1,ar_locator[x].length);
				document.getElementById('filterhistory').innerHTML=filters;
				if(ar_locator.length==3){
					var myAjax = new Ajax.Updater('ProductListing','index.php', {method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy+'&filters='+filters,
								onComplete : function(){
										if(first_time==1){
											resize_topban('delay');
											first_time=0;
										}else{
											resize_topban('nodelay');
										}	
								}							  
							} );
					var myAjax = new Ajax.Updater('CategoryList',current_file,
						{method: 'get', parameters:'action=draw_leftcatmenu&supercatid='+curSel+'&catid='+catid+'&filters='+filters} );
				}
			}else if(ar_locator[x].substring(0,loc)=="prodid"){
				var catid=cat_id;
				var productid=ar_locator[x].substring(loc+1,ar_locator[x].length);
				var myAjax = new Ajax.Updater('ProductListing','index.php', {method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+prodperpage+'&pgnumber='+pgnumber+'&show='+showy+'&productid='+productid,
								  onComplete:function(){
									if(first_time==1){
										resize_topban('delay');
										first_time=0;
									}else{
										resize_topban('nodelay');
									}
								  }
								  } );
				var myAjax = new Ajax.Updater('CategoryList',current_file,
						{method: 'get', parameters:'action=draw_leftcatmenu&supercatid='+curSel+'&catid='+catid} );
			
			}else if(ar_locator[x].substring(0,loc)=="pgnum"){
				var catid=cat_id;
				var result=ar_locator[x].substring(loc+1,ar_locator[x].length);
				ar_res=result.split("_");
				if (document.getElementById('filterhistory').innerHTML!=""){
					filters=document.getElementById('filterhistory').innerHTML;
					filterquery= "&filters="+filters;
				}else{
					filterquery="";
				}
				
				var myAjax = new Ajax.Updater('ProductListing','index.php', {method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid='+catid+'&prodperpage='+ar_res[1]+'&pgnumber='+ar_res[0]+'&show='+showy+filterquery,
				onComplete : function(){
								if(first_time==1){
									resize_topban('delay');
									first_time=0;
								}else{
									resize_topban('nodelay');
								}
					window.scroll(0,0);
				}
				
				} );
			}			 
		}
		
	}
}