﻿/*************************************************
Variables and constants
*************************************************/
var xSelectedMode = null;

var xSearchQuery = null;
var xSearchLatitude = null;
var xSearchLongitude = null;
var xSearchPage = null;
var xSearchFocusResult = null;

var xMissionTradingQuery = null;
var xMissionTradingType = null;
var xMissionTradingLatitude = null;
var xMissionTradingLongitude = null;
var xMissionTradingPage = null;
var xMissionTradingFocusResult = null;

var xDisabledTypes = null;

var xMapShapesArray = new Array();
var xMapPointsArray = new Array();
var xMapPointsCounter = 0;
var xMapZIndexCounter = 1500;

/*************************************************
Page initialiazition
*************************************************/

// init rsh library
window.dhtmlHistory.create(
{
	toJSON: function(o) {
		return Object.toJSON(o);
	}
	, fromJSON: function(s) {
		return s.evalJSON();
	}
});

function xStartMap() {
	// reload the page if it was opened with parameters instead of hash
	if (window.location.search != "") {
		window.location.replace(window.location.toString().replace("?", "#"));
		return;
	}

	// init rsh library
	try {
		dhtmlHistory.initialize();
		dhtmlHistory.addListener(xHistoryListener);
	}
	catch (exc) {
	}

	// init VE map
	map = new VEMap("MapDiv");
	map.LoadMap(new VELatLong(xConstMapDefaultLat, xConstMapDefaultLong), 11);
	map.ClearInfoBoxStyles();
	xLoadMapSettings();
	map.AttachEvent("onchangeview", xChangedView);

	var hash = window.location.hash;
	if (hash == null || hash == "") {
		hash = GetCookie("LastHash");
		if (hash != null && hash != "")
			xHistoryAdd(hash);
	}
	xHistoryListener(hash);

	// add AJAX autocompleter for the mission trading
	new Ajax.Autocompleter("MissionTradingQuery", "MissionTradingQueryAutoCompleteDiv", root + "Resources/AutoComplete", { paramName: "query"});
}

