// ---------
// Globals
// ---------
var gDialogHiddenFieldId = '';
var gCounter = 0;

function ieHover()
{
	var nav = document.getElementById("menu");
	if (nav)
	{
		var nodes = nav.getElementsByTagName("li");
		for (var i = 0; i < nodes.length; i++)
		{
			nodes[i].onmouseover = function () 
			{
				if (this.className.indexOf("hover") == -1)
				{
					this.className += " hover";
				}
			}
			nodes[i].onmouseout = function ()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}
if (window.attachEvent && !window.opera)
	window.attachEvent("onload", ieHover);
	
// Avantime start

// ---------
// Globals
// ---------
var gDialogHiddenFieldId = 'ctl00_DialogContainer_HiddenFieldDialogName';
var gCounter = 0;

// ---------------
// Toggles visibility of the specified element.
// ---------------
function toggleVisibility(elemId)
{
	var elem = document.getElementById(elemId);
	
	if (elem)
	{
		if (elem.style.display == 'none')
		{		
			elem.style.display = 'block';
		}
		else
		{			
			elem.style.display = 'none';
		}
	}
}

// ------------------------------------------------------------
// Changes the height of a number of elements so they are the same
// height. The input param is an array with elements-id:s.
// ------------------------------------------------------------
function adaptElementHeight(divNameArr, minHeight)
{
	// This script messes up the page when it's displayed in EPi's
	// edit mode so we need this rather ugly work-around to prevent
	// the script from running when the page is viewed in edit mode.
	if (window.parent.name == 'EditPanel')
	{
		return;
	}
	
    var height = minHeight;
    var divArr = Array();
    
    // Loop over all the divs to get the height if the highest
    for (i = 0; i < divNameArr.length; i++)
    {
        divArr[i] = document.getElementById(divNameArr[i]);
        
        if (divArr[i])
        {
			// We set the height to auto to erase any previously hardcoded pixel
			// values which will otherwise prevent this function from running correctly
			// more than one time. It's useful to be able to call this function
			// again when dynamic content is displayed on the page.
	        divArr[i].style.height = 'auto';
	        
	        if (divArr[i].clientHeight > height)
	        {
				height = divArr[i].clientHeight;
			}
        }
    }    

    // Set the height for all divs
    for (i = 0; i < divArr.length; i++)
    {
        if (divArr[i])
        {
            divArr[i].style.height = height + 'px';
        }
    }
}

// ------------------------------------------------------------
// Sets the height of all the children of the specified element
// to the height of the highest child.
// ------------------------------------------------------------
function adaptChildHeight(parentId)
{
	// This script messes up the page when it's displayed in EPi's
	// edit mode so we need this rather ugly work-around to prevent
	// the script from running when the page is viewed in edit mode.
	if (window.parent.name == 'EditPanel')
	{
		return;
	}

    var parentElement = document.getElementById(parentId);
    if (parentElement)
    {
        var maxHeight = 0;

        // Loop over all children to find the maxHeight
        for (i = 0; i < parentElement.childNodes.length; i++)
        {
            if (parentElement.childNodes[i].clientHeight > maxHeight)
            {
                maxHeight = parentElement.childNodes[i].clientHeight;
            }
        }

        // Loop over all children and set the height
        for (i = 0; i < parentElement.childNodes.length; i++)
        {
            if (parentElement.childNodes[i].style && 
                (parentElement.childNodes[i].style.clear != 'both'))
            {
                parentElement.childNodes[i].style.height = maxHeight + 'px';
            }
        }
    }
}

// ------------------------------------------------------------
// This function will fire a click event on the specified control when the 
// enter key is pressed in a text field. Attach this function to the 
// onkeypress-event on the text field like this:
// <input type="text" onkeypress="return fireClickOnEnter(event, 'IdOfControlToFireClickOn');">
// ------------------------------------------------------------
function fireClickOnEnter(evt, controlId)
{
    var control = document.getElementById(controlId);
    var keyCode = (typeof window.event == 'object') ? window.event.keyCode : evt.keyCode;

    // If enter is pressed -> fire click-event on the control
    if (control && (keyCode == 13))
    {
        control.focus();
        control.click();
        return false;
    }
    else
    {
        return true;
    }
}

// ------------------------------------------------------------
// This function will fire a postbackon the specified control when the 
// enter key is pressed in a text field. Attach this function to the 
// onkeypress-event on the text field like this:
// <input type="text" onkeypress="return postbackOnEnter(event, 'IdOfControlToFirePostbackOn');">
// ------------------------------------------------------------
function postbackOnEnter(evt, controlId)
{
    var keyCode = (typeof window.event == 'object') ? window.event.keyCode : evt.keyCode;

    // If enter is pressed -> do a postback
    if (keyCode == 13)
    {
		__doPostBack(controlId,'');
		return false;
    }
    else
    {
        return true;
    }
}

// ------------------------------------------------------------
// Returns the x coordinate of the specified object
// ------------------------------------------------------------
function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.clientLeft)
    {
        curleft += obj.clientLeft;
    }
    return curleft;
}

