/**
 * @author Marco Alionso Ramirez, marco@onemarco.com
 * @url http://onemarco.com
 * @version 1.0
 * This code is public domain
 */

/**
 * The Tooltip class is an addon designed for the Google Maps GMarker class. 
 * @constructor
 * @param {GMarker} marker
 * @param {String} text
 * @param {Number} padding
 */
function Tooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map){
	var div = document.createElement("div");
	div.appendChild(document.createTextNode(this.text_));
	div.className = 'tooltip';
	div.style.position = 'absolute';
	div.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}

Tooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker_,this.text_,this.padding_);
}

Tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}


/**************************
*	HELPER FUNCTIONS FOR ALL
*	GOOGLE MAPS
*
*	ALL INITIALIZATION OF MAPS
*	IS TO BE DONE IN MODULE ITSELF
*
****************************/


/**
*	Creates a marker icon as a sprite
*	@param	number	the position of the sprite image
*	@return	the created GIcon
**/
function sprite(number){
	var icon = new GIcon();
	icon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	icon.iconSize = new GSize(12, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	icon.sprite = {};
	icon.sprite.image = "/media/launch/layout/map-marker-sprites.png";
	icon.transparent = "/media/launch/layout/map-marker-sprites-transparent.png";
	icon.sprite.top = number * 20;
	
	return icon;
}

/**
*	Marker and Sidebar Mouseover Handler
*	@param	sidebarLink	reference to the map sidebar link
**/
function tooltipMouseover(sidebarLink){
	if(!(this.isInfoWindowOpen) && !(this.isHidden())){
		this.tooltip.show();
	}
}

/**
*	Marker and Sidebar Mouseout Handler
*	@param	sidebarLink	reference to the map sidebar link
**/
function tooltipMouseout(sidebarLink){
	this.tooltip.hide();
}

/**
*	Marker and Sidebar click Handler for SRPs
*	@param	url	reference to the map sidebar link
**/
function tooltipMouseup(url){
	window.location = url;
}

/**
*	Marker and Sidebar Mouseover Handler
*	@param	sidebarLink	reference to the map sidebar link
**/
function tooltipSidebarMouseover(sidebarLink){
	if(!(this.isInfoWindowOpen) && !(this.isHidden())){
		this.tooltip.show();
	}
	$(sidebarLink).addClass('mapHighlight');
}

/**
*	Marker and Sidebar Mouseout Handler
*	@param	sidebarLink	reference to the map sidebar link
**/
function tooltipSidebarMouseout(sidebarLink){
	this.tooltip.hide();
	$(sidebarLink).removeClass('mapHighlight');
}

/**
*	Marker and Sidebar Click Handler
**/
function markerClick(){
	this.tooltip.hide();
	this.openInfoWindowTabs(this.tab);
}

/**
*	InfoWindowOpen Handler
*	@param	sidebarLink	reference to the map sidebar link
**/
function infoWindowOpen(sidebarLink){
	this.isInfoWindowOpen = true;	
}

/**
*	InfoWindowClose Handler
*	@param	sidebarLink	reference to the map sidebar link
**/
function infoWindowClose(sidebarLink){
	this.isInfoWindowOpen = false;
}

/*// x: create clickable poly
function addPolyClick(polyline, html) {
	GEvent.addListener(polyline,"click",function(para){
	map.openInfoWindowHtml(para,html)
	});
}
*/

/**
*	Creates a map marker
*	@param	point	a GLatLng coordinate
*	@param	name	the text name of the marker point
*	@param	icon	reference to GIcon to use
*	@return	the created marker
**/
function createMarker(point,name,icon, url ) {
	
	// create marker
	var marker = new GMarker(point, icon);
		
	var ttmover = GEvent.callbackArgs(marker,tooltipMouseover);
	var ttmout = GEvent.callbackArgs(marker,tooltipMouseout);

	GEvent.addListener(marker,'mouseover',ttmover);
	GEvent.addListener(marker,'mouseout',ttmout);
	
	if( url != undefined ){
		
		var wire = GEvent.callbackArgs( marker, tooltipMouseup, url );
		GEvent.addListener(marker,'mouseup',wire);
	
	}

	return marker;
}

/**
*	Creates a map marker
*	@param	point	a GLatLng coordinate
*	@param	name	the text name of the marker point
*	@param	html	all html content to display in the infoWindow
*	@param	icon	reference to GIcon to use
*	@param	sidebarLink	reference to id of sidebar text link
*	@return	the created marker
**/
function createMarkerPopup(point,name,html, icon) {
	
	// create marker
	var marker = new GMarker(point, icon);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		this.tooltip.hide();
	});

	var ttmover = GEvent.callbackArgs(marker,tooltipMouseover);
	var ttmout = GEvent.callbackArgs(marker,tooltipMouseout);

	GEvent.addListener(marker,'mouseover',ttmover);
	GEvent.addListener(marker,'mouseout',ttmout);


	return marker;
}