/*************************************************
Utility functions
*************************************************/
function xToFixed5(number) {
	return number.toFixed(5).toString();
}
function xIsBirdsEye() {
	var mapStyle = map.GetMapStyle();
	return mapStyle == "b" || mapStyle == VEMapStyle.Birdseye || mapStyle == VEMapStyle.Oblique;
}
function xGetMapCenter() {
	//TODO: add 3D mode support
	if (xIsBirdsEye()) {
		var boundingRectangle = map.GetBirdseyeScene().GetBoundingRectangle();
		return new VELatLong(
			(boundingRectangle.TopLeftLatLong.Latitude + boundingRectangle.BottomRightLatLong.Latitude) / 2,
			(boundingRectangle.TopLeftLatLong.Longitude + boundingRectangle.BottomRightLatLong.Longitude) / 2);
	} else {
		return map.GetCenter();
	}
}
function xGetMapRectangle() {
	//TODO: add 3D mode support
	if (xIsBirdsEye()) {
		return birdsEyeScene = map.GetBirdseyeScene().GetBoundingRectangle();
	} else {
		return map.GetMapView();
	}
}
function xHideDetail() {
	new Effect.Opacity('DetailDiv', { from: 1, to: 0, duration: 0.5 });
	window.setTimeout(function() {
		$('DetailDiv').style.display = "none";
		MasterFooterRefresh();
	}, 450)
}
function xHideDetailImmediately() {
	$('DetailDiv').style.display = "none";
	MasterFooterRefresh();
}
function xShowDetail(innerHTML) {
	window.setTimeout(function() {
		$('DetailDiv').innerHTML = innerHTML;
		$('DetailDiv').style.display = "block";
		MasterFooterRefresh();
		new Effect.Opacity('DetailDiv', { from: 0, to: 1, duration: 0.5 });
	}, 500)
}
/*************************************************
Browser history function 
*************************************************/
// called on page init and on back-forward button clicked
function xHistoryListener(location) {
	// hide lightbox if needed
	try {
		myLightBox.end();
	}
	catch (e) {
	}

	// toQueryParams is prototype.js function
	if (location == null)
	  location = "";
	if (location.indexOf("#") == 0)
		location = location.substring(1);
	params = location.toQueryParams();
	var mode = params.Mode;
	if (mode == null)
		mode = "SearchWithDetail";
	var oldSelectedMode = xSelectedMode;
	xSelectedMode = mode;
	xNavigationShow();

	if (xSelectedMode == "ShowAll" || xSelectedMode == "ShowAllWithDetail") {
		xDisabledTypes = params.DisabledTypes;
		xDisabledTypesSetAll(xDisabledTypes);
		xLoadShowAll();
	}
	if (xSelectedMode == "Search" || xSelectedMode == "SearchWithDetail") {
		var loadedQuery = params.Query;
		var loadedLatitude = params.Latitude;
		var loadedLongitude = params.Longitude;
		var loadedDisabledTypes = params.DisabledTypes;

		if (oldSelectedMode != "Search" && oldSelectedMode != "SearchWithDetail" ||
			xSearchQuery != loadedQuery || xSearchLatitude != loadedLatitude || 
			xSearchLongitude != loadedLongitude || xDisabledTypes != loadedDisabledTypes) {
			xSearchQuery = loadedQuery;
			xDisabledTypes = loadedDisabledTypes;
			xDisabledTypesSetAll(xDisabledTypes);
			$("query").value = loadedQuery != null ? loadedQuery : "";
			if (loadedLatitude != null && loadedLongitude != null) {
				xSearchLatitude = loadedLatitude;
				xSearchLongitude = loadedLongitude;
			} else {
				var mapCenter = xGetMapCenter();
				xSearchLatitude = xToFixed5(mapCenter.Latitude);
				xSearchLongitude = xToFixed5(mapCenter.Longitude);
			}

			xDeleteAllPushpins();
			if (xSearchQuery != null && xSearchQuery != "") {
				xSearchFocusResult = false;
				xLoadSearchMap();
			}
		}
		xSearchPage = params.Page;

		if (xSearchQuery != null && xSearchQuery != "" && xSelectedMode == "Search")
			xLoadSearchText();
		else
			xHideDetailImmediately();
	}
	if (xSelectedMode == "MissionTrading" || xSelectedMode == "MissionTradingDetail") {
		loadedQuery = params.Query;
		var loadedType = params.Type;
		loadedLatitude = params.Latitude;
		loadedLongitude = params.Longitude;

		if (oldSelectedMode != "MissionTrading" && oldSelectedMode != "MissionTradingDetail" ||
			xMissionTradingQuery != loadedQuery || xMissionTradingType != loadedType || 
			xMissionTradingLatitude != loadedLatitude || xMissionTradingLongitude != loadedLongitude) {
			xMissionTradingQuery = loadedQuery;
			xMissionTradingType = loadedType;
			$("MissionTradingQuery").value = loadedQuery != null ? loadedQuery : "";
			$("MissionTradingOffering").checked = xMissionTradingType == "Both" || xMissionTradingType == "Offering" || xMissionTradingType == null;
			$("MissionTradingDemanding").checked = xMissionTradingType == "Both" || xMissionTradingType == "Demanding" || xMissionTradingType == null;
			if (loadedLatitude != null && loadedLongitude != null) {
				xMissionTradingLatitude = loadedLatitude;
				xMissionTradingLongitude = loadedLongitude;
			} else {
				mapCenter = xGetMapCenter();
				xMissionTradingLatitude = xToFixed5(mapCenter.Latitude);
				xMissionTradingLongitude = xToFixed5(mapCenter.Longitude);
			}

			xDeleteAllPushpins();
			if (xMissionTradingQuery != null && xMissionTradingQuery != "") {
				xMissionTradingFocusResult = false;
				xLoadMissionTradingMap();
			}
		}
		xMissionTradingPage = params.Page;

		if (xMissionTradingQuery != null && xMissionTradingQuery != "" && xSelectedMode == "MissionTrading")
			xLoadMissionTradingText();
		else
			xHideDetailImmediately();
	}

	var organisationId = params.OrganisationId;
	if (organisationId != null) {
		xLoadSubjectDetail(organisationId, "O");
		xLoadSubjectMap(organisationId, "O");
	}
	var activityId = params.ActivityId;
	if (activityId != null) {
		xLoadSubjectDetail(activityId, "A");
		xLoadSubjectMap(activityId, "A");
	}
	var newsId = params.NewsId;
	if (newsId != null) {
		xLoadSubjectDetail(newsId, "N");
		xLoadSubjectMap(newsId, "N");
	}
}
// add a new event into browser history
function xHistoryAdd(hash) {
	if (hash == null)
		hash = xHistoryPrepareHash();
	dhtmlHistory.add(hash);
}
// add subject detail selected into browser history
function xHistoryAddSubject(id, type) {
	var hash = xHistoryPrepareHash();
	if (type == "O")
		hash += "&OrganisationId=" + id;
	if (type == "A")
		hash += "&ActivityId=" + id;
	if (type == "N")
		hash += "&NewsId=" + id;
	dhtmlHistory.add(hash);
}
function xHistoryPrepareHash() {
	var result = "Mode=" + xSelectedMode;
	if ((xSelectedMode == "Search" || xSelectedMode == "SearchWithDetail") && xSearchQuery != "" && xSearchQuery != null)
		result += "&Query=" + xSearchQuery + "&Latitude=" + xSearchLatitude + "&Longitude=" + xSearchLongitude + "&Page=" + xSearchPage;
	if ((xSelectedMode == "Search" || xSelectedMode == "SearchWithDetail") && xDisabledTypes != "" && xDisabledTypes != null)
		result += "&DisabledTypes=" + xDisabledTypes;
	if ((xSelectedMode == "ShowAll" || xSelectedMode == "ShowAllWithDetail") && xDisabledTypes != "" && xDisabledTypes != null)
		result += "&DisabledTypes=" + xDisabledTypes;
	if ((xSelectedMode == "MissionTrading" || xSelectedMode == "MissionTradingWithDetail") && xMissionTradingQuery != "" && xMissionTradingQuery != null)
		result += "&Query=" + xMissionTradingQuery + "&Type=" + xMissionTradingType + "&Latitude=" + xMissionTradingLatitude + "&Longitude=" + xMissionTradingLongitude + "&Page=" + xMissionTradingPage;
	return result;
}