// ------------------------------------------------------------
// Returns the y coordinate of the specified object
// ------------------------------------------------------------
function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.clientTop)
    {
        curtop += obj.clientTop;
    }
    return curtop;
}

// ------------------------------------------------------------
// Builds an html-page for printing
// ------------------------------------------------------------
function printPage(pagename, activeTemplate,lang) 
{
  
	if (!window.print)
	{
		window.status = 'No print';
		return;
	}


	// Get the main content area and other stuff we need
	var contentdiv = document.getElementById('PrintDiv');
	
	// Switch top logo image depending on active theme
	var logoImage = "green";
	( activeTemplate == "Privatperson" ) ? logoImage = "green" : logoImage = "orange";
	
	if (lang == "sv") 
	{ logoImage= logoImage + "_sv"; }
	else  logoImage= logoImage + "_en";
	
	if (contentdiv)
	{
		var contentHtml = '<div style="padding: 50px 20px 0 20px;"><h1>' + pagename + '</h1>' + contentdiv.innerHTML + '</div>';
		
		var beginHtml = 
		      '<html>' +
			  '<head>' +
			  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">' + 
			  '<link rel="stylesheet" type="text/css" href="/Styles/print.css">' +
			  '<title>Etac - ' + pagename + '</title>' +
//			  '<style type="text/css">' + 
//			  'table { background-color: black; }' +
//			  '</style>' +
			  '</head>' +
			  '<body style="margin:0px;">';
		// <div style="height:67px; width:680px; padding:0px;">
		// </div><div class="clearfix" style="height:20px; line-height:20px; font-size:20px;"></div>
		var topImageHtml = '<img class="Image" src="/App_Themes/Default/Images/Tools/PdfGeneratorLogo.png" width="250px" align="right" />';
		var endHtml = '</body></html>';

		var printWin = window.open('about:blank','','width=700,height=600,scrollbars=yes,toolbar=yes');
		printWin.document.open();
		printWin.document.write(beginHtml +
		                        topImageHtml + 
		                        contentHtml + 
		                        endHtml);

		//printWin.document.close();
		printWin.print();
		//printWin.close();
	}
}

// ------------------------------------------------------------
// Adapt the main divs on a campaign page.
// ------------------------------------------------------------
function adaptMainCampaignDivs()
{
	adaptElementHeight(new Array('LeftAreaDiv','RightAreaDiv'), 400);
}

// ---------------
// Toggles the visibility of a campaign comment.
// ---------------
function toggleComment(id)
{
	toggleVisibility('CommentLess' + id);
	toggleVisibility('CommentMore' + id);
	adaptMainCampaignDivs();
}

// ---------------
// Updates the flash when a new value is selected in the amount drop-down
// ---------------
function updateCampaignHeartFlash(select, moduleId)
{
	var value = select.options[select.selectedIndex].value;
	var heartFlash = document.getElementById('moduleflash_' + moduleId);

	if (heartFlash)
	{
		heartFlash.sendValueToFlash(value);
	}
}


