//Fire up the RSH to control the history state
	
	window.dhtmlHistory.create({toJSON: function(o) { return Object.toJSON(o);},fromJSON: function(s) { return s.evalJSON();}});
		
	//Fire up the RSH to control the history state
	window.onload = initialize;      
    function initialize() {
        dhtmlHistory.initialize();        
        dhtmlHistory.addListener(historyChange);
		if(document.location.hash==''){
			finderContent('Alpha','A',1);
		}else{			
			var re = new RegExp ('#', 'gi');
			var newLocation=document.location.hash.replace(re, '');			
			var	newLocation=newLocation.split("=");				
			finderContent(newLocation[0],newLocation[1],1);		
		}
	}
	 
	function historyChange(newLocation,historyData){
		//If we are back to the start of the history
		if(newLocation==''){
			finderContent('Alpha','A',1);
			return false;
		}		
		var	newLocation=newLocation.split("=");
		finderContent(newLocation[0],newLocation[1]);				  
	}	
	
	function alphaChange(ch){
		//change each href on the alpha search to cater for the chnage between Store and placesToEat
		var links=$('alpha_form_holder').getElementsByTagName('a');
		for(i=0;i<links.length;i++){
			links[i].rel=ch;
		}
	}
	
	function resetDD(a){
		if(a!=1){$('category').value='';}
		if(a!=2){$('brand').value='';}
		if(a!=3){$('service').value='';}
	}
	
	function uncover(a){		
		if(a==1){
			$('changingContent').hide();
			$('MapContent').style.visibility='';	
			$('MapContent').style.height='567px'
			if($('CMK_WEB')){$('CMK_WEB').style.height='567px'}
			setTimeout(function(){
				tabLoad()
			},1500);
			tabChange('Map',1)
		}else if(a==2){
			if (jQuery.browser.msie && jQuery.browser.version < 7) {
				$('changingContent').style.display='inline';
				tabLoad()
			}else{				
				jQuery("#changingContent").slideDown(1000,function(){
					tabLoad()
				});								
			}		
			if($('MapContent')){$('MapContent').style.height='0px';};
			if($('CMK_WEB')){$('CMK_WEB').style.height='0px';}
		}else{
			$('changingContent').show();	
			$('MapContent').style.height='0px'
			if($('CMK_WEB')){$('CMK_WEB').style.height='0px'}
		}	
		cleardown();
	}
	
	function tabLoad(){
		$('loadTab1').hide();$('realTab1').show();												   
		$('loadTab2').hide();$('realTab2').show();
		$('loadTab3').hide();$('realTab3').show();
		$('loadTabGif2').src='/assets/images/store/btn_placestoeat_off_anim.gif';
		$('loadTabGif3').src='/assets/images/store/btn_map_off_anim.gif';	
	}
	
	function tabChange(type,a){		
		if(a==0){
			$('TopNavSF').className="active";
			if((type=='Alpha')||(type=='Store')||(type=='Brand')||(type=='Cat')||(type=='Service')){
				$('loadTab1').show();$('realTab1').hide();
				if($('tab2')){$('tab2').className='pte';}
				if($('tab3')){$('tab3').className='map';}
			}
			if(type=='Places'){	
				$('loadTab2').show();$('realTab2').hide();
				if($('tab1')){$('tab1').className='sf';}
				if($('tab3')){$('tab3').className='map';}
				
			}
			if(type=='Map'){
				$('loadTab3').show();$('realTab3').hide();
				if($('tab1')){$('tab1').className='sf';}
				if($('tab2')){$('tab2').className='pte';}
			}
		}else{
			if((type=='Alpha')||(type=='Store')||(type=='Brand')||(type=='Cat')||(type=='Service')){		
				if($('tab1')){$('tab1').className='sf_active';}
				if(a==1){
					$('loadTab1').hide();$('realTab1').show();	
				}
			}
			if(type=='Places'){						
				if($('tab2')){$('tab2').className='pte_active';}
				if(a==1){
					$('loadTab2').hide();$('realTab2').show();	
				}
				$('TopNavSF').className="";
			}			
			if(type=='Map'){
				setTimeout(function(){
					if($('tab3')){$('tab3').className='map_active';}
					if(a==1){
						$('loadTab3').hide();$('realTab3').show();	
					}
				},1000)
			}
		}
	}

	function finderContent(type,val,start){
		if(start!=2){
			tabChange(type,0)
		}
		//Sort through search type			
		var vurl='/assets/includes/front/storefinder/sf_finder.cfm';
		if(type=='Alpha'){var postBody='Alpha='+val;}		
		if(type=='Cat'){var postBody='Category='+val;}			
		if(type=='Places'){var postBody='Places='+val;}
		if(type=='Brand'){var postBody='Brand='+val;}
		if(type=='Service'){var postBody='Service='+val;}
		if(type=='Store'){
			var postBody='Store='+val;
			var vurl='/assets/includes/front/storefinder/sf_store.cfm';
		}
		if(type=='Map'){
			if(type=='Map'){dhtmlHistory.add("Map=0", true);$('SearchControls').hide();uncover(1);}
			return false;
		}				
		jQuery.ajax({
			  type: 'POST',
			  data: postBody ,
			  url: vurl,
			  cache: true,
			  dataType: 'HTML',
			  success: function(success){					
				//Write content to temp div to counter image caching, then clear that down				
				$('tempContent').innerHTML=success;	
				//Sort out back button
				if(type=='Store'){					
					backButtonChange(type);	
					$('MapLI').show()
				}	
				//Removed 500 setTimeout					
				$('changingContent').innerHTML=$('tempContent').innerHTML;	
				if((type=='Alpha')&&(start!=1)){dhtmlHistory.add("Alpha="+val, true);$('SearchControls').show();resetDD();} 
				if((type=='Cat')&&(start!=1)){dhtmlHistory.add("Cat="+val, true);$('SearchControls').show();resetDD(1);$('category').value=unescape(val);} 
				if((type=='Brand')&&(start!=1)){dhtmlHistory.add("Brand="+val, true);$('SearchControls').show();resetDD(2);$('brand').value=unescape(val);} 
				if((type=='Service')&&(start!=1)){dhtmlHistory.add("Service="+val, true);$('SearchControls').show();resetDD(3);$('service').value=unescape(val);} 
				if(type=='Places'){dhtmlHistory.add("Places="+val, true);$('SearchControls').hide();resetDD();}
				
				if(type=='Store'){dhtmlHistory.add("Store="+val, true);}
				if((start==1)&&(type!='Places')){$('SearchControls').show();$('drop_down_holder').show();resetDD();}	
				if(start==1){
					uncover(2);	
					tabChange(type,2);
				}else{
					uncover(0)	
					tabChange(type,1);
				}								
			 }			  
			});						
		return false;
	}	                     
	
	function backButtonChange(type){
		if(document.location.hash!=''){
			if(document.location.hash.indexOf('Alpha')>=0){
				$('BackBttn').className='sf_back_sr';
				$('BackBttn').innerHTML='<a href="javascript:history.back();" title="Back to search results"><span>Back to search results</span></a>';
			}
			if(document.location.hash.indexOf('Places')>=0){
				$('BackBttn').className='sf_back_pl';
				$('BackBttn').innerHTML='<a href="javascript:history.back();" title="Back to Places to Eat"><span>Back to Places to Eat</span></a>'
			}
			if(document.location.hash.indexOf('Map')>=0){
				$('BackBttn').className='sf_back_sf';
				$('BackBttn').innerHTML='<a href="javascript:void(0)" onclick="finderContent(\'Alpha\',\'A\');" title="Back to Store Finder"><span>Back to Store Finder</span></a>'
			}
			if(document.location.hash.indexOf('Cat')>=0){
				$('BackBttn').className='sf_back_cat';
				$('BackBttn').innerHTML='<a href="javascript:history.back();" title="Back to Category search"><span>Back to Category search</span></a>'
			}
			if(document.location.hash.indexOf('Brand')>=0){
				$('BackBttn').className='sf_back_br';
				$('BackBttn').innerHTML='<a href="javascript:history.back();" title="Back to Brand"><span>Back to Brand search</span></a>'
			}
			if(document.location.hash.indexOf('Service')>=0){
				$('BackBttn').className='sf_back_sv';
				$('BackBttn').innerHTML='<a href="javascript:history.back();" title="Back to Service search"><span>Back to Service search</span></a>'
			}
		}else{
			$('BackBttn').className='sf_back_sf';
			$('BackBttn').innerHTML='<a href="javascript:void(0)" onclick="finderContent(\'Alpha\',\'A\');" title="Back to Store Finder"><span>Back to Store Finder</span></a>'
		}			
	}
	
	function getTextFromFlash(storeName) {	
		//change to friendlyURL	
		var re = new RegExp ('\'', 'gi');
		storeName = storeName.replace(re, '');		
		var re = new RegExp (' ', 'gi');
		storeName = storeName.replace(re, '-');		
		var re = new RegExp ('/', 'gi');
		storeName = storeName.replace(re, '-');		
		var re = new RegExp ('&', 'gi');
		storeName = storeName.replace(re, 'and');	
	  	finderContent('Store',storeName);
		return storeName + " received";
		
	}

	 function getFlashMovie(movieName) {
	 	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	  	return (isIE) ? window[movieName] : document[movieName];
	 }

	function getMapStore(unitID,storeName,storeImage){			
	 	finderContent('Map',0)	
		if(jQuery.browser.msie){var sec=500;}else{var sec=500;}
		setTimeout(function(){					
			if(unitID>=700){var a="unit";}else if(unitID>=500){var a="rmu";}else{var a="unit";};
			getFlashMovie("CMK_WEB").sendTextToFlash(a+unitID, storeImage, storeName);			
	  	},sec)
	}
	
	function cleardown() {
	  	try{if($('CMK_WEB')){getFlashMovie("CMK_WEB").sendCleardownToFlash();}}catch(err){}
	}

