var explainid=0;
function explain(id) {
    if (explainid != 0 && document.getElementById('explain' + explainid)) {
		document.getElementById('explain'+explainid).style.display = 'none';
	}
	if(explainid!=id) {

        //close other popups
       AssertOnlyPopup();
        
        var visibleElement = document.getElementById('explain'+id);
	    var elmTitle = visibleElement.innerHTML;
	    var alignElm = null;
	    try
	    {
	        if(visibleElement.previousSibling.tagName == 'TR')
	        {
	            alignElm = visibleElement.previousSibling;
	        }
	        else
            {
                alignElm = visibleElement.previousSibling.previousSibling;
            }
	        
	    }
	    catch(e)
	    {
	        alignElm = visibleElement;
	    }
	    
	    if(!elmTitle && !alignElm) return;
	    
	    this.div=document.createElement('div');
	    arrObjHolder['infoLyr']=this.div;

        this.div.style.top = getElmPosition.GetY(alignElm) +'px';
        this.div.style.position = 'absolute';

	    this.div.id='tooltip-container';
	    this.div.zIndex=10000;
    	
	    this.div.innerHTML='<div id="popupWrapper2"><div class="popup2"><p class="closeWindow"><a href="#" onclick="return HidePopup();">St&auml;ng</a></p>'+ elmTitle +'</div></div>';
    	
    	initModalBackPlate();
	    centerPopup(this.div);
    	document.body.appendChild(this.div);
    	
	    //document.getElementsByTagName('body')[0].appendChild(this.div);
	
		explainid=id;
	} else {
		explainid=0;
	}
}

/*------------------ Den stora etikettpopupen ---------------- */
function openLabelPopup()
{
    currentLink = document.getElementById('labelsContent');
	var titleText = currentLink.innerHTML;
	if(!titleText) return;

    AssertOnlyPopup();

    var divTooltipContainer = document.getElementById('tooltip-container');
    if (divTooltipContainer == null)
    {        	
	    this.div=document.createElement('div');
	    arrObjHolder['infoLyr']=this.div;

        this.div.style.top = '150px';
        this.div.style.position = 'absolute';

	    this.div.id='tooltip-container';
	    this.div.zIndex=9999;
    }
    this.div.style.display = 'block';

	initModalBackPlate();
	
	this.div.innerHTML='<div id="popupWrapper2"><div class="popup"><p class="closeWindow"><a href="#" onclick="return HidePopupAndFade();">St&auml;ng</a></p>'+ titleText +'</div></div>';
	
	centerPopup(this.div);
	
    document.body.appendChild(this.div);

	hideShowSelectBoxesForIe6('hidden');
	return false;
}

function openQuiz(nr) {
    var html = 'No quiz....';
    AssertOnlyPopup();
    if (nr == 1) {
        html = '<iframe width="440" height="355" frameborder="0" scrolling="no" src="http://client.millionmind.com/?api_key=ff346e4815362dabee501369d1b0365f5d621fc6&gid=1" style="border: 0px none; margin-right: 0px; margin-top: 0px; background-color: #ffffff;" id="innerframe" name="innerframe"></iframe>';
    }
    if (nr == 2) {
        html = '<iframe width="440" height="355" frameborder="0" scrolling="no" src="http://client.millionmind.com/?api_key=ff346e4815362dabee501369d1b0365f5d621fc6&gid=3" style="border: 0px none; margin-right: 0px; margin-top: 0px; background-color: #ffffff;" id="innerframe" name="innerframe"></iframe>';
    }
    if (nr == 3) {
        html = '<iframe width="440" height="355" frameborder="0" scrolling="no" src="http://client.millionmind.com/?api_key=ff346e4815362dabee501369d1b0365f5d621fc6&gid=4" style="border: 0px none; margin-right: 0px; margin-top: 0px; background-color: #ffffff;" id="innerframe" name="innerframe"></iframe>';
    }
    if (nr == 4) {
        html = '<iframe width="440" height="355" frameborder="0" scrolling="no" src="http://client.millionmind.com/?api_key=ff346e4815362dabee501369d1b0365f5d621fc6&gid=5" style="border: 0px none; margin-right: 0px; margin-top: 0px; background-color: #ffffff;" id="innerframe" name="innerframe"></iframe>';
    }
    var divContainer = document.getElementById('quizpopup' + nr);
    if (divContainer == null) {
        this.div = document.createElement('div');
        arrObjHolder['infoLyr'] = this.div;

        this.div.style.top = '150px';
        this.div.style.position = 'absolute';
        this.div.id = 'quizpopup' + nr;
        this.div.zIndex = 9999;
    }
    this.div.style.display = 'block';

    initModalBackPlate();

    this.div.innerHTML = '<div id="popupWrapper2" style="width: 480px; max-width: 480px"><div class="popup"><p class="closeWindow"><a href="#" onclick="return HidePopupAndFade();">St&auml;ng</a></p>' + html + '</div></div>';

    centerPopup(this.div);

    document.body.appendChild(this.div);

    hideShowSelectBoxesForIe6('hidden');
    return false;
}