/*************************************************
Load/Save map settings
*************************************************/
function xSaveMapSettings() {
	try {
		// save last hash
		SetCookie("LastHash", window.location.hash);
		// get map mode and style
		var mapMode = map.GetMapMode();
		SetCookie("MapMode", mapMode);
		var mapStyle = map.GetMapStyle();
		SetCookie("MapStyle", mapStyle);

		if (mapStyle == "b" || mapStyle == VEMapStyle.Birdseye || mapStyle == VEMapStyle.Oblique) {
			// if birdseye, save orientation
			var birdseyeOrientation = map.GetBirdseyeScene().GetOrientation();
			SetCookie("MapBirdseyeOrientation", birdseyeOrientation);
		}
	}
	catch (exc) {
	}
}

function xLoadMapSettings() {
	var mapMode = GetCookie("MapMode");
	if (mapMode != null && parseFloat(mapMode) != NaN)
		map.SetMapMode(parseFloat(mapMode));
	var mapStyle = GetCookie("MapStyle");
	if (mapStyle == null)
		mapStyle = "h"
	map.SetMapStyle(mapStyle);

	if (mapStyle == "b" || mapStyle == VEMapStyle.Birdseye || mapStyle == VEMapStyle.Oblique) {
		// if birdseye, load orientation
		birdseyeOrientation = GetCookie("MapBirdseyeOrientation");
		if (birdseyeOrientation != null)
			window.setTimeout("map.SetBirdseyeOrientation('" + birdseyeOrientation + "');", 50);
	}
}

