var ESSY = {};
					
ESSY.functions = {

	themeDir: 'this variable is automatically set by wordpress inside the document.ready function...',
	searchFieldDefaultText: "Zoek",
	animationSpeed: 1000,
	loopSpeed: 7500,
	forcedSubmenu: null,
	nextTimer: null,
	debug: true,

	init: function () {
		$('body').supersleight();
		this.moveBanners();
		this.rainbowNavigation();
		this.cufonMe();
		this.enableCurrentSubmenu();
		this.searchFieldInit();
		this.doBindings();
		this.albumLink();
	},
	
	albumLink: function () {
		if($('.ngg-galleryoverview')) {
			var link = '<a href="/er-is-meer/mediabank/">Terug naar albums</a><br /><br />';
			$('.ngg-galleryoverview').prepend(link);
			$('.ngg-galleryoverview').append(link);
		}
	},
		
	showMap: function() {
		$("#mappane").show();
	},
	
	moveBanners: function() {
	
		var empty = true;
		
		$('.pagebanner').each(function() {
			if($(this).html()!='') {
				empty = false;				
			}
		});
		
		if(!empty) {
			var banners = $("#pagebanners").clone();
			
			$("#pagebanners").remove();
			
			$("#blogpane").filter('.blogpane_sub').html(banners);	
							
			$(".pagebanner h1").addClass('nadia');
			
			$("#pagebanners").show();
			$("#blogpane").filter('.blogpane_sub').show();
		}
		
		var citaat = $("#quote").clone();
		if($(citaat).html()!='') {
			$("#quote").remove();
			$("aside").append(citaat);
			$("#quote").show();
		}
		
		var homebanner = $("#homepagebanner").clone();
		if($(homebanner).html()!='') {
			$("#homepagebanner").remove();
			$("#ticketpane").append(homebanner);
			$("#homepagebanner").show();
		}
	},
	
	geoLocation: function() {
		if (navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(this.GeoSuccess, this.GeoError);
			
			ESSY.functions.trace('has geocapabilities!');
			
		} else {
			ESSY.functions.trace('Geen geolocation beschikbaar voor deze browser');
		}
	},
	
	GeoSuccess: function( pPosition ) {
		
		var adres = pPosition.address.street + ' ' + pPosition.address.streetNumber + ', ' +  pPosition.address.city +  ', ' +  pPosition.address.country;
		
		ESSY.functions.trace('Geolocation gevonden: ' + adres);
		
		var van = $("#mappress0_saddr").attr("value");
		var naar = $("#mappress0_daddr").attr("value");
		
		if(van=='') {
			$("#mappress0_saddr").attr("value", adres);
		} else if(naar=='') {
			$("#mappress0_daddr").attr("value", adres);
		}
		
		$("#mapsubmit").trigger('click');
			
	},
	
	GeoError: function() {
		ESSY.functions.trace('Geolocation error');
	},
	
	rainbowNavigation: function() {
		var colors = new Array ("red","orange","yellow","green","blue","purple");
		
		var index = 0;
		$("#mainmenu").find("li").each(function() {
			$(this).find("a.nadia").attr("id", colors[index]);
			index++;
		});
		
		index = 0;
		$("ul.submenu").each(function() {
			$(this).addClass(colors[index]);
			index++;
			$(this).attr("style", "z-index:"+index+";");		
			$(this).find('a').addClass('nadia');
		});
		
	},
	
	enableCurrentSubmenu: function() {
		/* enable current page submenu (if a currentpage is in the tree */
		var current = $("#mainmenu").find(".current_page_item").attr("id");
		if(!current) {
			current = $("#mainmenu").find(".current_page_ancestor").attr("id");
		}
		
		if(current) {
			$("#submenu_" + current).show();
		} else if(ESSY.functions.forcedSubmenu) {
			$("#submenu_" + ESSY.functions.forcedSubmenu).show();
		}
		ESSY.functions.trace('Current (parent)page: ' + current);	
		
		// enable page-wide heading colors via a body class
		if(current) {
			var node_class = $("#submenu_" + current).attr("class");
			var color = node_class.substr(8);
			ESSY.functions.trace(color);
			$("body").addClass(color);
		}
	},
		
	doBindings: function() {
		//$("#next_article").bind('click', ESSY.functions.nextArticle);
		$("#search").bind('blur', ESSY.functions.searchFieldBlur);
		$("#search").bind('focus', ESSY.functions.searchFieldFocus);		
		$("#mainmenu").find("li").bind('mouseover', ESSY.functions.mainMenuOver);
		$("#mainmenu").find("li").bind('mouseout', ESSY.functions.mainMenuOut);
		$(".submenu").hover(
			function () {
				$(this).addClass("hover");
			},
			function () {
				$(this).removeClass("hover");
			}
		);
	},
		
	mainMenuOver: function() {

		var menu_id = $(this).attr("id");
		
		/* destroy all submenus */
		$(".submenu").hide();
		
		/* show this submenu */
		$("#submenu_" + menu_id).show();
		
	},
	
	mainMenuOut: function() {

		var menu_id = $(this).attr("id");
			
		var current_page_id = $('.current_page_item').attr("id");
		
		if(current_page_id!=menu_id) {
			/* Set timer to hide this submenu */
			setTimeout("ESSY.functions.hideSubMenu("+menu_id+")",ESSY.functions.loopSpeed);
			ESSY.functions.trace('mouseout triggered for: ' + menu_id);		
		}
	
	},
	
	hideSubMenu: function( pID ) {
		if( $("#submenu_" + pID).is(':visible') ) {
			if($("#submenu_" + pID).hasClass('hover')) {
				ESSY.functions.trace('submenu '+pID+' is now hovererd by mouse,..re-delay the fadeOut');
				setTimeout("ESSY.functions.hideSubMenu("+pID+")",ESSY.functions.loopSpeed);
			} else {
				$("#submenu_" + pID).fadeOut(1000, function() {
					/* show the submenu of the current_page_parent or current_page (if any) */
					ESSY.functions.trace('submenu '+pID+' is now gone,...resetting system');
					ESSY.functions.enableCurrentSubmenu();
				});
			}
		} else {
			ESSY.functions.trace('submenu '+pID+' was already hidden');
		}
	},
	
	nextArticle: function() {
	
	},
	
	rebindNext: function() {
		
	},
	
	setArticleBackground_old: function () {
		var image = $("#article_highlight").find('.polaroid').attr('longdesc');
		ESSY.functions.trace(image);
		if( (!image) || (image=='') ) {
			$("header").css({});
		} else {
			$("header").attr("style", "background:url('/wp-content/thumbnails/Enschede_takeover.jpg');");
		}
	},
	
	setArticleBackground: function () {
		var image = $("#article_highlight").find('.polaroid').attr('longdesc');
		ESSY.functions.trace(image);
		if( (!image) || (image=='') ) {
			$("#header_img").fadeOut(1000, function() {
				$("#header_img").css({});
			});
			
		} else {
			$("#header_img").attr("style", "background:url('/wp-content/thumbnails/Enschede_takeover.jpg');");
			$("#header_img").fadeIn(1000);
		}
		
	},
	
	setPageBackground: function( pImage ) {
		ESSY.functions.trace(pImage);
		if( (!pImage) || (pImage=='') ) {
			
			$("header").attr("style", "background:url('/wp-content/themes/ess/images/header_visual.jpg');");
		} else {
			$("header").attr("style", "background:url('"+pImage+"');");
		}
		var sidebarHeight = $("aside").height();
		var pageHeight = sidebarHeight + 119;
		var contentHeight = pageHeight - 400;
		
		if(contentHeight>0) {
			$("#main_content").attr("style", "min-height:" + contentHeight + "px;");
		}
	},
		
	searchFieldInit: function() {
		$("#search").attr('value', this.searchFieldDefaultText);
	},	
	
	searchFieldBlur: function() {
		var currentValue = $("#search").attr('value');
		if(currentValue=='') {
			ESSY.functions.searchFieldInit();
		}
	},
	
	searchFieldFocus: function() {
		var currentValue = $("#search").attr('value');
		if(currentValue==ESSY.functions.searchFieldDefaultText) {
			$("#search").attr('value', '');
		}
	},
	
	cufonMe: function() {
		Cufon.replace('.nadia', {hover: true});
	},
			
	trace: function (trace){
		if(ESSY.functions.debug) {
			if(typeof console =='object') {
				if(typeof trace =='string' || typeof console.dir !='function'){
					console.info("Trace: "+trace);
				}else{
					console.dir(trace);
				}
			}
		}
	}

};