var HidePopupAndFade=function(){
    try
    {
        if (arrObjHolder['infoLyr'] != null)
        {
	        arrObjHolder['infoLyr'].innerHTML='';
	        arrObjHolder['infoLyr'].style.display='none';
	    }
	        
	    var fadeDiv = document.getElementById('fade');
	    showFlash();
        if(fadeDiv != null) 
        {
            fadeDiv.style.display = 'none';
            RemoveElement(fadeDiv);
            showFlash();
        }
        else
        {
            var arrFadeElems=getElementsByClassName(document, "div", "fade");
            for(var i = 0; i < arrFadeElems.length; i++)
            {
                RemoveElement(arrFadeElems[i]);
                showFlash();
            }
        }
	    
	    hideShowSelectBoxesForIe6('visible');
		
		return false;
	}
	catch(exc)
	{
	}
}

var RemoveElement=function(elm){
    try
    {
	    elm.parentNode.removeChild(elm);
	}
	catch(exc)
	{
	}
}

var AssertOnlyPopup=function(elmId){
    try
    {
        var popupToShow = getElementsByClassName(document, "div", "popup2");
        for(var i = 0; i < popupToShow.length; i++)
        {
            var parentDiv = popupToShow[i].parentNode;
            parentDiv.style.display = "none";
        }
        
        var popupToShow = getElementsByClassName(document, "div", "popup");
        for(var j = 0; j < popupToShow.length; j++)
        {
            var parentDiv = popupToShow[j].parentNode;
            parentDiv.style.display = "none";
            showFlash();
        }
	}
	catch(exc)
	{
	}
}


/*bubbel methods - Made by Gustaf Lindqvist for the Youth site*/
function showBubble(BubbleName) {
	if (document.getElementById(BubbleName).style.display != 'block')
		document.getElementById(BubbleName).style.display = 'block';
	else
		document.getElementById(BubbleName).style.display = 'none';
}

function showWordBubble(BubbleName, Top) {
	if (document.getElementById(BubbleName).style.display != 'block')
	{
		document.getElementById(BubbleName).style.display = 'block';
		document.getElementById(BubbleName).style.top = Top;
	}
	else
		document.getElementById(BubbleName).style.display = 'none';
}
/*3 Ord methods - Made by Gustaf Lindqvist for the Youth site*/
function swapYouthWord(headLineShowWord, showWord, headLinehideWord1, hideWord1, headLinehideWord2, hideWord2)
{
		document.getElementById(showWord).style.display = 'block';
		document.getElementById(hideWord1).style.display = 'none';
		document.getElementById(hideWord2).style.display = 'none';
		
		// get hold of the objects
		var headLineheadLineShowWord = document.getElementById(headLineShowWord);
		var headLineheadLinehideWord1 = document.getElementById(headLinehideWord1);
		var headLineheadLinehideWord2 = document.getElementById(headLinehideWord2);
		
		// set the cssclasses
		headLineheadLineShowWord.setAttribute("class", "click first");
		headLineheadLineShowWord.setAttribute("className", "click first");
		
		headLineheadLinehideWord1.setAttribute("class", "click");
		headLineheadLinehideWord1.setAttribute("className", "click");
		
		headLineheadLinehideWord2.setAttribute("class", "click");
		headLineheadLinehideWord2.setAttribute("className", "click");
		
}

