/* FUNCTIONS FOR DRAWING GOOGLE MAPS WITH GPS VISUALIZER (http://www.gpsvisualizer.com/) */


// Define parameters of different marker types
var gpsv_icons = {
	circle: { is:[15,15],ia:[7,7],ss:[15,15],iwa:[12,4],isa:[7,11] }
	,pin: { is:[15,26],ia:[7,25],ss:[30,26],iwa:[7,1],isa:[12,16] }
	,square: { is:[15,15],ia:[7,7],ss:[15,15],iwa:[12,4],isa:[7,11] }
	,triangle: { is:[15,15],ia:[7,7],ss:[15,15],iwa:[12,4],isa:[7,11] }
	,diamond: { is:[15,15],ia:[7,7],ss:[15,15],iwa:[12,4],isa:[7,11] }
	,google: { is:[20,34],ia:[9,34],ss:[37,34],iwa:[9,2],isa:[18,25] }
	,googlemini: { is:[12,20],ia:[6,20],ss:[22,20],iwa:[5,1],isa:[10,15] }
}

// Make sure defaults have been set; if not, everything breaks
if (!self.default_icon_style) { default_icon_style = 'pin'; }
if (!self.default_icon_color) { default_icon_color = 'red'; }

// Create a default icon for all markers
var defaultIcon = new GIcon();
defaultIcon.image = "icons/"+default_icon_style+"/"+default_icon_color+".png";
defaultIcon.shadow = "icons/"+default_icon_style+"/shadow.png";
defaultIcon.iconSize = new GSize(gpsv_icons[default_icon_style]['is'][0],gpsv_icons[default_icon_style]['is'][1]);
defaultIcon.iconAnchor = new GPoint(gpsv_icons[default_icon_style]['ia'][0],gpsv_icons[default_icon_style]['ia'][1]);
defaultIcon.shadowSize = new GSize(gpsv_icons[default_icon_style]['ss'][0],gpsv_icons[default_icon_style]['ss'][1]);
defaultIcon.infoWindowAnchor = new GPoint(gpsv_icons[default_icon_style]['iwa'][0],gpsv_icons[default_icon_style]['iwa'][1]);
defaultIcon.infoShadowAnchor = new GPoint(gpsv_icons[default_icon_style]['isa'][0],gpsv_icons[default_icon_style]['isa'][1]);

// Set up some styles
document.writeln('		<style type="text/css">');
document.writeln('			.wpt { font: 10px Verdana,sans-serif; }');
document.writeln('			.tooltip {');
document.writeln('				background-color:#FFFFFF; filter:alpha(opacity=80); -moz-opacity:0.8;');
document.writeln('				border:1px solid #666666; padding:2px;');
document.writeln('				font:10px Verdana,sans-serif; white-space: nowrap;');
document.writeln('			}');
document.writeln('		</style>');


function GPSV_Waypoint(lon,lat,name,desc,url,color,style,width) {
	
	var tempIcon = new GIcon(defaultIcon);
	if (style || color) {
		style = (style&&gpsv_icons[style])?style:default_icon_style;
		color = (color)?color:default_icon_color;
		tempIcon.image = "icons/"+style+"/"+color+".png";
		tempIcon.shadow = "icons/"+style+"/shadow.png";
		tempIcon.iconSize = new GSize(gpsv_icons[style]['is'][0],gpsv_icons[style]['is'][1]);
		tempIcon.iconAnchor = new GPoint(gpsv_icons[style]['ia'][0],gpsv_icons[style]['ia'][1]);
		tempIcon.shadowSize = new GSize(gpsv_icons[style]['ss'][0],gpsv_icons[style]['ss'][1]);
		tempIcon.infoWindowAnchor = new GPoint(gpsv_icons[style]['iwa'][0],gpsv_icons[style]['iwa'][1]);
		tempIcon.infoShadowAnchor = new GPoint(gpsv_icons[style]['isa'][0],gpsv_icons[style]['isa'][1]);
	}
	var w = (google_api_version > 1) ? new GMarker( new GLatLng(lat,lon), tempIcon ): new GMarker( new GPoint(lon,lat), tempIcon );
	var text = '';
	if (name) {
		if (url && url != null) { text = text + '<B><A target="_blank" href="'+url+'">'+name+'</A></B>'; }
		else { text = text + '<B>'+name+'</B>'; }
	}
	if (desc) {
		desc = desc.replace(/(\(\d\d\d\) ?\d\d\d-\d\d\d\d)/g,'<NOBR>$1</NOBR>'); // don't break phone numbers
		text = text + '<BR>'+desc+'';
	}
	if (eval(width) > 200) { width = 'width:'+width+'px;'; } // apparently you can't make it less than 217 (let's leave 17 for the close box though)
	if (text) { GEvent.addListener(w, "click", function(){ w.openInfoWindowHtml('<DIV style="text-align: left; '+width+'"><SPAN class="wpt">'+text+'</SPAN></DIV>'); }); }
	gmap.addOverlay(w);
	
	if (eval(google_api_version) < 2) {
		// v1 tooltips, adapted from http://www.econym.demon.co.uk/googlemaps1/tooltips.htm
		var topElement = w.images[0];
		if (w.iconImage) { topElement = w.iconImage; }
		if (w.transparentIcon) { topElement = w.transparentIcon; }
		if (w.imageMap) { topElement = w.imageMap; }
		topElement.setAttribute("title",name);
	} else {
		// v2 tooltips, adapted from http://www.econym.demon.co.uk/googlemaps/tooltips4.htm
		if (!document.getElementById('gpsv_tooltip')) { tooltip = GPSV_Initialize_Marker_Tooltip(); }
		if (name) {
			w.tooltip = '<div class="tooltip">'+name+'</div>';
			GEvent.addListener(w,'mouseover', function() { GPSV_Create_Marker_Tooltip(w); });
			GEvent.addListener(w,'mouseout', function() { tooltip.style.visibility = 'hidden' });
		}
	}
	w.text = '<DIV style="text-align: left; '+width+'"><SPAN class="wpt">'+text+'</SPAN></DIV>';
	w.my_name = name;
	return w;
}

