var CalendarMenuDelay = 250; // delay before displaying main menu popup [ms]
var CalendarToolTipDelay = 250; // delay before displaying calendar event popup [ms]

var enteringTimer = new Array();
var enteringTimming = new Array();
var leavingTimer = new Array();
var leavingTimming = new Array();
var refs = [];

/* --- START OF Jumping elements fix --- */
/*
	There is need for jQuery styling for Sampo. But it is run after all content is loaded, so user can see jumping 
	when page is being loaded. This is fix for that. jQuery stuff is call earlier than commonly. 
*/

var checkingTimer = null;
var checkingTimerCount = 0;
var checkingTimerInterval = 10;
var checkingTimerMaxWaitTime = 10000;

function RunImmediately() {
	checkingTimer = setInterval(AdditionalStyling, checkingTimerInterval);
}

function AdditionalStyling() {
	/* Sampo HTML editor additional styling */

	var editorContent = $('.desc');
	var keyFigures = $('.key-figures');

	if (editorContent.length == 0 || keyFigures.length == 0) {
		checkingTimerCount++;
		if (checkingTimerCount > checkingTimerMaxWaitTime / checkingTimerInterval) clearInterval(checkingTimer);
	} else {
		clearInterval(checkingTimer);
	}
	
	// content editor last element margin
	$('.desc').children(':last-child').css('margin-bottom', '0px');

	// annual reports - content editor text width
	var keyFiguresContent = keyFigures.find('.content');
	if (keyFiguresContent.length > 0) {

		var contentImage = new Image();
		contentImage = keyFiguresContent.find('img');
		if (contentImage.length > 0) {
			keyFiguresContent.find('.desc').css('float', 'left');
			keyFiguresContent.find('.desc').css('width', 699 - contentImage.width() - 16);
		}

	}
}
/* --- END OF Jumping elements fix --- */


