jQuery(document).ready(function() {
	sitefunctions.textresize();
	sitefunctions.initialiseMenu();
	sitefunctions.initialiseFancyboxInGallery();
	sitefunctions.initialiseGlossary();
	sitefunctions.initialiseAcc();
	sitefunctions.initialiseBookmarkTooltip();
});

// we need to use window.load since Safari isn't able to retrieve the height of the div otherwise
jQuery(window).load(function() {
	$('.box-image').each(function(index) {
	    image = $(this).find("img").attr("src");
	    height = $(this).height();
	    $(this).html("").css("background-image", "url("+image+")").css("height", height-3); /* Height bug in IE7 */
	});
});

var sitefunctions = {
	textresize : function(){

		var $cookie_name = "FontSize";
		var originalFontSize = $("#container").css("font-size");
		var normalFont = "0.75em"
		var biggerFont = "0.9em"
		var biggestFont = "1.1em"
		
		// insert the links into the meta menu
		$('#metamenu').append('<li class="font-chooser"><a id="font-normal" href="#">A</a> <a id="font-bigger" href="#">A</a> <a id="font-biggest" href="#">A</a></li>')
		
		// if exists load saved value, otherwise store it
		if($.cookie($cookie_name)) {
			var $getSize = $.cookie($cookie_name);
			$("#container").css({fontSize : $getSize });
			$("#hmenu li a").css("font-size", "14px");
			$("#hmenu ul li a").css("font-size", "12px");
			$(".boxtitle-home").css("font-size", "13px");
			$("#flash-wrapper").css("font-size", "12px");
		} else {
			$.cookie($cookie_name, originalFontSize, {path:'/'});
		}
		
		// normal font-size
		$("#font-normal").bind("click", function() {			
			$("#container").css("font-size", normalFont);
			$("#hmenu li a").css("font-size", "14px");
			$("#hmenu ul li a").css("font-size", "12px");
			$(".boxtitle-home").css("font-size", "13px");
			$("#flash-wrapper").css("font-size", "12px");
			$.cookie($cookie_name, normalFont, {path:'/'});
			return false;
		});
		
		// bigger font-size
		$("#font-bigger").bind("click", function() {
			$("#container").css("font-size", biggerFont);
			$("#hmenu li a").css("font-size", "14px");
			$("#hmenu ul li a").css("font-size", "12px");
			$(".boxtitle-home").css("font-size", "13px");
			$("#flash-wrapper").css("font-size", "12px");
			$.cookie($cookie_name, biggerFont, {path:'/'});
			return false;
		});
		
		// biggest font-size
		$("#font-biggest").bind("click", function() {
			$("#container").css("font-size", biggestFont);
			$("#hmenu li a").css("font-size", "14px");
			$("#hmenu ul li a").css("font-size", "12px");
			$(".boxtitle-home").css("font-size", "13px");
			$("#flash-wrapper").css("font-size", "12px");
			$.cookie($cookie_name, biggestFont, {path:'/'});
			return false;
		});
	},
	
	initialiseMenu : function() {
		$("#hmenu > li").hoverIntent({
		  over: function(){
		   $(this).addClass('sfhover');
		   $(this).find("a:first").stop().addClass('active');
		   //$(this).find("ul.first-level").stop(true, true).slideDown('fast').show();
		  },
		  out: function(){
		  $(this).removeClass('sfhover');
		  $(this).find("a:first").stop().removeClass('active');
		   //$(this).find("ul.first-level").stop(true, true).slideUp('fast');
		  },
		  timeout:0
		});	
		
		$("ul.first-level a").click(function(){
			//$("ul.first-level").stop(true, true).slideUp('fast');
		});
		
		// Hide the menu so that it can fade in
		//$("ul.first-level").hide();
	},
	
	initialiseFancyboxInGallery : function(){
		// Initialise the Lightbox in the carouosel-elements
		//$("a.gallery-group").fancybox();	
		//PF: this was buggy in jCarousel: with circular mode, on each loop the items were regenerated and the fancybox did nothing. So we apply fancybox inside jCarousel.js
	},

	
	initialiseGlossary : function(){
		$('#content-wrapper acronym[title]').qtip({ 
		style: { 
	      width: 202,
	      padding: 5,
	      background: '#fff',
	      color: '#333',
	      textAlign: 'left',
	      border: 0,
	      name: 'dark' // Inherit the rest of the attributes from the preset dark style
	   	},
	   	position: {
	      corner: {
	         target: 'topRight',
	         tooltip: 'bottomMiddle'
	      },
	      adjust: { x: -20, y: 0 }
	    }
		});
	},
	
	initialiseAcc : function(){
		// Hide boxes that can be toggled, add an effect for opening them
		$(".toggle").each(function(){
	    	$(this).nextAll(".togglebox:first").hide();
	    });
	
		$(".toggle").click(function()
	    {
	        var initial = $(this).hasClass('open');
            $(this)[initial ? "removeClass" : "addClass"]("open");
	    	$(this).nextAll(".togglebox:first").slideToggle(800);
	    });
	},
	
	initialiseBookmarkTooltip : function(){
		$('a#bookmark').qtip({ 
		style: { 
	      width: 202,
	      padding: 5,
	      background: '#fff',
	      color: '#333',
	      textAlign: 'left',
	      border: 0,
	      name: 'dark' // Inherit the rest of the attributes from the preset dark style
	   	},
	   	position: {
	      corner: {
	         target: 'topRight',
	         tooltip: 'bottomMiddle'
	      },
	      adjust: { x: -25, y: 0 }
	    },
	    content: { text: '<p style="text-align:center;"><a href="#"><img src="fileadmin/templates/febi/img/twitter-icon.png" alt="Twitter" /></a>&nbsp;&nbsp;&nbsp;<a href="#"><img src="fileadmin/templates/febi/img/facebook-icon.gif" alt="Facebook" /></a>&nbsp;&nbsp;&nbsp;<a href="#"><img src="fileadmin/templates/febi/img/rss-icon.jpg" alt="RSS" /></a></p>' },
	    hide: { when: 'mouseout', fixed: true }
		});
	}
}
