var cMap = null;
var hoverPin = new Image();
hoverPin.src = "pics/pin/pin2.png";

function OnLoad()
{
	var startMapID = GetOT();				//ID of the current map... 1 in the beginning for overall OT view
	var mapDivId = "map";						//ID of the div in which the map is to be displayed ("map" is in index.php)
	var startMask = GetMask();			//type of the pins to be displayed (3F = 111111 = all pins are displayed)

	cMap = new MyMap(startMapID, mapDivId);		//map "class" (NOT THE ACTUAL MAP ITSELF)
	LoadModule();							//load modules to the right (calendar etc)
	cMap.LoadMap(startMapID);	//handler to the gMap itself (NOT THE "CLASS")
	cMap.LoadPins(startMask);	//load all pins filtered by startMask
}

function GetMask()
{
	//var cookie = document.cookie;
	//if(cookie == null)
		return 0xFC;

	var vars = cookie.split(";");

	for(var i = 0; i < vars.length; i++)
	{
		var curVar = vars[i].split("=");
		if(curVar[0] == " mask")
			return curVar[1];
	}
	return 0x3F;
}

//fired when a marker is clicked
function OnClickMarker(marker)
{
	//all markers contain their type
	if(marker.type == TYPE_OT)
		window.location.href = "index.php?site=0&ot="+marker.href;	//if OT then href referes to a map id
	else if(marker.type == TYPE_FEWO)
		ShowFewo(marker.id);//if FEWO then display the detail site
	else if(marker.type == TYPE_KIRCHE)
		window.open(marker.href);
	else if(marker.type == TYPE_INFO)
		ShowPopup(marker.tooltip);
	else
		alert("error... kein marker type gesetzt:\nmarker.type = \""+marker.type+"\"");
}

function OnBackButton()
{
	window.location.href="index.php?site=0&ot=1";
}

function LoadModule ()
{
	/*var moduleDiv = document.getElementById("moduleDiv");
	var maskArray = GetMarkerTypeArray();
	var moduleCode = "";

	moduleCode += "<form name='MaskCheckboxes'>"+
					"<table id='maskCheckboxTable'><tr><th class='bgdarkblue'>"+
						"<p class='fontModuleHeader''>Ansicht</p>"+
					"</th></tr>";
	for(var i=0; i<maskArray.length; i++)//var mask in maskArray)
	{
		var mask = maskArray[i];
		if(mask.mask & cMap.showMask || cMap.showMask == 0) //0 means everything is to be displayed
			moduleCode += "<tr><td><span class='fontMaskCheckbox'><input type='checkbox' name='MaskCheckboxes' value='"+mask.mask+"' onchange='OnChangeMask(this)' checked='checked'><nobr>"+mask.name+"</nobr></span></td></tr>";
		else
			moduleCode += "<tr><td><span class='fontMaskCheckbox'><input type='checkbox' name='MaskCheckboxes' value='"+mask.mask+"' onchange='OnChangeMask(this)'><nobr>"+mask.name+"</nobr></span></td></tr>";
	}

	moduleCode += "</table></form>"

	moduleDiv.innerHTML = moduleCode;*/
	var today = new Date();
	calShowCalendar(
			{divId:"termineKalender",
			 month:today.getMonth()+1, year:today.getFullYear(),
			 browseable:0,
			 navigationType:"symbols",
			 preDiv:null,
			 postDiv:null,
			 browseable: true,
			 tblId:"calSmall",
			 overlayHorizontalAlign:"left",
			 overlayVerticalAlign:"center",
			 overlayFade: false,
			 overlayFadeSpeed: 0,
			 overlayDelay: 100,
			 onEventClick:"static",
			 onEventClickStaticVerticalAlign:"center",
			 onEventClickStaticHorizontalAlign:"left"});
}

function OnChangeMask(elem)
{
	var Mask = cMap.showMask;
	var changeMask = elem.value;			//mask which is to be set/removed

	//Mask is to be set
	if(elem.checked == true)
		Mask |= changeMask;
	//Mask is to be removed
	else
		Mask ^= changeMask;

	cMap.LoadPins(Mask);
}

function ShowFewo(pinId)
{
	window.location.href="index.php?site=4&id="+pinId+"&ot="+cMap.id;
}

function ZoomPicture(picObj)
{
	var oldElem = document.getElementById("zoompic");
	if(oldElem)
		document.body.removeChild(oldElem);

	var overlayDiv = document.createElement("div");
	overlayDiv.setAttribute("id","zoompic");

	document.body.appendChild(overlayDiv);

	var closeBtn = document.createElement("input");
	closeBtn.setAttribute("type", "button");
	closeBtn.setAttribute("value", "X");
	closeBtn.parentDiv = overlayDiv;
	closeBtn.onclick = CloseZoomPicture;

	var br = document.createElement("br");

	var img = document.createElement("img");
	img.src = picObj.src;
	img.parentDiv = overlayDiv;
	img.onclick = CloseZoomPicture;

	overlayDiv.appendChild(closeBtn);
	overlayDiv.appendChild(br);
	overlayDiv.appendChild(img);
}

function CloseZoomPicture()
{
	var zoomDiv = this.parentDiv;
	document.body.removeChild(zoomDiv);
}

function ShowPopup(content)
{
	var oldElem = document.getElementById("popup");
	if(oldElem)
	{
		document.body.removeChild(oldElem);
		return;
	}

	var overlayDiv = document.createElement("div");
	overlayDiv.setAttribute("id","popup");

	document.body.appendChild(overlayDiv);

	var closeBtn = document.createElement("input");
	closeBtn.setAttribute("type", "button");
	closeBtn.setAttribute("value", "schliessen");
	closeBtn.setAttribute("id","popupClose");
	closeBtn.parentDiv = overlayDiv;
	closeBtn.onclick = ClosePopup;


	var contentDiv = document.createElement("div");
	contentDiv.innerHTML = content;
	contentDiv.parentDiv = overlayDiv;
	contentDiv.onclick = ClosePopup;
	contentDiv.setAttribute("id","popupContent");
	var titlebarDiv = document.createElement("div");
	titlebarDiv.parentDiv = overlayDiv;
	titlebarDiv.setAttribute("id","popupTitlebar");

	titlebarDiv.appendChild(closeBtn);
	overlayDiv.appendChild(titlebarDiv);
	overlayDiv.appendChild(contentDiv);
}

function ClosePopup()
{
	var zoomDiv = this.parentDiv;
	document.body.removeChild(zoomDiv);
}

function submitSearch()
{
	var site = document.createElement("input");
	site.name = "site";
	site.value = "5";
	site.style.height="1px";
	site.style.width="1px";
	site.style.visibility="hidden";
	site.style.position = "absolute";
	site.style.right = "5px";

	document.forms["searchform"].appendChild(site);
	var pers = document.forms["searchform"]["suchePers"].value;
	var preis = document.forms["searchform"]["suchePreis"].value;
	if(pers != "" || preis != "")
		document.forms["searchform"].submit();
	else
		alert("Bitte mindestens ein Suchkriterium ausfüllen!")
}

function submitRoute()
{
	var plzort = document.forms["routeform"]["herkunftplzort"].value;
	var strasse = document.forms["routeform"]["herkunftstrasse"].value;
	var adresse = plzort + " " + strasse;
	var props = cMap.GetMapProps();

	var url = "index.php?site=7&from="+adresse+"&otid="+props.id;
	window.location.href = url;
}