$(document).ready(function() {
	AdditionalStyling();

	// searchbox input validation
	$(".search-button").click(function() {
		var searchText = $(".search-text");
		return (searchText.val() != searchText.attr('title'));
	});


	if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
		$('#main-menu > ul > li > a').css('display', 'block');
		$('#main-menu').find('.submenu').css('margin-top', '0');
	}


	if (navigator.appVersion.indexOf("Mac") != -1) {
		if ($.browser.mozilla) {
			$('.en').find('#main-menu > ul > li > a').css('padding', '0 17px 0 16px');
		}
	}

	var ieZoomOn = false;


	//main menu top and left position. Resize / scroll 
	$(window).scroll(function() {
		var mainMenu = $('#main-menu');
		var pageContent = $('.wrapper');
		var pageContentOffset = pageContent.offset();
		//header bottom
		var headerBottom = $('#header').height();
		if ($('#globalbanner').length > 0) {
		    headerBottom = headerBottom + $('#globalbanner').height();
		}
		

		if (mainMenu.length > 0) {

			if ($.browser.msie) {
				var rect = document.body.getBoundingClientRect();
				var zoomLevel = Math.round((rect.right - rect.left) / document.body.clientWidth * 100);
				if (zoomLevel != 100) ieZoomOn = true;
			}

			var menuOffset = $('#main-menu').offset();
			var menuTop = menuOffset.top;

			if (ieZoomOn) {
				mainMenu.css('top', headerBottom);
				mainMenu.css('position', 'static');
				$('#header').css('margin-bottom', '0');
			}
			else if ($(window).scrollTop() < headerBottom) {

				mainMenu.css('top', headerBottom);
				mainMenu.css('position', 'static');
				$('#header').css('margin-bottom', '0');
			}
			else {
				mainMenu.css('position', 'fixed');
				mainMenu.css('overflow', 'visible');
				mainMenu.css('z-index', '100');
				mainMenu.css('top', 0);
				mainMenu.css('left', pageContentOffset.left + 11);
				$('#header').css('margin-bottom', '45px');

				if ($.browser.msie && $.browser.version == '6.0') {
					mainMenu.css('position', 'absolute');
					mainMenu.css('z-index', '100');
					mainMenu.css('top', $(document).scrollTop());
					if ($('#globalbanner').length > 0) {
		                mainMenu.css('top', $(document).scrollTop()-$('#globalbanner').height());
		            }
					mainMenu.css('left', '11px');
				}



				var windowLeft = $(window).scrollLeft();
				var pageLeft = 0;
				if ($.browser.msie) { pageLeft = 11; }

				if (windowLeft > pageLeft) {
					var staticLeft = parseInt(windowLeft) - 11;
					staticLeft = '-' + staticLeft + 'px';
					staticLeft.toString();
					mainMenu.css('left', staticLeft);

					if ($.browser.msie && $.browser.version == '6.0') {
						mainMenu.css('position', 'absolute');
						mainMenu.css('left', '11px');
					}

				}


			}

		}

	}).resize(function() {
		var mainMenu = $('#main-menu');
		var pageContent = $('.wrapper');
		var pageContentOffset = pageContent.offset();
		var headerBottom = $('#header').height();
		if ($('#globalbanner').length > 0) {
		    headerBottom = headerBottom + $('#globalbanner').height();
		}


		if (mainMenu.length > 0) {

			if ($.browser.msie) {
				var rect = document.body.getBoundingClientRect();
				var zoomLevel = Math.round((rect.right - rect.left) / document.body.clientWidth * 100);
				if (zoomLevel != 100) ieZoomOn = true;
			}

			var menuOffset = $('#main-menu').offset();
			var menuTop = menuOffset.top;


			if (ieZoomOn) {
				mainMenu.css('top', headerBottom);
				mainMenu.css('position', 'static');
				$('#header').css('margin-bottom', '0');
			}

			else if ($(window).scrollTop() < headerBottom) {
				mainMenu.css('top', headerBottom);
				mainMenu.css('position', 'static');
				$('#header').css('margin-bottom', '0');
			}

			else {
				mainMenu.css('position', 'fixed');
				mainMenu.css('z-index', '100');
				mainMenu.css('top', 0);
				mainMenu.css('left', pageContentOffset.left + 11);

				$('#header').css('margin-bottom', '45px');

				if ($.browser.msie && $.browser.version == '6.0') {
					mainMenu.css('position', 'absolute');
					mainMenu.css('z-index', '100');
					mainMenu.css('top', $(window).scrollTop());
					if ($('#globalbanner').length > 0) {
		                mainMenu.css('top', $(document).scrollTop()-$('#globalbanner').height());
		            }
					mainMenu.css('left', '11px');
				}

				var windowLeft = $(window).scrollLeft();
				var pageLeft = 0;
				if ($.browser.msie) { pageLeft = 11; }

				if (windowLeft > pageLeft) {
					var staticLeft = parseInt(windowLeft) - 11;
					staticLeft = '-' + staticLeft + 'px';
					staticLeft.toString();
					mainMenu.css('left', staticLeft);

					if ($.browser.msie && $.browser.version == '6.0') {
						mainMenu.css('position', 'absolute');
						mainMenu.css('left', '11px');
					}

				}

			}

		}
	});

	//--------------------------------------------------------



	//main menu dropdown
	//------------------------------------------------------------

	var menuLinks = $('#main-menu > ul > li');

	if (menuLinks.length > 0) {

		var menuOffset = $('#main-menu').offset();
		var menuRight = menuOffset.left + 980;



		//add padding to last menu item 
		var lastOne = $('#main-menu > ul > li:last');
		var lastOneOffset = lastOne.offset();

		if ((lastOneOffset.left + lastOne.width()) < menuRight) {
			var spaceLeft = menuRight - parseInt(lastOneOffset.left);
			//alert(lastOneOffset.left);
			var toAdd = spaceLeft - lastOne.width();
			var elPadding = lastOne.children('a').css('padding-right');
			var elPaddingNum = elPadding.slice(0, (elPadding.length - 2))
			lastOne.find('a:first').css('padding-right', (parseInt(elPaddingNum) + parseInt(toAdd)));


			if (navigator.appVersion.indexOf("Mac") != -1) {
				if ($.browser.mozilla) {
					lastOne.find('a:first').css('padding-right', (parseInt(elPaddingNum) + parseInt(toAdd) - 1));
				}
			}


		}


		menuLinks.bind("mouseenter", function() {
			var self = $(this);
			var menuOffset = $('#main-menu').offset();
			var menuRight = menuOffset.left + 980;
			//DelayedTrigger(true, CalendarMenuDelay, $(this), function(self)
			{
				self.addClass('open');

				var subMenu = self.find('.submenu');

				if (subMenu.length > 0) {

					subMenu.css('display', 'inline');

					var subMenuContentHeight = self.find('.submenu-content').height();

					self.find('.submenu-content').children('.column').css('height', subMenuContentHeight);

					//submenu-content width
					var cols = self.find('.column');

					if (cols.length > 0) {
						if (cols.length == 4) subMenu.css('width', '723px');
						else if (cols.length == 3) subMenu.css('width', '544px');
						else if (cols.length == 2) subMenu.css('width', '366px');
						else subMenu.css('width', '189px');
					}

					var subMenuOffset = subMenu.offset();
					var subMenuWidth = subMenu.width();
					var tabOffset = $(this).offset();
					var tabLeft = tabOffset.left + 1;
					var subMenuRight = tabLeft + subMenuWidth;
					var mainMenuLeft = menuOffset.left;

					if ($('#main-menu').css('position') == 'static') { mainMenuLeft = mainMenuLeft - 11; }


					if (subMenuRight >= menuRight) { subMenu.css('left', menuRight - subMenu.width() - mainMenuLeft); }
					else { subMenu.css('left', tabLeft - mainMenuLeft); }

					if ($.browser.msie && $.browser.version == '6.0') {
						subMenu.find('.overlay').css('height', subMenu.height() + 5).css('width', subMenu.width());
						subMenu.find('.column > ul > li > a').css('display', 'inline');
						//hide select boxes
						var contentSelectBoxes = $('.content-main').find('select');
						contentSelectBoxes.css('display', 'none');
					}

					if ($.browser.msie && /7.0/.test(navigator.userAgent)) {
						subMenu.find('.column > ul > li > a').css('display', 'inline');
					}

					if ($.browser.msie && /8.0/.test(navigator.userAgent)) {
						subMenu.find('.column > ul > li > a').css('display', 'block');
					}



				}
			} //);

		}).bind("mouseleave", function() {
			var self = $(this);
			//DelayedTrigger(false, CalendarMenuDelay, $(this), function(self)
			{
				self.find('.submenu').css('left', '0');
				self.find('.submenu').css('display', 'none');
				self.find('.submenu-content').children('.column').css('height', 'auto');
				self.removeClass('open');

				//show hidden select boxes
				if ($.browser.msie && $.browser.version == '6.0') {
					var contentSelectBoxes = $('.content-main').find('select');
					contentSelectBoxes.css('display', 'block');
				}

			} //);
		});

	}
	//-------------------------------------------------------- 




	//content text resizing
	// ------------------------------------------------------------
	var txtLargerButton = $('.text-larger');
	var txtSmallerButton = $('.text-smaller');
	var arrFontSizes = ['font-smaller3', 'font-smaller2', 'font-smaller1', '', 'font-larger1', 'font-larger2', 'font-larger3', 'font-larger4'];

	// A+ button
	txtLargerButton.click(function() {

		var bodyClass = $('body').attr('class');
		var fontParams = bodyClass.indexOf('font');

		if (fontParams > 0) {
			var currentFontSize = bodyClass.substring(fontParams);
		}
		else currentFontSize = '';

		for (var i = 0; i < arrFontSizes.length; i++) {
			if (currentFontSize == arrFontSizes[i]) var nextFontIndex = i + 1;
		}

		if (nextFontIndex != arrFontSizes.length) {
			$('body').removeClass(currentFontSize).addClass(arrFontSizes[nextFontIndex]);
			document.cookie = "textsize=" + escape(arrFontSizes[nextFontIndex]) + ";path=/";
		}


		//if in frontpage - resize fixed content height - if calendar buttons too close to calendar
		if ($('.frontpage').length > 0) {
			checkFrontpageCalandarHeight();
			FixContentHeight($('.highlight-fixed'));
		}
		//if in investors frontpage - resize fixed content height
		if ($('.investors-frontpage').length > 0) {
			FixContentHeight($('.highlight-fixed'));
		}


	});

	// A- button
	txtSmallerButton.click(function() {

		var bodyClass = $('body').attr('class');
		var fontParams = bodyClass.indexOf('font');

		if (fontParams > 0) {
			var currentFontSize = bodyClass.substring(fontParams);
		}
		else currentFontSize = '';

		for (var i = 0; i < arrFontSizes.length; i++) {
			if (currentFontSize == arrFontSizes[i]) var nextFontIndex = i - 1;
		}

		if (nextFontIndex >= 0) {
			$('body').removeClass(currentFontSize).addClass(arrFontSizes[nextFontIndex]);
			document.cookie = "textsize=" + escape(arrFontSizes[nextFontIndex]) + ";path=/";
		}


		//if in frontpage - resize fixed content height - if calendar buttons too close to calendar
		if ($('.frontpage').length > 0) {
			checkFrontpageCalandarHeight();
			FixContentHeight($('.highlight-fixed'));
		}
		//if in investors frontpage - resize fixed content height
		if ($('.investors-frontpage').length > 0) {
			FixContentHeight($('.highlight-fixed'));
		}



	});

	//--------------------------------------------------------

	// style form select-elements 	
	var selectBoxElement = $('.form-area').find('select');
	if (selectBoxElement.length > 0) styleSelectBoxes(selectBoxElement);

	var selectBoxElement2 = $('.key-figures').find('select');
	if (selectBoxElement2.length > 0) styleSelectBoxes(selectBoxElement2);

	var selectBoxElement3 = $('.content-selection').find('select');
	if (selectBoxElement3.length > 0) styleSelectBoxes(selectBoxElement3);

	var selectBoxElement4 = $('.calendar-year').find('select');
	if (selectBoxElement4.length > 0) styleSelectBoxes(selectBoxElement4);


	// add required field asterix mark	
	var xFormsRequired = $('.xforms-required').children('label');
	if (xFormsRequired.length > 0) {
		$(xFormsRequired).each(function(i) {
			var labelText = $(this).text() + ' *';
			$(this).text(labelText);
		});
	}


	//sitemap 
	if ($('.sitemap').length > 0) {

		$('.column:last').css('width', '245px');
		$('.column:last').children('ul').css('width', '245px');
		$('.column:last').children('ul').find('li').css('width', '245px');

	}

	// close submenu layer when mouseleave over styled input field -- ff bug	
	if ($.browser.mozilla) {
		var inputFields = $('input:text');
		if (inputFields.length > 0) {
			inputFields.bind("mouseover", function() {
				var subMenuOpen = $('#main-menu').find('.open');
				subMenuOpen.attr('class', '').find('.submenu').css('display', 'none');
			});
		}
	}


	// LightBox close button
	var closeButton = $("#CloseButton");
	closeButton.click(function() {
		var player = $('#InternetTV').get(0);
		if (player) player.stopVideo();

		window.parent.tb_remove();
		return false;
	});


	// opera input element positions
	if ($.browser.opera) {
		$('.search-text').css('padding-top', '0');
		$('.select-box').css('padding-top', '0');
	}



	// calendar day / month mouseover with ie6
	if ($.browser.msie && $.browser.version == '6.0') {

		if ($('.calendar-month').length > 0) {
			var calDays = $('.calendar-month').find('td');
			calDays.bind("mouseenter", function() {
				$(this).addClass('cal-hover');

			}).bind("mouseleave", function() {
				$(this).removeClass('cal-hover');
			});
		}


		if ($('.calendar-year').length > 0) {
			var calDays = $('.calendar-year').find('td');
			calDays.bind("mouseenter", function() {
				$(this).addClass('cal-hover');

			}).bind("mouseleave", function() {
				$(this).removeClass('cal-hover');
			});
		}

	}
	//investors frontpage link-buttons
	if ($('.investors-frontpage').find('table.link-list').length > 0) {
		var investorLinks = $('table.link-list').find('td');
		investorLinks.bind("mouseenter", function() {
			$(this).addClass('item-hover');
		}).bind("mouseleave", function() {
			$(this).removeClass('item-hover');
		}).click(function() {
			var eventUrl = $(this).find('a').attr('href');
			if ($(this).find('a').attr('target') === '_blank') {
			    window.open(eventUrl);
			} else {
			    window.location = eventUrl;
			}
			return false;
		});
	}


	//clear header search-field
	var searchField = $('.search-text');

	searchField.focus(function() {
		if (this.value == this.title) {
			this.value = "";
		}
		$(this).css("color", "#000");
	}).blur(function() {
		if (!this.value.length) {
			$(this).css("color", "#666");
			this.value = this.title;
		}
	}).focus().blur();


	//clear form text-fields
	var formField = $('input:text.labeled');

	formField.focus(function() {
		if (this.value == this.title) {
			this.value = "";
		}
		$(this).css("color", "#000");
	}).blur(function() {
		if (!this.value.length) {
			$(this).css("color", "#666");
			this.value = this.title;
		}
	}).focus().blur();

	//clear form textarea-fields
	var formTextarea = $('textarea.labeled');

	formTextarea.focus(function() {
		if (this.value == this.title) {
			this.value = "";
		}
		$(this).css("color", "#000");
	}).blur(function() {
		if (!this.value.length) {
			$(this).css("color", "#666");
			this.value = this.title;
		}
	}).focus().blur();




	// ---- QuickFeedback ---
	var quickFeedbackButton = $('#QuickFeedbackButton');
	if (quickFeedbackButton.length > 0) {

		quickFeedbackButton.click(function() {

			var t = this.title || this.name || null;
			var a = $('#QuickFeedbackForm').attr('action');
			var g = this.rel || false;
			tb_show(t, a, g);
			this.blur();

			var TB = $('#TB_window');
			TB.find('iframe').attr("allowTransparency", "true");

			return false;
		});
	}



	// ------- Thickbox IFrame auto sizing ------
	var thickboxes = $(".thickbox");
	if (thickboxes.length > 0) {
		thickboxes.click(function() {
			CheckLightBox($(this));
		});
	}
	
	function CheckLightBox(elem) {
		var iframe = $('iframe#TB_iframeContent');
		if (iframe.length == 0) { setTimeout(CheckLightBox(elem), 50); return; }
		iframe.load(function() {
			iframe.attr("allowTransparency", "true");
		    var url = elem.attr('href');
		    if (url.indexOf('width') < 0 && url.indexOf('height') < 0)
			    ResizeIFrame();
		});
	}
	function ResizeIFrame() {
		var tbwindow = document.getElementById('TB_window');
		var iframe = document.getElementById('TB_iframeContent');
		var footer = iframe.contentWindow.document.getElementById('footer');

		//var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
		//var h = 20 + window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
		var w = (iframe.contentWindow.document.body.offsetWidth);
		var h = (footer.offsetTop + footer.offsetHeight);

		//alert(iframe.contentWindow.document.body.offsetHeight + 'px    '
		//+ iframe.contentWindow.document.body.scrollHeight + 'px   '
		//+ iframe.contentWindow.document.height + 'px    '+
		//+ iframe.contentWindow.document.body.style.height + 'px    '
		//+ footer.offsetTop + 'px'		
		//);

		iframe.style.width = w + 'px';
		iframe.style.height = h + 'px';

		// centering
		var w2 = parseInt(iframe.style.width);
		var h2 = parseInt(iframe.style.height);
		//<div id="TB_window" style="margin-left: -315px; width: 630px; margin-top: -220px; display: block;">
		tbwindow.style.marginLeft = (parseInt(tbwindow.style.marginLeft) - (w - w2) / 2) + 'px';
		tbwindow.style.marginTop = (parseInt(tbwindow.style.marginTop) - (h - h2) / 2) + 'px';
		tbwindow.style.width = (parseInt(tbwindow.style.width) + (w - w2)) + 'px';
	}
	// ------------------------------


	// Does sone front page changes - it is called when DOM is changed - it means also after ajax changes
	InitFrontPage();

	// redirecting after calendar year dropdown changes
	HandleCalendarYearDropDownChanges();
	
	// Front page news ticker initialization
	if ($('#ticker UL LI').length > 1)
	{
	    $('#ticker UL').show();
        $('#ticker').vTicker({
           speed: 500,
           pause: 5000,
           showItems: 1,
           animation: 'fade',
           mousePause: true,
           height: 0,
           direction: 'up'
        });	
    }

});

