﻿/*
Author:			Dennis Milandt
Description:	User Interface scripts
*/



/*************************/
/* Variables             */
/*************************/
var oneLinerDisplayTime = 4000;
var oneLinerFadeOutTime = 2000;

var sifrLight = { src: '/sIFR/MaxOT Light v3.swf' };
var sifrRegular = { src: '/sIFR/MaxOT Regular v3.swf' };
var sifrSemibold = { src: '/sIFR/MaxOT SemiBold v3.swf' };

var searchBoxPreValue = '';
var searchBoxActive = false;


/*************************/
/* Activate sIFR fonts   */
/*************************/
sIFR.activate(sifrLight, sifrRegular);




/*************************/
/* User Interface        */
/*************************/
var UI = {
	// User Interface setup function.
	setup: function()
	{
		Accordion.setup();
		Nav.setup();
		Search.setup();
		SearchForm.setup();
		Forms.setup();
		sIRFHelper.setup();
		OneLiner.setup();
		Print.setup();
		Employees.setup();
	}
}





/*************************/
/* OneLiner              */
/*************************/
var OneLiner = {
	// OneLiner setup function.
	setup: function()
	{
		setTimeout(OneLiner.fadeOut, oneLinerDisplayTime);
	},
	fadeOut: function()
	{
		$('#oneliner').fadeOut(oneLinerFadeOutTime);
	}
}



/*************************/
/* Navigation            */
/*************************/
var Nav =
{
	setup:function()
	{
		Nav.setHoverEventsIfNoMenuItemSelected();
	},
	setHoverEventsIfNoMenuItemSelected:function()
	{
		var firstRowOfMenuItems = $('div.menuNoLine ul.level2');
		if(firstRowOfMenuItems.length == 0)
			firstRowOfMenuItems = $('div.menuWithLine ul.level2');
		var selectedFirstRowMenuItem = Math.max($('div.menuNoLine ul.level2 li a.selected').length, $('div.menuWithLine ul.level2 li a.selected').length);
		if(selectedFirstRowMenuItem == 0)
			firstRowOfMenuItems.children('li').hover(Nav.lvl1Hover, function(){});
	},
	lvl1Hover:function()
	{
		Nav.reset();
		$(this).children('ul.level3').show();
	},
	reset:function()
	{
		$('div.menuNoLine ul.level2 ul.level3').hide();
		$('div.menuWithLine ul.level2 ul.level3').hide();
	}
}
function ao(o)
{
	var s="";
	for(var i in o)
		s+=i+" = " + o[i]+"\n";
	alert(s);
}
var _Nav = {
	// Navigation setup function.
	setup: function()
	{
		var nav = $('#nav');
		var lvl1 = $('#nav ul.lvl1');

		nav.hover(function() { }, Nav.reset);

		if (!lvl1.hasClass('sel'))
			lvl1.children('li').hover(Nav.lvl1Hover, function() { });
	},
	// Hover event for level 1 navigation items
	lvl1Hover: function()
	{
		var lvl2 = $('#nav ul.lvl2');

		Nav.reset();

		$(this).children('ul.lvl2').show();

		$(this).children('a').addClass('hover');

		//if (!navDisableHoverOnLvl2)
		//	Search.position();
	},
	// Reset/init navigation
	reset: function()
	{
		var nav = $('#nav');

		// Remove hover class on all links
		nav.find('a').removeClass('hover');

		// Hide all lvl 2 lists that is not selected
		$('#nav ul.lvl2').hide();
		$('#nav li.sel ul.lvl2').show();

		// Position search box
		Search.position();
	}
}