/* Function to help textbox friendly */
function focusTBText(tb, text)
{
    try
    {
        if(tb != null)
        {
            if(tb.value == text)
            {
                tb.value = '';
            }
        }
    }
    catch(e)
    {}
}

function blurTBText(tb, text)
{
    try
    {
        if(tb != null)
        {
            if(tb.value == '')
            {
                tb.value = text;
            }
        }
    }
    catch(e)
    {}
}

var getElmPosition={
	GetX : function(obj){
		var curleft=0;
		if(obj.offsetParent){while(1){curleft += obj.offsetLeft;if(!obj.offsetParent) break;obj=obj.offsetParent;};}
		else if(obj.x){curleft += obj.x;}
		return curleft;
	},
	GetY : function(obj){
		var curtop=0;
		if(obj.offsetParent){while(1){curtop += obj.offsetTop;if(!obj.offsetParent) break;obj=obj.offsetParent;};}
		else if(obj.y){curtop += obj.y;}
		return curtop;
		
	}
};

/*
* ----------------------------------------------------
* REQUIRED TOOLTIP-SPECIFIC FUNCTIONS
* ----------------------------------------------------
*/

var arrObjHolder=new Array();

var currentLink = null;

/* FUNCTIONALITY FOR NEW TOOLTIPS
----------------------------------------------------*/
var initTooltips = function() {
    var arrTipElems = getElementsByClassName(document, "*", "tooltip");
    var intElems = arrTipElems.length;
    for (var i = 0; i < intElems; i++) {
        var TipElm = arrTipElems[i];
        if (TipElm.className.indexOf('selected') > 0) {
            DisplayTooltipInternal(TipElm);
        }
        if (TipElm.getAttribute('rel')) {
            addEvent(TipElm, 'click', DisplayTooltip);
        }
    }
}

/* DISPLAY TOOLTIP
----------------------------------------------------*/

var DisplayTooltipInternal = function(currentLink) {

    //close other popups
    AssertOnlyPopup();

    var elmTitle = currentLink.getAttribute('rel');
    var titleText = currentLink.innerHTML;
    if (!elmTitle) return;

    currentLink.div = document.createElement('div');
    arrObjHolder['infoLyr'] = currentLink.div;
    currentLink.div.style.top = (jQuery(window).scrollTop() + 50) + "px";//Math.min(getElmPosition.GetY(currentLink), (document.documentElement.scrollTop + getViewport.Height() - 400)) + 'px';
    currentLink.div.style.position = 'absolute';
    currentLink.div.id = 'tooltip-container';
    currentLink.div.zIndex = 9999;

    initModalBackPlate();

    if (!elmTitle.match(/^<p>/i)) {
        elmTitle = "<p>" + elmTitle + "</p>";
    }

    currentLink.div.innerHTML = '<div id="popupWrapper2"><div class="popup2 tagExplanation"><p class="closeWindow"><a href="#" onclick="return HidePopup();">St&auml;ng</a></p><h3>' + titleText + '</h3>' + elmTitle + '</div></div>'
    document.getElementsByTagName('body')[0].appendChild(currentLink.div);

    centerPopup(currentLink.div);

    hideShowSelectBoxesForIe6('hidden');
}

