// JavaScript Document
	var map;
	var localSearch = new GlocalSearch();
    var geocoder = null;
	var icon = new GIcon();
	icon.image = "http://www.google.com/mapfiles/marker.png";
	icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	icon.iconSize = new GSize(20, 34);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(10, 34);

	function grayOut(vis, options) {  
		// Pass true to gray out screen, false to ungray  
		// options are optional.  This is a JSON object with the following (optional) properties  
		// opacity:0-100         
		// Lower number = less grayout higher = more of a blackout   
		// zindex: #             
		// HTML elements with a higher zindex appear on top of the gray out  
		// bgcolor: (#xxxxxx)    
		// Standard RGB Hex color code  
		// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});  
		// Because options is JSON opacity/zindex/bgcolor are all optional and can appear  
		// in any order.  Pass only the properties you need to set.  
		var options = options || {};   
		var zindex = options.zindex || 50;  
		var opacity = options.opacity || 70;  
		var opaque = (opacity / 100);  
		var bgcolor = options.bgcolor || '#000000';  
		var dark=document.getElementById('darkenScreenObject');  
		if (!dark) {    
			// The dark layer doesn't exist, it's never been created.  So we'll    
			// create it here and apply some basic styles.    
			// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    
			var tbody = document.getElementsByTagName("body")[0];    
			var tnode = document.createElement('div');           // Create the layer.        
			tnode.style.position='absolute';                 // Position absolutely        
			tnode.style.top='0px';                           // In the top        
			tnode.style.left='0px';                          // Left corner of the page        
			tnode.style.overflow='hidden';                   // Try to avoid making scroll bars                    
			tnode.style.display='none';                      // Start out Hidden        
			tnode.id='darkenScreenObject';                   // Name it so we can find it later    
			tbody.appendChild(tnode);                            // Add it to the web page    
			dark=document.getElementById('darkenScreenObject');  // Get the object.  
		}  
		if (vis) {    
			// Calculate the page width and height     
			if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {                   
				var pageWidth = document.body.scrollWidth+'px';        
				var pageHeight = document.body.scrollHeight+'px';    
			} else if( document.body.offsetWidth ) {                 
				var pageWidth = document.body.offsetWidth+'px';      
				var pageHeight = document.body.offsetHeight+'px';    
			} else {    
				var pageWidth='100%';       
				var pageHeight='100%';    
			}       //set the shader to cover the entire page and make it visible.    
			dark.style.opacity=opaque;                          
			dark.style.MozOpacity=opaque;                       
			dark.style.filter='alpha(opacity='+opacity+')';     
			dark.style.zIndex=zindex;            
			dark.style.backgroundColor=bgcolor;      
			dark.style.width= pageWidth;    
			dark.style.height= pageHeight;  
			dark.style.display='block';                            
		} else {     
			dark.style.display='none';  
		}
	}
	
	function showcorpbox(productclicked){	
		document.getElementById("requestname").value = "corp_quoterequest_" + productclicked;	
		grayOut(true); 
		document.getElementById('corpbox').style.display='block'; 
		return false;
	}
	
	function showlocationbox(e,name, building, town, postcode)  {
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) {
		  posx = e.pageX;
		  posy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
		  posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		  posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}

		if (!postcode) {
			$('#loc_error').show();
		}
		else {
			$('#loc_error').hide();
		}
		
		var location = name;
		$('#title_name').html(name);
		
		if(building) location = location+ ", "+building;
		if(town) location = location+ ", "+town;
		if(postcode) location = location+ ", "+postcode;
		
		$('#loc_details').html(location);
		
		//send across correct data to Google map print
		var printloc;
		if(postcode) {
			printloc = building+", "+town+ ", "+postcode;
		} else {
			printloc = name+ ", "+building+", "+town;
		}
		$('#loc_print').html("<a target=\"_blank\" onclick=\"pageTracker._trackPageview('/outgoing/maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q="+printloc+"\');\" href=\"http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q="+printloc+"\">View Printable Google Map</a>");
		
		centerPopup("locationbox",posx,posy);

	}
	
	function centerPopup(id,posx,posy,wWidth,wHeight){
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;				  
		var popupHeight = $("#"+id).height();
		var popupWidth = $("#"+id).width();
		

		if(wWidth!=null){
			popupWidth=wWidth;
		}
		
		if(wHeight!=null){
			popupHeight=wHeight
		}
		

		var totalHeight=windowHeight+posy;
		//centering
		var topposition = getScrollY() + (windowHeight-popupHeight)/2;
		 $("#"+id).css({
			//"position": "fixed",
			"position": "absolute",
			"width":popupWidth,
			"height":popupHeight,
			"top":topposition+"px",			
			"left": windowWidth/2-popupWidth/2,
			"z-index":3000					
		  })
		  .fadeIn("slow");

			
		// Close popup	
		$("#titlebar a").click(function() { 
			$("#"+id).fadeOut('slow');
			grayOut(false);	
		});
	}
	
	
	
	function usePointFromPostcode(postcode, callbackFunction, Name) {
		// UK Postcode (Geocoder 1)
		localSearch.setSearchCompleteCallback(null, 
			function() {
				if (localSearch.results[0])
				{		
					var resultLat = localSearch.results[0].lat;
					var resultLng = localSearch.results[0].lng;
					var point = new GLatLng(resultLat,resultLng);
					callbackFunction(point, Name);
				}else{
					$('#loc_error').show();
				}
			});	
			
		localSearch.execute(postcode + ", UK");
	}
	
	function initialize() {
		//Non- UK Postcode (Geocoder 2)
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
      }
    }

	function filterPostcode(PostCode)
	{
		//some locations have alternate postcodes for satnav use - reverse this conversion
		var result;
		switch (PostCode) {
			case "CW5 6PQ": result = 'CW5 6GD'; break;
			default: result = PostCode;
		}
		return result;
	}
	
    function showAddress(Name, Building, Town, PostCode) {
		//Use Postcode Geocoder for best accuracy if postcode available - if not pass to standard Geocoder
		if(PostCode) {
			var point;
			PostCode = filterPostcode(PostCode);
			usePointFromPostcode(PostCode,finishedMap,Name);

		//No postcode to start with.
		} else {
			var address;
			address = Name+ ", "+Building+", "+Town;
			//start 2nd geocoder
			initialize();
			//alert(address);
			if (geocoder) {
				geocoder.getLatLng(address,
			  		function(point) {
						if (!point) {
				  			$('#loc_error').show();
						} else {
							finishedMap(point, Name);
						}
			  		}
				);
			}
			//end if statement
		}
    }
	
	function finishedMap(point, Name) {
		map = new GMap2(document.getElementById("locationmap"));
		map.setCenter(point, 15);
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
		//add point to map
		var marker = new GMarker(point);
		map.addOverlay(marker);
		marker.openInfoWindowHtml(Name);	
		
	}

function getScrollY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
					scrOfY = window.pageYOffset; scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
					scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
					scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfY;
	
}
