/******************************************************
	Index:
	  
	0) Init
	1) Preview Map
	2) Fullscreen Map		
******************************************************/

/* --------------------------------------------------------------------------------------------- */
/* 1) Init
/* --------------------------------------------------------------------------------------------- */
var map = {
	preview: {
		id: 'preview_map'
	},
	fullscreen: {
		id: 'fullscreen_map'
	},
	topinMap: 		null,
	verticalMargin:	77
};


map.preview.init = function(portal, content, ths_active, category) {
	
	// Set Controls
	var c = ['loading', 'type', 'zoom'];
	
	// Init Topin-Map
	if(map.topinMap == null) {
		map.topinMap = new TopinMap(this.id, {
			type:		'topo',
			controls:	c,
			bounds:		new GLatLngBounds()
		});
		
		map.topinMap.enableScrollWheelZoom();
		map.topinMap.enableContinuousZoom();
		
		// Select Content
		map.selectContent(content, category, null, ths_active);
		
		// Set Bounds
		var b = map.getBounds(portal);
		map.topinMap.setBounds(new GLatLngBounds(new GLatLng(b[0], b[1]), new GLatLng(b[2], b[3])));
	}
	
}

map.fullscreen.init = function(portal, content, categories, selection, mylocation, country, ths_active) {
	
	// Set Controls
	var c = ['loading', 'type', 'zoom', 'categories'];

	// Init Topin-Map
	if(map.topinMap == null) {
		var b = map.getBounds(portal, country);

		map.topinMap = new TopinMap(this.id, {
			type:		'topo',
			controls:	c
		});
		
		// Select Content
		map.selectContent(content, categories, selection, ths_active);
		
		// Set MyLocation
		if(mylocation)
			map.setMylocation(mylocation);
			
		// Set Bounds
		map.topinMap.setBounds(new GLatLngBounds(new GLatLng(b[0], b[1]), new GLatLng(b[2], b[3])));
	}
	
	// Set Maximum Height
	map.setMaximumHeight(this);
}

map.fullscreen.resize = function() {

	// Set Maximum Height
	map.setMaximumHeight(this);
}

/* --------------------------------------------------------------------------------------------- */
/* 2) Map Applications */
/* --------------------------------------------------------------------------------------------- */

// 2.1) Select Content
// .......................................................................
map.selectContent = function(content, categories, selection, ths_active) {

	var self = this;

	this.topinMap.selectPlatform(content, function(res, params) {
		
		// CATEGORIES ......................................
		// Show Specific Categories
		if(categories) self.topinMap.showCategories(categories.split(';'));
		// Show All Categories
		else self.topinMap.showCategories();
		// CATEGORIES ....................................../


		// SELECTION (Poi) ......................................
		if(selection) {
			var s = selection.split('|');
			switch(s[0]){
				
				case 'poi':
					var zoomLevel = parseInt(s[2]) || 10;
					self.topinMap.setCenter(self.topinMap.selectPoi(s[1]), zoomLevel);
					break;
					
				case 'mylocation':
					var zoomLevel = parseInt(s[3]) || 10;
					self.topinMap.setCenter(new GLatLng(s[1], s[2]), zoomLevel);

					break;
			}
		}
		// SELECTION (Poi) ....................................../

		map.topinMap.enableScrollWheelZoom();
		map.topinMap.enableContinuousZoom();
		map.topinMap.activateControl('categories');
		
	}, null);


	if(ths_active == 'ths_active')
		map.topinMap.selectSpecial('specials', function(res, data) { self.topinMap.showCategories([], 'specials'); }, null);

}


map.setMylocation = function(mylocation) {

	var parts = mylocation.split('|');
	var lat = parts[0];
	var lng = parts[1];

	// Set Icon
	var gIcon = new GIcon(G_DEFAULT_ICON, 'http://www.topin.travel/img/icons/i-am-here-icon.png');
	gIcon.iconSize = new GSize(45, 60);
	gIcon.iconAnchor = new GPoint(21, 48);
	gIcon.shadow = 'http://www.topin.travel/img/icons/i-am-here-icon-shadow.png';
	gIcon.shadowSize = new GSize(76, 60);
	
	var gMap = map.topinMap.getMap();
	var gMarker = new GMarker(new GLatLng(lat, lng),  { icon: gIcon, clickable: false });
	gMap.addOverlay(gMarker);
}

map.getBounds = function(portal, country) {
	
	switch(portal) {
	
/*		case 'afz':
			switch(country) {
				
				case 'CH':
					return [46, 6, 47.5, 10.5];
					
				case 'DE':
					return [48, 7, 54, 13];
					
				case 'AT':
					return [47, 10, 49, 17];
				
				default:
					return [42.261049, -3.208008, 57.136239, 19.643555];
			}
			
*/
		case 'afz':
			return [-43.068888, -60.468750, 75.497157, 150.468750];
			
		case 'groups':
			return [45.771355, 5.630493, 47.897931, 10.695190];

		case 'edom':
			return [-43.068888, -60.468750, 75.497157, 150.468750];
	}
}


map.setMaximumHeight = function(mapType) {
	
	if(map.topinMap) {
		var dim = getInnerDimension();
		h = dim.y - map.verticalMargin;

		$(mapType.id).style.height = px(h);
		map.topinMap.checkResize();
	}
}
