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";
		
	if (newLocation=="home") {
		document.getElementById('mainsearch').value = "";
		document.getElementById('mainsearch').style.visibility = "hidden";
		document.getElementById('mainsearch_cover').style.visibility = "visible";
		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');
						}
						get_twit()
						
						//document.getElementById('search_input_id').value="";
						var myAjax = new Ajax.Updater('CategoryList',current_file,
						{method: 'get', parameters:'action=draw_leftcatmenu&catid='+catid+'&supercatid='+curSel, 
							onComplete:function(){
								var leftAccordion = new accordion('#left_accordion', {
									classNames : {
										toggle : 'left_menu_sub',
										toggleActive : 'left_menu_sub_selected',
										content : 'left_menu_sub_content'
										},
									defaultSize : {
										width : 175,
										height: 221
										},
									direction : 'vertical',
									onEvent   : 'mouseover'
								});
							}
						} );
					}				
				} );
		}
	}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);
				curSel=id;
				
				if(ar_locator.length==1){
					catid='new';
					catSel="0";
					
					var name = document.getElementById('top_menu_2_main_'+id).innerHTML; //getting the name from the top menu
					var 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(cat_id=="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 inChar = "|";
							var outChar = " ";
							var newString = searchstring.split(inChar);
							newString = newString.join(outChar);
							searchstring = newString;
							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');
											}
											document.getElementById('mainsearch').value=unescape(searchstring);
											document.getElementById('mainsearch').style.visibility = "visible";
											document.getElementById('mainsearch_cover').style.visibility = "hidden";
											document.getElementById('mainsearch').focus();
											document.getElementById('mainsearch_optionbox').innerHTML = document.getElementById('mainsearch_optionbox_copy').innerHTML;
											document.getElementById('mainsearch_optionbox').style.height="auto";
											allow_search=1;
                                                                                        
                                                                                            var myAjax = new Ajax.Updater('CategoryList',current_file,
                                                                                                                            {method: 'get', parameters:'action=draw_leftcatmenu&supercatid='+curSel, 
                                                                                                                                    onComplete:function(){
                                                                                                                                            var leftAccordion = new accordion('#left_accordion', {
                                                                                                                                                    classNames : {
                                                                                                                                                            toggle : 'left_menu_sub',
                                                                                                                                                            toggleActive : 'left_menu_sub_selected',
                                                                                                                                                            content : 'left_menu_sub_content'
                                                                                                                                                            },
                                                                                                                                                    defaultSize : {
                                                                                                                                                            width : 175,
                                                                                                                                                            height: 220
                                                                                                                                                            },
                                                                                                                                                    direction : 'vertical',
                                                                                                                                                    onEvent   : 'mouseover'
                                                                                                                                            });
                                                                                                                                    } //close oncomplete
                                                                                                                            } );//close left menu
                                                                                        
										}} );
						}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 if(ar_locator.length==2){
					document.getElementById('filterhistory').innerHTML="";
					pgnumber=1;
					document.getElementById('WaitMessage').style.visibility="visible";
					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(){
									for(x=1;x<21;x++){
										if(document.getElementById('filtermenu_'+x)){
											if(x>5){
												document.getElementById('filtermenu_'+x).style.height="0px";
												document.getElementById('filtermenu_'+x).style.visibility="hidden";
												document.getElementById('filterarrow_'+x).className="arrow_right";
											}
										}
									}
								}
							});
				}					
			}else if(ar_locator[x].substring(0,loc)=="filters"){
				catid=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,
						onComplete:function(){
							for(x=1;x<21;x++){
								if(document.getElementById('filtermenu_'+x)){
									if(x>5){
										document.getElementById('filtermenu_'+x).style.height="0px";
										document.getElementById('filtermenu_'+x).style.visibility="hidden";
										document.getElementById('filterarrow_'+x).className="arrow_right";
									}
								}
							}
						}
					} );
				}
			}else if(ar_locator[x].substring(0,loc)=="prodid"){
                            catid = cat_id;
                            var productid=ar_locator[x].substring(loc+1,ar_locator[x].length);
                            //we have product but its part of the search
                            if(catid=='search'){
                                if (searchstring!='empty'){
                                    var inChar = "|";
                                    var outChar = " ";
                                    var newString = searchstring.split(inChar);
                                    newString = newString.join(outChar);
                                    searchstring = newString;
                                    var myAjax = new Ajax.Updater('ProductListing','index.php', {method: 'get', parameters:'sort='+sorty+'&imgsize='+imgsize+'&catid=search&searchstr='+searchstring+'&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, 
                                                                                        onComplete:function(){
                                                                                                var leftAccordion = new accordion('#left_accordion', {
                                                                                                        classNames : {
                                                                                                                toggle : 'left_menu_sub',
                                                                                                                toggleActive : 'left_menu_sub_selected',
                                                                                                                content : 'left_menu_sub_content'
                                                                                                                },
                                                                                                        defaultSize : {
                                                                                                                width : 175,
                                                                                                                height: 220
                                                                                                                },
                                                                                                        direction : 'vertical',
                                                                                                        onEvent   : 'mouseover'
                                                                                                });
                                                                                        } //close oncomplete
                                                                                } );//close left menu
                                                      }
                                                      } );
                                    }
                            }else{        
                                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+'&productid='+productid} );
                            }
			
			}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);
				}
				
				} );
			}			 
		}
	}
	pageTracker._trackPageview(newLocation);
}
