var currentpopup;
var popupcontent = '';
var popupanim = {time:0, top:0, left:0, width:0, height:0, changeTop:0, changeLeft:0, changeHeight:0, changeWidth:0, duration:0.0, timer:null, element:null};

var popupheight = 480;
var popupwidth = 445;
var popuplocationoverride = false;

function initpopups() {
	div = document.createElement('div');
	div.id = 'popanim';
	div.className = 'popupanim';
	document.getElementById('wrap').appendChild(div);
	initTooltips();
	initSearch();
}

function initInternalLink(a) {
	if (a.target == '_blank')
		return;
		
	if (document.addEventListener)
		a.addEventListener('click', preview, false);
	else
		a.attachEvent('onclick', preview);
}

var widePopup = false;
var widePopups = '|6K.|13K|16K|22K|24K|28K|30K|32K|8M.|11M|18M|';

function preview(e) {
	if (!ie) {
		e.preventDefault();
		target = e.target;
	}
	else {
		e.cancelBubble = true;
		target = e.srcElement;
	}
	
	while (target.tagName != 'A')
		target = target.parentNode;
	
	var url = target.href;
	
	tmpstr = target.parentNode.id;
	if (tmpstr == 'Bolagsstyrningsmodell' || tmpstr ==	'TabRemunerationandAttendanceBoD') {
		widePopup = true;
	}
	else {
		if (target.id != '') return true;
		ind = url.lastIndexOf('/');
		widePopup = (widePopups.indexOf('|' + url.substring(ind + 1, ind + 4) + '|') > -1);
	}
	
	popupanim.time = 0;
	popupanim.top = e.clientY - 5;
	popupanim.left = e.clientX - 5 - document.getElementById('wrap').offsetLeft;
	popupanim.height = 10;
	popupanim.width = 10;
	popupanim.changeTop = (60 + (480 - popupheight) / 3) - popupanim.top;
	
	if (popuplocationoverride) {
		popupanim.changeLeft = -popupanim.left + ((890 - popupwidth) / 2);
	}
	else if (popupanim.left > 450) 
		popupanim.changeLeft = 10 - popupanim.left;
	else
		popupanim.changeLeft = (columnWidth + 30) - popupanim.left;
		
	
	popupanim.changeWidth = popupwidth - 10;
	popupanim.changeHeight = popupheight - 10;
	popupanim.duration = 25;
	popupanim.element = document.getElementById('popanim');
	
	if (widePopup) {
		popupanim.changeLeft = 50 - popupanim.left;
		popupanim.changeWidth = 845;
	}
	
	if (target.className == 'diagramlink') {
		// this link has no target page - the popup-content resides within the link body.
		popupcontent = '<div class="popup-header"><h1>' + target.getElementsByTagName('span')[0].innerHTML + '</h1><a href="" onclick="hidePopup(this); return false;"><img src="../../images/popup/close.png" /></a></div><div class="popup-content">';
		popupcontent += target.parentNode.getElementsByTagName('div')[0].innerHTML;
		
		currentpopup = document.createElement('div');
		currentpopup.className = 'popup';
		
		div = document.createElement('div');
		div.className = 'popup-top';
		currentpopup.appendChild(div);
		
		div = document.createElement('div');
		div.className = 'popup-left';
		currentpopup.appendChild(div);
		
		div = document.createElement('div');
		div.className = 'popup-body';
		div.innerHTML = popupcontent;
		currentpopup.appendChild(div);
		
		div = document.createElement('div');
		div.className = 'popup-right';
		currentpopup.appendChild(div);
		
		div = document.createElement('div');
		div.className = 'popup-bottom';
		currentpopup.appendChild(div);
		
		document.getElementById('wrap').appendChild(currentpopup);
		setCloseButton(popupanim.left, popupanim.top);
		
		if ( typeof(hhLogInlinePopup) == "function" )
			hhLogInlinePopup(target.getElementsByTagName('span')[0].innerHTML);
	}
	else 
	{
		if (url.substring(0, 4) == 'file') { // running with file:// protocol (local)
			ind = url.lastIndexOf('/');
			ind = url.lastIndexOf('/', ind - 1);
			rooturl = url.substring(0, ind + 1);
			url = url.substring(ind + 1);
			url = url.replace(/\//g,"-");
			url = rooturl + 'PopupPages/' + url;
		}
		else { // running with http
			tmpstr = '';
			var rooturl = url;
			while (tmpstr != '/') {
				ind = rooturl.lastIndexOf('/');
				rooturl = rooturl.substring(0, ind);
				tmpstr = rooturl.substring(rooturl.length - 3, rooturl.length - 2);
			}
			url = url.substring(ind + 1);
			url = url.replace(/http:\/\//g,"");
			url = url.replace(/.aspx/g,"");
			url = url.replace(/.html/g,"");
			url = url.replace(/\//g,"-");
			url = url.replace(/\#/,"-");
			url = url.replace(/\(/,"");
			url = url.replace(/\)/,"");
			url = url.replace(/:/,"");
			url = rooturl + "/PopupPages/" + url + ".html";
		}
		fetchpage(url);
	}
	
	popupanim.element.style.left = popupanim.left + 'px';
	popupanim.element.style.top = popupanim.top + 'px';
	popupanim.element.style.width = '10px';
	popupanim.element.style.height = '10px';
	popupanim.element.style.display = 'block';
	
	popupanim.timer = setInterval("popupDoAnim();", 15);
	
	return false;
}

function closePopupWithEscape() {
	// look for h1. Everyone above no 2 is a popup that should be closed
	h1 = document.getElementsByTagName('h1');
	if (h1.length > 2) {
		a = h1[h1.length - 1].parentNode.getElementsByTagName('a');
		a[a.length - 1].click();
	}
	
}

function hidePopup(a) {
	resumeHidePopup(a);
}

function resumeHidePopup(a) {
	point = a.getElementsByTagName('img')[0].id.substring(1).split('_');
	currentpopup = a.parentNode.parentNode.parentNode;
	
	popupanim.time = 0;
	popupanim.height = currentpopup.offsetHeight - 20;
	popupanim.width = 435;
	
	if (currentpopup.id != 'resultPanel') {
		popupanim.top = currentpopup.offsetTop + 13;
		popupanim.left = currentpopup.offsetLeft + 10;
		currentpopup.parentNode.removeChild(currentpopup);
	}
	else {
		currentpopup.style.display = 'none';
		popupanim.top = 73;
		popupanim.left = 425;
	}
	
	popupanim.changeTop = point[1] - popupanim.top;
	popupanim.changeLeft = point[0] - popupanim.left;
	
	if (popupanim.changeLeft > 0) {
		//popupanim.left = 10;
		//popupanim.changeLeft = point[0];
	}
	
	
	popupanim.changeWidth = -435;
	popupanim.changeHeight = -popupanim.height;
	
	popupanim.element.style.left = popupanim.left + 'px';
	popupanim.element.style.top = popupanim.top + 'px';
	popupanim.element.style.width = popupanim.width + 'px';
	popupanim.element.style.height = popupanim.height + 'px';
	popupanim.element.style.display = 'block';
	
	popupanim.timer = setInterval("doHidePopup();", 15);
	
}

function doHidePopup()
{
	if (popupanim.time > popupanim.duration) {
		clearInterval(popupanim.timer);
		popupanim.timer = null;
		popupanim.element.style.display = 'none';
	}
	else {
		//left
		move = cubicOut(popupanim.time, popupanim.left, popupanim.changeLeft, popupanim.duration)
		popupanim.element.style.left = parseInt(move) + 'px';
		
		
		//top
		move = cubicOut(popupanim.time, popupanim.top, popupanim.changeTop, popupanim.duration)
		popupanim.element.style.top = parseInt(move) + 'px';
		
		//width
		move = cubicOut(popupanim.time, popupanim.width, popupanim.changeWidth, popupanim.duration)
		popupanim.element.style.width = parseInt(move) + 'px';
		
		//height
		move = cubicOut(popupanim.time, popupanim.height, popupanim.changeHeight, popupanim.duration)
		popupanim.element.style.height = parseInt(move) + 'px';
		
		popupanim.time++;
	}
}


function fetchpage(href) {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		try	{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				//alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			popupcontent = xmlHttp.responseText;
			
			currentpopup = document.createElement('div');
			currentpopup.className = 'popup';
			
			div = document.createElement('div');
			div.className = 'popup-top';
			currentpopup.appendChild(div);
			
			div = document.createElement('div');
			div.className = 'popup-left';
			currentpopup.appendChild(div);
			
			div = document.createElement('div');
			div.className = 'popup-body';
			div.innerHTML = popupcontent;
			currentpopup.appendChild(div);
			
			div = document.createElement('div');
			div.className = 'popup-right';
			currentpopup.appendChild(div);
			
			div = document.createElement('div');
			div.className = 'popup-bottom';
			currentpopup.appendChild(div);			
			
			document.getElementById('wrap').appendChild(currentpopup);
			setCloseButton(popupanim.left, popupanim.top);
		}
	}
	
	if (href.indexOf('?') > -1)
		url = href + '&rand=' + parseInt(Math.random()*99999999);
	else
		url = href + '?rand=' + parseInt(Math.random()*99999999);
	
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
	return true;
}

function popupDoAnim()
{
	if (popupanim.time > popupanim.duration) {
		clearInterval(popupanim.timer);
		popupanim.timer = null;
		popupanim.element.style.display = 'none';
		
		if (currentpopup.id != 'resultPanel') {
			currentpopup.style.left = (parseInt(popupanim.element.style.left) - 17) + 'px';
			currentpopup.style.top = (parseInt(popupanim.element.style.top) - 14) + 'px';
			if (widePopup)
				currentpopup.className = 'popup widepopup';
		}
		currentpopup.style.display = 'block';
	}
	else {
		//left
		move = cubicOut(popupanim.time, popupanim.left, popupanim.changeLeft, popupanim.duration)
		popupanim.element.style.left = parseInt(move) + 'px';
		
		//top
		move = cubicOut(popupanim.time, popupanim.top, popupanim.changeTop, popupanim.duration)
		popupanim.element.style.top = parseInt(move) + 'px';
		
		//width
		move = cubicOut(popupanim.time, popupanim.width, popupanim.changeWidth, popupanim.duration)
		popupanim.element.style.width = parseInt(move) + 'px';
		
		//height
		move = cubicOut(popupanim.time, popupanim.height, popupanim.changeHeight, popupanim.duration)
		popupanim.element.style.height = parseInt(move) + 'px';
		
		popupanim.time++;
	}
}

function setCloseButton(x, y) {
	// assign an ID to the close button. This ID will be splitted later to locate the end point of the close-animation
	img = currentpopup.getElementsByTagName('img')[0];
	img.id = 'p' + x + '_' + y;
}

/* =============== Tooltips ============================================= */

function initTooltips() {
	var wrap = document.getElementById('wrap');
	var tools = document.getElementById('tools').getElementsByTagName('a');
	for (var i = 0; i < 4; i++) {
		div = document.createElement('div');
		div.innerHTML = '<span class="tooltip-body">' + tools[i].title + '</span><span class="tooltip-right"><br /></span><div class="tooltip-bottom"></div>';
		tools[i].title = '';
		div.className = 'tooltip';
		div.style.left = (336 + tools[i].parentNode.offsetLeft) + 'px';
		div.id = tools[i].id + '_tooltip';
		wrap.appendChild(div);
		addEvent(tools[i], 'mouseover', showtooltip);
		addEvent(tools[i], 'mouseout', hidetooltip);
	}
}

function showtooltip() {
	if (tooltip_visible) 
		return;
	document.getElementById(this.id + '_tooltip').style.display = 'block';
}

function hidetooltip() {
	if (tooltip_visible) 
		return;
	document.getElementById(this.id + '_tooltip').style.display = 'none';
}

/* --------- individual tool functions ------------ */

var tooltip_visible = false;

function startlisten(a) {
	if (tooltip_visible)
		return;
		
	if ( typeof(hhLogEvent) == "function" )
	    hhLogEvent("Listen");
		
	url = location.href;
	pos = url.indexOf('localhost/');
	if (pos > 0) {
		url = url.substring(url.indexOf('localhost/') + 10);
		url = 'http://teliasonera.ar.halvarsson.se/' + url;
	}
	
	url = encodeURIComponent(url);
	if (lang == 'en')
		llang = 'us';
	else 
		llang = lang;

	a.className = 'active';
	tooltip = document.getElementById(a.id + '_tooltip');
	tooltip.getElementsByTagName('span')[0].style.display = 'none';
	strhtml = '<div class="tooltip-body"><object '
			+ '\nclassid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
			+ '\ncodebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
			+ '\nwidth="190" '
			+ '\nheight="16"> '
		+ '\n<param name="movie" value="../../images/flash/listen.swf?lang=' + llang + '&url=' + url + '" />'
		+ '\n<param name="scale" value="noborder" />'
		+ '\n<param name="quality" value="high" />'
		+ '\n<param name="bgcolor" value="#FFFFFF" />'
		+ '\n<param name="wmode" value="trssansparent" />'
		+ '\n<embed src="../../images/flash/listen.swf?lang=' + llang + '&url=' + url + '"'
			+ '\nquality="high"'
			+ '\nbgcolor="#FFFFFF"'
			+ '\nscale="noborder"'
			+ '\nwidth="190"'
			+ '\nheight="16"'
			+ '\ntype="application/x-shockwave-flash" '
			+ '\npluginspace="http://www.macromedia.com/go/getflashplayer">'
		+ '\n</embed>'
		+ '\n</object></div>';
	
	strhtml += '<a href="#" onclick="closetooltip(this); return false;"><img src="../../images/popup/close-small.gif" /></a>';
	span = document.createElement('span');
	span.className = 'tooltip-body';
	span.innerHTML = strhtml;
	tooltip.insertBefore(span, tooltip.firstChild);
	tooltip_visible = true;
}

function startprint(a) {
	if (tooltip_visible)
		return;
	
	if ( typeof(hhLogEvent) == "function" )
	    hhLogEvent("Print");

	a.className = 'active';
	tooltip = document.getElementById(a.id + '_tooltip');
	tooltip.className = 'tooltip largetooltip';
	tooltip.getElementsByTagName('span')[0].style.display = 'none';
	strhtml = '<div class="tooltip-body">' +
		'<p>' + translate('Use the pdf version for optimal print quality') + '.</p>' +
		'<p><a class="pdf" href="http://www.teliasonera.com/investor_relations/reports/annual_report/2007/' + translate('in_english.pdf') + '" target="_blank">' + translate('Annual Report 2007 (131 pages, 3.8 mb)') + '</a></p>' +
		'</div>';
	strhtml += '<a href="#" onclick="closetooltip(this); return false;" ><img src="../../images/popup/close-small.gif" /></a>';
	span = document.createElement('span');
	span.className = 'tooltip-body';
	span.innerHTML = strhtml;
	tooltip.insertBefore(span, tooltip.firstChild);
	tooltip_visible = true;
}

function closetooltip(img) {
	tooltip = img.parentNode.parentNode;
	tooltip.style.display = 'none';
	tooltip.className = 'tooltip';
	spans = tooltip.getElementsByTagName('span');
	tooltip.removeChild(spans[0]);
	spans[0].style.display = 'block';
	tooltip_visible = false;
	try {
		document.getElementById(tooltip.id.replace('_tooltip', '')).className = '';
	}
	catch(ex) {}
	panelReminder = false;
	panelTermsOfUse = false;
	panelNewUser = false;
	panelAllcomments = false;
	panelFriend = false;
}

//--

function starthelp(a) {
	if ( typeof(hhLogEvent) == "function" )
	    hhLogEvent("Help");

	a.className = 'active';
	tooltip = document.getElementById(a.id + '_tooltip');
	tooltip.className = 'tooltip largetooltip';
	tooltip.getElementsByTagName('span')[0].style.display = 'none';
	switch (lang) {
		case 'sv':
			strhtml = '<div class="tooltip-body">' +
				'<p><strong>Hj&auml;lpavsnitt</strong></p>' +
				'<p><a href="../Ovriginformation/Hjalpavsnitt/Hittaiarsredovisningen.html">Hitta i &aring;rsredovisningen</a></p>' +
				'<p><a href="../Ovriginformation/Hjalpavsnitt/Bladdrainomettkapitel.html">Bl&auml;ddra inom ett kapitel</a></p>' +
				'<p><a href="../Ovriginformation/Hjalpavsnitt/Attanvandalankar.html">Att anv&auml;nda l&auml;nkar</a></p>' +
				'<p><a href="../Ovriginformation/Hjalpavsnitt/Kommentarsfunktionen.html">Kommentarsfunktionen</a></p>' +
			'</div>';
			break;
		case 'en':
			strhtml = '<div class="tooltip-body">' +
				'<p><strong>Instructions</strong></p>' +
				'<p><a href="../AdditionalInformation/Instructions/HowtonavigateintheAnnualReport.html">How to navigate in the Annual Report</a></p>' +
				'<p><a href="../AdditionalInformation/Instructions/Howtobrowsewithinachapter.html">How to browse within a chapter</a></p>' +
				'<p><a href="../AdditionalInformation/Instructions/Touselinks.html">How to use hyperlinks</a></p>' +
				'<p><a href="../AdditionalInformation/Instructions/Comments.html">How to use the comment function</a></p>' +
			'</div>';
			break;
		case 'fi':
			strhtml = '<div class="tooltip-body">' +
				'<p><strong>Ohjeet</strong></p>' +
				'<p><a href="../Lisatietoja/Ohjeet/Nainliikutvuosikertomuksessa.html">N&auml;in liikut vuosikertomuksessa</a></p>' +
				'<p><a href="../Lisatietoja/Ohjeet/Kappaleenselaaminen.html">Kappaleen selaaminen</a></p>' +
				'<p><a href="../Lisatietoja/Ohjeet/Linkkienkayttaminen.html">Linkkien k&auml;ytt&auml;minen</a></p>' +
				'<p><a href="../Lisatietoja/Ohjeet/Kommenttitoiminto.html">Kommenttitoiminto</a></p>' +
			'</div>';
			break;
	}
		
	strhtml += '<a href="#" onclick="closetooltip(this); return false;" ><img src="../../images/popup/close-small.gif" /></a>';
	span = document.createElement('span');
	span.className = 'tooltip-body';
	span.innerHTML = strhtml;
	tooltip.insertBefore(span, tooltip.firstChild);
	a = tooltip.getElementsByTagName('a');
	for (var i = 0; i < a.length - 1; i++) {
		initInternalLink(a[i]);
	}
	tooltip_visible = true;
}