YAHOO.util.Event.onContentReady("storeLocatorMap", function () {
	swfobject.embedSWF(
		YAHOO.cw.urlPrepend+"flash/StoreFinder.swf", 
		"storeLocatorMap", 
		"572", 
		"310", 
		"9.0.0", 
		YAHOO.cw.urlPrepend+"flash/expressInstall.swf",
		{
			apiKey:YAHOO.cw.googleMapsApiKey,
			urlPrepend:YAHOO.cw.urlPrepend,
			storesUrl:'xml/Store/storelist',
			debug:'false'
		},
		{
			wmode:'transparent',
			allowscriptaccess:'always' // this should be the default but add to make sure
		}
	);
	
	YAHOO.util.Event.on('formStoreLocator', 'submit', function(e){
		// prevent the form submitting
		YAHOO.util.Event.preventDefault(e);

		locateTown();
	});
	
	// default position of the store locator map if we have not been
	// passed a town/postcode
	YAHOO.cw.defaultLatitude = 53.984358;
	YAHOO.cw.defaultLongitude = -1.52092;
	YAHOO.cw.defaultZoom = 12;
});

// if a town is set look up the co-ordinates
function locateTown(){
	
	
	
		
	var geocoder = new GClientGeocoder();		
	var postcodeTown = document.getElementById('townPostcode').value;

	if (postcodeTown != ""){
		
		var storeLocatorMovie = swfobject.getObjectById('storeLocatorMap');
		storeLocatorMovie.removeWelcome();
		
		geocoder.getLocations(postcodeTown + ' ,uk', handleResponse);		
	}
}

// called when we have a response from the geocoder
function handleResponse(response){	
	
	var responseDiv = document.getElementById('storeLocatorResponse');
	
	YAHOO.util.Dom.removeClass('storeLocator', 'success');
	YAHOO.util.Dom.removeClass('storeLocator', 'failure');
	
	if (!response || response.Status.code != 200) {
		// failed
		
		responseDiv.innerHTML = "<p>Sorry, we were unable to find that address.</p>";
		responseDiv.innerHTML += "<p>If you entered a UK postcode please ensure you included the space. 'LS6 3BE' not 'LS63BE'</p>";
		
		YAHOO.util.Dom.setStyle('storeLocatorResponse', 'display', 'block');
		YAHOO.util.Dom.addClass('storeLocator', 'failure');
	} 
	else {
		
		// success
		
		YAHOO.util.Dom.setStyle('storeLocatorResponse', 'display', 'block');
		YAHOO.util.Dom.addClass('storeLocator', 'success');
		
		responseDiv.innerHTML = "<p>Not the place you were looking for?</p>";
		responseDiv.innerHTML += "<p>Try adding a city to your search, i.e. if you searched for 'Headingley' try 'Headingley, Leeds'</p>";
		
		place = response.Placemark[0];
			    	
		var placeLat = place.Point.coordinates[1];
		var placeLng = place.Point.coordinates[0];
			   
		// update the flash map 	
		//setMapCenter(placeLat, placeLng, 11);
		
		// get a list of nearby stores sorted by distance
		var postData = "lat="+placeLat+"&lng="+placeLng+"&townPostcode="+escape(document.getElementById('townPostcode').value);
		YAHOO.util.Connect.successEvent.subscribe(responseSuccess);
		YAHOO.util.Connect.asyncRequest('POST', YAHOO.cw.urlPrepend+"xml/Store/locatorResults", null, postData);	
	}
}
	
// ajax response - update the results under the flash map
function responseSuccess(type, args){
	YAHOO.util.Connect.successEvent.unsubscribe(responseSuccess);
				
	if(args[0].responseText !== undefined){
		document.getElementById('storeLocatorResults').innerHTML = args[0].responseText;
		
		var nearestDistance = document.getElementById('nearestDistance').innerHTML;
		
		
		var zoom = Math.round((document.getElementById('nearestZoom').innerHTML) - (nearestDistance/3)-2);
		
		if (zoom < 9)
			zoom = 9;
			
		//alert(Math.round(document.getElementById('nearestZoom').innerHTML));
		//alert(zoom);
		setMapCenter(
			document.getElementById('nearestLat').innerHTML,
			document.getElementById('nearestLng').innerHTML,
			zoom
		);
		
		
	}
}


// called from flash when map ready to use
function mapReady(){
	if (document.getElementById('townPostcodePost').value != ""){
		// townPostcodePost is a hidden field - if this is set then we have a post value
		document.getElementById('townPostcode').value = document.getElementById('townPostcodePost').value;
		// lookup the value
		locateTown();		
		
		
	}
	else {
		
		// use the default latlng set at the top of this script
		setMapCenter(YAHOO.cw.defaultLatitude,YAHOO.cw.defaultLongitude,YAHOO.cw.defaultZoom);
	}
}

// calls the setMapCenter function of the flash movie
// to reposition the map to the new co-ordinates
function setMapCenter(lat, lng, zoom){
	var storeLocatorMovie = swfobject.getObjectById('storeLocatorMap');
	
	if (zoom == null)
		zoom = 11;
	
	storeLocatorMovie.setMapCenter(lat, lng, zoom);
	
}

YAHOO.util.Event.onContentReady("storeLocator",function () {
	var townPostcodeField = document.getElementById('townPostcode');
	
	YAHOO.util.Event.on(townPostcodeField, 'click', setBg);
	YAHOO.util.Event.on(townPostcodeField, 'blur', resetBg);
	
	function setBg(e){		
		YAHOO.util.Dom.addClass('storeLocator', 'active');
	}
	
	function resetBg(e){	
		YAHOO.util.Dom.removeClass('storeLocator', 'active'); 
	}
});

/**
 *	Form popup - store locator
 *	when displayStoreContactForm is called
 *  open the yui popup and update the store id value
 **/
var formPanel;

function displayStoreContactForm(id, lat, lng, zoom) {
	
	document.getElementById('storeContactFormAddressHolder').innerHTML = document.getElementById('storeInfo_'+id).innerHTML;
	
	formPanel.show();
	//setMapCenter(lat,lng,zoom);
	document.getElementById('contact_store_id').value = id+'';
	
	
}

YAHOO.util.Event.onContentReady("storeContactForm",function () {
	
	var viewportwidth;
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
	      viewportwidth = window.innerWidth,
	      viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 
	
	formPanel = new YAHOO.widget.Panel("storeContactForm", 
	{ 
		width:"840px", 
		visible:false, 
		modal:true, 
		constraintoviewport:true, 
		draggable:true,
		effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.4},
		x:((viewportwidth/2)-420),
		y:0
	} );
	formPanel.render();
	
	if (!document.getElementById('kobroForm2')){
		YAHOO.util.Dom.setStyle('formCopy', 'display', 'none');
		formPanel.show();
	}
		
	if(YAHOO.util.Dom.getElementsByClassName('errors', 'ul', 'storeContactForm').length > 0){
		
		formPanel.show();
			
	}
		
		
});

/**
 * end Form popup
 **/