/*************************************************
Pushpin functions
*************************************************/
// delete all pushpins and init global variables
function xDeleteAllPushpins() {
	map.DeleteAllShapes();
	map.HideInfoBox();

	xMapShapesArray = new Array();
	xMapPointsArray = new Array();
	xMapPointsCounter = 0;
}
function xAddPushpin(id, latitude, longitude, name, description, type, icon, thumbnailDocumentId) {
	newlatitude = latitude;
	newlongitude = longitude;
	// check for duplicated pushpins
	for (i = 0; i < xMapPointsArray.length; i++) {
		var mapPoint = xMapPointsArray[i];
		if (mapPoint.Latitude == newlatitude && mapPoint.Longitude == newlongitude) {
			// if duplicated point already exists, move it a little bit randomly
			newlatitude = latitude + Math.floor(Math.random() * 10 - 5) * 0.00001;
			newlongitude = longitude + Math.floor(Math.random() * 10 - 5) * 0.00001;
			i = 0;
		}
	}
	var point = new VELatLong(newlatitude, newlongitude);
	var shape = new VEShape(VEShapeType.Pushpin, point);
	shape.SetCustomIcon(root + "content/img/ICON" + icon + ".gif");
	var fullName = "<img src='" + root + "Content/img/ICON" + icon + ".gif' class='MapInfoBoxIconImg' /><div class='MapInfoBoxTitleDiv'>" + name + "</div>";
	shape.SetTitle(fullName);
	if (type == "O")
		shape.SetZIndex(1400);
	if (type == "N")
		shape.SetZIndex(1300);

	var fullDescription = ""; 
	if (thumbnailDocumentId != null && thumbnailDocumentId != "")
		fullDescription += "<a href='javascript: void(0);' onclick='xSelectDetailFromMap(" + id + ", \"" + type + "\");'><img src='" + root + "Documents/Thumbnail?DocumentId=" + thumbnailDocumentId + "' class='MapInfoBoxImg' /></a>";
	fullDescription += "<div class='MapInfoBoxDescriptionDiv'>" + description + "</div>";
	fullDescription += "<div class='MapInfoBoxSeeDetailDiv'><a href='javascript: void(0);' onclick='xSelectDetailFromMap(" + id + ", \"" + type + "\");'>See detail ...</a></div>";
	shape.SetDescription(fullDescription);
	map.AddShape(shape);
	xMapShapesArray[type + id] = shape;
	xMapPointsArray[xMapPointsCounter++] = point;
}
function xFocusAllPushpins() {
	if (xMapPointsCounter > 0)
		map.SetMapView(xMapPointsArray);
}
/*************************************************
Mode changing functions
*************************************************/
function xSelectMode(mode) {
	if (xSelectedMode == mode)
		return;

	var oldSelectedMode = xSelectedMode;
	xSelectedMode = mode;

	if (xSelectedMode == "ShowAll") {
		xHideDetailImmediately();
		xLoadShowAll();
	}
	if (xSelectedMode == "Search") {
		if (xSearchQuery != null && xSearchQuery != "") {
			xDeleteAllPushpins();
			xHideDetailImmediately();
			xSearchFocusResult = false;
			xLoadSearchMap();
			xLoadSearchText();
		} else {
			xDeleteAllPushpins();
			xHideDetailImmediately();
		}
	}
	if (xSelectedMode == "MissionTrading") {
		if (xMissionTradingQuery != null && xMissionTradingQuery != "") {
			xDeleteAllPushpins();
			xHideDetailImmediately();
			xMissionTradingFocusResult = false;
			xLoadMissionTradingMap();
			xLoadMissionTradingText();
		} else {
			xDeleteAllPushpins();
			xHideDetailImmediately();
		}
	}

	if (xSelectedMode == "Search" || xSelectedMode == "ShowAll" || xSelectedMode == "MissionTrading")
		xHistoryAdd();

	xNavigationShow();
}
function xNavigationShow() {
	if (xSelectedMode == "Search" || xSelectedMode == "SearchWithDetail") {
		$("NavigationSearch").show();
		$("NavigationShowAll").hide();
		$("NavigationMissionTrading").hide();
		$("MapIconsToolbarDiv").show();
		$("NavagationSearchDiv").className = "BoxContentMapSelectedTabDiv";
		$("NavagationShowAllDiv").className = "BoxContentMapTabDiv";
		$("NavagationMissionTradingDiv").className = "BoxContentMapTabDiv";
		$("query").focus();
	}
	if (xSelectedMode == "ShowAll" || xSelectedMode == "ShowAllWithDetail") {
		$("NavigationSearch").hide();
		$("NavigationShowAll").show();
		$("NavigationMissionTrading").hide();
		$("MapIconsToolbarDiv").show();
		$("NavagationSearchDiv").className = "BoxContentMapTabDiv";
		$("NavagationShowAllDiv").className = "BoxContentMapSelectedTabDiv";
		$("NavagationMissionTradingDiv").className = "BoxContentMapTabDiv";
	}
	if (xSelectedMode == "MissionTrading" || xSelectedMode == "MissionTradingWithDetail") {
		$("NavigationSearch").hide();
		$("NavigationShowAll").hide();
		$("NavigationMissionTrading").show();
		$("MapIconsToolbarDiv").hide();
		$("NavagationSearchDiv").className = "BoxContentMapTabDiv";
		$("NavagationShowAllDiv").className = "BoxContentMapTabDiv";
		$("NavagationMissionTradingDiv").className = "BoxContentMapSelectedTabDiv";
		$("MissionTradingQuery").focus();
	}
	MasterFooterRefresh();
}
/*************************************************
Organisation/Activity/News detail page navigation
*************************************************/
function xSelectDetailFromText(id, type) {
	if (xSelectedMode == "ShowAll")
		xSelectedMode = "ShowAllWithDetail";
	if (xSelectedMode == "Search")
		xSelectedMode = "SearchWithDetail";
	if (xSelectedMode == "MissionTrading")
		xSelectedMode = "MissionTradingWithDetail";

	xLoadSubjectDetail(id, type);
	xLoadSubjectMap(id, type);
	xHistoryAddSubject(id, type);
}
function xSelectDetailFromMap(id, type) {
	if (xSelectedMode == "ShowAll")
		xSelectedMode = "ShowAllWithDetail";
	if (xSelectedMode == "Search")
		xSelectedMode = "SearchWithDetail";
	if (xSelectedMode == "MissionTrading")
		xSelectedMode = "MissionTradingWithDetail";

	xLoadSubjectDetail(id, type);
	xHistoryAddSubject(id, type);
}
function xLoadSubjectDetail(id, type) {
	xHideDetail();
	var selectedShape = xMapShapesArray[type + id];
	if (selectedShape != null)
		selectedShape.SetZIndex(xMapZIndexCounter++);

	var url = null;
	if (type == "O")
		url = "Organisations/DetailPart?OrganisationId=" + id;
	if (type == "A")
		url = "Activities/DetailPart?ActivityId=" + id;
	if (type == "N")
		url = "News/DetailPart?NewsId=" + id;

	var request = new Ajax.Request(
    root + url,
	{
		method: 'get',
		onSuccess: xLoadSubjectDetailDone,
		onFailure: ReportError
	}
  );
}
function xLoadSubjectDetailDone(request) {
	xShowDetail(request.responseText);
}
function xLoadSubjectMap(id, type) {
	selectedShape = xMapShapesArray[type + id];
	if (selectedShape != null) {
		xFocus(id, type);
	} else {
		var url = null;
		if (type == "O")
			url = "Organisations/MapPart?OrganisationId=" + id;
		if (type == "A")
			url = "Activities/MapPart?ActivityId=" + id;
		if (type == "N")
			url = "News/MapPart?NewsId=" + id;

		var request = new Ajax.Request(
			root + url,
		{
			method: 'get',
			onSuccess: xLoadSubjectMapDone,
			onFailure: ReportError
		}
		);
	}
}
function xLoadSubjectMapDone(request) {
	eval(request.responseText);
}
function xFocus(id, type) {
	selectedShape = xMapShapesArray[type + id];
	if (selectedShape == null)
		return;

	selectedPoint = selectedShape.GetPoints()[0];
	var zoom = map.GetZoomLevel();
	if (zoom < 14)
		map.SetCenterAndZoom(selectedPoint, 14);
	else
		map.SetCenter(selectedPoint);

	window.setTimeout("xFocusDone(" + id + ", '" + type + "', true);", 1000);
}
function xFocusDone(id, type, retry) {
	var selectedShape = xMapShapesArray[type + id];
	if (selectedShape == null)
		return;

	if (xIsBirdsEye()) {
		if (!retry) 
			xShowInfoBox(selectedShape);
		else 
			window.setTimeout("xFocusDone(" + id + ", '" + type + "', false);", 1000);
		
	} else {
		var pushpinLatLong = selectedShape.GetPoints()[0];
		var mapCenterLatLong = xGetMapCenter();
		var latitudeDifference = pushpinLatLong.Latitude - mapCenterLatLong.Latitude;
		var longitudeDifference = pushpinLatLong.Longitude - mapCenterLatLong.Longitude;

		if (latitudeDifference > -0.005 && latitudeDifference < 0.005 && longitudeDifference > -0.005 && longitudeDifference < 0.005)
			xShowInfoBox(selectedShape);
		else if (retry)
			window.setTimeout("xFocusDone(" + id + ", '" + type + "', false);", 1000);
	}
}

