// Create the application
$.application = {
	// Initialize the main object
	init: function() {
		// Enable slogan switching on the home page
		$('#home a.slogan').bind({
			mouseenter: function() {
				var sloganText = $(this).attr('title');
				var sloganClass = 'slogan-' + $(this).parent().attr('id');
				
				$('#slogan').text(sloganText).attr('class', sloganClass);
			},
			mouseleave: function() {
				var sloganText = $('#logo a.slogan').attr('title');
				
				$('#slogan').text(sloganText).attr('class', 'slogan-logo');
			}
		});
		
		// Enable news / agenda switching on the home page
		$('#news-agenda h2:not(.active) a').live('click', function() {
			// Switch the button states
			$('#news-agenda h2').toggleClass('active');
			
			// Switch the shown content
			$('#news-agenda div').toggleClass('active');
		});
		
		// Enable navigation for the highlights on the home page
		$.application.tabsNextPrevious($('#tabs'), 'Volgende', 'Vorige', null);
		
		// Enable image zooming
		var firstImage = $('#images').children(':first');
		$('<a class="lightbox enlarge-image" href="' + firstImage.attr('href') + '" rel="' + firstImage.attr('rel') + '">Vergroot afbeelding</a>').insertAfter($('#images').children(':last'));
		
		// IE7 positioning bug fix
		$('#home h2#slogan').css('top', '45px');
		$('#home #main-navigation').css({
			'top' : '80px'
		});
		
		// Locating External links
		$("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
			window.open(this.href);	
			return false;
		});
		
		// Open files in a new window
		$("a[href*='uploads/media']").bind('click', function() {
			window.open(this.href);	
			return false;
		});
		
		// Open newsletters in a new window
		$("a[href*='records/nieuwsbrieven']").bind('click', function() {
			window.open(this.href);	
			return false;
		});
		
		// Lock the map when markers are being added
		$('.tx-rggooglemap-pi1 input').bind('change', function() {
			$.application.lockMarkersOnMapLoad();
		});
		
		// Enable map directions calculation
		$('#directionsform a.maproute').live('click', function() {
			// Swap the destination and start address if requested
			if ($(this).attr('id') == 'gmapfrom') {
				$('#directionsform #user-address').attr('name', 'daddr');
				$('#directionsform #location-address').attr('name', 'saddr');
			} else {
				$('#directionsform #user-address').attr('name', 'saddr');
				$('#directionsform #location-address').attr('name', 'daddr');
			}
			
			// Swap the active links
			$('#directionsform a').toggleClass('active');
			
			return false;
		});
		
		// Move opened info windows below map type controls
		$('#map').bind('mouseup', function() {
			GEvent.addListener(map, 'infowindowopen', function() {
				// Get the opened info window
				var openedInfoWindow = map.getInfoWindow();
				
				// Move the info window a few pixels down
				GEvent.addListener(map, 'moveend', function() {
					// Move the map down
					map.panBy(new GSize(0, 32));
					
					// Remove the event to prevent an endless loop
					GEvent.clearListeners(map, 'moveend');
				});
			});
		});
	},
	
	tabsNextPrevious: function(tabContainer, nextText, previousText, options) {
		// Enable tabbed navigation
		var allTabs = tabContainer.tabs(options);
		
		// Get the tab container id
		var tabContainerId = tabContainer.attr('id');
		
		// Create navigation buttons for each tab
		$('#' + tabContainerId + ' .ui-tabs-panel').each(function(i){
			var totalSize = $('#' + tabContainerId + ' .ui-tabs-panel').size() - 1;
			var nextTab = i + 2;
			var previousTab = i;
			
			// Determine the tabs to link to
			if (i == totalSize) {
				var nextTab = 1;
			}
			if (i == 0) {
				previousTab = totalSize + 1;
			}
			
			// Create the navigation links		
			$(this).append("<a href='#' class='gotoNextTab tabNavigator' rel='" + nextTab + "'>" + nextText + "</a>");
			$(this).append("<a href='#' class='gotoPreviousTab tabNavigator' rel='" + previousTab + "'>" + previousText + "</a>");
			
			// Add a class to this panel
			$(this).addClass('previousNextPanel');
		});
		
		// Make elements outside tabs visible
		tabContainer.css('overflow', 'visible');
		
		$('#' + tabContainerId + ' .tabNavigator').click(function() {
			allTabs.tabs('select', $(this).attr("rel"));
			return false;
		});
	},
	
	lockMarkersOnMapLoad: function() {
		// Define the input disable function
		var disableFilters = function() { $('.tx-rggooglemap-pi1 input').attr('disabled', 'disabled'); };
		
		// Disable the filters after a short delay
		setTimeout(disableFilters, 100, 'JavaScript');
		
		// Add a loading indicator
		$('#map').prepend('<div id="map-loading"></div>');
		
		// Get the map container height
		var mapContainerHeight = $('#map').height();
		
		// Adjust the loading frame height
		$('#map-loading').css({
			'height' : mapContainerHeight
		});
		
		// Remove the indicator and re-enable the filter when the a marker has been plotted
		GEvent.addListener(map, 'addoverlay', function() {
			// Remove the loading indicator
			$('#map-loading').remove();
			
			// Re-enable the filters
			$('.tx-rggooglemap-pi1 input').removeAttr('disabled');
		});
	}
};

// Replace text with Cufon
Cufon.replace('h1')('#content-middle h2')('#highlights h2')('h4#mainpagetitle');

// Initialize the application
$(document).ready(function() {
	// Initialize the main object
	$.application.init();
});
