
var destinationBrowser = (function() {

	var currentTab = 'all';	
	var currentSubtab = {};
	var map;
	var mapVisible = false;
	var justAnimating = false;
	var markers = {};
	var destinations = {};
	var imagePath = '';

	
	function initBoxes() {
		$('#destination-list').find('div.destination-box')
			.hover(function() {
				$(this).css('backgroundPosition', '-270px -5px');					
			}, function() {
				$(this).css('backgroundPosition', '-5px -5px');					
			})	
			.click(function() {
				location.href = $(this).find('a').attr('href');			
			});
	}
	
	
	function initTabs() {
		$('#destination-tabs li').click(function() {
			var id = (this.id).split('-').reverse()[0];
			selectTab(id);	
			return false;		
		});			
	}	
	
	
	function initSubtabs(wrc) {
		if(typeof currentSubtab[wrc] !== 'undefined') {
			return;
		}
		var $subtabs = $('#destination-subtabs-' + wrc);
		if(!$subtabs.length) {
			currentSubtab[wrc] = false;
			return;
		}
		$subtabs.find('li').click(function() {
			var id = (this.id).split('-')[3];
			var wrc = (this.id).split('-')[2];
			selectSubtab(wrc, id);
			return false;
		});
		currentSubtab[wrc] = 'all';
	}
	
	
	function initMap() {
		$('#d-browser-map-button-link').click(function() {
			if(mapVisible) {
				hideMap();
			} else {
				showMap();
			}
			return false;
		});
	}
	
	
	function showMap() {
		if(mapVisible || justAnimating) {
			return;
		}				
		$('#d-browser-map').slideDown('slow', function() {
			justAnimating = false;
			mapVisible = true;
			$('#d-browser-map-button-link').html('Karte ausblenden').removeClass('closed').addClass('open');		
			if(!mapIsLoaded()) {
				$('d-browser-map-loader').show();
				loadMap();
			} else if(!map) {
				createMap();
			} else {
				map.checkResize();
			}			
		});	
	}
	
	
	function hideMap() {
		if(!mapVisible || justAnimating) {
			return;
		}			
		$('#d-browser-map').slideUp('slow', function() {
			$('#d-browser-map-button-link').html('Karte anzeigen').removeClass('open').addClass('closed');	
			justAnimating = false;
			mapVisible = false;					
		});			
	}	
	
	
	function mapIsLoaded() {
		return typeof GMap2 != 'undefined';	
	}
	
	
	function loadMap() {
		google.load("maps", "2", {
			language: 'de',
			callback: createMap
		});
	}
	
	
	function createMap() {
		if(map || !mapVisible || !mapIsLoaded()) {
			return;
		}
		$('#d-browser-map').html('');
		
		var mapElm = $('#d-browser-map').get(0);
		var center = new GLatLng(25, 0);
				
		map = new GMap2(mapElm);		
		map.setCenter(center, 1);
		map.setMapType(G_PHYSICAL_MAP);
		map.enableContinuousZoom();

		map.addControl( new GSmallZoomControl3D());	
		
		for(var id in destinations) {			
			markers[id] = new GMarker(new GLatLng(destinations[id].lat, destinations[id].lng), {
				icon: getMarkerIcon()
			});
			map.addOverlay(markers[id]);	
			GEvent.addListener(markers[id], 'click', getMarkerHandler(id));		
		}		
		
		$(window).unload(function() {
			GUnload();
		});
	}	
	
	
	function getMarkerHandler(id) {
		return function() {
			openMarker(id);
		}
	}	
	
	
	function getMarkerIcon() {
		var icon = new GIcon();
		icon.image = imagePath + 'markers/orange_small.png';
		icon.shadow = imagePath + 'markers/shadow_small.png';
		icon.iconSize = new GSize(14, 24);	
		icon.shadowSize = new GSize(26, 24);
		icon.iconAnchor = new GPoint(7, 24);
		icon.infoWindowAnchor = new GPoint(7, 0);
		icon.infoShadowAnchor = new GPoint(7, 0);
		return icon;
	}
	
	
	function openMarker(id) {
		var marker = markers[id];
		var h = $('#destination-' + id).html();	
		h = '<div class="mapInfoWindow">' + h + '</div>';	
		marker.openInfoWindowHtml(h);
	}	
	
	
	function selectTab(tabId) {			
		if(tabId === currentTab || justAnimating) {
			return;
		}	
		
		initSubtabs(tabId);		
		
		if(currentSubtab[currentTab] && currentSubtab[tabId]) {
			$('#destination-subtabs-' + currentTab).hide()
			$('#destination-subtabs-' + tabId).show();
		} else if(currentSubtab[currentTab]) {
			justAnimating = true;
			$('#destination-subtabs-' + currentTab).slideUp('fast', function() {
				justAnimating = false;
			});			
		} else if(currentSubtab[tabId]) {
			justAnimating = true;
			$('#destination-subtabs-' + tabId).slideDown('fast', function() {
				justAnimating = false;
			});
		}
		
		filterDestinations(tabId, currentSubtab[tabId] || 'all');		
			
		$('#d-tab-' + currentTab).removeClass('d-tab-selected');
		$('#d-tab-' + tabId).addClass('d-tab-selected');			
		
		currentTab = tabId;
	}	
	
	
	function selectSubtab(tabId, subtabId) {
		if(subtabId === currentSubtab[tabId]) {
			return;
		}	
		
		filterDestinations(tabId, subtabId);
		
		var $oldTab = $('#d-subtab-' + tabId + '-' + currentSubtab[tabId]);
		var $newTab = $('#d-subtab-' + tabId + '-' + subtabId);
		
		$oldTab.removeClass('d-subtab-selected d-subtab-first-selected d-subtab-last-selected');
		$newTab.addClass('d-subtab-selected');	
		if($newTab.hasClass('d-subtab-first')) {
			$newTab.addClass('d-subtab-first-selected');
		}	
		if($newTab.hasClass('d-subtab-last')) {
			$newTab.addClass('d-subtab-last-selected');
		}	

		currentSubtab[tabId] = subtabId;
	}


	function filterDestinations(tabId, subtabId) {
		
		var $list = $('#destination-list');
		var show, hide;
		
		if(currentTab === 'all') {
			show = false;
		} else if(tabId === 'all') {
			show = 'div.destination-box';
		} else if(subtabId === 'all') {
			show = 'div.db-wr-' + tabId;
		} else {
			show = 'div.db-c-' + subtabId;
		}
		
		if(tabId === 'all') {
			hide = false;
		} else if(subtabId === 'all') {
			hide = 'div.destination-box::not(div.db-wr-' + tabId + ')';
		} else if(subtabId !== 'all') {
			hide = 'div.destination-box::not(div.db-c-' + subtabId + ')';
		}
	
		if(hide) {
			$list.find(hide).hide();
		}
		if(show) {
			$list.find(show).show();
		}
		
		if(typeof document.body.style.maxHeight == "undefined" && !$('#d-browser-ie-fix').length) {
			$list.append('<div class="clearer" id="d-browser-ie-fix"></div>');
		}
	}	
	
	
	return {
		
		init: function() {			
			initBoxes();
			initTabs();	
			initMap();					
		},
		
		
		setDestinationList: function(list) {
			destinations = list;		
		},
		
		
		setImagePath: function(path) {
			imagePath = path;
		}
	
	}



})();