var DisplayTooltipFromElement = function(elm) {

    //close other popups
    AssertOnlyPopup();

    var elmTitle = document.getElementById(elm).innerHTML;
    if (!elmTitle) return;

    var currentLink = document.getElementById(elm);
    currentLink.div = document.createElement('div');
    arrObjHolder['infoLyr'] = currentLink.div;
    currentLink.div.style.top = Math.min(getElmPosition.GetY(currentLink), (document.documentElement.scrollTop + getViewport.Height() - 400)) + 'px';
    currentLink.div.style.position = 'absolute';
    currentLink.div.id = 'tooltip-container';
    currentLink.div.zIndex = 9999;

    initModalBackPlate();

    if (!elmTitle.match(/^<p>/i)) {
        elmTitle = "<p>" + elmTitle + "</p>";
    }

    currentLink.div.innerHTML = '<div id="popupWrapper2"><div class="popup2 tagExplanation"><p class="closeWindow"><a href="#" onclick="return HidePopup();">St&auml;ng</a></p>' + elmTitle + '</div></div>'
    document.getElementsByTagName('body')[0].appendChild(currentLink.div);

    centerPopup(currentLink.div);

    hideShowSelectBoxesForIe6('hidden');
}


var DisplayTooltip = function(event) {
    DisplayTooltipInternal(this);
}

/* HIDE TOOLTIP
----------------------------------------------------*/

var HidePopup=function(){
    try
    {
	    arrObjHolder['infoLyr'].innerHTML='';
	    arrObjHolder['infoLyr'].style.display='none';
	    hideShowSelectBoxesForIe6('visible');
	    HidePopupAndFade();
	    return false;
	}
	catch(exc)
	{
	}
}


/*
* ----------------------------------------------------
* REQUIRED NON-SPECIFIC FUNCTIONS
* ----------------------------------------------------
*/

function printAnswers()
{
    var divContent = document.getElementById('printResult');
    if(divContent != null)
    {
        var content = divContent.innerHTML;
        var win = window.open("", "newwin", "height=500, width=500,toolbar=yes,scrollbars=yes,menubar=no");
        win.document.write(content);
        win.print();
    }
    return false;
}

function printQuestions()
{
    var divContent = document.getElementById('printTest');
    if(divContent != null)
    {
        var content = divContent.innerHTML;
        var win = window.open("", "newwin", "height=500, width=500,toolbar=yes,scrollbars=yes,menubar=no");
        win.document.write(content);
        win.print();
    }
    return false;
}

function hideShowSelectBoxesForIe6(displayMode) 
{       
    if(typeof document.body.style.maxHeight != 'undefined')
    {
        return;
    }
                                     
    var e = document.getElementsByTagName('select');

    for (var i=0; i<e.length; i++)
    {
        document.getElementById(e[i].id).style.visibility = displayMode;
    }
}

function closeAllBut(popupElem)
{
    return ;
}

function initAjaxStuff()
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(OnEndRequest);
}

function OnEndRequest(sender,args)
{        
    var popupSourceDiv = document.getElementById('popupWrapper2');
    var popupDestinationDiv = document.getElementById('popupContent');

    if (popupSourceDiv != null && popupDestinationDiv != null)
    {
        popupDestinationDiv.innerHTML = '<div id="' + popupSourceDiv.id + '" class="' + popupSourceDiv.className + '" >' + popupSourceDiv.innerHTML + '</div>';
        popupSourceDiv.style.display = 'none';
        popupSourceDiv.innerHTML = '';
    }
    
    centerPopups();
}


function openPopupML(fade)
{
    centerPopups();
    if (fade == 'True')
    {
        initModalBackPlate();
    }
}

function closePopupML()
{
    hideShowSelectBoxesForIe6('visible');
    
    closePopupWithFade();
}


function centerPopups(e)
{
	if(!e) e=window.event;
    try
    {    
        
        var toolContainer = document.getElementById("tooltip-container");
        var popupContent = document.getElementById("popupContent");
        if(toolContainer)
        {
            centerPopup(toolContainer);
        }
        if(popupContent.childNodes.length == 0){
			positionPopup(document.getElementById("popupWrapper2"));
		}
        else if(popupContent.childNodes.length >0){
			positionPopup(popupContent);
        }
        CancelEvent(e);
    }
    catch(e)
    { //ignore
    }
}

