﻿/// <reference path="gmap_js_helper.js" />
this.MapLoaded = false;
var IsInIframe = (top.location.href != self.location.href);
var NumberOfReservedCells = 8;
var NumberOfDays = 28;


// die Seite _im_ iframe enhaelt diese Funktion
// _muss_ per onload gerufen werden
function get_scroll_ht() {
	if (IsInIframe) {
		var height = document.height;
		if (height == null)
			height = document["documentElement"].scrollHeight;
		parent.ResizeIframe("ReservationOverview", height);
	}
}



function SwitchView(ViewName) {
	var ReservationOverviewList = document.getElementById("ReservationOverviewList");
	var ReservationOverviewMap = document.getElementById("ReservationOverviewMap");
	var ViewSelectionList = document.getElementById("ViewSelectionList");
	var ViewSelectionMap = document.getElementById("ViewSelectionMap");

	switch (ViewName) {
		case "List":
			ReservationOverviewList.style.display = "";
			ViewSelectionList.className = "ViewActive";
			ReservationOverviewMap.style.display = "none";
			ViewSelectionMap.className = "ViewInactive";
			break;
		case "Map":
			ReservationOverviewList.style.display = "none";
			ViewSelectionList.className = "ViewInactive";
			ReservationOverviewMap.style.display = "";
			ViewSelectionMap.className = "ViewActive";
			if (!this.MapLoaded) {
				document.getElementById("map").style.height = (IsInIframe) ? (parent.document.body.clientHeight - 200) + "px" : "800px";
				LoadGoogleMap();
				this.MapLoaded = true;
			}
			break;
	}

	get_scroll_ht();
}


function VisualizeReservationOverviewList(Communities, HeaderDays, FirstSundayIndex) {
	var LatestCanton = "";
	var Cantons = new Array();
	var HeaderDayCellsHtml = ""

	// build day-cells for header
	for (var k = 0; k < NumberOfDays; ++k) {
		var CssClassTag = "";
		if (k == 0)
			CssClassTag = " class=\"Today\"";
		else if ((k - FirstSundayIndex) % 7 == 0)
			CssClassTag = " class=\"Sunday\"";

		HeaderDayCellsHtml += "<th" + CssClassTag + ">" + ((HeaderDays[k] < 10) ? "0" : "") + HeaderDays[k] + "</th>";
	}

	// for each community item
	for (var i = 0; i < Communities.length; ++i) {
		// cell declarations
		var Cells = Communities[i];
		var ReservationSystemID = Cells[0];
		var Canton = Cells[1];
		var CommunityName = Cells[2];
		var MailDelivery = Cells[3];
		var CreditCard = Cells[4];
		var CommunityLink = Cells[5];
		var LocationLat = Cells[6];
		var LocationLng = Cells[7];

		// header & begin of new table
		if (Canton !== LatestCanton) {
			LatestCanton = Canton;
			Cantons.push(Canton);
			if (i !== 0)	// close table of latest canton
				document.write("</table>");
			document.write(GetTableBeginTag(Canton) + "<tr><th class=\"Kanton\">" + Canton + "</th><th></th>" + HeaderDayCellsHtml + "</tr>");
		}

		// normal community row
		var CommunityRowHtml = "<tr onclick=\"window.open('" + CommunityLink + "', 'ReservationSystem" + ReservationSystemID + "')\" onmouseover=\"HighlightRow(this)\" onmouseout=\"UnHighlightRow(this)\"><td class=\"Gemeinde\">";
		CommunityRowHtml += "<a href=\"" + CommunityLink + "\" target=\"ReservationSystem" + ReservationSystemID + "\">" + CommunityName + "</a></td><td class=\"IconCell\">";
		// Mail delivery icon (if specified)
		if (MailDelivery != null) {
		    var MailDeliveryImageName = (MailDelivery == 1) ? "Assets/MailDelivery.png" : "Assets/NoMailDelivery.png";
			var MailDeliveryText = (MailDelivery == 1) ? "Postversand" : "kein Postversand";
			CommunityRowHtml += "<img class=\"MailDelivery\" src=\"" + MailDeliveryImageName + "\" alt=\"" + MailDeliveryText + "\" title=\"" + MailDeliveryText + "\" />";
		}
		if (CreditCard != null) {
		    var CreditCardImageName = (CreditCard == 1) ? "Assets/CreditCard.png" : "Assets/NoCreditCard.png";
			var CreditCardText = (CreditCard == 1) ? "Online-Zahlungsmöglichkeiten" : "keine Online-Zahlungsmöglichkeiten";
			CommunityRowHtml += "<img class=\"CreditCard\" src=\"" + CreditCardImageName + "\" alt=\"" + CreditCardText + "\" title=\"" + CreditCardText + "\" />";
		}
		CommunityRowHtml += "</td>";

		// days
		for (var j = NumberOfReservedCells; j < NumberOfReservedCells + NumberOfDays; ++j) {
		    var AvailableAmountText = (Cells[j] == undefined) ? "" : Cells[j];

			// Cell Style
		    var CssClass = (Cells[j] == undefined) ? "ResFull" : "ResNone";
			if (j == NumberOfReservedCells)	// today (first cell)
				CssClass += "Today";
			else if ((j - NumberOfReservedCells - FirstSundayIndex) % 7 == 0)	// selected (every 7th cell)
				CssClass += "Selected";

			CommunityRowHtml += "<td class=\"" + CssClass + "\">" + AvailableAmountText + "</td>";
		}

		document.write(CommunityRowHtml + "</tr>");

		if (i == (Communities.length - 1)) // close last table
			document.write("</table>");
	}


	// populate DropDownList with cantons
	var ddlCanton = document.getElementById("ddlCanton");
	for (var i = 0; i < Cantons.length; i++) {
		ddlCanton.options[i + 1] = new Option(Cantons[i], Cantons[i]);
	}
}