function xShowInfoBox(shape) {
	shape.SetZIndex(xMapZIndexCounter++);
	map.ShowInfoBox(shape);
}

/*************************************************
	Search
*************************************************/
function xSearch() {
	var query = $("query").value;
	if (query == "")
		return;
	xSelectedMode = "Search";
	var mapCenter = xGetMapCenter();
	xSearchQuery = query;
	xSearchLatitude = xToFixed5(mapCenter.Latitude);
	xSearchLongitude = xToFixed5(mapCenter.Longitude);
	xSearchPage = 1
	xSearchFocusResult = true;

	xDeleteAllPushpins();
	xLoadSearchMap();
	xLoadSearchText();
	xHistoryAdd();
}

function xSearchPaging(page) {
	xSearchPage = page;

	xLoadSearchText();
	xHistoryAdd();
}

function xLoadSearchMap() {
	var parameters = {
		query: xSearchQuery,
		disabledTypes: xDisabledTypes,
		latitude: xSearchLatitude,
		longitude: xSearchLongitude
	}

	var request = new Ajax.Request(
    root + "Search/MapPart",
		{
			method: 'get',
			parameters: parameters,
			onSuccess: xLoadSearchMapDone,
			onFailure: ReportError
		}
  );
}
function xLoadSearchMapDone(request) {
	eval(request.responseText);
	if (xSearchFocusResult)
		xFocusAllPushpins();
}

