// ------------------------------------------------------------
// Global variables
// ------------------------------------------------------------
var gArticleRows = new Array();

// ------------------------------------------------------------
// Called from the onload-event on the body-tag
// ------------------------------------------------------------
adaptArticleRows();
adaptMainDivs();
function pageLoaded()
{
    expandMiddleModules();
    expandPictureBox();
}
function KeyPress(e,pageName, appRoot)
{
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		if(event.keyCode == 17)
		{
			document.getElementById("printKeys").value = e.keyCode;
		}
		else if(event.keyCode == 80)
		{
			if(document.getElementById("printKeys").value == 17)
			{
				printPage(pageName, appRoot);
				document.getElementById("printKeys").value = '';
			}
		}
		else
		{
			document.getElementById("printKeys").value = e.keyCode;
		}
	}
}
// ------------------------------------------------------------
// 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 leftBorderDiv = document.getElementById('leftmenuborderdiv');

    if (leftDiv && mainDiv && leftBorderDiv)
    {        
        // min-height
        var height = 350;
        
        // Find the highest div
        if (leftDiv.clientHeight > height) height = leftDiv.clientHeight;
        if (mainDiv.clientHeight > height) height = mainDiv.clientHeight;

        // Set new height
        mainDiv.style.height = height + 'px';
        
		// leftBorderDiv and leftExpandBorderDiv are contained within leftDiv and 
		// contains the border for the left menu so we need to re-size them explicitly 
		// to make the border the right height. 
		// leftExpandBorderDiv is only used on page types that override the left menu 
		// area to put something below the left menu (eg banners)
		// The 26 pixels are for the leftmenu header which are included in leftDiv but
		// not in leftBorderDiv.        
        var leftExpandBorderDiv = document.getElementById('leftmenuexpandborderdiv');
        if (leftExpandBorderDiv)
        {        
			leftExpandBorderDiv.style.height = (height - 26 - leftBorderDiv.clientHeight) + 'px';
		}
		else
		{
			leftBorderDiv.style.height = (height - 26) + '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';
            }
        }
    }
}

// ------------------------------------------------------------
// 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;
    }
}

// ------------------------------------------------------------
// Builds an html-page for printing
// ------------------------------------------------------------
function printPage(pageName, appRoot) 
{
	if (!window.print)
	{
		window.status = 'No print';
		return;
	}

	// Get the main content area and other stuff we need
	var breadcrumbdiv = document.getElementById('breadcrumbdiv');
	var contentdiv    = document.getElementById('contentdiv');
	var footerdiv     = document.getElementById('sitefooterdiv');

	if (contentdiv)
	{
        var logoHtml = '<div style="float: left; padding-left: 20px;"><img src="' + appRoot + 'images/logo_printable.gif" alt="" /></div>'
		var breadcrumbHtml = breadcrumbdiv ? breadcrumbdiv.innerHTML : '';
        logoHtml += '<div style="float: right; padding-right: 20px;">' + breadcrumbHtml + '</div><hr style="clear: both;"></hr><br />';
		var contentHtml    = contentdiv.innerHTML;
        var footerHtml     = footerdiv     ? '<hr>' + footerdiv.innerHTML : '';

		var beginHtml = 
		      '<html>' +
			  '<head>' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/structure.css">' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/main.css">' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/units.css">' +
			  '<style> .PrintExclude { visibility: hidden; position: absolute; top: 0px; height: 0px } </style>' +
			  '<title>Flens kommun - ' + pageName + '</title>' +
			  '</head>' +
			  '<body style="margin: 10px 0px 10px 0px">';

		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 + 
		                        logoHtml +
		                        contentHtml + 
		                        footerHtml + 
		                        endHtml);
		printWin.document.close();

		printWin.print();
	}
}

// ------------------------------------------------------------
// Opens the Tipsa-window
// ------------------------------------------------------------
function openTipPage(url)
{
    w = window.open(url, 'TipWindow', 'width=340,height=450,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=no,status=yes');
    w.focus();
}

// ------------------------------------------------------------
// Opens the DatePicker-window
// ------------------------------------------------------------
function openDatePicker(fieldId, evt)
{
    var field = document.getElementById(fieldId);
    if (field)
    {
        if (evt)
        {
            var left = (typeof window.event == 'object') ? window.event.x : evt.pageX;
            var top  = (typeof window.event == 'object') ? window.event.y : evt.pageY;
        }
        else
        {
            var left = (screen.width  - 250) / 2;
            var top  = (screen.height - 190) / 2;
        }

        var win = window.open(gAppRoot + 'Pages/DatePicker.aspx?field=' + fieldId, 'calendarPopup', 'top=' + top + ',left=' + left + ',width=250,height=190,resizable=yes,status=yes');
        win.focus();
    }
}

// ------------------------------------------------------------
// Opens special print window for the event list
// ------------------------------------------------------------
function openEventListPrint(url)
{
    window.open(url, 'EventPrintWindow', 'width=700,height=600,scrollbars=yes,toolbar=yes');
}

function expandMiddleModules()
{
	if (document.getElementById('MiddleModule1') &&
		document.getElementById('MiddleModule2') &&
		document.getElementById('MiddleModule3') &&
		document.getElementById('MiddleModule4'))
	{
		var div1 = document.getElementById('MiddleModule1').clientHeight;
		var div2 = document.getElementById('MiddleModule2').clientHeight;
		var div3 = document.getElementById('MiddleModule3').clientHeight;
		var div4 = document.getElementById('MiddleModule4').clientHeight;
		
		if(div1 > div2)
		{
			document.getElementById('MiddleModule2').style.height = div1 +"px";
		}
		if(div2 > div1)
		{
			document.getElementById('MiddleModule1').style.height = div2+"px";
		}
		if(div3 > div4)
		{
			document.getElementById('MiddleModule4').style.height = div3 +"px";
		}
		if(div4 > div3)
		{
			document.getElementById('MiddleModule3').style.height = div4+"px";
		}
	}
}
function expandPictureBox()
{
	if (document.getElementById('mainContentDiv') && 
		document.getElementById('LeftMenu') &&
		document.getElementById('PictureBoxDiv'))
	{
		var mainContent = document.getElementById('mainContentDiv').clientHeight;
		var leftMenu = document.getElementById('LeftMenu').clientHeight;
		document.getElementById('PictureBoxDiv').style.height = (mainContent-leftMenu) + "px";
	}
}