/*************************/
/* Search Top            */
/*************************/
var Search =
{
    // Search setup function.
    setup: function () {
        //		var searchLink = $('#nav ul.lvl1 .searchLink');
        var searchLink = $('.searchLink');
        var searchBox = $('#search');
        if (searchLink.length > 0) {
            searchBox.hide();
            searchLink.href = "#";
            searchLink.toggle(
			function () {
			    searchBox.fadeIn(500);
			    Search.activate();
			    searchBox.find('input.txt').focus();
			    return false;
			},
			function () {
			    searchBox.fadeOut(500);
			    Search.reset();
			    return false;
			});
        }

        Search.position();

        var box = $('#search input.txt');
        searchBoxPreValue = box.val();

        box.focus(Search.activate);

        box.keydown(function (e) {
            var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
            if (key == 27) // ESC
                Search.hardReset();
        });
    },
    // Activates the search box
    activate: function () {
        var box = $('#search input.txt');
        var btn = $('#search input.search-btn');

        if (!searchBoxActive) {
            box.blur(Search.reset);

            searchBoxActive = true;

            box.val('');
            box.addClass('active');

            btn.fadeIn();
        }
    },
    // Resets the search box if the search box is empty or the user hasn't typed anything
    reset: function () {
        var box = $('#search input.txt');
        var btn = $('#search input.search-btn');

        if (box.val() == '' || box.val() == searchBoxPreValue) {
            box.unbind('blur');

            searchBoxActive = false;

            box.removeClass('active');
            box.val(searchBoxPreValue);
            btn.fadeOut();

            box.blur();
        }
    },
    // Resets the search box no matter if the user has typed text in it or not
    hardReset: function () {
        var box = $('#search input.txt');

        box.val('');

        Search.reset()
    },
    // Position the search box based on where the navigation ends
    position: function () {
        var topMenu = $('ul.level2');
        var activeSubMenu = $('ul.level2 li.selected ul.level3');

        var topMenuWidth = 0;
        var subMenuWidth = 0;
        topMenu.children().each(function () { topMenuWidth += $(this).outerWidth(true); })
        activeSubMenu.children().each(function () { subMenuWidth += $(this).outerWidth(true); })

        var xPos = Math.min(700, topMenuWidth);
        var yPos = parseInt(document.getElementById("menuContainer").offsetTop, 10)

        $('#search').css('left', xPos + 33 + 'px');
        $('#search').css('top', yPos + 2 + 'px');
    },
    old_position: function () {
        var lvl1 = $('#nav ul.lvl1');
        if (!lvl1 || !lvl1.position())
            return;

        var lvl2sel = $('#nav ul.lvl1 li.sel ul.lvl2');
        var lvl2hover = $('#nav ul.lvl1 li.hover ul.lvl2');

        var navWidth = 0;
        lvl1.children().each(function () { navWidth += $(this).outerWidth(true); });
        if (navWidth > 700)
            navWidth = 700;
        $('#search').css('left', navWidth + 33 + 'px');

        var navTop = 73;
        navTop += lvl1.position().top;
        $('#search').css('top', navTop + 'px');
    },
    search: function () {
        var box = $('#search input.txt');
        window.location = '/solutions/soegning.aspx?search=' + box.val();
    }
}


/*************************/
/* Search Form           */
/*************************/
var SearchForm = {
	// Search Form setup function.
	setup: function()
	{
		$('#content .searchForm .searchBtn')
		.mouseover(function()
		{
			$(this).attr('src', '/images/btn_searchform_search_hover.gif');
		})
		.mouseout(function()
		{
			$(this).attr('src', '/images/btn_searchform_search.gif');
		});
	}
}




/*************************/
/* Accordion             */
/*************************/
var Accordion = {
	// Accordion setup function.
	setup: function()
	{
		$('.accordion').accordion(
		{
			header: 'div.header',
			active: '.default',
			animated: 'easeslide',
			autoHeight: true,
			event: 'mousemove',
			selectedClass: 'header-active',
			fillSpace: false
		});
	}
}



/*************************/
/* Forms                 */
/*************************/
var Forms = {
	// Forms setup functuion.
	setup: function(obj)
	{
	},
	submitForm: function(obj)
	{
		$(obj).parents('form').submit();
	}
}



/*************************/
/* Print                 */
/*************************/
var Print = {
	// Print setup function.
	setup: function()
	{
		$('a.printLink').bind("click", function()
		{
			print();
			return false;
		});
	}
}



/*************************/
/* Employees             */
/*************************/
var Employees = {
	// Employees setup function.
	setup: function()
	{

	},
	loadEmployee: function(path)
	{
		if (path == undefined || path == null || path == '')
		{
			$('#topBannerEmployeePicture').hide().find('img').attr('src', '/images/gx_blank.gif');
		}
		else
		{
			path = path.replace(/\'/g, '');
			$('#topBannerEmployeePicture img').attr('src', path);
			$('#topBannerEmployeePicture').show();
		}
	}
}