// Following two functions are used 
// for opening lightbox from flash.
function OpenLightBoxFromFlash(title, url)	{
    tb_show(title,url, false);
    CheckLightBoxOpenedFromFlash();
}

function CheckLightBoxOpenedFromFlash() {
	var iframe = $('iframe#TB_iframeContent');
	if (iframe.length == 0) { setTimeout(CheckLightBoxOpenedFromFlash(), 50); return; }
	iframe.load(function() {
		iframe.attr("allowTransparency", "true");
	});
}


// Here place functions should be re-run after async changes in front page (e.g. after calendar controling).
function InitFrontPage()
{
	// ---- Calendar ---
	CalendarHookAsyncHandler();
	CalendarHookToolTips();
	// -----------------

	//frontpage fixed content height
	if($('.frontpage').length>0){
	    FixContentHeight($('.highlight-fixed'));
	    checkFrontpageCalandarHeight();
	}

	//investors page content height fixing
	if($('.investors-frontpage').length>0){
	    FixContentHeight($('.highlight-fixed'));
    }
	
	
}
function CalendarHookAsyncHandler()
{
	var calDiv = $('#AjaxCalendarPlace');
	if (calDiv.length > 0)
	{
		var frontPage = $("#FrontPageMark").length > 0; // are we on the front page?
		CalendarRedirectLinks(calDiv, calDiv.find('.calendar-navigation a'), frontPage);
		CalendarRedirectLinks(calDiv, calDiv.find('.cal-filter-links a'), frontPage);
	}
}