function GetTableBeginTag(Canton) {
	return "<table id=\"" + Canton + "\" align=\"Center\" class=\"ReservationsTable\">";
}


// Highlighting
function HighlightRow(TableRow) {
	TableRow.style.backgroundColor = "#b1c4d1";
}
function UnHighlightRow(TableRow) {
	TableRow.style.backgroundColor = "";
}
function HighlightColumn(TableName, ColumnIndex) {
	var Table = document.getElementById(TableName);
	for (var i = 0; i < Table.rows.length; i++)
		Table.rows[i].cells[ColumnIndex].style.backgroundColor = "#b1c4d1";

	// highlight header row in table below
	var Tables = document.getElementsByTagName("table");
	for (var i = 1; i < Tables.length - 1; i++) {
		if (Tables[i].id == TableName && i != (Tables.length - 2)) {
			Tables[i + 1].rows[0].cells[ColumnIndex].style.backgroundColor = "#b1c4d1";
			break;
		}
	}
}
function UnHighlightColumn(TableName, ColumnIndex) {
	var Table = document.getElementById(TableName);
	for (var i = 0; i < Table.rows.length; i++)
		Table.rows[i].cells[ColumnIndex].style.backgroundColor = "";

	// unhighlight header row in table below
	var Tables = document.getElementsByTagName("table");
	for (var i = 1; i < Tables.length - 1; i++) {
		if (Tables[i].id == TableName && i != (Tables.length - 2)) {
			Tables[i + 1].rows[0].cells[ColumnIndex].style.backgroundColor = "";
			break;
		}
	}
}


