
// ------------------------------------------------------------
// Global variables
// ------------------------------------------------------------
var gArticleRows = new Array();


// ------------------------------------------------------------
// Changes the height of the main divs of the page so
// they are the same height.
// ------------------------------------------------------------
function adaptMainDivs()
{
    var leftDiv		   = document.getElementById('leftareadiv');
    var mainDiv		   = document.getElementById('mainareadiv');
    var rightDiv	   = document.getElementById('rightareadiv');
    var mainandleftdiv = document.getElementById('mainandleftdiv');

    // min-height
    var height = 400;
       
    // find the highest div
    if (leftDiv  && leftDiv.clientHeight  > height) height = leftDiv.clientHeight;
    if (mainDiv  && mainDiv.clientHeight  > height) height = mainDiv.clientHeight;
    if (rightDiv && rightDiv.clientHeight > height)	height = rightDiv.clientHeight;
    if (mainandleftdiv && mainandleftdiv.clientHeight > height) height = mainandleftdiv.clientHeight;

    // set new height
    if (leftDiv)  leftDiv.style.height  = height + 'px';
    if (mainDiv)  mainDiv.style.height  = height + 'px';
    if (rightDiv) rightDiv.style.height = height + 'px';
    if (mainandleftdiv) mainandleftdiv.style.height = height + 'px';
}


// ------------------------------------------------------------
// Adapts the height of the rows containing articles
// ------------------------------------------------------------
function adaptArticleRows()
{
    // Loop over all the article rows specified in the global array
    for (var i = 0; i < gArticleRows.length; i++)    
    {
        alignChildHeight(gArticleRows[i]);
    }
}

// ------------------------------------------------------------
// Sets the height of all the children of the specified element
// to the height of the highest child.
// ------------------------------------------------------------
function alignChildHeight(parentId)
{
    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';
            }
        }
    }
}


// ---------------
// Open window for object on map
// ---------------
function openObjectMapWindow(url)
{
	new_str = url.replace(" ","");
	old_str = url;
	while (old_str != new_str)
	{
        old_str = new_str;
        new_str = new_str.replace(" ","");	
	}
	url = new_str;
	
	// Size the window slightly smaller than the screen
    var width  = (screen.width  * 0.8);
    var height = (screen.height * 0.7);

    // Center window on screen
    var left = (screen.width  - width)  / 2;
    var top  = (screen.height - height) / 2;
	
    w = window.open(url, 'ObjectMapWindow', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=yes,status=yes');
    
    if(w != null)
		w.focus();
}


// 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 
// onkeypressed-event on the text field.
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;
    }
}		

function showText(intID) {
	document.getElementById('div' + intID).style.display = 'block';
	document.getElementById('link' + intID).style.display = 'none';	
}
function hideText(intID) {
	document.getElementById('div' + intID).style.display = 'none';
	document.getElementById('link' + intID).style.display = 'block';	
}