function CalendarRedirectLinks(calDiv, links, frontPage)
{
	for (var i = 0; i < links.length; i++)
	{
		var link = links.eq(i);

		// changes location to http handler

		var href = link.attr('href');
		var pos = -1;
		//for (var j = 0; j < href.length; j++) if (href[j] == '/') pos = j;

		var onFinnishSite = $(".fi").length > 0;
		var url = '/CalendarHttpHandler.aspx';
		if (onFinnishSite) url = '/fi' + url;
		//if (pos >= 0) url = href.substring(0, pos) + url;

		var pos = href.indexOf('?');
		if (pos != -1) url += href.substring(pos);
						
		link.click(function()
		{
			var url = $(this).attr('href', url);
			calDiv.load(url, { fp: (frontPage ? 1 : 0) }, function()
			{
				InitFrontPage();
			});
			return false;
		});

		link.attr('href', url);
	}
}

function CalendarHookToolTips()
{
	var calEventLink = $('.with-event');

	if (calEventLink.length > 0)
	{
		
		// create mouseover area around each calendar day element that has event 
		$('.with-event').each(function(i)
		{
			$(this).children().wrapAll(document.createElement('span'));
			//$(this).find('img').wrapAll(document.createElement('div')).parent('div').addClass('calEvent-images');
		});

		var tooltipLink = $('.with-event').find('span'); 
		tooltipLink.addClass('tooltip-link');
		
	    tooltipLink.each(function(i)
	    {
		    var filterImages = $(this).find('.calEvent-images');
	        if (filterImages.length>0){
	            $(this).find('.tooltip-frame').insertAfter($(this));
	        }
	    });
		

		// set mouseover, mouseleave and click events
		var overlayNode = $('.with-event');
		overlayNode.bind("mouseenter", function()
		{
			var self = $(this);
			//DelayedTrigger(true, CalendarToolTipDelay, $(this), function(self)
			{
				$('.with-event').find('.tooltip-frame').removeClass('tooltip-open');

				var calFrame = self.find('.tooltip-frame');
				var tooltipContent = calFrame.find('.tooltip-content');
				
				calFrame.addClass('tooltip-open');
				//if ($.browser.msie && $.browser.version=='6.0') {
				//    calFrame.addClass('tooltip-open');
				//}
				//else {
				//    calFrame.addClass('tooltip-open').fadeIn('slow');
				//}
				
				self.parent('.with-event').addClass('cal-hover');

				if ($.browser.msie && $.browser.version=='6.0')
				{
					calFrame.find('.overlay').css('height', tooltipContent.height() + 3);
				}
			}//);
		}).bind("mouseleave", function()
		{
			var self = $(this);
			//DelayedTrigger(false, CalendarToolTipDelay, $(this), function(self)
			{
				$('.with-event').find('.tooltip-frame').removeClass('tooltip-open');
				self.parent('.with-event').removeClass('cal-hover');
			}//);
		}).click(function()
		{

			var eventUrl = $(this).find('a').attr('href');
			window.location = eventUrl;
			return false;
		});
	}
}

