/**
 * Map YUI popup
 */
var locBody = '<div id="mapContainer" class="full"></div><div id="dirContainer" class="hide"></div>';
var locFoot = '<label for="dirFrom">Starting address</label><input type="hidden" id="dirTo" name="dirTo" value="" />';
locFoot    += '<input type="text" id="dirFrom" name="dirFrom" value="" /><div id="dirButtonBox"></div>';

YAHOO.ps.container.location = new YAHOO.widget.Dialog("loc",  
					   								  { width:"700px", 
									 					fixedcenter:true, 
									 					close:true, 
										 				draggable:false, 
										 				modal:true,
										 				visible:false,
										 				effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.50}
									   				  } 
										 			 );
YAHOO.ps.container.location.setBody(locBody);	
YAHOO.ps.container.location.setFooter(locFoot);											  
YAHOO.ps.container.location.render(document.body);

YAHOO.ps.Location = function() { };
YAHOO.ps.Location.map;
YAHOO.ps.Location.dir;
YAHOO.ps.Location.geo;
YAHOO.ps.Location = {
	initialize:function(){
		YAHOO.ps.Location.map = new GMap2(document.getElementById("mapContainer"));        
		YAHOO.ps.Location.dir = new GDirections(YAHOO.ps.Location.map, document.getElementById("dirContainer"));
        YAHOO.ps.Location.geo = new GClientGeocoder(); 
        GEvent.addListener(YAHOO.ps.Location.dir, "error", YAHOO.ps.Location.handleError);
        // Map controls
		YAHOO.ps.Location.map.addControl(new GSmallMapControl());
		YAHOO.ps.Location.map.addControl(new GMapTypeControl());
		YAHOO.ps.Location.map.removeMapType(G_SATELLITE_MAP);
        // Directions and Print buttons
        var oDirButton   = new YAHOO.widget.Button({ label:"Get Directions", id:"getDirButton", container:"dirButtonBox", onclick: { fn: YAHOO.ps.Location.loadMapDirections } });
    },    
	setStart:function(from){
		document.getElementById('dirFrom').value = from;	
    },  
	setDestination:function(to){
		document.getElementById('dirTo').value = to;		
    },  
    showLink:function(el,addr){
    	YAHOO.ps.Location.geo.getLatLng(addr, function(point) {
			if (point) {
				YAHOO.util.Dom.removeClass(el,'hide');
				YAHOO.ps.Location.setDestination(addr);
				YAHOO.util.Event.addListener(el, "click", function () {
					YAHOO.ps.Location.loadMapDirections();
					YAHOO.ps.container.location.show();					
				});
			} else {
				YAHOO.util.Dom.addClass(el,'hide');
			}
		});
    },
    // If to and from addresses set it will attempt to load directions
    // If only to (listing address) is found load map only
	loadMapDirections:function(){
		// Clear map overlays and remove any existing directions
		YAHOO.ps.Location.map.clearOverlays();
		document.getElementById('dirContainer').innerHTML = '';
		// Get values
		var from = document.getElementById('dirFrom').value;
		var to   = document.getElementById('dirTo').value;
		
		// Display directions if both addresses are present. Otherwise show location 
		// for destination address or default map if destination is empty or invalid
		if (from != '' && to != '') {
			YAHOO.ps.Location.showDirBox();
			YAHOO.ps.Location.dir.load("from: " + from + " to: " + to, { "locale" : "en_US" });	
		} else if (YAHOO.ps.Location.geo && to != '') {
			YAHOO.ps.Location.hideDirBox();
			YAHOO.ps.Location.geo.getLatLng(to, function(point) {
				if (point) {
					YAHOO.ps.Location.map.setCenter(point, 15);
                    var icon = new GIcon();
					icon.image = "/resources/images/g-green-arrow.png";
					icon.iconSize = new GSize(24, 34);
					icon.iconAnchor = new GPoint(11, 34);
				
					icon.shadow = "/resources/images/g-shadow-arrow.png";
					icon.shadowSize = new GSize(34, 34);
					icon.infoShadowAnchor = new GPoint(11, 34);	
						    
					YAHOO.ps.Location.map.addOverlay(new GMarker(point, icon));
				} 
			});
		} 
    },
    // Shows directions box (animation can be used at later date)
    showDirBox:function(){
    	YAHOO.util.Dom.removeClass('dirContainer','hide');
    	YAHOO.util.Dom.removeClass('mapContainer','full');
    	YAHOO.ps.Location.map.checkResize();
    },
    // Hides directions box (animation can be used at later date)
    hideDirBox:function(){
    	YAHOO.util.Dom.addClass('dirContainer','hide');
    	YAHOO.util.Dom.addClass('mapContainer','full');
    	YAHOO.ps.Location.map.checkResize();
    },     
	// Todo: build error functionality for Google directions problems  
    handleError:function(){}
};
YAHOO.ps.Location.initialize();