function xLoadSearchText() {
	xHideDetail();
	var parameters = {
		query: xSearchQuery,
		disabledTypes: xDisabledTypes,
		latitude: xSearchLatitude,
		longitude: xSearchLongitude,
		page: xSearchPage
	}

	var request = new Ajax.Request(
    root + "Search/TextPart",
	{
		method: 'get',
		parameters: parameters,
		onSuccess: xLoadSearchTextDone,
		onFailure: ReportError
	}
  );
}
function xLoadSearchTextDone(request) {
	xShowDetail(request.responseText);
}

/*************************************************
	Mission trading
*************************************************/
function xMissionTrading() {
	var query = $("MissionTradingQuery").value;
	if (query == "")
		return;
	xSelectedMode = "MissionTrading";		
	xMissionTradingQuery = query;
	xMissionTradingPage = 1;
	xMissionTradingFocusResult = true;

	var checkedOffering = $("MissionTradingOffering").checked
	var checkedDemanding = $("MissionTradingDemanding").checked
	if (checkedOffering && !checkedDemanding)
		xMissionTradingType = "Offering";
	if (!checkedOffering && checkedDemanding)
		xMissionTradingType = "Demanding";
	if (checkedOffering && checkedDemanding)
		xMissionTradingType = "Both";
	if (!checkedOffering && !checkedDemanding)
		xMissionTradingType = "None";
	
	xMissionTradingFocusResult = true;
	
	xDeleteAllPushpins();
	var mapCenter = xGetMapCenter();
	xMissionTradingLatitude = xToFixed5(mapCenter.Latitude);
	xMissionTradingLongitude = xToFixed5(mapCenter.Longitude);

	xLoadMissionTradingMap();
	xLoadMissionTradingText();
	xHistoryAdd();
}

function xMissionTradingPaging(page) {
	xMissionTradingPage = page;

	xLoadMissionTradingText();
	xHistoryAdd();
}