function FindObject(obj)
{
	for (var i = 0; i < refs.length; i++)
	{
		if (refs[i] == obj) return i;
	}

	return -1;
}

function DelayedTrigger(inout, timeAmount, self, fce)
{
	var obj = self.get(0); // element on which mouse cursor is
	//var id = -1;
	// indexOf is not supported in < IE7 !!!
	//var id = refs.indexOf(obj);

	var id = FindObject(obj);
	if (id == -1) refs.push(obj);
	id = FindObject(obj);

	//var id = refs.indexOf(obj);
	//var id = obj.toString();

	if (inout == true) // enter
	{
		if (leavingTimming[id] == 1)
		{
			clearTimeout(leavingTimer[id]);
		}

		{
			enteringTimming[id] = 1;
			enteringTimer[id] = setTimeout(function()
			{
				fce(self);
				enteringTimming[id] = false;
			}, timeAmount);
		}
	} else // leave
	{
		if (enteringTimming[id] == 1)
		{
			clearTimeout(enteringTimer[id]);
		}
		

		{
			leavingTimming[id] = 1;
			leavingTimer[id] = setTimeout(function()
			{
				fce(self);
				leavingTimming[id] = false;
			}, timeAmount);
		}
	}
}


function FixContentHeight(element)
{ 
	
	if (element.length > 0)
	{
		var fixedElement = element.find('.content');
		var fixedHeight = 0;
		
		fixedElement.each(function(i)
		{
			var elementHeight = $(this).height();
			if (elementHeight > fixedHeight)
			{
				fixedHeight = elementHeight;
			}
		});
		fixedElement.css('height', fixedHeight + 'px');
		
		fixedElement.find('.action-links').css('position', 'absolute').css('bottom', 0).css('left', 0);
		
		if ($.browser.msie && $.browser.version=='6.0')
		{
		    element.find('.calendar-content').css('left', '16px');
	    }
		
	}
}