var centerPopup = function (pop) {
    var w = jQuery(pop).children().width();

    if (w == 0) {
        w = 300;
    }

    var leftPos = ((jQuery(window).width() - w) / 2) + "px";

    pop.style.left = leftPos;
    pop.style.top = (jQuery(window).scrollTop() + 50) + "px";
}

var getViewport={
	Height:function(){
		if(typeof window.innerHeight!='undefined'){
			return window.innerHeight
		}
		else if(typeof document.documentElement!='undefined'&&typeof document.documentElement.clientHeight!='undefined'&&document.documentElement.clientHeight){
			return document.documentElement.clientHeight
		}
	},
	Width:function(){
		if(typeof window.innerWidth!='undefined'){
			return window.innerWidth
		}
		else if(typeof document.documentElement!='undefined'&&typeof document.documentElement.clientWidth!='undefined'&&document.documentElement.clientWidth){
			return document.documentElement.clientWidth
		}
	}
};

var positionPopup = function(obj) {

	var bwidth = getViewport.Width();
	var bheight = getViewport.Height();
	if (obj) 
	{
		obj.style.left = bwidth/2 - obj.offsetWidth/2 - 12 + "px";
		obj.style.top = (document.documentElement.scrollTop + bheight/2) - obj.offsetHeight/2 +  "px";
	}
};


var initModalBackPlate=function(){
var a=$('fade');
	if(!a){
		a=document.createElement('DIV');
		a.id='fade';a.className='popup2';
		a.style.position='absolute';
		a.style.zIndex=500;
		Fade.setOpacity(a,70);
		document.getElementsByTagName('BODY')[0].appendChild(a)
	}
};

function inputfocus(t, defaultValue)
{
    if(t.value == defaultValue)
    {
	    t.value='';
	}
}
function inputblur(t, defaultValue)
{
    if(t.value=='')
    {
	    t.value = defaultValue;
	}
}
function noenter() 
{
  return !(window.event && window.event.keyCode == 13); 
}

function textCounter(field, maxlimit) {
	var elm = document.getElementById('character_counter');

	if ((maxlimit - field.value.length) < 50) 
	{
		elm.style.color = "#ff0000";
	}
	else 
	{
		elm.style.color = "#000000";
	}


	elm.innerHTML = maxlimit - field.value.length;
}

function chooseall(elm) {
 var inputs = elm.parentNode.parentNode.getElementsByTagName('INPUT');
 for (var i = 0; i < inputs.length; i++) {
 if (inputs[i].type == "checkbox") {
 inputs[i].checked = true;
 }
 }
}

function choosenone(elm) {
 var inputs = elm.parentNode.parentNode.getElementsByTagName('INPUT');
 for (var i = 0; i < inputs.length; i++) {
 if (inputs[i].type == "checkbox") {
 inputs[i].checked = false;
 }
 }
}

// Genererar fel på denna sida: http://local.ekonomifakta.se/sv/Fakta/Skatter/Rakna-pa-dina-skatter/Jamfor-skatter-mellan-lander/ när man klickar på "alla länder"
/* jQuery specific functions */

jQuery(document).ready(function($)
{
	$('#article .tabpages').TabToggle();
	$('#main-content .dynamiclist').DynamicList();
	
});

function pageLoad(sender, args) {
    var e;
    
	jQuery('ul.share').sharePlugin();
	try {
		gapi.plusone.render(document.getElementsByTagName("plusone")[0], {"size": "small"});
	} catch (e) {}
	try {
		gapi.plusone.render(document.getElementsByTagName("g:plusone")[0], {"size": "small"});
} catch (e) { }


}

(function ($) {
    $.fn.sharePlugin = function () {
        if(this && $(this) && $(this).length > 0)
		{
			var container = $(this);
			container.find('a').attr('target','blank');
			var fb = container.find('li.facebook');
			if(fb.length > 0)
			{
				fb
					.find('iframe').hide().before($('<img src="/images/blank.gif" alt="" class="icon"/>').css('cursor','pointer').click(function(e)
					{
						$(this).hide().next().show();
					}));
			}
		}
    };
})(jQuery);

