// GMAPS API For jquery.
// By ikhuerta - ikhuerta@gmail.com

$.fn.extend({
	createMap : function (arr) {
		if (GBrowserIsCompatible()) {
		var areturn = [];
		var el;
		for (var i=0; i<this.length; i++) {
		el=this[i];
		
			if( el.id ) var id=el.id;
			else { var a=new Date; var id= "map_" + a.getTime() + i; el.id = id; }
			
			if (!arr) arr=[];
			if ( arr.zoom == "undefinded" ) arr.zoom = $.gm.config.zoom;
			if ( arr.center == "undefinded" ) arr.latLong = $.gm.config.latLong;
			if ( arr.dobleClickZoom == "undefinded" ) arr.dobleClickZoom = $.gm.config.dobleClickZoom;
			if ( arr.continuosZoom == "undefinded" ) arr.continuosZoom = $.gm.config.continuosZoom;
			if ( arr.dragMap == "undefinded" ) arr.dragMap = $.gm.config.dragMap;
			if ( arr.scrollWellZoom == "undefinded" ) arr.scrollWellZoom = $.gm.config.scrollWellZoom;
			if ( arr.viewControls == "undefinded" ) arr.viewControls = $.gm.config.viewControls;
			if ( arr.viewZoom == "undefinded" ) arr.viewZoom = $.gm.config.viewZoom;
			if ( arr.viewTypeMapList == "undefinded" ) arr.viewTypeMapList = $.gm.config.viewTypeMapList;
			if ( arr.infoWindowMaxWidth == "undefinded" ) arr.infoWindowMaxWidth = $.gm.config.infoWindowMaxWidth;
			
			var l = $.gm.maps.push( new GMap2( el ) );
			$.gm.maps[l-1].id = id;
			$.gm.maps[l-1].setCenter( arr.center , arr.zoom );
			$.gm.maps[l-1].container = $(el);
			
			
			// Bloques de HTML para crear en la web
			$.gm.maps[l-1].html = {
				typeMap :  '<ul class="mapviews"><li class="gmaps_normal"><a href="#" class="sel" onclick=\'$.gm.getMapById("'+id+'").setMapType(G_NORMAL_MAP); $.gm.typeListOnClick(this); return false\'>Callejero</a></li><li class="gmaps_satelite"><a href="#" onclick=\'$.gm.getMapById("'+id+'").setMapType(G_SATELLITE_TYPE); $.gm.typeListOnClick(this); return false\'>Satélite</a></li><li class="gmaps_hybrid"><a href="#" onclick=\'$.gm.getMapById("'+id+'").setMapType(G_HYBRID_TYPE); $.gm.typeListOnClick(this); return false;\'>Híbrido</a></li></ul>',
				controls : '<ul class="mapcontrols"><li><a href="#" class="gmaps_moveUp" onclick=\'$.gm.getMapById("'+id+'").panDirection(0,1)\'>Up</a></li><li><a href="#" class="gmaps_moveDown" onclick=\'$.gm.getMapById("'+id+'").panDirection(0,-1)\'>Down</a></li><li><a href="#" class="gmaps_moveLeft" onclick=\'$.gm.getMapById("'+id+'").panDirection(1,0)\'>Left</a></li><li><a href="#" class="gmaps_moveRight" onclick=\'$.gm.getMapById("'+id+'").panDirection(-1,0)\'>Right</a></li></ul>',
				zoom : '<ul class="mapzoom" class="zoom_' + $.gm.maps[l-1].getZoom() + '"><li><a href="#" class="gmaps_zoomin" onclick=\'$.gm.getMapById("'+id+'").zoomIn()\'>+</a></li><li><a href="#" class="gmaps_zoomout" onclick=\'$.gm.getMapById("'+id+'").zoomOut()\'>-</a></li></ul>'				
			};
			
			
			$.gm.maps[l-1].view = { 
					normal : function () { 
						$.gm.maps[l-1].setMapType(G_NORMAL_MAP) 
					} ,
					satellite : function () { 
						$.gm.maps[l-1].setMapType(G_SATELLITE_TYPE) 
					} ,
					hybrid : function () { 
						$.gm.maps[l-1].setMapType(G_HYBRID_TYPE) 
					} ,
					typemaplist	 : function () { 
						var id = $.gm.maps[l-1].id;
						var parent = $.gm.maps[l-1].container.parent().prepend($.gm.maps[l-1].html.typeMap);
					},
					controls : function () { 
						$.gm.maps[l-1].container.append($.gm.maps[l-1].html.controls);
					},
					zoom : function () { 
						$.gm.maps[l-1].container.append($.gm.maps[l-1].html.zoom);
					}
					
				};
			
			if (arr.continuosZoom) $.gm.maps[l-1].enableContinuousZoom();
			if (arr.dobleClickZoom)  $.gm.maps[l-1].enableDoubleClickZoom();
			if (arr.scrollWellZoom) $.gm.maps[l-1].enableScrollWheelZoom();
			if (arr.viewControls) $.gm.maps[l-1].addControl(new GSmallMapControl());
			if (arr.viewZoom) $.gm.maps[l-1].view.zoom();
			if (arr.viewType) $.gm.maps[l-1].addControl(new GMapTypeControl());
			if (!arr.dragMap) $.gm.maps[l-1].disableDragging();
			
			
			// Bloques de HTML para crear en la web
			$.gm.maps[l-1].html = {
				typeMap :  '<ul class="mapviews"><li class="gmaps_normal"><a href="#" class="sel" onclick=\'$.gm.getMapById("'+id+'").setMapType(G_NORMAL_MAP); $.gm.typeListOnClick(this); return false\'>Callejero</a></li><li class="gmaps_satelite"><a href="#" onclick=\'$.gm.getMapById("'+id+'").setMapType(G_SATELLITE_TYPE); $.gm.typeListOnClick(this); return false\'>Satélite</a></li><li class="gmaps_hybrid"><a href="#" onclick=\'$.gm.getMapById("'+id+'").setMapType(G_HYBRID_TYPE); $.gm.typeListOnClick(this); return false;\'>Híbrido</a></li></ul>',
				controls : '<ul class="mapcontrols"><li><a href="#" class="gmaps_moveUp" onclick=\'$.gm.getMapById("'+id+'").panDirection(0,1)\'>Up</a></li><li><a href="#" class="gmaps_moveDown" onclick=\'$.gm.getMapById("'+id+'").panDirection(0,-1)\'>Down</a></li><li><a href="#" class="gmaps_moveLeft" onclick=\'$.gm.getMapById("'+id+'").panDirection(1,0)\'>Left</a></li><li><a href="#" class="gmaps_moveRight" onclick=\'$.gm.getMapById("'+id+'").panDirection(-1,0)\'>Right</a></li></ul>',
				zoom : '<ul class="mapzoom" class="zoom_' + $.gm.maps[l-1].getZoom() + '"><li><a href="#" class="gmaps_zoomin" onclick=\'$.gm.getMapById("'+id+'").zoomIn()\'>+</a></li><li><a href="#" class="gmaps_zoomout" onclick=\'$.gm.getMapById("'+id+'").zoomOut()\'>-</a></li></ul>'				
			};
			
			$.gm.maps[l-1].ready = function ( fnc ) { this.functionload = fnc;};
			$.gm.maps[l-1].interval = setInterval (function() {
					if ( $.gm.maps[l-1].isLoaded() && $.gm.maps[l-1].functionload ) {
						clearInterval ($.gm.maps[l-1].interval); 
						setTimeout( function () {$.gm.maps[l-1].functionload();},1500);
					}
				},100);
				
			$.gm.maps[l-1].createMark =  function (arr) { // {id , point, idicon }
				icon = $.gm.getIconById(arr.icon);
				themap = this;
				var l = $.gm.marks.push(new GMarker( arr.point , icon));
				var o = themap.addOverlay( $.gm.marks[l-1]  );
				$.gm.marks[l-1].click = function (fnc) { GEvent.addListener( this , "click", fnc );	}
				$.gm.marks[l-1].mouseover = function (fnc) { GEvent.addListener( this , "mouseover", fnc );	}
				$.gm.marks[l-1].mouseout = function (fnc) { GEvent.addListener( this , "mouseout", fnc );	}
				$.gm.marks[l-1].html = "";
				$.gm.marks[l-1].info = function () {
					if ( arguments[0] ) $.gm.marks[l-1].html = arguments[0];
					else $.gm.marks[l-1].openInfoWindowHtml($.gm.marks[l-1].html, {maxWidth: $.gm.config.infoWindowMaxWidth });
				};
				$.gm.marks[l-1].id = arr.id;
				if ( $.gm.marks[l-1].getIcon().imagehover ) {
					$.gm.marks[l-1].hover = function () {
						this.setImage( this.getIcon().imagehover );		
					}
					$.gm.marks[l-1].out = function () {
						this.setImage( this.getIcon().imageout );		
					}
				}
				return $.gm.marks[l-1];
			};
			
			areturn.push( $.gm.maps[l-1] );
		}
		
		areturn.createMark = areturn[0].createMark;
		areturn.view = areturn[0].view;
		areturn.html = areturn[0].html;
		areturn.ready = areturn[0].ready;
		return areturn;
		
		} else return false;
	} ,
	
	map : function () {
		if ( this[0].id && $.gm.getMapById( this[0].id ) ) {
				return $.gm.getMapById( this[0].id );
			}
	},
	maps : function () {
		var areturn = [];
		for (var i=0; i<this.length; i++) {
		el=this[i];
			if ( el.id && $.gm.getMapById( el.id ) ) {
				areturn.push( $.gm.getMapById( el.id ) );
			}
		}
		return areturn;
	}

	
});