function styleSelectBoxes(selectBoxElement){

    

	if ($.browser.msie && $.browser.version=='6.0')	{ return false;	}

	else
	{
		
		var aspFormMultiple = $('.xforms-select');

	    if (aspFormMultiple.length > 0) { 
	
	        var xFormsSel = aspFormMultiple.find('select');
	        xFormsSel.each(function(i) {    
	            $(this).wrapAll(document.createElement('span'))
			    $(this).parent('span').addClass('select-box')
		    });
		
		}
		// add span tag with style if not exist 
		selectBoxElement.each(function(i) {

			if (!($(this).parent('span').hasClass('select-box') || $(this).parent('span').hasClass('select-box-y') || $(this).parent('span').hasClass('select-box-m')))
			{
				$(this).wrapAll(document.createElement('span'))
				$(this).parent('span').addClass('select-box')
				
			}
		});
        
       
		selectBoxElement.selectbox({ debug: true });
		$('.select-box').css('border','none');
	}

}


// Open printer friendly window.
function newWindow(sURL, sTitle, sWidth, sHeight) {
  newWindow2 = window.open(sURL, sTitle, "toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=" + sWidth + ",height=" + sHeight)
}


function HandleCalendarYearDropDownChanges()
{
	var dropDownBox = $('div.calendar select.year');
	if (dropDownBox.length)
	{
		dropDownBox.change(function()
		{
			var selBox = $(this).get(0);
			var href = selBox.options[selBox.selectedIndex].value;

			if (href != "")
			{
				window.location = href;
			}
		});
	}
}



