
// ------------------------------------------------------------
// Called from the onload-event on the body-tag
// ------------------------------------------------------------
function pageLoaded()
{
    adaptMainDivs();
}


// Makes sure the three main divs are the same height.
function adaptMainDivs()
{
    var leftDiv		        = document.getElementById('leftareadiv');
    var mainDiv		        = document.getElementById('mainareadiv');
    var rightDiv	        = document.getElementById('rightareadiv');
    var rightPaddingDiv	    = document.getElementById('rightareapaddingdiv');
 
    // 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;
  
    // 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 (rightPaddingDiv) rightPaddingDiv.style.height = (height-2) + '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('footerdiv');

	if (contentdiv)
	{
        var logoHtml = '<div style="float: left; padding-left: 0px;"><img src="' + appRoot + 'images/avantime/logotyp.gif" alt="" /></div>'
//		var breadcrumbHtml = breadcrumbdiv ? breadcrumbdiv.innerHTML : '';
        logoHtml += '<div style="float: right; padding-right: 0px; padding-top: 20px; height: 34px;"> En utskrift från www.redcross.se </div><hr style="clear: both; height: 2px;"></hr><br />';
		var contentHtml    = contentdiv.innerHTML;
        var footerHtml     = footerdiv     ? '<hr style="height: 2px;">' + 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/Units.css">' +
			  '<link rel="stylesheet" type="text/css" href="' + appRoot + 'styles/Editor.css">' +
			  '<style> .PrintExclude { visibility: hidden; position: absolute; top: 0px; height: 0px } #minimaindiv{width: 570px; margin-left: 0px; margin-right: 0px;}</style>' +
			  '<title>RödaKorset - ' + pageName + '</title>' +
			  '</head>' +
			  '<body style="margin: 10px 50px 10px 50px; 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 + 
		                        footerHtml + 
		                        endHtml);
		printWin.document.close();

		printWin.print();
	}
}



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();
}

function closeDiv(id)
{ 
    
    var div	   = document.getElementById(id);
    div.style.display='none';
 
    
   
}
function openDiv(id)
{
    var div	   = document.getElementById(id);    
    div.style.display='block';
}



function openPopUppDiv(id)
{
    var articlediv = document.getElementById('articlediv');
    var div	   = document.getElementById(id);   
    div.style.display='block';
    div.style.left=  (( width()/2 )-( div.clientWidth/2 ))+ 'px';
    //alert(findPosY('articlediv'));
}
//function findPosY(obj)
//  {
//    var curtop = 0;
//    if(obj.offsetParent)
//        while(1)
//        {
//          curtop += obj.offsetTop;
//          if(!obj.offsetParent)
//            break;
//          obj = obj.offsetParent;
//        }
//    else if(obj.y)
//        curtop += obj.y;
//    return curtop;
//  }

  

//We need to now how menu divs that are open,
//So we dont run the top flash when we still have a open div
var countOpenDivs;

var pathToTopFLashXml;
var topFlashDiv;
function runTopFlash(divId,pathToXml){
    //If countOpenDivs is null sett to 0    
    if(!countOpenDivs)
        countOpenDivs=0;
    
    //We cant run the flash if we still have a open div    
     if (countOpenDivs>0)
        return; 
    
    if(!pathToTopFLashXml)
        pathToTopFLashXml=pathToXml;
    
    if(!topFlashDiv)
        topFlashDiv=divId;
    
      
    runFlash(divId,pathToTopFLashXml, '/Flash/bildspel_hem.swf', '355', '285', '7', '#FFFFFF') ;
}

function runPopUppFLash(divId,pathToXml,topFlashDiv){
   
    runFlash(divId,pathToXml, '/Flash/bildspel_popup.swf', '750', '510', '7', '#000000') ;  
    
    var div	   = document.getElementById(divId);   
    div.style.display='block';
    div.style.left=  (( width()/2 )-( div.clientWidth/2 ))+ 'px';
    
    
   
}


function runFlash(divId, pathToXml, pathToFlash, width, hight, version, bgColor)
{
    var so = new SWFObject(pathToFlash, "RedCross", width, hight, version, bgColor);
    so.addVariable("xmlUrl", pathToXml);
    so.addParam("wmode", "transparent");
    so.addVariable("divToHide", divId);
    so.write(divId);
}

function width()
{
	var frameWidth=0;
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
	}
	return frameWidth;
}