function xLoadMissionTradingMap() {
	var parameters = {
		query: xMissionTradingQuery,
		latitude: xMissionTradingLatitude,
		longitude: xMissionTradingLongitude,
		type: xMissionTradingType
	}

	var request = new Ajax.Request(
    root + "MissionTrading/MapPart",
		{
			method: 'get',
			parameters: parameters,
			onSuccess: xLoadMissionTradingMapDone,
			onFailure: ReportError
		}
  );
}
function xLoadMissionTradingMapDone(request) {
	eval(request.responseText);
	if (xMissionTradingFocusResult)
		xFocusAllPushpins();
}
function xLoadMissionTradingText() {
	xHideDetail();
	var parameters = {
		query: xMissionTradingQuery,
		latitude: xMissionTradingLatitude,
		longitude: xMissionTradingLongitude,
		type: xMissionTradingType,
		page: xMissionTradingPage
	}

	var request = new Ajax.Request(
    root + "MissionTrading/TextPart",
		{
			method: 'get',
			parameters: parameters,
			onSuccess: xLoadMissionTradingTextDone,
			onFailure: ReportError
		}
  );
}
function xLoadMissionTradingTextDone(request) {
	xShowDetail(request.responseText);
}
function xMissionTradingCheckboxChanged(checkbox) {
	if (!$("MissionTradingDemanding").checked && !$("MissionTradingOffering").checked) {
		if (checkbox == $("MissionTradingDemanding"))
			$("MissionTradingOffering").checked = true;
		else
			$("MissionTradingDemanding").checked = true;
	}
}

/*************************************************
Show all functions
*************************************************/
function xChangedView() {
	if (xSelectedMode == "ShowAll" || xSelectedMode == "ShowAllWithDetail")
		xLoadShowAll();
}
function xLoadShowAll() {
	xLoadShowAllMap();
	if (xSelectedMode == "ShowAll")
		xLoadShowAllText();
}
function xLoadShowAllMap() {
	mapRectangle = xGetMapRectangle();
	if (mapRectangle == null)
		return;
	var parameters = {
		topLeftLatitude: xToFixed5(mapRectangle.TopLeftLatLong.Latitude),
		topLeftLongitude: xToFixed5(mapRectangle.TopLeftLatLong.Longitude),
		bottomRightLatitude: xToFixed5(mapRectangle.BottomRightLatLong.Latitude),
		bottomRightLongitude: xToFixed5(mapRectangle.BottomRightLatLong.Longitude),
		disabledTypes: xDisabledTypes
	}

	var request = new Ajax.Request(
    root + "ShowAll/MapPart",
	{
		method: 'get',
		parameters: parameters,
		onSuccess: xLoadShowAllMapDone,
		onFailure: ReportError
	}
  );
}
function xLoadShowAllMapDone(request) {
	xDeleteAllPushpins();
	eval(request.responseText);
}
function xLoadShowAllText() {
	xHideDetail();
	mapRectangle = xGetMapRectangle();
	if (mapRectangle == null)
		return;
	var parameters = {
		topLeftLatitude: xToFixed5(mapRectangle.TopLeftLatLong.Latitude),
		topLeftLongitude: xToFixed5(mapRectangle.TopLeftLatLong.Longitude),
		bottomRightLatitude: xToFixed5(mapRectangle.BottomRightLatLong.Latitude),
		bottomRightLongitude: xToFixed5(mapRectangle.BottomRightLatLong.Longitude),
		disabledTypes: xDisabledTypes
	}

	var request = new Ajax.Request(
    root + "ShowAll/TextPart",
	{
		method: 'get',
		parameters: parameters,
		onSuccess: xLoadShowAllTextDone,
		onFailure: ReportError
	}
  );
}
function xLoadShowAllTextDone(request) {
	xShowDetail(request.responseText);
}