(function ($) {
    $.fn.DynamicList = function () {
        if(this && $(this) && $(this).length > 0)
		{
			
			var rePaint = function(orgContainer,returnObject)
			{
				if(!returnObject)
				{
					orgContainer.find('li:visible').removeClass('globalodd').each(function(k,objli){ if(k%2==0) {$(objli).addClass('globalodd'); } });
				}
				else
				{
					orgContainer.find('ul.lvl0 > li').removeClass('globalodd').each(function(k,objli){ if(k%2==0) { $(objli).addClass('globalodd'); } });
					return orgContainer;
				}
			};

			$(this).each(function(o,obj)
			{
				obj = $(obj);
				var container = obj.clone(true);
				
				container
					.find('ul').each(function(i,ul)
					{
						ul = $(ul);
						var lvl = ul.parents('ul').length,
							replacedEMWidth = 0;
						ul.addClass('lvl'+lvl)
							.children('li').each(function(j,li)
							{
								li=$(li);
								var classname = '';
								if(li.children('ul').length > 0)
								{
									classname += 'hasChildren ';
									li.click(function(e)
									{
										e.stopPropagation();
										if($(e.target).parent('li').andSelf().filter('.hasChildren').length === 0) { return; }
										$(this).toggleClass('expanded');
										rePaint($(this).closest('div.dynamiclist'));
									}).children('strong:eq(0), em:eq(0)').addClass('lvl'+lvl+'-first first');
								}
								li.addClass(classname);
							});
						if(lvl === 0)
						{
							ul.children("li").children("em").each(function(k,em)
							{
								em = $(em);
								var emVal = em.text();
								if(emVal.match(/([\+\-\=])/ig) && emVal.match(/([\+\-\=])/ig)[0])
								{
									var match = emVal.match(/([\+\-\=])/ig)[0];
									if(match && match == '+') { em.addClass('plussign replaced'); }
									if(match && match == '-') { em.addClass('minussign replaced'); }
									if(match && match == '=') { em.addClass('equalsign replaced'); }
									em.text(emVal.replace(/([\+\-\=])/ig,''));
								}
							});
						}
					}).end()
					.addClass('activated');
				obj.replaceWith(rePaint(container,true));

				var replacedEMWidth = 0;
				
				container.bind('recalculateWidths',function()
				{
					$(this).find('em.replaced:visible').each(function(i,em)
					{
						em = $(em);
						if(em.width() > replacedEMWidth) { replacedEMWidth = em.width(); }
					}).width(replacedEMWidth);
				});

				container.trigger('recalculateWidths');
				
			});
		}
    };
})(jQuery);


(function ($) {
    $.fn.TabToggle = function () {
        if(this && $(this) && $(this).length > 0)
		{
			var container = $(this);
			var tablinkscontainer = $('<div class="tablinks"/>');
			var locationHash = (window.location.hash) ? window.location.hash : '';
			container
				.find('.tabpage').hide().end()
				.find('h2.tablink').detach().appendTo(tablinkscontainer).end()
				.find('a').click(function(e)
				{
					var objLink = $(this);
					var strHref = objLink.attr('href');
					var target = $(strHref+'-content');
					if(target && target.length > 0)
					{
						container
							.find('.tabpage').hide().end()
							.find('.tablinks a').removeClass('active');

						target.show(0,function(){
							 if (jQuery.browser.msie){this.style.removeAttribute("filter");}
							if(target.find('.dynamiclist').length > 0)
							{
								target.find('.dynamiclist').trigger('recalculateWidths');
							}
						});
						
						objLink.addClass('active');
					}
				});

			tablinkscontainer.prependTo(container);
			
			var currenttablink = tablinkscontainer.find("a[href = '"+locationHash+"']").click();
			if(currenttablink.length == 0)
			{
				tablinkscontainer.find('a:eq(0)').click();
			}

		}
    };
})(jQuery);
