/*
+----------------------------------------------------------------------+
| TouroGoogleMaps is a google API widget for contact pages around touro.edu
| see TouroGoogleMaps.class.php for documentation
+----------------------------------------------------------------------+
| author: thomas biegeleisen <thomas.biegeleisen@touro.edu>
| last modified: feb 24 2008
+----------------------------------------------------------------------+
*/

var TouroGoogleMaps_globalTemp;
TouroGoogleMaps = function(id, currentSite, locations, open, rootURL, sites, coords, locsites) {
	this.id = id;
	this.currentSite = currentSite;
	this.locations = locations;
	this.open = open;
	this.rootURL = rootURL;
	this.sites = sites;
	this.coords = coords;
	this.locsites = locsites;

	// base properties/objects
	this.map;
	this.mapDiv;
	this.baseIcon;
	this.markers = new Object;
	this.latlng = new Object;
	this.loc = new Object;
	this.window = window;
	// we need a reference from window back to the instantied class
	this.window.obj = this;
	if ( this.window.opera ) {
		// [TODO: opera 9 doesn't seem to fire when you resize...]
		// this.window.document.body.addEventListener("resize",this.setPositionFromListener,false);
		// this.window.opera.addEventListener("resize",this.setPositionFromListener,false);
	} else if ( this.window.attachEvent ) {
		this.window.attachEvent("onresize",this.setPositionFromListener);
	} else if ( this.window.addEventListener ) {
		this.window.addEventListener("resize",this.setPositionFromListener,false);
	}
}
TouroGoogleMaps.prototype.setPositionFromListener = function() {
	this.obj.setPosition(this.obj);
}
TouroGoogleMaps.prototype.setPosition = function(ref) {
	// use a reference since this might be called by any object
	var coords = findPos(ref.mapDiv);
	coords[0] = parseInt(coords[0]) ;
	coords[1] = parseInt(coords[1]) ;
	if ( ref.loc['align'] == "bottom" ) {
		var arrowWidthPadding = ref.loc['arrowWidth'] + ref.loc['arrowPadding'] ;
		var arrowHeightPadding = ref.loc['arrowHeight'] + ref.loc['arrowPadding'] ;
		var closedTop = parseInt(coords[1] + (parseInt(getHeight(ref.mapDiv)) - ref.loc['height'] + ref.loc['linkHeight'] + arrowHeightPadding));
		ref.loc['closedTop'] = closedTop ;
		// extra pixel is for border (not counted in element dimensions)
		ref.loc['openTop'] = closedTop - ref.loc['linkHeight'] + ref.loc['height'] - arrowHeightPadding - 1;
		// set coords
		ref.loc['div'].style.left = coords[0] + "px" ;
		if ( ref.loc['open'] ) {
			ref.loc['div'].style.top = ref.loc['openTop'] + "px" ;
		} else {
			ref.loc['div'].style.top = ref.loc['closedTop'] + "px" ;
		}
	}
}
TouroGoogleMaps.prototype.initialize = function() {
	this.mapDiv = document.getElementById(this.id + "_map") ;
	// create icon
	this.baseIcon = new google.maps.Icon;
	this.baseIcon.image = this.rootURL + "/global/images/modules/google_maps/google_maps_touro.png" ;
	this.baseIcon.shadow = this.rootURL + "/global/images/modules/google_maps/google_maps_touro_shadow.png" ;
	this.baseIcon.shadowSize = new google.maps.Size(37,34);
	this.baseIcon.iconSize = new google.maps.Size(20,34);
	this.baseIcon.iconAnchor = new google.maps.Point(10,33);
	this.baseIcon.transparent = this.rootURL + "/global/images/modules/google_maps/google_maps_touro_transparent.png" ;
	this.baseIcon.infoWindowAnchor = new google.maps.Point(10,0);
	// create map object
	this.map = new google.maps.Map2(this.mapDiv) ;
	for ( i in this.sites ) {
		var temp_latlng = new google.maps.LatLng(this.coords[i].lat,this.coords[i].lng) ;
		if ( i == this.currentSite || i == 1 ) {
			this.map.setCenter(temp_latlng, 16) ;
		}
		var marker = this.createMarker(temp_latlng,i) ;
		this.markers[i] = marker;
		this.latlng[i] = temp_latlng;
		this.map.addOverlay(marker);
		if ( i == this.currentSite ) {
			marker.openInfoWindowHtml(this.sites[i]);
		}
	}
	this.map.addControl(new GLargeMapControl()); 
	if ( this.locations ) {
		this.locationsInitialize() ;
	}
}
TouroGoogleMaps.prototype.locationsInitialize = function() {
	// layering
	var coords = findPos(this.mapDiv);
	this.mapDiv.style.zIndex = 1;
	coords[0] = parseInt(coords[0]) ;
	coords[1] = parseInt(coords[1]) ;
	this.loc['arrowPadding'] = 2 ;
	this.loc['arrowHeight'] = 4 ;
	this.loc['arrowWidth'] = 10;
	this.loc['linkHeight'] = 25;
	this.loc['width'] = parseInt(getWidth(this.mapDiv)) - 2;
	this.loc['height'] = 240;
	this.loc['align'] = "bottom";
	this.loc['linktextClosed'] = "View Touro Locations around the country" ;
	this.loc['linktextOpen'] = "Click here to close this window" ;
	this.loc['div'] = document.getElementById(this.id + "_locations") ;
	this.loc['div'].style.display = "" ;
	this.loc['div'].style.width = this.loc['width'] + "px" ;
	this.loc['div'].style.height = this.loc['height'] + "px" ;
	this.loc['div'].style.borderWidth = "1px" ;
	this.loc['div'].style.borderStyle = "solid" ;
	this.loc['div'].style.borderColor = "#ccc";
	this.loc['div'].style.backgroundColor = "#fff" ;
	this.loc['div'].style.position = "absolute" ;
	this.loc['div'].style.zIndex = parseInt(this.mapDiv.style.zIndex) - 1;
	// set html
	// dynamically grab css properties from permalink elements
	var html = "<div style=\"padding: 5px; text-align:center;\"><div><table cellspacing=\"0\" cellpadding=\"2\" style=\"margin:0 auto;\"><tr><td align=\"center\" valign=\"middle\"><img src=\"" + this.rootURL + "/global/images/logos/touro_circle32.png\" alt=\"Touro College logo\" style=\"border:0;display:block;\" \/><\/td><td align=\"left\" valign=\"middle\"><div class=\"google_maps_infobox_title\">Touro Locations around the country<\/div><div class=\"google_maps_infobox_description\">Click on a location to view it on the map:<\/div><\/td><\/tr><\/table><\/div><div id=\"" + this.id + "_locations_iframe\"><\/div><\/div>" ;
	if ( this.loc['align'] == "bottom" ) {
		var arrowWidthPadding = this.loc['arrowWidth'] + this.loc['arrowPadding'] ;
		var arrowHeightPadding = this.loc['arrowHeight'] + this.loc['arrowPadding'] ;
		var closedTop = parseInt(coords[1] + (parseInt(getHeight(this.mapDiv)) - this.loc['height'] + this.loc['linkHeight'] + arrowHeightPadding));
		this.loc['closedTop'] = closedTop ;
		// extra pixel is for border (not counted in element dimensions)
		this.loc['openTop'] = closedTop - this.loc['linkHeight'] + this.loc['height'] - arrowHeightPadding - 1;

		// set coords
		this.loc['div'].style.left = coords[0] + "px" ;
		var initialText;
		var initialArrow;
		if ( this.open ) {
			this.loc['div'].style.top = this.loc['openTop'] + "px" ;
			this.loc['open'] = true;
			initialText = this.loc['linktextOpen'] ;
			initialArrow = "top" ;
		} else {
			this.loc['div'].style.top = this.loc['closedTop'] + "px" ;
			this.loc['open'] = false;
			initialText = this.loc['linktextClosed'] ;
			initialArrow = "bottom" ;
		}
		this.loc['div'].innerHTML = "<div style=\"display:block;width:100%;height:" + (this.loc['height'] - arrowHeightPadding - this.loc['linkHeight']) + "px;\">" + html + "<\/div><div><table cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%;height:" + this.loc['linkHeight'] + "px;\"><tr><td align=\"center\" valign=\"middle\" style=\"height:100%;\"><a id=\"" + this.id + "_toggle_link\" class=\"google_maps_infobox_website\" href=\"javascript:" + this.id + ".locationStartMove();\"><span id=\"" + this.id + "_toggle_link_text\" style=\"font-size:16px;font-weight:bold;\">" + initialText + "<\/span><\/a><\/td><\/tr><\/table><\/div><div><table cellspacing=\"0\" cellpadding=\"0\" style=\"display:block;width:100%;height:" + arrowHeightPadding + "px;\"><tr><td align=\"center\" valign=\"middle\" style=\"height:" + arrowHeightPadding + "px;\"><a href=\"javascript:" + this.id + ".locationStartMove();\"><img src=\"" + this.rootURL + "\/global\/images\/trans.png\" style=\"width:" + parseInt(parseInt(this.loc['width'] - arrowWidthPadding)/2) + "px;height:" + arrowHeightPadding + "px;border:0;display:block;\" alt=\"\" /><\/a><\/td><td align=\"center\" valign=\"middle\" style=\"height:" + arrowHeightPadding + "px;\"><a href=\"javascript:" + this.id + ".locationStartMove();\"><img src=\"" + this.rootURL + "\/global\/images\/modules\/google_maps\/google_maps_arrow_" + initialArrow + ".png\" id=\"" + this.id + "_locations_arrow\" style=\"border:0;display:block;\" alt=\"\" /><\/a><\/td><td align=\"center\" valign=\"middle\" style=\"height:" + arrowHeightPadding + "px;\"><a href=\"javascript:" + this.id + ".locationStartMove();\"><img src=\"" + this.rootURL + "\/global\/images\/trans.png\" style=\"width:" + parseInt(parseInt(this.loc['width'] - arrowWidthPadding)/2) + "px;height:" + arrowHeightPadding + "px;border:0;display:block;\" alt=\"\" /><\/a><\/td><\/tr><\/table><\/div>" ;
	}
	this.loc['easing'] = 0.3;
	this.loc['initialized'] = true;
	this.loc['targetTop'] ;
	this.loc['interval'] ;
	this.loc['moving'] = false;
	// now dump the locations
	var holder = document.getElementById(this.id + "_locations_iframe");
	var iframe = new IFrame(holder);
	var hrefCSS = document.getElementById(this.id + "_toggle_link");
	var hrefCSSFontFamily = getStyle(hrefCSS,"font-family");
	var hrefCSSFontSize = getStyle(hrefCSS,"font-size");
	var hrefCSSColor = getStyle(hrefCSS,"color");
	var hrefCSSTextDecoration = getStyle(hrefCSS,"text-decoration");
	// basic styles for iframe body
	iframe.doc.body.style.margin = "0";
	iframe.doc.body.style.padding = "0";
	iframe.id = "google_maps_locations_iframe_element";
	var div = iframe.doc.createElement("div");
	div.style.padding = "5px";
	iframe.style.border = "0";
	iframe.style.width = (this.loc['width'] - 30) + "px";
	iframe.style.borderWidth = "1px 0 1px 1px" ;
	iframe.style.borderColor = "#999";
	iframe.style.borderStyle = "solid" ;
	for ( i in this.locsites ) {
		div.innerHTML += "<div style=\"padding:2px;\">" + this.locsites[i] + "<\/div>" ;
	}
	iframe.doc.body.appendChild(div);
	for ( i in this.locsites ) {
		var thisDiv = iframe.doc.getElementById('iframe_location_href_' + i) ;
		thisDiv.style.fontFamily = hrefCSSFontFamily;
		thisDiv.style.fontSize = hrefCSSFontSize;
		thisDiv.style.color = hrefCSSColor;
		thisDiv.style.textDecoration = hrefCSSTextDecoration;
	}
	if ( !this.loc['open'] ) {
		holder.style.display = "none" ;
	}
	this.loc['iframe'] = iframe;
	this.loc['iframeDiv'] = holder;
	this.loc['toggleLinkText'] = document.getElementById(this.id + "_toggle_link_text") ;
}
TouroGoogleMaps.prototype.locationStartMove = function() {
	if ( this.loc['moving'] ) return ;
	if ( this.loc['open'] ) {
		this.loc['targetTop'] = this.loc['closedTop'] ;
		this.loc['toggleLinkText'].innerHTML = this.loc['linktextClosed'] ;
	} else {
		this.loc['iframeDiv'].style.display = "" ;
		this.loc['toggleLinkText'].innerHTML = this.loc['linktextOpen'] ;
		this.loc['targetTop'] = this.loc['openTop'] ;
	}
	this.loc['moving'] = true ;
	this.loc['interval'] = setInterval(this.id + ".locationMove()",20);
}
TouroGoogleMaps.prototype.locationMove = function() {
	var coords = findPos(this.loc['div']);
	var vy = (this.loc['targetTop'] - parseInt(coords[1])) * this.loc['easing'];
	var newY = parseInt(coords[1]) + vy;
	var dy = Math.abs(this.loc['targetTop'] - newY);
	if ( dy < 3 ) {
		clearInterval(this.loc['interval']) ;
		this.loc['moving'] = false ;
		var arrow = document.getElementById(this.id + "_locations_arrow");
		if ( this.loc['targetTop'] == this.loc['closedTop'] ) {
			this.loc['open'] = false ;
			this.loc['iframeDiv'].style.display = "none" ;
			arrow.src = arrow.src.replace(/top/,"bottom") ;
		} else {
			this.loc['open'] = true ;
			arrow.src = arrow.src.replace(/bottom/,"top") ;
		}
		this.setPosition(this);
	} else {
		this.loc['div'].style.top = newY + "px" ;
	}
}
TouroGoogleMaps.prototype.createMarker = function(temp_latlng,i) {
	var marker = new google.maps.Marker(temp_latlng,this.baseIcon);
	marker.map = this.map;	// make map available to markers
	marker.obj = this;
	marker.id = i;
	google.maps.Event.addListener(marker, "click", function() {
		if ( marker.map.getInfoWindow().isHidden() === false ) {
			marker.closeInfoWindow();
		}
		marker.openInfoWindowHtml(marker.obj.sites[marker.id]) ;
	});
	return marker;
}
TouroGoogleMaps.prototype.openInfoWindow = function(i) {
	this.map.setCenter(this.latlng[i], 16);
	this.markers[i].openInfoWindowHtml(this.sites[this.markers[i].id]);
}
TouroGoogleMaps.prototype.moveToAddressAndCreateMarker = function(address,addressHtml) {
	// set globally accessible variable to object instance so callback function can see it
	TouroGoogleMaps_globalTemp = this;
	var geocoder = new google.maps.ClientGeocoder;
	geocoder.getLatLng(address, function(point) {
		if ( point ) {
			TouroGoogleMaps_globalTemp.map.setCenter(point, 16) ;
			var marker = new google.maps.Marker(point);
			marker.map = TouroGoogleMaps_globalTemp.map;
			google.maps.Event.addListener(marker, "click", function() {
				if ( marker.map.getInfoWindow().isHidden() === false ) {
					marker.closeInfoWindow();
				}
				marker.openInfoWindowHtml(addressHtml) ;
			});
			TouroGoogleMaps_globalTemp.map.addOverlay(marker);
			marker.openInfoWindowHtml(addressHtml);
		}
	});
	TouroGoogleMaps_globalTemp = null;
}
TouroGoogleMaps.prototype.moveToPointAndCreateMarker = function(point,html) {
	var point = new google.maps.LatLng(point.lat,point.lng) ;
	this.map.setCenter(point, 16) ;
	var marker = new google.maps.Marker(point);
	marker.map = this.map;
	google.maps.Event.addListener(marker, "click", function() {
		if ( marker.map.getInfoWindow().isHidden() === false ) {
			marker.closeInfoWindow();
		}
		marker.openInfoWindowHtml(html) ;
	});
	this.map.addOverlay(marker);
	marker.openInfoWindowHtml(html) ;
}
