// ------------------------------------------------------------
// Global variables
// ------------------------------------------------------------
var gArticleRows = new Array();


// ------------------------------------------------------------
// Sets the height of all the children of the specified element
// to the height of the highest child.
// ------------------------------------------------------------
function alignChildHeight(parentId, 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 parentElement = document.getElementById(parentId);
    if (parentElement)
    {
        var maxHeight = minHeight;

        // 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';
            }
        }
    }
}




// ------------------------------------------------------------
// Changes the height of the main divs of the page so
// they are the same height.
// ------------------------------------------------------------
function adaptMainDivs( 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;
	    }

    // Get the divs
    var leftDiv  = document.getElementById('ctl00_FullContentRegion_LeftContentAreaDiv');
    var mainDiv  = document.getElementById('ctl00_FullContentRegion_MainContentAreaDiv');
    var rightDiv  = document.getElementById('ctl00_FullContentRegion_RightContentAreaDiv');
    
        // set new height
    if (leftDiv)  leftDiv.style.height  = '';
    if (mainDiv)  mainDiv.style.height  = '';
    if (rightDiv) rightDiv.style.height = '';

    // find the highest div
    if (leftDiv  && leftDiv.clientHeight  > minHeight) minHeight = leftDiv.clientHeight;
    if (mainDiv  && mainDiv.clientHeight  > minHeight) minHeight = mainDiv.clientHeight;
    if (rightDiv && rightDiv.clientHeight > minHeight)	minHeight = rightDiv.clientHeight;

    // set new height
    if (leftDiv)  leftDiv.style.height  = minHeight + 'px';
    if (mainDiv)  mainDiv.style.height  = minHeight + 'px';
    if (rightDiv) rightDiv.style.height = minHeight + 'px';
}

// ------------------------------------------------------------
// Changes the height of the main divs of the page so
// they are the same height.
// ------------------------------------------------------------
function adaptListBoxDivs(boxleft, boxbody, boxright, minHeight )
{

    // Get the divs
    var leftDiv  = document.getElementById(boxleft);
    var mainDiv  = document.getElementById(boxbody);
    var rightDiv  = document.getElementById(boxright);

    // reset new height
    if (leftDiv)  leftDiv.style.height  = '';
    if (mainDiv)  mainDiv.style.height  = '';
    if (rightDiv) rightDiv.style.height = '';

    // find the highest div
    if (leftDiv  && leftDiv.clientHeight  > minHeight) minHeight = leftDiv.clientHeight;
    if (mainDiv  && mainDiv.clientHeight  > minHeight) minHeight = mainDiv.clientHeight;
    if (rightDiv && rightDiv.clientHeight > minHeight)	minHeight = rightDiv.clientHeight;

    // set new height
    if (leftDiv)  leftDiv.style.height  = minHeight + 'px';
    if (mainDiv)  mainDiv.style.height  = minHeight + 'px';
    if (rightDiv) rightDiv.style.height = minHeight + '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 
// 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;
    }
}		




// ---------------
// Hides all windowed controls in browsers that
// can't display layers on top of them.
// ---------------
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';
		}
	}
}

// ---------------
// Shows all windowed controls.
// ---------------
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';
	    }
    }
}



// ---------------
// Opens a dialog. The first parameter should be the dialog name.
// ---------------
function openDialog()
{
	var dialogOrig	= document.getElementById(arguments[0]); 
	
	dialogOrig.style.display = 'block';
	
	var dialogBody  = document.getElementById('DialogBody');
	var dialog		= document.getElementById('DialogContainer');
	dialog.style.display = 'block';
	
	dialog.style.width = dialogOrig.clientWidth + 10 + 'px';
	
	dialogBody.innerHTML = dialogOrig.innerHTML;

	hideWindowedControls();
	positionDialog();

	dialogOrig.style.display = 'none';
	showDialogBackground();
	
}



// ---------------
// Creates a semi-transparent div and places it over the entire screen.
// ---------------
function showDialogBackground()
{
	var bgDiv = document.getElementById('DialogBG');
	var dialogContainer = document.getElementById('DialogContainer');
	if (bgDiv)
	{
	    var height = 0; // bgDiv.clientHeight;
	    if(height < document.getElementById('Page').clientHeight) height = document.getElementById('Page').clientHeight;
	    
	    if(height < dialogContainer.clientHeight)
    	    height = dialogContainer.clientHeight;
	    
	    
	    height = height + scrollOffset.y;
	    
		bgDiv.style.height	= height + 'px';
		bgDiv.style.display	= 'block';

	}
}

// ---------------
// Hides the semi-transparent div
// ---------------
function hideDialogBackground()
{
	var bgDiv = document.getElementById('DialogBG');
	if (bgDiv)
	{
		bgDiv.style.display = 'none';
	}
}



// ---------------
// Positions the dialog layer in the center of the screen.
// ---------------
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';    		 		
	}
}


// ---------------
// Closes the dialog-layer.
// ---------------
function closeDialog()
{
	var dialog		= document.getElementById('DialogContainer');
	if (dialog)
	{
		dialog.style.display = 'none';
				
		hideDialogBackground();
		showWindowedControls();
	}
}















// ---------------
// Returns the size of the viewport.
// ---------------
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;
}

// ---------------
// Returns the scroll offset
// ---------------
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;
}