// mouse event handlers
function MouseEventHandler(e) {
	var MouseEvent = (e) ? e : event; // ensures Browser compatibility
	var EventSource = (MouseEvent.srcElement) ? MouseEvent.srcElement : MouseEvent.target;
	if ((EventSource.tagName == "TD" || EventSource.tagName == "TH") && document.getElementById("ReservationOverviewList").style.display != "none")	// if event was fired from a TableCell
	{
		var TableName = EventSource.offsetParent.id;
		var CellIndex = EventSource.cellIndex;
		if (CellIndex <= 1 || TableName == "")	// do not highlight the first two column
			return;

		switch (MouseEvent.type.toLowerCase()) {
			case "mouseover":
				HighlightColumn(TableName, CellIndex);
				break;
			case "mouseout":
				UnHighlightColumn(TableName, CellIndex);
				break;
		}
	}
}
document.onmouseover = MouseEventHandler;
document.onmouseout = MouseEventHandler;


// Filtering
function FilterByCanton() {
	var Tables = document.getElementsByTagName("table");
	var SelectedCanton = document.getElementById("ddlCanton").value;

	for (var i = 1; i < Tables.length - 1; i++) {
		var Canton = Tables[i].id;
		Tables[i].style.display = (Canton !== SelectedCanton && SelectedCanton !== "all") ? "none" : "";
	}
	get_scroll_ht();
}








// Google Map
function LoadGoogleMap() {
	if (GBrowserIsCompatible()) {
		this.map = new GMap2(document.getElementById("map"));
		var OverviewMap = new GOverviewMapControl();
		map.addControl(OverviewMap);
		map.setUIToDefault();
		map.setCenter(new GLatLng(47.133687, 8.492431), 8);
		var mgrOptions = { borderPadding: 0, trackMarkers: false };
		this.mgr = new MarkerManager(this.map, mgrOptions);

		PopulateMapFilters();
		SetMarkers();
	}
}

// Set Google Map Markers
function SetMarkers() {
	var ddlDaySelection = document.getElementById("ddlDaySelection");
	var MinAmount = document.getElementById("ddlAmount").value;
	var DayIndex = ddlDaySelection.value;
	var DayName = ddlDaySelection.options[ddlDaySelection.selectedIndex].text;

	DayIndex = parseInt(DayIndex, 10);

	mgr.clearMarkers();
	var allMarkers = [];

	// for each community item
	for (var i = 0; i < Communities.length; ++i) {
		// cell declarations
		var Cells = Communities[i];
		var CommunityName = Cells[2];
		var AvailableToday = (Cells[NumberOfReservedCells + DayIndex] == undefined) ? 0 : Cells[NumberOfReservedCells + DayIndex];

		if (Cells[6] != "") {
			var ShowIcon = new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/ms/micons/" + ((AvailableToday >= MinAmount) ? "green" : "red") + "-dot.png");
			ShowIcon.iconSize = new GSize(30, 30);
			var Marker = new GMarker(new GLatLng(Cells[6], Cells[7]), { title: CommunityName + " (" + AvailableToday + " verfügbar)", icon: ShowIcon });
			Marker.bindInfoWindowHtml("<span style=\"font-family: Verdana; font-size: 10px;\"><span style=\"font-size: 13px; font-weight: bold;\">" + CommunityName + "</span><br /><br />" + ((DayIndex == 0) ? "Heute verfügbar: " : "Am " + DayName + " verfügbar: ") + AvailableToday + " Stück<br /><br /><a href=\"" + Cells[5] + "\" style=\"color: #00496c\" target=\"_blank\">Karte reservieren</a></span>");
			allMarkers.push(Marker);
		}
	}

	mgr.addMarkers(allMarkers, 8);
	mgr.refresh();
}



function PopulateMapFilters() {
	var ddlDaySelection = document.getElementById("ddlDaySelection");
	var MonthName = ThisMonthName;
	for (var i = 0; i < HeaderDays.length; i++) {
		if (HeaderDays[i] == 1 && i != 0)   // set next month name
			MonthName = NextMonthName;
		ddlDaySelection.options[i] = new Option(HeaderDays[i] + ". " + MonthName, i);
	}

	var ddlAmount = document.getElementById("ddlAmount");
	for (var i = 0; i < 30; i++)
		ddlAmount.options[i] = new Option((i + 1) + " Stück", i + 1);
}