/*************************************************
Disabled types functions
*************************************************/
function xDisabledTypesSwitch(type) {
	var enabledValue = xDisabledTypes == null || xDisabledTypes.indexOf(type) == -1;
	xDisabledTypesSet(type, !enabledValue);
}
function xDisabledTypesSet(type, enabledValue) {
	if (xSelectedMode == "SearchWithDetail" && (xSearchQuery == null || xSearchQuery == "")) {
		xSelectedMode = "ShowAllWithDetail";
		xNavigationShow();
	}
	
	var iconTd = $(type + "IconTd");
	var textTh = $(type + "TextTh");
	if (enabledValue) {
		xDisabledTypes = xDisabledTypes.replace(type + ",", "");
		iconTd.title = "Hide all " + textTh.innerHTML;
		iconTd.className = type + "Td";
		textTh.title = "Hide all " + textTh.innerHTML;
		textTh.className = "EnabledTh";
	} else {
		if (xDisabledTypes == null)
			xDisabledTypes = type + ",";
		else
			xDisabledTypes += type + ",";
		iconTd.title = "Show all " + textTh.innerHTML;
		iconTd.className = "BW" + type + "Td";
		textTh.title = "Show all " + textTh.innerHTML;
		textTh.className = "DisabledTh";
	}
	xHistoryAdd();
	if (xSelectedMode == "ShowAll" || xSelectedMode == "ShowAllWithDetail")
		xLoadShowAll();
	if (xSelectedMode == "Search" || xSelectedMode == "SearchWithDetail")
		xSearch();
}
function xDisabledTypesSetAll() {
	var tableElement = $("MapIconsToolbarTable");
	for (var rowCounter = 0; rowCounter < tableElement.rows.length; rowCounter++) {
		var rowElement = tableElement.rows[rowCounter];
		for (var cellCounter = 0; cellCounter < rowElement.cells.length; cellCounter++) {
			var cellElement = rowElement.cells[cellCounter];
			if (cellElement.id) {
				if (cellElement.id.indexOf("IconTd") != -1) {
					var type = cellElement.id.replace("IconTd", "");
					var textTh = $(type + "TextTh");
					if (xDisabledTypes == null || xDisabledTypes.indexOf(type + ",") == -1) {
						cellElement.title = "Hide all " + textTh.innerHTML;
						cellElement.className = type + "Td";
					} else {
						cellElement.title = "Show all " + textTh.innerHTML;
						cellElement.className = "BW" + type + "Td";
					}
				} else if (cellElement.id.indexOf("TextTh") != -1) {
					type = cellElement.id.replace("TextTh", "");
					if (xDisabledTypes == null || xDisabledTypes.indexOf(type + ",") == -1) {
						cellElement.title = "Hide all " + cellElement.innerHTML;
						cellElement.className = "EnabledTh";
					} else {
						cellElement.title = "Show all " + cellElement.innerHTML;
						cellElement.className = "DisabledTh";
					}
				}
			}
		}
	}
}

function xShowNeighborhood(enabledType) {
	var tableElement = $("MapIconsToolbarTable");
	var newDisabledTypes = "";
	for (var rowCounter = 0; rowCounter < tableElement.rows.length; rowCounter++) {
		var rowElement = tableElement.rows[rowCounter];
		for (var cellCounter = 0; cellCounter < rowElement.cells.length; cellCounter++) {
			var cellElement = rowElement.cells[cellCounter];
			if (cellElement.id) {
				if (cellElement.id.indexOf("IconTd") != -1) {
					var type = cellElement.id.replace("IconTd", "");
					var textTh = $(type + "TextTh");
					if (type == enabledType) {
						cellElement.title = "Hide all " + textTh.innerHTML;
						cellElement.className = type + "Td";
					} else {
						cellElement.title = "Show all " + textTh.innerHTML;
						cellElement.className = "BW" + type + "Td";
					}
				} else if (cellElement.id.indexOf("TextTh") != -1) {
					type = cellElement.id.replace("TextTh", "");
					if (type == enabledType) {
						cellElement.title = "Hide all " + cellElement.innerHTML;
						cellElement.className = "EnabledTh";
					} else {
						cellElement.title = "Show all " + cellElement.innerHTML;
						cellElement.className = "DisabledTh";
						newDisabledTypes += type + ",";
					}
				}
			}
		}
	}
	xDisabledTypes = newDisabledTypes;
	xSelectMode("ShowAll");
}

/*************************************************
Tag cloud search
*************************************************/
function xTagSelect(tagName) {
	xSelectedMode = "Search";
	$("query").value = tagName;
	xNavigationShow();
	xSearch();
}

/*************************************************
Location change
*************************************************/
function xLocationSearch() {
	map.Find(null, $("Location").value, null, null, null, 1, false, false, false, true, xLocationSearchCompleted);
}

function xLocationSearchCompleted(layer, resultsArray, places, hasMore, veErrorMessage) {
	if (places == null)
		alert($("Location").value + " wasn't found.");
}
