function Application()
{
	this.rootPath = 'http://www.gogeo.nl/';
	this.forumObject = false;
	this.contentObject = false;
	this.adminObject = false;
	this.mapObject = false;
	this.panelsObject = false;
	this.loginObject = false;
	this.loggedIn = false;
	this.controller = false;
	this.action = false;

	this.initialize = function()
	{
		// Preloading....
		this.preLoading();
	}
	
	this.run = function(controller, action, hasMap, loggedIn)
	{
		this.loggedIn = loggedIn;
		this.controller = controller;
		this.action = action;

		// bg selection
		if(this.controller != 'Admin') { this.getTheme(); }

		// Navigation menu
		$("#nav ul").css({display: "none"}); // Opera Fix
		$("#nav li").hover(function() { $(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400); },function() { $(this).find('ul:first').css({visibility: "hidden"}); });
		
		// Google maps
		if(hasMap) { this.mapObject = new Map(); this.mapObject.initialize(); }

		// esc key for closing overlays in general
		$(document).keydown( function(e) { if( e.which == 27) { tb_remove(); }});
		
		// check if splash cookie present and if not then
		//if(!this.readCookie('splash_panel'))
		//{
		//	this.createCookie('splash_panel', 'splash=true', 365)
		//	tb_show('', '?keepThis=true&TB_inline?height=448&width=814&inlineId=splash_panel&modal=true', null);
		//}
			
		// Forum
		this.panelsObject = new Panels();
		if(this.controller == 'Forum')
		{
			//if(!$.cookie('geotalk_balloon')) { $('#geotalk_info').show(); }

			if(!this.readCookie('geotalk_balloon'))
			{
				$('#geotalk_info').show();
			}
			
			$("#keywords").keypress(function (e)
			{
				if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
				{
					if($("#keywords").get(0).value.length > 0 && $("#keywords").get(0).value != 'Zoek in forum')
					{
						applicationObject.forumObject.searchForums();
					}
					return false;
				}
				else
				{
					return true;  
				}
			}); 

			this.forumObject = new Forum();
			this.forumObject.viewLatestMessages();
			this.panelsObject.addPanel('geotalkbody');
			this.panelsObject.addPanel('panelBody');

			// Div scroller
			if(this.action == 'forums')
			{
				$("#create_topic_button").click(function(event)
				{
					applicationObject.forumObject.createTopic(1);
				});
			}
			if(this.action == 'viewtopicdirect')
			{
				$("#new_reply_button").click(function(event)
				{
					applicationObject.forumObject.createAnswer($.url.param("id"));
				});
			}

			$('#scrollPanel01').jScrollPane({showArrows:true, scrollbarWidth:15});

			// Markers
			if(hasMap)
			{
				this.mapObject.markerManagerObject.loadMarkerGroup('7');
				this.mapObject.markerManagerObject.loadMarkerGroup('12');
			}

			if(jQuery.browser.msie) { var pos = $('#ieButtonFix').position(); var pos2 = $('#iePanelHeaderFix').position(); $('#ieButtonFix').css({ left: (pos.left + 213) }); $('#iePanelHeaderFix').css({ left: (pos2.left) }); }
			
			// Form events
			this.autoClear();
		}

		// Content and combinations
		if(this.controller == 'Content')
		{
			if(this.action == 'home')
			{
				// Forum
				this.forumObject = new Forum();
				this.forumObject.viewTopicSpeakerUser();
		
				// Panels
				this.panelsObject.addPanel('geotalkbody');
				this.panelsObject.addPanel('panelBody');
				this.panelsObject.addPanel('geogamebody');
				this.panelsObject.addPanel('askaquestionbody');
		
				// Div scroller
				$('#scrollPanel01').jScrollPane({ showArrows:true, scrollbarWidth:15 });
				
				// Div combobox
				$('#selectType').customSelect(4);
				
				// Markers
				if(hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('6'); }
				
				// Form events
				this.autoClear();
			}
			if(this.action == 'about' || this.action == 'air' || this.action == 'water' || this.action == 'land' || this.action == 'cool')
			{
				// Content
				this.contentObject = new Content();

				// Panels
				this.panelsObject.addPanel('geocoolstuffbody');
				this.panelsObject.addPanel('panelBody');
				this.panelsObject.addPanel('geogamebody');

				// Selected tab
				this.contentObject.switchPage($.url.param("page_id"));

				if(this.action == 'cool')
				{
					if($.url.param('id') != '')
					{
						setTimeout("applicationObject.contentObject.getCoolstuff($.url.param('id'));", 1000);
					}
				}
				// Scroll panel
				$('#scrollPanel01').jScrollPane({showArrows:true, scrollbarWidth:15});
			}
			if(this.action == 'disclaimer' || this.action == 'privacy' || this.action == 'partners' || this.action == 'companies' || this.action == 'contact' || this.action == 'day' || this.action == 'game')
			{
				// Content
				this.contentObject = new Content();

				// Panels
				this.panelsObject.addPanel('geocoolstuffbody');
				this.panelsObject.addPanel('panelBody');
				this.panelsObject.addPanel('geotalkbody');

				if(this.action == 'companies')
				{
					this.contentObject.getCompanyByType(0);
					if($.url.param('id') != '')
					{
						setTimeout("applicationObject.contentObject.showCompanyInfoWindow($.url.param('id'));", 2000);
					}
				}
				if(this.action == 'day')
				{
					if($.url.param('id') != '')
					{
						setTimeout("applicationObject.contentObject.showEducationInfoWindow($.url.param('id'));", 2000);
					}
					if($.url.param('type').length > 0)
					{
						this.contentObject.getEducationsByType(parseInt($.url.param('type')));
						this.contentObject.selectedTab = parseInt($.url.param('type'));
					}
					else
					{
						this.contentObject.getEducationsByType(0);
					}
				}
				if(this.action == 'game')
				{
					//swfobject.registerObject("player","9.0.98","videos/expressInstall.swf");
				}
				else
				{
					$('#scrollPanel01').jScrollPane({showArrows:true, scrollbarWidth:15});
				}
			}
			if(this.action == 'studywork' || this.action == 'mbo' || this.action == 'hbo' || this.action == 'wo')
			{
				// Content
				this.contentObject = new Content();

				// Panels
				this.panelsObject.addPanel('geocompanybody');
				this.panelsObject.addPanel('panelBody');

				// Selected tab
				this.contentObject.switchPage($.url.param("page_id"));

				$('#scrollPanel01').jScrollPane({showArrows:true, scrollbarWidth:15});
			}			
			if(this.action == 'studywork' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('2, 3, 4'); }
			if(this.action == 'mbo' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('2'); }
			if(this.action == 'hbo' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('3'); }
			if(this.action == 'wo' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('4'); }
			if(this.action == 'companies' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('5'); }
			if(this.action == 'cool' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('6'); }
			if(this.action == 'day' && hasMap) { this.mapObject.markerManagerObject.loadMarkerGroup('8'); }
			
			if(jQuery.browser.msie)
			{
				var pos = $('#ieButtonFix').position();
				var pos2 = $('#iePanelHeaderFix').position();
				var pos3 = $('#iePanToolFix').position();
				
				if(this.action == 'home') { $('#ieButtonFix').css({ left: (pos.left - 2) }); } else { $('#ieButtonFix').css({ left: (pos.left + 213) }); }
				$('#iePanelHeaderFix').css({ left: (pos2.left) });
				$('#iePanToolFix').css({ left: (pos3.left + 5) });
				$('#iePanToolFix').css({ top: (pos3.top + 129) });
			}
		}

		// When not logged in provide login panel
		this.adminObject = new Admin();
		if(!loggedIn)
		{
			// Login bindings
			var self = this;
			$(document).bind('keydown', 'Ctrl+z', function() { 
				
				if($('#TB_window').length == 0)
				{
					self.adminObject.showLogin();
				}
			});
		}
		// Admin
		if(this.controller == 'Admin')
		{
			if(this.action == 'content_edit') { $('#content').wysiwyg(); }

			if(this.action == 'coolstuff_add') { $('#description').wysiwyg(); }
			if(this.action == 'coolstuff_edit') { $('#description').wysiwyg(); }
			
			if(this.action == 'day_add') { $('#description').wysiwyg(); }
			if(this.action == 'day_edit') { $('#description').wysiwyg(); }
			
			if(this.action == 'company_add') { $('#description').wysiwyg(); }
			if(this.action == 'company_edit') { $('#description').wysiwyg(); }

			if(this.action == 'partner_add') { $('#description').wysiwyg(); }
			if(this.action == 'partner_edit') { $('#description').wysiwyg(); }
		}
		
		// i.e. png fix
		if(jQuery.browser.msie && jQuery.browser.version < 7) { DD_belatedPNG.fix('.png_bg'); }

		/*
		GEvent.addListener(applicationObject.mapObject.map, "mousemove", function(latlng)
		{ 
			// debug
			var pixelLocation = applicationObject.mapObject.map.fromLatLngToDivPixel(latlng);
			applicationObject.msg(latlng);
			applicationObject.msg(applicationObject.mapObject.map.getZoom());
		});
		*/
	}

	this.destroy = function()
	{
		// clean up
		GUnload();
	}

	this.require = function(src)
	{
		// add the required script
		var scriptElem = document.createElement('script');
		scriptElem.setAttribute('src',src);
		scriptElem.setAttribute('type', 'text/javascript');
		document.getElementsByTagName('head')[0].appendChild(scriptElem);
	}

	this.getQueryVariable = function(variable)
	{
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++)
		{
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return false;
	}
	
	this.preLoading = function()
	{
		try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
		if (!$('#preload')) return
	    
		var preload = $('#preload');
		var preloadImages = new Array();
		var sHtml = '';

		for(i=0; i<preloadImages.length; i++)
		{
			sHtml += '<img src="images/' + preloadImages[i] + '" />'
		}
		
		preload.html(sHtml);
		preload.hide();
	}
	
	this.autoClear = function()
	{
		var inp = document.getElementsByTagName('input');
		for(var i = 0; i < inp.length; i++) {
			if(inp[i].type == 'text') {
				inp[i].setAttribute('rel',inp[i].defaultValue)
				inp[i].onfocus = function() {
					if(this.value == this.getAttribute('rel')) {
						this.value = '';
					} else {
						return false;
					}
				}
				inp[i].onblur = function() {
					if(this.value == '') {
						this.value = this.getAttribute('rel');
					} else {
						return false;
					}
				}
				inp[i].ondblclick = function() {
					this.value = this.getAttribute('rel')
				}
			}
			if(inp[i].type == 'password') {
				inp[i].setAttribute('rel',inp[i].defaultValue)
				inp[i].onfocus = function() {
					if(this.value == this.getAttribute('rel')) {
						this.value = '';
					} else {
						return false;
					}
				}
				inp[i].onblur = function() {
					if(this.value == '') {
						this.value = this.getAttribute('rel');
					} else {
						return false;
					}
				}
				inp[i].ondblclick = function() {
					this.value = this.getAttribute('rel')
				}
			}
		}
		var inp = document.getElementsByTagName('textarea');
		for(var i = 0; i < inp.length; i++) {
				inp[i].setAttribute('rel',inp[i].defaultValue)
				inp[i].onfocus = function() {
					if(this.value == this.getAttribute('rel')) {
						this.value = '';
					} else {
						return false;
					}
				}
				inp[i].onblur = function() {
					if(this.value == '') {
						this.value = this.getAttribute('rel');
					} else {
						return false;
					}
				}
				inp[i].ondblclick = function() {
					this.value = this.getAttribute('rel')
				}
		}
	}
	
	this.getTheme = function()
	{
		var theme_id = $.cookie('theme_id');
		if(theme_id == null)
		{
			theme_id = '';
		}
		document.body.style.backgroundImage='url(images/bg'+theme_id+'.gif)';
	}

	this.setTheme = function(id)
	{
		var theme_id = id || '';
		document.body.style.backgroundImage='url(images/bg'+theme_id+'.gif)';
		$.cookie('theme_id', theme_id);
	}
	
	this.preloadImages = function(images)
	{
		var i, newImage;
		for (i=0;i<images.length;i++)
		{
			newImage = new Image();
			newImage.src = images[i];
		}
	}
	
	this.createCookie = function(name, value, days)
	{
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else
		{
			var expires = "";
		}
		document.cookie = name+"="+value+expires+"; path=/";
	}

	this.readCookie = function (name)
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	this.msg = function(msg)
	{
		GLog.write(msg, '#000000');
	}
	
	this.openLink = function(URI)
	{
		if(URI.substr(0, 7) == 'mailto:')
		{
			document.location.href=URI;
		}
		else
		{
			win = window.open(URI,"secWin","scrollbars=yes,resizable=yes,location=yes"); //width=600,height=600,left=100,
			win.focus;
		}
	}
}