﻿$(document).ready(function(){

	// Divide menu into columns
	$("#navigationTop li ul li ul.m3rd").wrapList({ itemsPerColumn: 5 });
	$("#navigationTop li ul li ul").addClass("m3rd");
	
	$("#sitemap .m1st").wrapList({ itemsPerColumn: 4 });
	$("#sitemap .m1st").siblings().addClass("m1st sitemapLevel");
	
	// hotlinks-menu dropdown
	$("#hot-links .aid-dd").hide();
	$("#hot-links li").hover(function(){
		$(this).children(".aid-dd").slideDown(300);
	}, function(){
		$(this).children(".aid-dd").slideUp(500);
	});
	
	// Print page function
	$('#print_btn').click(function(){
		window.print();
	});
	
	// Spot configuration - Tómas
	function spotsConfig(){
		
		// Assign Left or Right values to spots
		if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent))  {
			var middle = document.body.clientWidth/2-1;
		} else {
			var middle = window.innerWidth/2-10;
		}
		
		$(window).load(function(){ // Loads after images are loaded so it is possible to calculate height

			// Position absolute-image to bottom
			$(".absolute-image").each(function() {
				var self = $(this);
				var selfWidth = self.width();
				$(".image-right .spot-content").css('padding-right', self.width() + 'px');
				//	element doesnt refresh in webkit after padding-right change, need to force
				if (navigator.userAgent.indexOf('AppleWebKit') > -1) $(".image-right .spot-content").hide().show(1);
			}); 

		
			// Check image width and apply padding to create space between image and text if necessary
			$(".main-content-text p img").each(function(){
				var self = $(this);
				var parentWidth = $(".main-content-text").width();
				var selfWidth = self.width();
				if (selfWidth < parentWidth-105){
					self.css('padding-left', 10+'px');
				}
			});
		});
	}
	spotsConfig();
	

	// Topmenu hover opacity fix
	$("#navigationTop .m3rd li a").hover(function () { // To make it work with a cheat empty level.
		$(this).fadeTo(100, 0.33);
		$(this).fadeTo(500, 1);
	});
	
	//Foldy list items
	$(".foldy .foldy-content").css({ display: 'none' });
	$(".foldy h4").click(function() {
		$(this).next().slideToggle("slow");
		$(this).toggleClass("foldy-up");
	});

	//Site Search
/*
	$(".searchfield").bind("keypress", function(e) {
		var domain = $(this).siblings('.searchbutton').attr('href');
		var characterCode;
		if (e && e.which) { //if which property of event object is supported (NN4)
			e = e;
			characterCode = e.which; //character code is contained in NN4's which property
		}
		else {
			e = event;
			characterCode = e.keyCode; //character code is contained in IE's keyCode property
		}
		if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
			// Function is registered for multiple elements. Event is fired from input element
			// referenced from e.target
			var query = Url.encode(e.target.value);
			window.location = domain + query;
			return false;
		}

		return true;
	});

	$(".searchbutton").click(function(e) {
		// Function is registered for multiple elements. Event is fired from A element
		// We really only need e.target.previousSibling but FF handling of empty text nodes
		// requires to find firstchild by loop
		var domain = $(this).attr('href');
		var c = e.target.parentNode.childNodes;
		for (var i = 0; i < c.length; i++) {
			if (/searchfield/i.test(c[i].className))
				break;
		}
		var query = Url.encode(c[i].value);
		$(".searchbutton").attr('href', domain + query);
	});	
*/	
});


/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode: function(string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode: function(string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode: function(string) {
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if ((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode: function(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while (i < utftext.length) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

function linkSelectionRedirectUrl(value) {
	if (value == '')
		return;
	
	var values = value.split('|');

	if (values.length != 2) {
		return;
	}
	
	var url = values[0];
	var target = values[1];

	if (target == '_blank') {
		window.open(url);
	}
	else {
		document.location.href = url;
	}
}