function checkFrontpageCalandarHeight()
{
    var calendarContent = $('.calendar-content');
    var calendarContentHeight = calendarContent.height();
    var calendarMonth = $('.calendar-month');
    var calNavigateMonth =  $('.calendar-content').find('.action-links');
    var calendarMonthBottom = calendarMonth.position().top+calendarMonth.height();
    var calNavigateMonthTop = calNavigateMonth.position().top;
    
    if((calNavigateMonthTop-calendarMonthBottom)<0)
    {
        var newContentHeight = calendarContentHeight+(calendarMonthBottom-calNavigateMonthTop)
        calendarContent.css('height',newContentHeight+'px');
        FixContentHeight($('.frontpage').find('.highlight-fixed'));
    }

}

function embedOverlayFlash(flash, xml) {
    var flashvars = {};
    //flashvars.xml_path ="/xml/sammon_vaiheet-fi.xml";
    flashvars.xml_path = xml;
    var params = {};
    params.wmode = "transparent";
    params.bgcolor = "#000000";

    var attributes = {};

    attributes.id = "overlayflash";
    attributes.name = "overlayflash";

    //swfobject.embedSWF("/swf/sammon_vaiheet.swf", "overlayflash", "100%", "100%", "9", false, flashvars, params, attributes);
    swfobject.embedSWF(flash, "overlayflash", "100%", "100%", "9", false, flashvars, params, attributes);
    swffit.fit("overlayflash", 998, 615);
}

function closeOverlayFlash() {
    $('#overlay').html('<div id="overlayflash"></div>');
}
    
    

