jQuery.noConflict();
var map;
var directionsPanel;
var directions;
var point = new Object;
var lat;
var lng;
var div;
var title = "Merlin Edge Inc.";
var address = "Suite 100, 602 – 12th Ave. S.W. ";
var city = "Calgary";
var country = "Canada";
var province = "Alberta";
var pc = "T2R 1J3";
var phone = "Phone: 403-237-7684";
var fax = "Fax: 403-237-7745";

jQuery(document).ready(init_google);
function init_google()
{
	if (jQuery("#map_canvas").get(0)) 
		{
			map_locator();
			updatemap(title, address, city, country, province, pc);
		}
	if (jQuery("#directions").get(0)) map_directions();
}

function map_locator(){
		map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GScaleControl());
		map.addControl(new GMapTypeControl());
}

function CGCallback(responce) {
    if (responce.Status.code != 200) {
		if (responce.Status.code == G_GEO_UNKNOWN_ADDRESS)
                             alert("Google Map Error:\nNo corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + responce.Status.code);
							 else  alert("Google Map Error:\nError code: " + responce.Status.code);
	  return;
    }
	lat = responce.Placemark[0].Point.coordinates[1];
    lng = responce.Placemark[0].Point.coordinates[0];
	map.setCenter(new GLatLng(lat, lng), 12);
	var market = new GMarker(new GLatLng(lat, lng));
	GEvent.addListener(market, 'click', function() {market.openInfoWindowHtml(div); });
	//GEvent.addListener(market, 'mouseover', function() {market.openInfoWindowHtml(div); });
	//GEvent.addListener(market, 'mouseout', function() { market.closeInfoWindow(); });
	map.addOverlay(market);
	market.openInfoWindowHtml(div);
  }


function updatemap(title, address, city, country, prov, pc){
	div = "<div class='descr'><h3>"+title+"</h3><p>"+address+'<br />'+city+', '+prov+', '+country+'<br />Postal Code: '+pc+"<br />"+phone+"<br />"+fax+"</p></div>";
	var cg = new GClientGeocoder();
	cg.getLocations(address+' '+city+' '+prov+' '+country, CGCallback);
}

function map_directions(){
		directionsPanel = document.getElementById("directions");
		directions = new GDirections(map, directionsPanel);
		//directions.load("165 cougar Ridge Cir SW Calgary AB Canada to 12th Ave. S.W. Calgary Alberta");
		jQuery(jQuery("input").get(1)).click(
		function (){
				jQuery("#error").text("");
				var from = jQuery("input").get(0).value;
				var str = from+" to "+address+" "+city+" "+country;
				 
				directions.load(str);
      			GEvent.addListener(directions, "load", GDSuccess);
				GEvent.addListener(directions, "error", GDError);
				function GDSuccess(){
					
					
				}
				function GDError(){
					jQuery("#error").text("Can't find direction.");
				}
			});
		jQuery(jQuery("input").get(2)).click(
			function (){
				directions.clear();
				updatemap(title, address, city, country, province, pc);
				
			}
		);
}