function printPage(id) 
{
	var da = (document.all) ? 1 : 0;
	var pr = (window.print) ? 1 : 0;
	
	/*
	if(!pr)
		return;
	*/
	var printArea = document.getElementById(id);
	
	if(printArea == null && da) 
		printArea = document.all.mainareadiv;
	
	if(printArea) 
	{
		var sStart = "<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"<%=Configuration.RootDir%>styles/structure.css\"><link rel=\"stylesheet\" type=\"text/css\" href=\"<%=Configuration.RootDir%>styles/editor.css\"><link rel=\"stylesheet\" type=\"text/css\" href=\"<%=Configuration.RootDir%>styles/units.css\"></head><body onload=\"javascript:window.print();\">";
		sStop = "</body></html>";

		var w = window.open('','printWin','width=650,height=440,scrollbars=yes');
		wdoc = w.document;
		wdoc.open();
		wdoc.write( sStart + printArea.innerHTML ) ;
		wdoc.writeln( sStop );
		wdoc.close();
	}
}

    function adjustDivHeight()
    {
        // This script get the largest div-height between the two divs: 
        // startpageleftmenudiv and startpagemaincontentdiv. Then it sets the dotted line-div height
        // to match the largest height.
      try
      {
        var divHeightleft = document.getElementById('startpageleftmenudiv').clientHeight;
        var divHeightright = document.getElementById('startpagemaincontentdiv').clientHeight;
        if(divHeightleft > divHeightright)
        {
            document.getElementById('dotteddiv').style.height = divHeightleft+'px';
        }
        else
        {   
            document.getElementById('dotteddiv').style.height = divHeightright+'px';
        } 
      }
      catch(err)
      {
      }
    }
    function adjustMasterPageDivHeight()
    {
        // This script get the largest div-height between the the two divs: 
        // startpageleftmenudiv and startpagemaincontentdiv. Then it sets the dotted line-div height
        // to match the largest height.
        
        var divHeightmiddle = document.getElementById('maincontainerdiv').clientHeight;
        if(document.getElementById('dotteddivMasterPage') != null)
        {
            document.getElementById('dotteddivMasterPage').style.height = divHeightmiddle+'px';
        }    
    }
    function adjustDivWidth()
    { 
        var DivWidth = document.getElementById('maincontainerdiv').clientWidth;        
        document.getElementById('horizontaldotMaindiv').style.width = DivWidth+'px';      
        
        var innerDivWidth = document.getElementById('headerdiv').clientWidth;
        document.getElementById('horizontaldots').style.width = innerDivWidth+'px';        
    }
    
    function adjustModules()
    {
       // This script get the largest div-height between the newslist-div and modules-div.
       // Then adjust the heights so that they are of the same largest height.
       try
       {
           var topModuleHeight = document.getElementById('toprightmoduleinnerdiv').clientHeight;
           var middleModuleHeight = document.getElementById('middlerightmoduleinnerdiv').clientHeight;
           var bottomModuleHeight = document.getElementById('bottomrightmoduleinnerdiv').clientHeight;
           
           var newslistNotiserHeight = document.getElementById('newslistNotiser').clientHeight;
           var newslistMotdragHeight = document.getElementById('newslistMotdrag').clientHeight;
           
           var sumNewslistHeight = (newslistNotiserHeight+newslistMotdragHeight);
           var sumModuleHeight = document.getElementById('toprightmodule').clientHeight + document.getElementById('middlerightmodule').clientHeight + document.getElementById('bottomrightmodule').clientHeight +4;
           
           if(sumModuleHeight > sumNewslistHeight)
           {
                var newslistNotiserHeight = document.getElementById('newslistNotiser').style.height = ((sumModuleHeight/2)+'px');
                var newslistMotdragHeight = document.getElementById('newslistMotdrag').style.height = ((sumModuleHeight/2)+'px');
           }
           else
           {
                var sumHeightModule = (((sumNewslistHeight - sumModuleHeight)/3));
                
                document.getElementById('toprightmoduleinnerdiv').style.height = (topModuleHeight+sumHeightModule)+'px';
                document.getElementById('middlerightmoduleinnerdiv').style.height = (middleModuleHeight+sumHeightModule)+'px';
                document.getElementById('bottomrightmoduleinnerdiv').style.height = (bottomModuleHeight+sumHeightModule+1)+'px';
           }  
       }
       catch(err)
       {
       }
    }
    function adjustKalendarieLinje()
    {
        try{
        
            var textWidth = document.getElementById('KalendarieText').clientWidth;
            document.getElementById('KalendarieLinje').style.width = textWidth+'px';
        }
        catch(err)
        {
        }
    }
    function adjustTopBannerHeight()
    {
        try
        {
            var imageBanner = document.getElementById('imagebannerdiv').clientHeight;
            var headerDiv = document.getElementById('headerdiv').clientHeight;
            
            if(imageBanner < headerDiv)
            {
                var newHeight = (headerDiv - imageBanner) / 2;
                document.getElementById('spacer1').style.height = newHeight+'px';
                document.getElementById('spacer2').style.height = newHeight+'px';
            }
        }
        catch(err)
        {
        }
    }



function showPicture(index)
{
    // Make sure index is valid
    if ((index < gPictureCount) && (index > -1))
    {
        // Change picture on the image tag
        var imageTag = document.getElementById(gImageTagId);
        if (imageTag)
        {
            imageTag.src    = gPictureUrls[index];
            imageTag.alt    = gPictureDescriptions[index];
            gCurrentPicture = index;
        }
        
        // Change the PictureText
        var divText = document.getElementById('PictureText');
        if (divText)
        {
			divText.innerHTML = gPictureDescriptions[index];
        }

        // Change the current picture label
        var spanTag = document.getElementById('CurrentPictureSpan');
        if (spanTag)
        {
            spanTag.innerHTML = (gCurrentPicture + 1);
        }
    }    
}