/**
*	Creates a map marker
*	@param	point	a GLatLng coordinate
*	@param	name	the text name of the marker point
*	@param	html	all html content to display in the infoWindow
*	@param	icon	reference to GIcon to use
*	@param	sidebarLink	reference to id of sidebar text link
*	@return	the created marker
**/
function createMarkerSidebar(point,name,html, icon, sidebarLink) {
	
	// create marker
	var marker = new GMarker(point, icon);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		this.tooltip.hide();
	});
	
	
	
	var ttmover = GEvent.callbackArgs(marker,tooltipSidebarMouseover,sidebarLink);
	var ttmout = GEvent.callbackArgs(marker,tooltipSidebarMouseout,sidebarLink);
	var mclick = GEvent.callback(marker,markerClick);
//
//	
//	// see http://onemarco.com/examples/Tooltip_v2/  !!!!!!!!!!!!!!!!!!!!!!!!!
	GEvent.addDomListener(sidebarLink,'mouseover',ttmover);
	GEvent.addDomListener(sidebarLink,'mouseout',ttmout);
	GEvent.addListener(marker,'mouseover',ttmover);
	GEvent.addListener(marker,'mouseout',ttmout);
//	//GEvent.addListener(marker,'click',mclick);
	GEvent.addListener(marker,'infowindowopen',GEvent.callbackArgs(marker,infoWindowOpen,sidebarLink));
	GEvent.addListener(marker,'infowindowclose',GEvent.callbackArgs(marker,infoWindowClose,sidebarLink));

	return marker;
}

/**
*	Creates a map marker
*	@param	point	a GLatLng coordinate
*	@param	name	the text name of the marker point
*	@param	html	all html content to display in the infoWindow
*	@param	icon	reference to GIcon to use
*	@param	sidebarLink	reference to id of sidebar text link
*	@return	the created marker
**/
function createMarkerMajorSidebar(point,name,html, icon, sidebarLink, sidebarMajorLink) {
	
	// create marker
	var marker = new GMarker(point, icon);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		this.tooltip.hide();
	});
	
	
	
	var ttmover = GEvent.callbackArgs(marker,tooltipSidebarMouseover,sidebarLink);
	var ttmout = GEvent.callbackArgs(marker,tooltipSidebarMouseout,sidebarLink);
	var mclick = GEvent.callback(marker,markerClick);
//
//	
//	// see http://onemarco.com/examples/Tooltip_v2/  !!!!!!!!!!!!!!!!!!!!!!!!!
	GEvent.addDomListener(sidebarLink,'mouseover',ttmover);
	GEvent.addDomListener(sidebarLink,'mouseout',ttmout);
	GEvent.addListener(marker,'mouseover',ttmover);
	GEvent.addListener(marker,'mouseout',ttmout);
//	//GEvent.addListener(marker,'click',mclick);
	GEvent.addListener(marker,'infowindowopen',GEvent.callbackArgs(marker,infoWindowOpen,sidebarLink));
	GEvent.addListener(marker,'infowindowclose',GEvent.callbackArgs(marker,infoWindowClose,sidebarLink));
	
	ttmover = GEvent.callbackArgs(marker,tooltipSidebarMouseover,sidebarMajorLink);
	ttmout = GEvent.callbackArgs(marker,tooltipSidebarMouseout,sidebarMajorLink);	
	
	GEvent.addDomListener(sidebarMajorLink,'mouseover',ttmover);
	GEvent.addDomListener(sidebarMajorLink,'mouseout',ttmout);
	GEvent.addListener(marker,'mouseover',ttmover);
	GEvent.addListener(marker,'mouseout',ttmout);
	

	return marker;
}


/*
 $(document).ready(initializeDirections);
 
    

    function initializeDirections() {
      if (GBrowserIsCompatible()) {      
        mapItMap = new GMap2(document.getElementById("mapIt-map"));
		mapItMap.addMapType(G_PHYSICAL_MAP);
		mapItMap.addControl(new GSmallZoomControl3D());
		var mapControl = new GHierarchicalMapTypeControl();
		mapControl.clearRelationships();
		mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels", false);
		mapItMap.addControl(mapControl);
		mapItMap.setMapType(G_PHYSICAL_MAP);
		var mapBounds = new GLatLngBounds();
		
//		 the below needs to be called after the map is pop-upped
//		 in addition to here.  The setCenter call here initializes the map center regardless
//		 of the map width & height, and then is called again after the markers are added
		mapItMap.setCenter(new GLatLng(40.771592,-111.888189), 8);
      }
    }
*/