
// ------------------------------------------------------------
// 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';
}

function HideAndShow(elementToHide, elementToShow)
{
    document.getElementById(elementToHide).style.display = 'none';
    document.getElementById(elementToShow).style.display = 'block';
}
// ------------------------------------------------------------
// 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=\"/styles/structure.css\"><link rel=\"stylesheet\" type=\"text/css\" href=\"/styles/editor.css\"><link rel=\"stylesheet\" type=\"text/css\" href=\"<%=Configuration.RootDir%>styles/units.css\"></head><body style=\"padding:10px;\" 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 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();
}



//myField accepts an object reference, myValue accepts the text strint to add 
function insertAtCursor(id, myValue) { 
    var myField =document.getElementById(id);

    //IE support 
    if (document.selection) 
    { 
        myField.focus(); 

        sel = document.selection.createRange(); 
        sel.text = myValue; 

    } 
    //Mozilla/Firefox/Netscape 7+ support 
    else if (myField.selectionStart || myField.selectionStart == '0') 
    { 
        var startPos = myField.selectionStart; 
        var endPos = myField.selectionEnd; 
        myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length); 
    } 
    else 
    { 
        myField.value += myValue; 
    } 
} 



function uploadImg(imgID, pageID,fileName,imgPathID)
{
    if(fileName)
    {
        var pathToDialog = '/Pages/UploadPictureDialog.aspx?sid='+pageID+'&fileName='+fileName;
        
        var imgP =window.showModalDialog(pathToDialog,null, 'dialogWidth:420px;dialogHeight:220px;help=no;resizable:yes;scroll:no;status:yes;unadorned:yes;');
        
      
        document.getElementById(imgID).src=imgP;//För att visa bild
        document.getElementById(imgPathID).value=imgP;//Sökvägen till bilden i hidenfield
        document.getElementById(imgID).style.display=imgP?'block':'none';
    }
    else
    {
        alert('Ange en titel');
    }
}

var big = false;
function changeFont()
{   if(!big){
        document.getElementsByTagName("body")[0].style.fontSize = "0.9em";
        big=true;
    }
    else{
        big=false;
        document.getElementsByTagName("body")[0].style.fontSize = "0.7em";
    }
        
}
