
// ------------------------------------------------------------
// Global variables
// ------------------------------------------------------------
var gArticleRows = new Array();

// ------------------------------------------------------------
// Called from the onload-event on the body-tag
// ------------------------------------------------------------
function pageLoaded()
{
    adaptArticleRows();
    adaptMainDivs();
}


// ------------------------------------------------------------
// 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');
    var rightDivLine   = document.getElementById('rightarealine');

    // 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;
    if(rightDivLine && rightDivLine.clientHeight > height) height = rightDivLine.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';
    if (rightDivLine) rightDivLine.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';
            }
        }
    }
}

// ------------------------------------------------------------
// 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;
	}

	var contentdiv    = document.getElementById('mainareadiv');
	var footerdiv     = document.getElementById('sitefooterdiv');
    var printableModuleHeader	= $('.addToPrintHeader');
    var printableModuleBody		= $('.addToPrintBody');
    
    var hasModule = printableModuleHeader.length>0&&printableModuleHeader.length==printableModuleBody.length;
    var moduleHtml = '';
    if(hasModule){
		moduleHtml = '<div id="rightarealine" style="background-image: url(/images/hr_staende.gif); WIDTH: 1px; background-repeat: repeat-y; float: left; height: 80%;"></div><div class="printModuleWrapper">';
		$(printableModuleHeader).each(function(index){
			
			moduleHtml +='<div class="printModuleDiv" ><div class="printModuleHeader"><h1 class="h1PrintModule">'+$(this).html() + '</h1></div><br/><div class="printModuleBody">' +$(printableModuleBody[index]).html() +'</div></div>'
		});
		moduleHtml += '</div>';
    }
	if (contentdiv)
	{
       isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1; 
       if(isIE6){
			var logoHtml = '<div style="float: right; padding-left: 0px; padding-right: 28px; margin-bottom:5px;"><img src="/images/printlogo.gif" alt="" width="250" /></div>'
       }
       else{
			var logoHtml = '<div style="float: right; padding-left: 0px; padding-right: 28px; margin-bottom:5px;"><img src="/images/printlogo.png" alt="" width="250" /></div>'
       }
        logoHtml += '<div style="float: left; padding-left: 4px; padding-top: 20px; height: 34px;"> En utskrift fr&aring;n sigtunahem.se </div><div id="rightarealine" style="background-image: url(/images/hr_liggande.gif); WIDTH: 99%; background-repeat: repeat-x; float: left; height: 2px; margin-left: 0px;"></div><p><br /><br /><br /></p>';	
		var contentHtml    = hasModule? '<div class="mainPrintContentWrapper" >'+ contentdiv.innerHTML + '</div>':contentdiv.innerHTML;
        var footerHtml     = footerdiv     ? '<div class="printFooterDiv"><p><br /><br /></p><div id="rightarealine" style="background-image: url(/images/hr_liggande.gif); WIDTH: 90%px; background-repeat: repeat-x; float: left; height: 2px;"></div><br />' + footerdiv.innerHTML +'</div>' : '';
		
		var mainWidth = hasModule? '470px':'540px';
		var beginHtml = 
		      '<html>' +
			  '<head>' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/structure.css" media="all" />' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/main.css" media="all" />' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/units.css" media="all" />' +
			  '<style> .PrintExclude { visibility: hidden; position: absolute; top: 0px; height: 0px } #minimaindiv{width: '+mainWidth+'; margin-left: 0px; margin-right: 0px;}</style>' +
			  '<title>Sigtunahem - ' + pageName + '</title>' +
			  '</head>' +
			  '<body style="margin: 5px 0px 5px 0px;background:none;background-color:#ffffff;">';
				
		var endHtml = '</body></html>';

		var printWin = window.open('about:blank','','width=720,height=600,scrollbars=yes,toolbar=yes');
		printWin.document.open();
		printWin.document.write(beginHtml + 
		                        logoHtml +
		                        contentHtml + 
		                        moduleHtml + 
		                        footerHtml + 
		                        endHtml);
		                        
		printWin.document.close();

		printWin.print();
	}
}


function openPrintWindow(url)
{
    w = window.open(url, 'PrintWindow', 'width=666,height=614,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=yes,status=yes');
    w.focus();
}


// ---------------
// Display the specified picture. 
// Used to browse pictures on the object page.
// ---------------
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);
        }
    }    
}

// ---------------
// Open window for viewing all pictures
// ---------------
function openPictureWindow(url)
{
    w = window.open(url, 'PictureViewWindow', 'location=no,scrollbars=yes,menubar=yes,toolbar=yes,resizable=yes');
    w.focus();
}

// ---------------
// Open SendTip-window
// ---------------
function TipPage(url)
{
    w = window.open(url, 'PrintOfficeWindow', 'width=340,height=600,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=no,status=yes');
    w.focus();
}

//----------------
// Shows a YouTube video
//----------------
function showYoutubeVideo(url, insertIntoID, title, autoplay, width, height) {
    var swfObject;

    if (autoplay == true)
        autoplay = "&autoplay=1;"
    else
        autoplay = "";
	
    swfObject = new SWFObject(url + "&enablejsapi=1&playerapiid=ytplayer&color1=0x006699&color2=0x54abd6&rel=0" + autoplay, "ytapiplayer", width, height, "8", "#FFFFFF");
    swfObject.addParam("wmode", "transparent");
    swfObject.write(insertIntoID);
    
    adaptMainDivs();
}

function setBgColor(element, color)
{
    if(element)
    {
        element.style.backgroundColor = color;
    }
}
