/**
 * URL Parameters for Mini Map:
 * 
 * width: Width of the map (in Pixels)
 * height: Height of the map (in Pixels)
 * lat: Latitude to start the map at (do not use in combination with "keywords")
 * lng: Longitude to start the map at (do not use in combination with "keywords")
 * zoom: Google Maps Zoom (0-18) (do not use in combination with "keywords")
 * keywords: Address information for geocoding (overwrites "lat", "lng" or "zoom")
 * hidesearchbar: Hide search panel and region dropdown (Shows by default)
 * hideaccessiblelist: Hide the accessibility list (Shows by default)
 * categories: Comma-separated titles of all demand categories (defaults to all)#
 * owneremail: Email address to an account on the map service, which limits markers to this owner
 */

// TODO implement _t()
var i18n = i18n || {};

// language strings
jQuery.i18n.addDictionary({
	'REQUESTERROR': "Request failed - please try again."
});
/*
// static methods
demandmap.App.errorMessage = function(msg, level) {
	if(!level) level = "topError";
	
	$('body').prepend("<p class=\"" + jQuery.text(level) + "\">" + jQuery.text(msg) + "</p>");
};

demandmap.App.ajaxErrorHandler = function(response) {
	mesq.App.errorMessage(jQuery.i18n._t('REQUESTERROR'));
};
*/
jQuery(document).ready(function(){
	
	// show all elements that should be hidden without js
	jQuery('.hide-without-js').show();
	
	// @todo Encapsulate better
	if(jQuery.jget['hidesearchbar']) {
		jQuery('#Control').hide();
	}
	
	jQuery("#PopupLinkHolder a").livequery('click', function() {
		var url = jQuery('base').attr('href') + jQuery(this).attr('href');
		window.open(url, "Popup", "status=0,toolbar=0");
		return false;
	});
	
	var mapOptions = getMapOptions();
	jQuery('#Map').DemandMap_MapView(mapOptions);
	
	/*
	jQuery('#DemandMap').hide();
	jQuery('#Map').bind('DemandMap:mapload', function(e) {
		jQuery('#DemandMap').show();
	});
	*/
	
	jQuery('#RegionControl').DemandMap_RegionControl('#Map');
	
	jQuery('#SupplierControl').DemandMap_SupplierControl('#Map');
	
	jQuery('#MapSearchForm').DemandMap_SearchBar('#Map');
	
	var demandCategoryOptions = {
		// The mini map should show all categories by default, as no controls
		// might be available to influence the display
		allCategoriesChecked: (jQuery('#DemandMap.mini').length)
	};
	jQuery('#DemandCategoryControl').DemandMap_CategoryControl('#Map', demandCategoryOptions);
	
	jQuery('#DemandPointList').DemandMap_DemandPointList({}, '#DemandCategoryControl');
	
	jQuery('#BookmarkSearch').DemandMap_SearchLink('#Map', '#Form_MapSearchForm');
	jQuery('#PopupLinkHolder').DemandMap_SearchLink('#Map', '#Form_MapSearchForm');
	
	jQuery('#MapEdit').DemandMap_MapEdit('#Map', {
		markerIconsByID: mapOptions.markerIconsByID,
		defaultMarkerIcon: mapOptions.clusterIconSmall
	});
	
	// tabs for visual map and accessible list
	if(jQuery.jget['hideaccessiblelist']) {
		jQuery('div.dataViews > ul').hide();
	} else {
		jQuery('div.dataViews ul').tabs({
			show: function() {
				jQuery('#Map').fn('checkResize');
			}
		});
	}
	
	// keyboard navigation
	jQuery.keyNav();
	jQuery.whenPressed('m',function(){ 
		jQuery('.dataViews ul').tabs('select', 0);
	});
	jQuery.whenPressed('a',function(){ 
		jQuery('.dataViews ul').tabs('select', 1); 
	});
	jQuery.whenPressed('d',function(){ 
		jQuery('.ui-accordion-container').accordion('activate', 0); 
		jQuery('.ui-accordion-container .ui-accordion-link:eq(0)').focus();
	});
	jQuery.whenPressed('n',function(){ 
		jQuery('.ui-accordion-container').accordion('activate', 1); 
		jQuery('.ui-accordion-container .ui-accordion-link:eq(1)').focus();
	});
	jQuery.whenPressed('c',function(){ 
		jQuery('.accessibilityControlAnchor a').click(); 
	});
	jQuery.whenPressed('s',function(){ 
		jQuery('input[name=q]').focus(); 
	});
	
	
	function getMapOptions() {
		var mapOptions = {};
	
		// get markers
		var baseURL =jQuery('base').attr('href');
		var iconURL = baseURL + 'marketdemandmap/images/markers/%s-24.png';
		var shadowURL = baseURL + 'marketdemandmap/images/markers/shdw-24.png';
		var markerIconsByID = {};

		// "demandCategories" is a global variable rendered in a <script> tag
		for(id in demandCategories) {
			var icon = new GIcon(G_DEFAULT_ICON, jQuery.sprintf(iconURL, demandCategories[id]['IconIdentifier']));
			icon.shadow = shadowURL;
			icon.iconSize = new GSize(24,33);
			icon.shadowSize = new GSize(41,30);
			icon.iconAnchor = new GPoint(12,33);
			markerIconsByID[id] = icon; 
		}
		mapOptions.markerIconsByID = markerIconsByID;
		
		// cluster icons
		var clusterIconSmall = new GIcon(G_DEFAULT_ICON, baseURL + 'marketdemandmap/images/markers/cluster-24.png');
		clusterIconSmall.shadow = baseURL + 'marketdemandmap/images/markers/shdw-24.png';
		clusterIconSmall.iconSize = new GSize(24,33);
		clusterIconSmall.shadowSize = new GSize(41,30);
		clusterIconSmall.iconAnchor = new GPoint(12,33);
		mapOptions.clusterIconSmall = clusterIconSmall;
	
		var clusterIconMedium = new GIcon(G_DEFAULT_ICON, baseURL + 'marketdemandmap/images/markers/cluster-28.png');
		clusterIconMedium.shadow = baseURL + 'marketdemandmap/images/markers/shdw-28.png';
		clusterIconMedium.iconSize = new GSize(28,39);
		clusterIconMedium.shadowSize = new GSize(51,36);
		clusterIconMedium.iconAnchor = new GPoint(14,39);
		mapOptions.clusterIconMedium = clusterIconMedium;
	
		var clusterIconLarge = new GIcon(G_DEFAULT_ICON, baseURL + 'marketdemandmap/images/markers/cluster-32.png');
		clusterIconLarge.shadow = baseURL + 'marketdemandmap/images/markers/shdw-32.png';
		clusterIconLarge.iconSize = new GSize(32,45);
		clusterIconLarge.shadowSize = new GSize(55,41);
		clusterIconLarge.iconAnchor = new GPoint(16,45);
		mapOptions.clusterIconLarge = clusterIconLarge;
		
		// set mini map default options
		if(jQuery('#DemandMap.mini').length) {
			mapOptions.width = 500;
			mapOptions.height = 300;
		}

		mapOptions.resizeable = jQuery('#DemandMap').is('.popup') || jQuery('#DemandMap').is('.mini');

		// Get global _MAP_OPTIONS if they are defined.
		if(typeof _MAP_OPTIONS != 'undefined') {
			var el;
			for(el in _MAP_OPTIONS) mapOptions[el] = _MAP_OPTIONS[el];
		}
		
		return mapOptions;
	}
});