
/** Include Darksnow Libraries */
include('./includes/mods/dsAjax.js');
include('./includes/mods/dsInterface.js');
include('./includes/mods/dsSearchBox.js');

/** Include map specific JavaScript */
include('./map/includes/mapControl.js');
include('./map/includes/mapSidebar.js');
include('./map/includes/mapMarkers.js');
include('./map/includes/mapContext.js');
include('./map/includes/mapContent.js');
include('./map/includes/mapResort.js');
include('./map/includes/markerclusterer_packed.js');

/**
* Globals to be kept in sync with mapGlobals.php
*/
var COOKIE_SEARCH_MASK	= 'dsSearchMask';
var COOKIE_MAP_MASK		= 'dsMapMask';
var COOKIE_MAP_LOCATION	= 'dsMapPos';
var COOKIE_MAP_ZOOM		= 'dsMapZoom';
var COOKIE_MAP_TYPE		= 'dsMapType';

var PARAM_MAP_LOCATION	= 'll';
var PARAM_MAP_ZOOM		= 'zm';
var PARAM_MAP_TYPE		= 'tp';
var PARAM_MASK			= 't';
var PARAM_IDENT			= 'id';
var PARAM_NORTH_EAST	= 'ne';
var PARAM_SOUTH_WEST	= 'sw';
var PARAM_FUNCTION		= 'f';
var PARAM_RATING		= 'r';
var PARAM_RESORT		= 'rs';
var PARAM_DATA			= 'd';
var PARAM_CAT			= 'cat';

var MASK_DEFAULT	= 2; // Just resorts
var MAP_PRECISION	= 6;

var TYPE_MINE		= 0x8;
var TYPE_AIRPORT	= 0x4;
var TYPE_RESORT		= 0x2;
var TYPE_USER		= 0x1;
var ZOOM_AIRPORT    = 5;
var ZOOM_RESORT     = 7;
var LABEL_MINE		= 'me';
var LABEL_AIRPORT	= 'ap';
var LABEL_RESORT	= 'rs';
var LABEL_USER		= 'us';
var LABEL_KNOW		= 'kn';
var LABEL_PICS		= 'px';


/**
 *  Instance globals used throughout the code
 */
var pop = null;
var map = null;
var uid = null;
var username = null;
var logged_in = false;
var auth_map_edit = false;
var auth_map_owner = false;
var gKnowledgeCategories = new Array();
var mapUsers = null;

/**
 * Resize the map to fit available space
 */
function resizeMap() {
	$('map-content').style.height = (window.getViewPortHeight() - 10) + 'px';
}

/**
 * Scroll the window to the top of the map
 */
function scrollToMap() {
	window.scrollTo(0, $('map-content').offsetTop - 5);
}

/**
 * Decide what to do when a mask updates
 */
function updateMask(chk) {
	switch (chk.parentNode.id) {
	case 'searchOptions': doUpdateSearchUrl(); break;
	case 'mapOptions': updateMapMask(chk); break;
	}
}

function showMessage(mess, append) {
	if (empty(append)) append = false;
	if (append) {
		mess += '\n\nTry reloading the page then trying again.\n\nIf problems persist please leave a message in the bugs forum.';
	}
	if (empty(pop)) alert(mess);
	else pop.open(mess);
}

var initCount = 0;

/**
* Overall init function to start the ball rolling.
* Only called once the page has loaded
*/ 
function init() {
	if (initCount > 100) {
		showMessage("Failed to load supporting libraries in the time limit.\n\nUable to show the map!", true);
		return;
	}
	if ( typeof(DsAjax) == 'undefined'
		|| typeof(DsPopup) == 'undefined'
		|| typeof(initMap) == 'undefined'
		|| typeof(initSidebar) == 'undefined'
		|| typeof(DsSearchBox) == 'undefined'
		|| typeof(initMarkers) == 'undefined'
		|| typeof(initContextMenu) == 'undefined'
		|| typeof(getKnowledgeCategories) == 'undefined'
		|| typeof(getDefaultContent) == 'undefined'  ) {
		initCount++;
		debug('Delaying Load...');
		window.setTimeout('init()', 20);
		return;
	}
	window.onresize = resizeMap;
	window.resizeMap();
	initSidebar();
	initMap('map');
	getKnowledgeCategories();
	if (mapActive) {
		doUpdateSearchUrl();
		initMarkers(map);
		loadMarkers(getMapMask(), false);
		initContextMenu();
	}
	pop = new DsPopup('popupContainer');
	if (uid == 53) { // If it's Darksnow logged in
		include('./map/includes/mapTest.js');
	}
}

window.onload = init;
