// Locoos WP Maps plugin by Didier Durand
//
// http://www.locoos.com


/**
 * Class to store all single map related parameters: markers, geo position,
 * map type, map options, overlays, HTML element to hook to, style etc.
 */

function MapData(elem, defaultW, defaultH) {
	// DOM related stuff
	this.elem=elem;
	this.inheritedStyle=null;
	this.className=null;
	this.inheritedID=null;
	this.defaultW=defaultW;
	this.defaultH=defaultH;

	// Map related stuff
	this.type=G_NORMAL_MAP;
	this.geoCoord=null;
	this.zoom=null;
	this.overviewMapControl=true;
	this.controls=true;
	this.showMarkers=true;

	this.defaultGeoCoord=new GLatLng(46.76209,6.388721);
}


/**
 * This is the method that effectively creates the Google Map based
 * on the MapData object.
 */

MapData.prototype.createGoogleMap = function() {
	var realMap;
	var mapNode;

	mapNode = document.createElement("div");

	if (this.className) mapNode.className = "map " + this.className;
	else mapNode.className = "map";

	if (this.inheritedStyle) mapNode.style.cssText = this.inheritedStyle;

	mapNode.style.display = "block";
	if (mapNode.style.visibility == "hidden")
		 mapNode.style.visibility="inherit";

	this.elem.appendChild(mapNode);


	if (mapNode.style.width == 0) {
		if (this.defaultW.toString().indexOf("%")!=-1)
			mapNode.style.width = this.defaultW;
		else mapNode.style.width = this.defaultW + "px";
	}

	if (mapNode.style.height == 0) {
		if (this.defaultH.toString().indexOf("%")!=-1)
			mapNode.style.height = this.defaultH;
		else mapNode.style.height = this.defaultH + "px";
	}

	if (this.inheritedID) mapNode.id = this.inheritedID;
	else mapNode.id = "googlemap-" + Math.ceil(10000*Math.random());

	realMap = new GMap2(mapNode);
	var mapType = realMap.getCurrentMapType();
    this.zoom=13;
	realMap.setCenter(this.defaultGeoCoord, this.zoom);
	realMap.setMapType(G_HYBRID_MAP);


	if (this.controls) {
		if (parseInt(mapNode.style.height)>=320)
			realMap.addControl(new GLargeMapControl());
		else realMap.addControl(new GSmallMapControl());

		realMap.addControl(new GMapTypeControl());
		realMap.addControl(new GScaleControl());

		if (this.overviewMapControl)
			realMap.addControl(new GOverviewMapControl());
	}
	
	realMap.setMapType(this.type);
}



/**
 * Walks through html source looking for {{locoosmap}} tag
 */

MapPlugin.prototype.spotMapContainers = function() {

	var body = $$('body').reduce();
	var html = body.innerHTML;
	
	var newHtml = html.replace(/{{locoosmap}}/g, '<div class="locoosmap"></div>');
	body.update(newHtml);
	
	var mapContainers = $$('.locoosmap');
	var createMapData = function (elem) {
		return new MapData(elem,this.defaultW,this.defaultH);
	}
	
	this.maps = mapContainers.collect(createMapData.bind(this));
	return;
	
}
		


/**
 * After parsing and creating all MapData objects, efficiently create
 * the found maps.
 */

MapPlugin.prototype.createMaps = function() {
	var map;

	while ((map=this.maps.pop())) {
		map.createGoogleMap();
	}
}




/*
 * Object created by window.onload() event, contains global maps options as
 * sizes etc, starts and finishes all maps fetching and creation proccess.
 *
 */
function MapPlugin(_defaultWidth, _defaultHeight) {
	this.defaultW = _defaultWidth;
	this.defaultH = _defaultHeight;

	this.maps=new Array;

	this.spotMapContainers();
	this.createMaps();
}



/**
 * Creates triggers on page loading/unloading events for plugin initialization.
 */

function MapPluginInit(_defaultWidth, _defaultHeight) {
	
	if (! GBrowserIsCompatible()) return;

	var oldOnLoad = window.onload;
	var oldOnUnload = window.onunload;

	var instantiate = function() {
		new MapPlugin(_defaultWidth, _defaultHeight);
	}

	if (typeof window.onload != 'function')
		window.onload = instantiate;
	else window.onload = function() {
		oldOnLoad();
		instantiate();
	}

	if (typeof window.onunload != 'function')
		window.onunload = GUnload;
	else window.onunload = function() {
		oldOnUnload();
		GUnload();
	}
}
