﻿		
function calcScrollbarWidth()
{
	var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;
    
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '100px';
    scr.style.left = '100px';
    scr.style.width = '200px';
    scr.style.height = '150px';
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';
    scr.appendChild(inn);
    document.body.appendChild(scr);
    scr.style.overflow = 'hidden';
    wNoScroll = inn.offsetWidth;
    scr.style.overflow = 'scroll';
    wScroll = inn.offsetWidth;
    if (wNoScroll == wScroll) 
    	wScroll = scr.clientWidth;
    document.body.removeChild(document.body.lastChild);
    var a = (wNoScroll - wScroll);
    
    return a;
}

var scrollbarWidth = null;
function getScrollbarWidth()
{
	if (scrollbarWidth === null)
	{
		scrollbarWidth = calcScrollbarWidth();
		return scrollbarWidth;
	}
	else
	{
		return scrollbarWidth;
	}
}

function setDictToAuto(dictionary)
{
	switch(dictionary)
	{
		case 'daen':
		  dictionary = 'a000';
		  break;
		case 'daty':
		  dictionary = 'a001';
		  break;
		case 'dafr':
		  dictionary = 'a002';
		  break;
		case 'dase':
		  dictionary = 'a004';
		  break;
		case 'dasp':
		  dictionary = 'a005';
		  break;
		default:
		  dictionary = 'ddno';
		  break;
	}
	
	return dictionary;
}

function tabSwitch(dictionary)
{
	$.historyLoad(dictionary);
	//return false;
}

function setTabSwitch(dictionary)
{
	var obj = document.getElementById(dictionary+'_result');
	
	if (!obj)
		return;
	
	setCurrentDictTab(dictionary);
	
	var results = document.getElementById('searchResults');

	for (var i=0; i<results.childNodes.length; i++)
	{
		var child = results.childNodes[i];
		
		if (child.tagName == 'DIV')
			child.style.display = "none";
	}

	obj.style.display = '';
	
	menuSetActive(dictionary+'_menu');			

	avoidScrollbarPageJump();
}

function avoidScrollbarPageJump()
{
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		return;
	}
	
	
	if (isScrollBarPresent())
	{
		setPaddingOnMainElements(getScrollbarWidth());
	}
	else
	{
		setPaddingOnMainElements(0);
	}
}

function setPaddingOnMainElements(width)
{
	var chObj;
	var changeWidthElements = new Array("searchInnerWrap","content","beta","header");	
	for (var i = 0; i < changeWidthElements.length; i++)
	{
		if (chObj = document.getElementById(changeWidthElements[i]))
		{
			chObj.style.paddingLeft = width+"px";
		}
	}
}

function pageOnload(setFocus)
{
	//avoidScrollbarPageJump();
	fragmentAction();
	initpage();
	
	if (setFocus == true)
		document.getElementById("word").focus();
					
	var nt = new NewsTicker('newsticker');
	
	setTimeout("shiftOpacityUp('tooltip', 1000)",1000);
 	setTimeout("shiftOpacityDown('tooltip', 1000)",9000);
}

function fragmentAction()
{
	var url = document.location.href;
	var fragSplit = url.split("#");

	if (fragSplit.length > 1 && fragSplit[1].length > 0)
		tabSwitch(fragSplit[1]);
}
function setLogout(link)
{
	var loginMenu = document.getElementById('loginMenu');
	if (loginMenu.style.display == '')
		link.innerHTML = '<div class="loginClose"> <\/div>';
	else
		link.innerHTML = 'Log ind';					
}

function gotoDict(dict, method)
{
	var word = document.getElementById("word").value;
	if (word.length > 0)
		document.location.href = "/opslag.php?word="+escape(word)+"&dict="+method;
	else
		document.location.href = "/ordboger/"+dict+"/index.php?dict="+method;
}