﻿var cookie = readCookie("TSfont");

/*
addLoadEvent
http://simonwillison.net/2004/May/26/addLoadEvent/*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}


/*Prototype function 
http://www.dustindiaz.com/top-ten-javascript/
*/
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/*function checkStyle(title) {
	
}*/

/*Cookie creation functions
http://www.quirksmode.org/js/cookies.html
*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getPreferredStyleSheet() {
  return 'normal';
}

function prepareStyleSheet() {
  var cookie = readCookie("TSfont");

  if (cookie == 'small' || cookie == 'normal' || cookie == 'large') {
  	setActiveStyleSheet(cookie);
  }	
  else {
  	setActiveStyleSheet(getPreferredStyleSheet());
  }
}


function getPrintPreviewButton() {
	if (isFirstLevelScreen()) {
		return "";
	}
	else {
		return "<li id=\"print\"><a href=\"#\" title=\"print\" onclick=\"print_preview(); return false;\" class=\"print-link\"><span>Print</span></a></li>";
	}
}

function isFirstLevelScreen() {
	var numSlashes = countChars('/', location.href.substring(location.href.indexOf('teliasonera.')));
	return numSlashes <= 1;
}

function countChars(c, string) {
	var numOccurences = 0;
	for (i = 0; i<string.length; i++) {
		if (string.charAt(i) == c) {
			numOccurences++;
		}
	}
	return numOccurences;
}

function prepareHelpers() {
	if (!document.getElementById) return false;
	if (!document.getElementById("search-helpers")) return false;
	if (!document.getElementById("site")) return false;
	var help = $('search-helpers');
	var site = $('site');
		
	/*Insert class search-wide to allow wider helper bar*/
	addClass(help,"search-wide");
	
	/*Create print link*/
	site.innerHTML =  getPrintPreviewButton() + site.innerHTML;
		
	
	/*CREATE TEXT SIZE NAVIGATION*/
	/*Create label text*/
	var label1=document.createElement('LABEL');
	label1.className='accessibility';
	var txt1=document.createTextNode('Select font size:');
	label1.appendChild(txt1);
	
	/*Create unordered list*/
	var ul1=document.createElement('UL');
	ul1.setAttribute('id','text-size');
	
	/*Create small link*/
	var li1=document.createElement('LI');
	li1.setAttribute('id','small');
	ul1.appendChild(li1);
	var a1=document.createElement('A');
	a1.setAttribute('title','Smallest');
	a1.setAttribute('href','#');
	a1.onclick=function(){setActiveStyleSheet('small')};
	li1.appendChild(a1);
	var span1=document.createElement('SPAN');
	a1.appendChild(span1);
	var txt4=document.createTextNode('Smallest');
	span1.appendChild(txt4);
	
	/*Create normal link*/
	var li2=document.createElement('LI');
	li2.setAttribute('id','normal');
	ul1.appendChild(li2);
	var a2=document.createElement('A');
	a2.setAttribute('title','Normal');
	a2.setAttribute('href','#');
	a2.onclick=function(){setActiveStyleSheet('normal')};
	li2.appendChild(a2);
	var span2=document.createElement('SPAN');
	a2.appendChild(span2);
	var txt6=document.createTextNode('Normal');
	span2.appendChild(txt6);
	
	/*Create largest link*/
	var li3=document.createElement('LI');
	li3.setAttribute('id','large');
	ul1.appendChild(li3);
	var a3=document.createElement('A');
	a3.setAttribute('title','Largest');
	a3.setAttribute('href','#');
	a3.onclick=function(){setActiveStyleSheet('large')};
	li3.appendChild(a3);
	var span3=document.createElement('SPAN');
	a3.appendChild(span3);
	var txt8=document.createTextNode('Largest');
	span3.appendChild(txt8);
	
	/*Insert new elements to document*/
	insertAfter(label1,site);
	insertAfter(ul1,label1);	
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
	if (title != "Print Preview") {
		createCookie("style", title, 365);
	}
}

/*function adjustLayout(){

    var leftColumn = document.getElementById("press-releases");
    var rightColumn = document.getElementById("calendar-news");

    var leftColumnHeight = leftColumn.offsetHeight;
    var rightColumnHeight = rightColumn.offsetHeight;

    if(leftColumnHeight > rightColumnHeight) {
        var maxHeight = leftColumnHeight;
    } else {
        var maxHeight = rightColumnHeight;
    }

    leftColumn.style.height = maxHeight + 'px';
    rightColumn.style.height = maxHeight + 'px';
}*/


/**/
addLoadEvent(prepareHelpers);
addLoadEvent(prepareStyleSheet);
//addLoadEvent(adjustLayout);
