/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["require", "dijit._Widget"],
["require", "dijit._Container"],
["require", "dijit._Contained"],
["provide", "quelib.Map"],
["provide", "quelib.MapPolyline"],
["provide", "quelib.MapMarker"],
["provide", "quelib.MapMarkerTab"],
["provide", "quelib.MapSidebar"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["quelib.Map"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["quelib.Map"] = true;
dojo.require("dijit._Widget");
dojo.require("dijit._Container");
dojo.require("dijit._Contained");

// Setup callback to hopefully prevent memory leaks
dojo.connect(window, 'onunload', null, 'GUnload');

// Enable SVG in browsers that support it.
_mSvgEnabled = true;
_mSvgForced  = true; 

dojo.provide("quelib.Map");
dojo.provide("quelib.MapPolyline");
dojo.provide("quelib.MapMarker");
dojo.provide("quelib.MapMarkerTab");
dojo.provide("quelib.MapSidebar");

dojo.declare("quelib.Map",
	[dijit._Widget, dijit._Container],
{
	map: {},
	mtype: "",
	markers: [],
	lat: 38.024800,
	lng: -107.670543,
	zoom: 15,
	controls: "",
	overview: false,
	callback_data: "",

	postCreate: function() {
	},

	startup: function() {
		this.map = new GMap2(this.domNode);
		this.map.addMapType(G_PHYSICAL_MAP);
		if (this.controls != "false") {
			if (this.controls == "small") {
				this.map.addControl(new GSmallMapControl());
				this.map.addControl(new GHierarchicalMapTypeControl(true));
			} else {
				this.map.addControl(new GLargeMapControl());
				this.map.addControl(new GHierarchicalMapTypeControl());
			}
		}
		if (this.overview) {
			this.map.addControl(new GOverviewMapControl());
		}
		this.home();
		if (this.mtype == "satellite") {
			this.map.setMapType(G_SATELLITE_MAP);
		} else if (this.mtype == "hybrid") {
			this.map.setMapType(G_HYBRID_MAP);
		} else if (this.mtype == "physical") {
			this.map.setMapType(G_PHYSICAL_MAP);
		}
		dojo.forEach(this.getChildren(), function(child) { child.startup(); });

		this.postStartup();
	},

	home: function() {
		this.map.setCenter(new GLatLng(parseFloat(this.lat), parseFloat(this.lng)), parseFloat(this.zoom));
	},

	newMapType: function (name, shortname, urlfunc, levels, copyright) {5
		var layer = new GTileLayer(new GCopyrightCollection("Map Data: " + copyright), levels[0], levels[1]);
		layer.getTileUrl = urlfunc;
		layer.getCopyright = function(a,b) { return { prefix: "Map Data: ", copyrightTexts: [copyright]}; }
		var newtype = new GMapType([layer], G_SATELLITE_MAP.getProjection(), name, {shortName: shortname});
		this.map.addMapType(newtype);
		this.map.addControl(new GMapTypeControl());
		return newtype;
	},

	setMapType: function (maptype) {
		this.map.setMapType(maptype);
	},

	postStartup: function() {
	}
});


dojo.declare("quelib.MapPolyline",
	[dijit._Widget, dijit._Contained],
{
	polyline: {},
	parent: {},
	points: [],
	color: '#000000',
	weight: 2,
	opacity: 1,

	postCreate: function () {
		if (!this.parent.map) {
			this.parent = this.getParent();
		}
		dojo.eval("var pts = ["+this.points+"];");
		this.points = [];
		var me = this;
		dojo.forEach(pts, function(pt) {
				me.points.push(new GLatLng(pt[0], pt[1]));
			});
		this.polyline = new GPolyline(this.points, this.color, this.weight, this.opacity);
	},

	startup: function() {
		this.parent.map.addOverlay(this.polyline);
	}
});

dojo.declare("quelib.MapMarker",
	[dijit._Widget, dijit._Contained, dijit._Container],
{
	lat: "",
	lng: "",
	label: "Info",
	html: "",
	status: "",
	directions: false,
	draggable: "false",
	tabs: [],
	icon: "default",
	marker: {},
	parent: {},
	callback_data: "",

	postCreate: function() {
		if (!this.parent.map) {
			this.parent = this.getParent();
		}
		if (this.html.length >= 1) {
			this.domNode.innerHTML = this.html
		} else {
			this.html = this.domNode.innerHTML;
		}
		this.tabs = [];

		if (this.draggable == "enabled") {
			this.draggable = true;
			var isDragEnabled = true;
		}

		var point = new GLatLng(this.lat, this.lng);
		if (this.icon == 'start') {
			var icon = new GIcon(G_DEFAULT_ICON, 'http://www.google.com/mapfiles/dd-start.png');
			this.marker = new GMarker(point, {draggable: this.draggable, icon: icon});
		} else if (this.icon == 'end') {
			var icon = new GIcon(G_DEFAULT_ICON, 'http://www.google.com/mapfiles/dd-end.png');
			this.marker = new GMarker(point, {draggable: this.draggable, icon: icon});
		} else if (this.icon.match(new RegExp("^(camera)"))) {
			var icon = new GIcon(G_DEFAULT_ICON, 'http://www.google.com/mapfiles/'+this.icon+'.png');
			this.marker = new GMarker(point, {draggable: this.draggable, icon: icon});
		} else if (this.icon.match(new RegExp("^(red|blue|brown|green|orange|purple|grey|white|yellow|black)"))) {
			var icon = new GIcon(G_DEFAULT_ICON, 'http://www.google.com/mapfiles/marker_'+this.icon+'.png');
			this.marker = new GMarker(point, {draggable: this.draggable, icon: icon});
		} else if (this.icon.match(new RegExp("^sm(red|blue|brown|green|orange|purple|white|yellow|black)"))) {
			var color = this.icon.replace(new RegExp("^sm"), "");
			var icon = new GIcon();
			icon.image = 'http://labs.google.com/ridefinder/images/mm_20_'+color+'.png';
			icon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
			icon.iconSize = new GSize(12, 20);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
			this.marker = new GMarker(point, {draggable: this.draggable, icon: icon});
		} else {
			this.marker = new GMarker(point, {draggable: this.draggable});
		}
		var me = this;
		if (!isDragEnabled && this.draggable) {
			this.marker.disableDragging();
		} else {
			GEvent.addListener(this.marker, "dragend", function() { me.dragEnd(me.marker, me.callback_data); });
		}
		if (this.directions) {
			var loc = point.lat()+','+point.lng();
			this.tabs.push(new GInfoWindowTab("Directions", '<div>to here from:</div><form action="http://maps.google.com/maps" method="get" target="directions"><input type="text" name="saddr" id="saddr" value="" /><br /><input type="hidden" name="daddr" value="'+loc+'" /></form><div>from here to:</div><form action="http://maps.google.com/maps" method="get" target="directions"><input type="text" name="daddr" id="daddr" value="" /><br /><input type="hidden" name="saddr" value="'+loc+'" /></form>'));
		}
		GEvent.addListener(this.marker, "click", function() {
			this.openInfoWindowTabsHtml(me.tabs);
		});

		this.tabs.push(new GInfoWindowTab(this.label, this.domNode));
		this.parent.markers.push(this);
	},

	dragEnd: function(marker, callback_data) {
	},

	startup: function() {
		this.parent.map.addOverlay(this.marker);
		if (this.status == "open") {
			this.open();
		}
		dojo.forEach(this.getChildren(), function(child) { child.startup(); });
	},
	
	open: function() {
		GEvent.trigger(this.marker, 'click');
	},

	addTab: function(tab) {
		this.tabs.push(tab);
	}
});

dojo.declare("quelib.MapMarkerTab",
	[dijit._Widget, dijit._Contained],
{
	label: "",
	html: "",
	parent: {},

	postCreate: function() {
		this.parent = this.getParent();
		this.html = this.domNode.innerHTML;
		var tab = new GInfoWindowTab(this.label, this.html);
		this.parent.addTab(tab);
	},
	
	startup: function() {
		this.domNode.parentNode.removeChild(this.domNode);
	}
});

dojo.declare("quelib.MapSidebar",
	[dijit._Widget],
{
	map: "",

	postCreate: function() {
		var widget = dijit.byId(this.map);
		var me = this;
		dojo.forEach(widget.markers, function(marker) {
				var summary = dojo.create('div', { innerHTML: marker.html }, me.domNode, 'last');
				var link = dojo.create('a', {onclick: function() {GEvent.trigger(marker.marker, 'click');}, 'class': 'markerIcon', innerHTML: '<img src="http://www.google.com/mapfiles/marker_'+marker.icon+'.png" />' }, summary, 'first');
			});
	}
});

}

}};});

