var map= null;
var exml = null;
var showWarnings = 1;
var popup = 1;
var showform = true;
var args = new Object();


function getParameter(strParam) {
	var src = location.search;
	var length = strParam.length;

	var start = src.indexOf(strParam + "=");

	if ( length <= 0 || start == -1 ) return "";

	start += length + 1;

	var stop = src.indexOf("&", start);

	if ( stop == -1 ) stop = src.length;

	return src.substr(start, stop - start);
}



function initialize() {

	loadQueryString();
	
	showWarnings = args["warnings"];
	popup = args["popup"];
	showform= args["showform"];

	
	if(typeof(showWarnings) == "undefined")
	{
		showWarnings = "1";
	}

	
	if(typeof(popup) == "undefined")
	{
		popup = "1";
	}
	
	if(typeof(showform) == "undefined")
	{
		showform = "false";
	}
			
	if(popup != "0")
	{
		initPopup();
	}
	
	if(showWarnings == "0")
	{
		warnings = new Array();
	}
	
	
	if(showform == "true")
	{
		document.getElementById('content').style.display = 'block'; 
	}

	if ( GBrowserIsCompatible() ) {
	
		map = new GMap2( document.getElementById( "map_canvas" ) );
                map.setCenter(new GLatLng(59.545457, 16.207581),9);		
		GMap2.prototype.hoverControls = function( opt_noCloseIw ){
			var theMap = this;
			theMap.hideControls();
			GEvent.addListener(theMap, "mouseover", function() { theMap.showControls(); } );
			GEvent.addListener(theMap, "mouseout", function() { theMap.hideControls();modifyPopup(0); } );
			theMap.libraryCard = 19162;
			var theMap = this;
		}
		
		GMap.prototype.hoverControls = GMap2.prototype.hoverControls;
		map.setUIToDefault();
		map.hoverControls();
//map.addMapType(G_SATELLITE_3D_MAP);
		warnings.sort(sortfunction);
				
		exml = new GeoXml("exml", map, "me.kml?a");
//		exml = new GeoXml("exml", map, "me.kmz");
		GEvent.addListener(map, 'addoverlay', add);
		exml.parse();

		
		map.savePosition();
		

	}
}


function select( obj ) {

	var area = document.getElementById('area');
	var areaId = obj.name;
	var areaInfo = getAreaInfo(areaId);
	
	area.value = areaInfo;
	modifyFormArea(parseInt(areaId));
}


function loadQueryString()
{
	var query = window.location.search.substring(1);
	var pairs = query.split("&"); 
	
	for(var i = 0; i < pairs.length; i++) 
	{
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	} 
}

function getIcon( iconColor ) {
	if( ( typeof( iconColor ) == "undefined" ) || ( iconColor==null ) ) iconColor = "red";
	if( !icons[iconColor] ) {
		icons[iconColor] = new GIcon( icons["red"] );
		icons[iconColor].image = "http://labs.google.com/ridefinder/images/mm_20_"+ iconColor +".png";
		icons[iconColor].shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icons[iconColor].iconSize = new GSize(12, 20);
		icons[iconColor].shadowSize = new GSize(22, 20);
		icons[iconColor].iconAnchor = new GPoint(6, 20);
		icons[iconColor].infoWindowAnchor = new GPoint(5, 1);
	}
	return icons[iconColor];
}


function mouseOverPolygon(obj)
{
	if(obj.color == "#0000FF" && popup == "1")
	{
		obj.setStrokeStyle({opacity:1.0}); 
		obj.setFillStyle({opacity:0.1}); 
	}
	else if(obj.color != "#0000FF" && (popup == "1" || popup == "2"))
	{
		obj.setStrokeStyle({opacity:1.0}); 
		obj.setFillStyle({opacity:0.1});
	}

	
	if(popup == "1")
	{
		modifyPopup(obj.name);
	}
	else if(popup == "2" && obj.color != "#0000FF")
	{
		modifyPopup(obj.name);
	}
	
	map.getDragObject().setDraggableCursor("pointer");
}

function mouseOutPolygon(obj)
{
	if(obj.color == "#0000FF" && popup == "1")
	{
		obj.setStrokeStyle({opacity:0.0});
		obj.setFillStyle({opacity:0.27}); 
	}
	else if(obj.color != "#0000FF" && (popup == "1" || popup == "2"))
	{
	    obj.setStrokeStyle({opacity:0.0});
		obj.setFillStyle({opacity:0.6});
	}

	if(popup == "1" || popup == "2")
	{
		modifyPopup(0);
	}
	
	map.getDragObject().setDraggableCursor("default");
}


function add(obj) {


	GEvent.clearNode(obj);

	GEvent.addListener(obj, "mouseover", function(){mouseOverPolygon(obj);});
	GEvent.addListener(obj, "mouseout", function(){mouseOutPolygon(obj);});

	obj.setStrokeStyle({opacity:0.0}); 
	
	GEvent.addListener(obj, "click", function() { select(obj);});
	
	for(var i = 0; i < warnings.length; i++)
	{
		var areas = loopAreas[warnings[i].id];
		var warning = warnings[i];
		
		if(typeof(areas) != "undefined")
		{
			for(var j = 0; j < areas.length; j++)
			{
				var warning = warnings[i];
				if(showWarnings != "0" && obj.name == areas[j])
				{
					switch(warning.code)
					{
						case 9:
						   obj.setFillStyle({color:"#66CC33", opacity:0.6}); 
						   obj.setStrokeStyle({color:"#66CC33", opacity:0.6});
						   break;
						   
						case 6:
						case 7:
						case 8:
							obj.setFillStyle({color:"#FFFF00", opacity:0.6});
							obj.setStrokeStyle({color:"#FFFF00", opacity:0.6});
							break;
						
						default:
						   obj.setFillStyle({color:"#FF3333", opacity:0.6}); 
						   obj.setStrokeStyle({color:"#FF3333", opacity:0.6}); 
					}
				}
			}
		}
		else
		{
			
			if(showWarnings != "0" && obj.name == warning.id)
			{
				
				switch(warning.code)
				{
					case 9:
					   obj.setFillStyle({color:"#66CC33", opacity:0.6}); 
					   obj.setStrokeStyle({color:"#66CC33", opacity:0.6});
					   break;
					   
					case 6:
					case 7:
					case 8:
						obj.setFillStyle({color:"#FFFF00", opacity:0.6});
						obj.setStrokeStyle({color:"#FFFF00", opacity:0.6}); 
						break;
					
					default:
					   obj.setFillStyle({color:"#FF3333", opacity:0.6}); 
					   obj.setStrokeStyle({color:"#FF3333", opacity:0.6}); 
					
				}
			}
		}
		
	}

} 

