$(document).ready(function() {

	var map;
	var geocoder;
	var marker;
	var country;

	function initialize() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			geocoder = new GClientGeocoder();
			
		}
	}

	function showAddress(address,zoomlevel) {
		
		if (geocoder) {
			geocoder.getLatLng(
				address,
				function(point) {
					if (($("div.filter-location div:not(.inactive) img.map").length == 1)) {
						$("#map").hide();
					} else if (point){
						x = point+"";
						var latlong = x.split(",",2);
						var lat = latlong[0].substring(1);
						var long =latlong[1].substring(0,latlong[1].length-1);
						$("#map").show();
						map.setCenter(new GLatLng(lat,long), parseInt(zoomlevel));
						marker = new GMarker(new GLatLng(lat,long), { zIndexProcess: 50 });
						map.addOverlay(marker);
						var copyright = new GCopyright(1, new GLatLngBounds(point));
						map.addOverlay(copyright);				
						//marker.openInfoWindowHtml(address);
					} else {
						var latitude = $('div.filter-location div:not(.inactive) div.location span.latitude').text();
						var longitude = $('div.filter-location div:not(.inactive) div.location span.longitude').text();
						if (latitude && longitude) {
							$("#map").show();
							spot = new GLatLng(latitude, longitude)
							map.setCenter(spot, parseInt(zoomlevel));
							marker = new GMarker(spot, { zIndexProcess: 50 });
							map.addOverlay(marker);
							var copyright = new GCopyright(1, new GLatLngBounds(spot));
							map.addOverlay(copyright);

						} else {
							$("#map").hide();
						}
					}
				}
			);
		}
	}
	var region = $(document).getUrlParam("Region");  
	country = $(document).getUrlParam("Country"); 

	
	

	initialize();
	if(country==null)
	{		
		//HARD WIRE DEFAULT Boston Map Location for Page Load.
		showAddress("75 State Street, Boston, MA 02109, USA",14);
	}


	var region = $(document).getUrlParam("Region");  
	var country = $(document).getUrlParam("Country");  

	//Remove this commenting if you want to force the user to drill down.
	/*
		//hide all divs
		$("div.country").addClass("inactive");
		$("div.office-location").addClass("inactive");
		$("#map").hide();
	*/
	
	$('div.country a').each( function(id) {

		// Remove the default country	
		if($(this).attr('id') == "USA-Boston")
		{
			$('div.country li').removeClass("current");
			$("div.country").addClass("inactive"); 
			$('div.office-location:eq(' + id + ')').removeClass("current");
			$("div.office-location").addClass("inactive");
		}
	});


	$('div.country a').each( function(id) {

	if ($(this).attr('id') == country || (country == null && $(this).attr('id') == "USA-Boston")) 
		{
					
			if (country != null)
			{
				$('div.region li').removeClass("current");
			}
			if($(this).attr('id') == "USA-Boston")
			{
				region = "NorthAmerica";
				$('div.region li').removeClass("current");	
			}
			

			$('div.country li').removeClass("current");
			$(this).parent().addClass("current");
			$(this).parent().parent().parent().removeClass("inactive");
			$('div.office-location:eq(' + id + ')').removeClass("inactive");
			$('div.office-location:eq(' + id + ')').addClass("current");
			
				


			
			showAddress($('div.filter-location div:not(.inactive) p.address span.streetaddress1').text() 
				+ " " + $('div.filter-location div:not(.inactive) p.address span.streetaddress2').text()
				+ " " + $('div.filter-location div:not(.inactive) p.address span.city').text()
				+ " " + $('div.filter-location div:not(.inactive) p.address span.postcode').text()
				+ " " + $('div.filter-location div:not(.inactive) p.address span.country').text()
				, $('div.filter-location div:not(.inactive) p.address span.zoomlevel').text()

				);
		};
					   							  
		$(this).click(function() {
			
			$('div.country li').removeClass("current");			
			$(this).parent().addClass("current");

			//hide all open divs with class of answer
			$("div.office-location").addClass("inactive");
			 
			//get the corrsponding div from array referenced by Index and show it
			$('div.office-location:eq(' + id + ')').removeClass("inactive");
			$('div.office-location:eq(' + id + ')').addClass("current");
				
			showAddress($('div.filter-location div:not(.inactive) p.address span.streetaddress1').text() 
				+ " " + $('div.filter-location div:not(.inactive) p.address span.streetaddress2').text()
				+ " " + $('div.filter-location div:not(.inactive) p.address span.city').text()
				+ " " + $('div.filter-location div:not(.inactive) p.address span.postcode').text()
				+ " " + $('div.filter-location div:not(.inactive) p.address span.country').text()
				, $('div.filter-location div:not(.inactive) p.address span.zoomlevel').text()

				);
			 
		});
	});

	$('div.region a').each( function(i) {

		if ($(this).attr('id') == region) {
			$(this).parent().addClass("current");
		};
		
		$(this).click(function() {
		
			$('div.region li').removeClass("current");			
			$('div.country').parent().addClass("current");
			
			$("div.country").addClass("inactive"); //hide all open divs with class of answer
			$("div.country li").removeClass("current");		
						
			$('div.country:eq(' + i + ')').removeClass("inactive"); //get the corrsponding div from array referenced by Index and show it
			$(this).parent().addClass("current");
							 
		});
	});
	
});







/* Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
 getUrlParam: function(strParamName){
	  strParamName = escape(unescape(strParamName));
	  
	  var returnVal = new Array();
	  var qString = null;
	  
	  if ($(this).attr("nodeName")=="#document") {
	  	//document-handler
		
		if (window.location.search.search(strParamName) > -1 ){
			
			qString = window.location.search.substr(1,window.location.search.length).split("&");
		}
			
	  } else if ($(this).attr("src")!="undefined") {
	  	
	  	var strHref = $(this).attr("src")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else if ($(this).attr("href")!="undefined") {
	  	
	  	var strHref = $(this).attr("href")
	  	if ( strHref.indexOf("?") > -1 ){
	    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
	  		qString = strQueryString.split("&");
	  	}
	  } else {
	  	return null;
	  }
	  	
	  
	  if (qString==null) return null;
	  
	  
	  for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
			
	  }
	  
	  
	  if (returnVal.length==0) return null;
	  else if (returnVal.length==1) return returnVal[0];
	  else return returnVal;
	}
});