function GPSV_Initialize_Marker_Tooltip() {
	var tt = document.createElement('div');
	gmap.getPane(G_MAP_FLOAT_PANE).appendChild(tt);
	tt.id = 'gpsv_tooltip';
	tt.style.visibility = 'hidden';
	return (tt);
}

function GPSV_Create_Marker_Tooltip(marker) {
	// copied almost verbatim from http://www.econym.demon.co.uk/googlemaps/tooltips4.htm
	tooltip.innerHTML = marker.tooltip;
	var point=gmap.getCurrentMapType().getProjection().fromLatLngToPixel(gmap.fromDivPixelToLatLng(new GPoint(0,0),true),gmap.getZoom());
	var offset=gmap.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),gmap.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	tooltip.style.visibility = 'visible';
}

function GPSV_Toggle_Opacity (overlay_array) {
	if (google_api_version > 1) {
		var msg = '';
		for (j=0; j<overlay_array.length; j++) {
			var item = overlay_array[j];
			if (eval(item.drawElement)) {
				if (item.drawElement.style.display == 'none') { item.display(true); }
				else { item.display(false); }
			} else if (eval(item.images)) {
				for (var i=0; i < item.images.length; i++) {
					if (item.images[i].style.display == 'none') { item.images[i].style.display = ''; }
					else { item.images[i].style.display = 'none'; }
				}
//			} else if (eval(item.o) && typeof(item.o) == 'object') {
//				if (item.o.style.display == 'none') { item.o.style.display = ''; }
//				else { item.o.style.display = 'none'; }
//			} else if (eval(item.z) && typeof(item.z) == 'object') {
//				if (item.z.style.display == 'none') { item.z.style.display = ''; }
//				else { item.z.style.display = 'none'; }
//			} else {
//				msg = "The 'toggle track' function isn't working, because the 'z' object cannot be found.  Perhaps it has been supplanted by one of these:\n";
//				for (var x in item) {
//					if (x.length == 1 && typeof(eval('item.'+x)) == 'object') {
//						msg = msg + x + " (" + typeof(eval('item.'+x)) + ")\n";
//					}
//				}
			} else {
//				for (var x in item) {
//					var xtype = typeof(eval('item.'+x));
//					if (xtype != 'function') {
//						msg = msg + x + " (" + xtype + ") = " + eval('item.'+x) + ";  ";
//					}
//				}
				if (eval(item.eb) || eval(item.B)) {
					gmap.removeOverlay(item);
				} else {
					gmap.addOverlay(item);
				}
			}
//			if (msg) { msg = msg + "\n"; }
		}
		if (msg) { alert(msg); }
	} else {
		for (j=0; j<overlay_array.length; j++) {
			var item = overlay_array[j];
			if (eval(item.drawElement)) {
				if (item.drawElement.style.display == 'none') { item.drawElement.style.display = ''; }
				else { item.drawElement.style.display = 'none'; }
			} else if (eval(item.images)) {
				for (var i=0; i < item.images.length; i++) {
					if (item.images[i].style.display == 'none') { item.images[i].style.display = ''; }
					else { item.images[i].style.display = 'none'; }
				}
			}
		}
	}
}
function GPSV_Toggle_Label_Opacity (label,original_color) {
	original_color = Color_Hex2CSS(original_color);
	current_color = Color_Hex2CSS(label.style.color);
	dimmed_color = Color_Hex2CSS('#CCCCCC');
	if (current_color == dimmed_color) { label.style.color = original_color; }
	else { label.style.color = dimmed_color; }
}

// This one is obsolete now that the "Toggle_Opacity" function can handle tracks AND waypoints
function GPSV_Toggle_Track_Opacity (track_array,original_opacity) {
	for (j=0; j<track_array.length; j++) {
		var trk = track_array[j];
		if (trk.opacity == 0) { trk.opacity = original_opacity; }
		else { trk.opacity = 0; }
		trk.redraw(true);
	}
}

function Color_Hex2CSS(c) { 
	var rgb = new Array(); rgb = c.match(/([A-F0-9]{2})([A-F0-9]{2})([A-F0-9]{2})/i);
	if (rgb) {
		return ('rgb('+parseInt(rgb[1],16)+','+parseInt(rgb[2],16)+','+parseInt(rgb[3],16)+')');
	} else {
		return (c.replace(/ +/g,''));
	}
}




/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e) {
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e) {
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function() {
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e) {
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

// funcion para mostrar o ocultar un layer	  

var state = 'none';

function showhide(layer_ref) {

	if (state == 'block') {
	state = 'none';
	}
	else {
	state = 'block';
	}
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
	eval( "document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
	document.layers[layer_ref].display = state;
	}
	if (document.getElementById &&!document.all) {
	hza = document.getElementById(layer_ref);
	hza.style.display = state;
	}
}