function openDialog()
{
	var dialogName	= arguments[0];
	var dialog		= document.getElementById('DialogContainer');
	var hiddenField	= document.getElementById(gDialogHiddenFieldId);
    
	if (dialog && hiddenField)
	{
		dialog.className  = 'Dialog ' + dialogName;
		hiddenField.value = '';
		// Make a delimited string with the dialogname and parameters
		for (var i = 0; i < arguments.length; i++)
		{
			hiddenField.value += arguments[i];
			if (i+1 < arguments.length) hiddenField.value += '|';
		}
		__doPostBack(gDialogHiddenFieldId,'');

		hideWindowedControls();
		showDialogBackground();
	}
}

function hideWindowedControls()
{
	if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7))
	{
	    // Hides all listboxes
		for (var i = 0; i < document.getElementsByTagName('select').length; i++)
		{
			document.getElementsByTagName('select')[i].style.visibility = 'hidden';
		}
	}
}

function showDialogBackground()
{
	var bgDiv = document.getElementById('DialogBG');
	if (bgDiv)
	{
		var height = document.getElementById('main').clientHeight + 40;
		bgDiv.style.height	= height + 'px';
		bgDiv.style.display	= 'block';

		// IE6 and below needs a special class
		if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7))
		{
			bgDiv.className = 'DialogBGIE6';
		}	
	}
}

function showWindowedControls()
{
	if ((BrowserDetect.browser) == 'Explorer' && (BrowserDetect.version < 7))
	{
	    // Show all listboxes
		for (var i = 0; i < document.getElementsByTagName('select').length; i++)
	    {
		    document.getElementsByTagName('select')[i].style.visibility = 'visible';
	    }
    }
}

function getScrollOffset()
{
    scrollOffset = {};
    if (window.pageYOffset)// all except Explorer
    { 
    	scrollOffset.x = window.pageXOffset;
    	scrollOffset.y = window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
    {
    	scrollOffset.x = document.documentElement.scrollLeft;
    	scrollOffset.y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
    	scrollOffset.x = document.body.scrollLeft;
    	scrollOffset.y = document.body.scrollTop;
    }
    return scrollOffset;
}

// ---------------
// Closes the dialog-layer.
// ---------------
function closeDialog()
{
	var dialog		= document.getElementById('DialogContainer');
	var hiddenField	= document.getElementById(gDialogHiddenFieldId);
	if (dialog && hiddenField)
	{
		dialog.style.display = 'none';
		hiddenField.value	 = '';
		__doPostBack(gDialogHiddenFieldId,'');	
				
		hideDialogBackground();
		showWindowedControls();
	}
}

function positionDialog()
{
	var dialog = document.getElementById('DialogContainer');
	if (dialog)
	{
		var viewSize   = getViewportSize();
		var scrollSize = getScrollOffset();
		dialog.style.display = 'block';
		
		// Center window on screen
		dialog.style.left = (scrollSize.x + (viewSize.width  - dialog.clientWidth)  / 2) + 'px';
				
		var marginTop = dialog.clientHeight>viewSize.height ? ((viewSize.height-dialog.clientHeight)/2):100;
        var top = (scrollSize.y + ((viewSize.height - dialog.clientHeight) / 2) - marginTop);
		if( top>scrollSize.y)
		    dialog.style.top  = top  + 'px';    
		 else
		    dialog.style.top  = scrollSize.y  + 'px';    		 		
	}
}

// ---------------
// Hides the semi-transparent div
// ---------------
function hideDialogBackground()
{
	var bgDiv = document.getElementById('DialogBG');
	if (bgDiv)
	{
		bgDiv.style.display = 'none';
	}
}

function getViewportSize()
{
    size = {};
    if (window.innerHeight)
    {
    	size.width  = window.innerWidth;
    	size.height = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
    	size.width  = document.documentElement.clientWidth;
    	size.height = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
    	size.width  = document.body.clientWidth;
    	size.height = document.body.clientHeight;
    }
    return size;
}
