/*
*         ________           __   __                     __
*        / ____/ /___  _____/ /__/ /   ____  _________ _/ /
*       / /_  / / __ \/ ___/ //_/ /   / __ \/ ___/ __ `/ / 
*      / __/ / / /_/ / /__/ ,< / /___/ /_/ / /__/ /_/ / /  
*     /_/   /_/\____/\___/_/|_/_____/\____/\___/\__,_/_/   
*                                                          
*     Do good. Fast.                      
*/


function showYourLocationMap() {
    if ($('YourLocationMap')) {
	$('YourLocationMap').update('<img alt="Staticmap?center=55" src="http://maps.google.com/staticmap?center='+Cookie.get('lattitude')+','+Cookie.get('longitude')+'&amp;zoom=8&amp;size=200x70&amp;maptype=mobile&amp;markers='+Cookie.get('lattitude')+','+Cookie.get('longitude')+',greena&amp;key=ABQIAAAAetD5ZoEWBnjjt_tdVu_wXxQ6Q70nDL4VGIKG0lZ3HFEpUiBs0hQipxegGqZO_1dD2FVLb3WDz33WnQ&amp;sensor=false" />');
    }
}

document.observe("dom:loaded", function() {
    // Get location and store in Cookies for future use.
    var loc = Cookie.get('lattitude');
    if(!loc) {
        Cookie.set('lattitude', geoip_latitude(), 60*60);
        Cookie.set('longitude', geoip_longitude(), 60*60);
        showYourLocationMap();
    } else {
        showYourLocationMap();
    }



});




var Cookie = {
	set: function(name,value,seconds){
		if(seconds){
			d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			expiry = '; expires=' + d.toGMTString();
		}else
			expiry = '';
		document.cookie = name + "=" + value + expiry + "; path=/";
	},
	get: function(name){
		nameEQ = name + "=";
		ca = document.cookie.split(';');
		for(i = 0; i < ca.length; i++){
			c = ca[i];
			while(c.charAt(0) == ' ')
				c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null
	},
	unset: function(name){
		Cookie.set(name,'',-1);
	}
}

var divElementID;
var lat;
var lng;
function staticGoogleMapClick(imageElement, lat, lng) {
    divElementID = imageElement.up().identify();
    imageElement.up().update("Loading, please wait ...");
    lat = lat;
    lng = lng;
    google.load("maps", "2.x", {callback:function() {
        map = new google.maps.Map2(document.getElementById(divElementID));
        map.setCenter(new google.maps.LatLng(lat,lng), 10);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.addOverlay( new GMarker(new GLatLng(lat,lng)));
				var myLat = Cookie.get("lattitude");
				var myLng = Cookie.get("longitude");
				if (myLat && parseFloat(myLat) != lat &&  myLng && parseFloat(myLng) != lng) {
					var youIcon = new GIcon(G_DEFAULT_ICON);
					youIcon.image = "/images/gmapsmarker-you.png";
					pointer = new GMarker(new GLatLng(parseFloat(myLat),parseFloat(myLng)), youIcon);
					map.addOverlay(pointer);
				}
    }});
}