$.gm = {
	maps : [],
	icons : [],
	marks : [],
	images : [],
	config : {
		zoom : 6,
		center : new GLatLng(39.0,-5.0),
		dobleClickZoom : true,
		continuosZoom : true,
		dragMap : true,
		scrollWellZoom :false,
		viewControls : true,
		viewZoom : true,
		viewTypeMapList : true,
		infoWindowMaxWidth : 250
		},
	getMapById : function ( id ) {
		for (var i=0; i< $.gm.maps.length; i++)
			if ($.gm.maps[i].id == id ) return $.gm.maps[i];
	},
	newIcon : function ( id , values ){
		var icon = new GIcon();
		var p;
		
		if (values.image){
			icon.image = values.image;
			icon.imageout = values.image;
			var l = $.gm.images.push(new Image);
			$.gm.images[l-1].src=values.image;
		}
		if (values.imagehover){
			icon.imagehover = values.imagehover;
			l = $.gm.images.push(new Image);
			$.gm.images[l-1].src=values.imagehover;
		}
		if (values.shadow)
	 		icon.shadow = values.shadow;
		if (values.iconsize) {
	 		var p = values.iconsize.split(",");
	 		icon.iconSize =  new GSize(p[0],p[1]);
		}
		if (values.shadowSize) {
	 		var p = values.shadowSize.split(",");
	 		icon.shadowSize = new GSize(p[0],p[1]);
		}
	 	if (values.iconAnchor) {
			var p = values.iconAnchor.split(",");
			icon.iconAnchor = new GPoint(p[0],p[1]);
	 	}
		if (values.infoWindowAnchor) {
			var p = values.infoWindowAnchor.split(",");
	 		icon.infoWindowAnchor = new GPoint(p[0],p[1]);
		}
		
	 	icon.id=id;
	 	var l = $.gm.icons.push( icon );
	 	return $.gm.icons[l-1];
		
	},
	getIconById : function ( id ) {
		for (var i=0; i< $.gm.icons.length; i++)
			if ($.gm.icons[i].id == id ) return $.gm.icons[i];
	}, 
	getMarkById : function ( id ) {
		for (var i=0; i< $.gm.marks.length; i++)
			if ($.gm.marks[i].id == id ) return $.gm.marks[i];
	}, 
	typeListOnClick : function (el){
		$(el).parent().parent().find("a").removeAttr("class");
		$(el).addClass("sel");
		}
}