var googleMapsPanel;

var geocoder = null;

GoogleMapsPanel = function(el, userConfig) {
	if (arguments.length > 0) {
		GoogleMapsPanel.superclass.constructor.call(this, el, userConfig);
	}
};

// Inherit from YAHOO.widget.Panel
YAHOO.extend(GoogleMapsPanel, YAHOO.widget.Panel);

// Initialize the GoogleMapsPanel
GoogleMapsPanel.prototype.init = function(el, userConfig) {
	GoogleMapsPanel.superclass.init.call(this, el);
	
	this.beforeInitEvent.fire(GoogleMapsPanel);
	
	if (userConfig) {
		this.cfg.applyConfig(userConfig, true);
	}
	
	this.renderEvent.subscribe(this.onRender);
	
	this.initEvent.fire(GoogleMapsPanel);
};

// Set up the default values for the properties
GoogleMapsPanel.prototype.initDefaultConfig = function() {
	GoogleMapsPanel.superclass.initDefaultConfig.call(this);
	this.cfg.addProperty("address", {handler: this.configAddress, supressEvent:true, value: ""});
	this.cfg.addProperty("zoom", {handler: this.configZoom, supressEvent:true, value: ""});
	this.cfg.addProperty("long", {handler: this.configLong, supressEvent:true, value: ""});
	this.cfg.addProperty("lat", {handler: this.configLat, supressEvent:true, value: ""});
	this.cfg.addProperty("label", {handler: this.configLabel, supressEvent:true, value: ""});
};

GoogleMapsPanel.prototype.configAddress = function(type, args, obj) {
	var address = args[0];
	if (address && address != "" && this.searchBar) {
		//this.searchBar.searchTxt.value = address;
		//this.searchBar.searchMap();
	}
};

GoogleMapsPanel.prototype.configZoom = function(type, args, obj) {
	var zoom = args[0];
	if (zoom && zoom != "" && this.map) {
		//this.map.setZoom(zoom);
	}
};

GoogleMapsPanel.prototype.configLong = function(type, args, obj) {
	var long = args[0];
	if (long && long != "" && this.map) {
	}
};

GoogleMapsPanel.prototype.configLat = function(type, args, obj) {
	var lat = args[0];
	if (lat && lat != "" && this.map) {
	}
};

GoogleMapsPanel.prototype.configLabel = function(type, args, obj) {
	var label = args[0];
	if (label && label != "" && this.map) {
	}
};

GoogleMapsPanel.prototype.configWidth = function(type, args, obj) {
	GoogleMapsPanel.superclass.configWidth.call(this, type, args, obj);
	var width = args[0];
	if (width) {
		var intWidth = parseInt(width);
		intWidth -= 24;
		YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get("googlemap"), "width", "" + intWidth + "px");
	}
};

GoogleMapsPanel.prototype.configHeight = function(type, args, obj) {
	GoogleMapsPanel.superclass.configHeight.call(this, type, args, obj);
	var height = args[0];
	if (height) {
		var intHeight = parseInt(height);
		intHeight -= 39;
		YAHOO.util.Dom.setStyle(YAHOO.util.Dom.get("googlemap"), "height", "" + intHeight + "px");
	}
};

GoogleMapsPanel.prototype.onRender = function() {
	this.map = null;
};

GoogleMapsPanel.prototype.addMarker = function(point, label) {
	googleMapsPanel.map.setCenter(point, 13);
	var marker = new GMarker(point);
	googleMapsPanel.map.addOverlay(marker);
	googleMapsPanel.map.addControl(new GLargeMapControl());
	googleMapsPanel.map.addControl(new GMapTypeControl());
	marker.openInfoWindowHtml(label);
};

GoogleMapsPanel.prototype.doInitGoogleMap = function(address, zoom, long, lat, label) {
	if (GBrowserIsCompatible()) {
		googleMapsPanel.map = new GMap2(document.getElementById("googlemap"));
		googleMapsPanel.map.setCenter(new GLatLng(37.4419, -122.1419), 13);
		geocoder = new GClientGeocoder();
	}
	
	if (!label) {
		label = address;
	}
	
	if (long & lat) {
		googleMapsPanel.addMarker(new GLatLng(lat, long), label);
	} else if (geocoder && address) {
		geocoder.getLatLng(address,
          	function(point) {
				if (!point) {
              		alert(address + " not found");
            	} else {
            		googleMapsPanel.addMarker(point, label);
				}
          	}
		);
	}
	googleMapsPanel.googleMapsReady(address, zoom, long, lat, label);
};

GoogleMapsPanel.prototype.googleMapsReady = function(address, zoom, long, lat, label) {
	if (address) {
		googleMapsPanel.cfg.setProperty("address", address);
	}
	if (zoom) {
		googleMapsPanel.cfg.setProperty("zoom", zoom);
	}
	if (long) {
		googleMapsPanel.cfg.setProperty("long", long);
	}
	if (lat) {
		googleMapsPanel.cfg.setProperty("lat", lat);
	}
	if (label) {
		googleMapsPanel.cfg.setProperty("label", label);
	}
};

GoogleMapsPanel.prototype.startLoadGoogle = function(address, zoom, long, lat, label) {
	if (this.map == null) {
		this.doInitGoogleMap(address, zoom, long, lat, label);
	} else {
		this.googleMapsReady(address, zoom, long, lat, label);
	}
};

GoogleMapsPanel.prototype.showPanel = function(address, zoom, long, lat, label) {
	this.startLoadGoogle(address, zoom, long, lat, label);
	
	var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
	if (scrollY > 0) {
		googleMapsPanel.center();
	}
	
	googleMapsPanel.show();
};

YAHOO.util.Event.addListener(window, "unload", function() {

	GUnload();
	
});

YAHOO.util.Event.addListener(window, "load", function() {
	// create div structures
	var googleMapPanelDiv = document.createElement("DIV");
	googleMapPanelDiv.setAttribute("id", "googleMapsPanel");
	document.body.appendChild(googleMapPanelDiv);
	googleMapPanelDiv.innerHTML = 
		'<div class="hd"><div class="tl"></div><span class="infotitle">Property Location</span><div class="tr"></div></div>'
		+ '<div class="bd">'
		+ '		<div id="googlemap"></div>'
		+ '</div>'
		+ '<div class="ft"><div class="bl"></div><div class="br"></div></div>';

	googleMapsPanel = new GoogleMapsPanel("googleMapsPanel", { 
		fixedcenter:false,  visible:false, constraintoviewport:false, underlay: 'none', width: '800px', height: '500px' });
	googleMapsPanel.render();
	googleMapsPanel.center();
	YAHOO.util.Dom.setStyle(googleMapsPanel.element, "top", "120px");
	
});

