
// ------------------------------------------------------------
// 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';
}


// ------------------------------------------------------------
// 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 printPage(id, skinCss) 
{

	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\" media=\"screen\" href=\""+skinCss+"\"/><link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"/Templates/Public/Styles/Glossy/Styles.css\"/></head><body style='padding:10px;font-size:9px; text-align: left;' onload='javascript:window.print();'>";
	
        var sStop = "</body></html>";
   
		var w = window.open('','Skrivut','width=650,height=440,scrollbars=1');

     
	    w.document.open();
	    w.document.write(sStart + printArea.innerHTML) ;
	    w.document.writeln( sStop );
	    w.document.close();	

    }
    
}
function TipPage(id)
{
    w = window.open('/pages/TipFriend.aspx?PageId='+id, 'PrintOfficeWindow', 'width=340,height=600,location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=no,status=yes');
    w.focus();
}

function SwitchText(control, oldText)
{
    if(control.value == oldText)
        control.value = '';
    else if(!control.value)
        control.value = oldText;
      
}


function ChangeColor(obj, textColor, bgColor)
{   
    if(obj && obj.style){
        obj.style.backgroundColor=bgColor; 
        obj.style.color=textColor;
        if(obj.childNodes[0].childNodes[0] && obj.childNodes[0].childNodes[0].style)
            obj.childNodes[0].childNodes[0].style.color=textColor;
        
        if(obj.childNodes[1].childNodes[0] && obj.childNodes[1].childNodes[0].style)
            obj.childNodes[1].childNodes[0].style.color=textColor;
    }
}

function togleDisplaySearch(id)
{
    var block = document.getElementById('block_'+id);
    var togle= document.getElementById('togle_'+id);
    
    if(block.style.display=='none')
    {
        block.style.display='block';
        togle.innerHTML  ='D&#246;lj';
    }
    else
    {
        block.style.display='none';
        togle.innerHTML  ='Visa allt';
    }
}

function togleSelectGrape(id)
{
    var togle= document.getElementById(id);
    
    if(togle.style.display=='none')
        togle.style.display='block';
    else
        togle.style.display='none';
    
}


	
	



function setDispRow(id,clear,name)
{
    if(clear)
    {
        document.getElementById(id).innerHTML='Mina vinlistor';
    }
    else
    {
        document.getElementById(id).innerHTML='Visa vinslistan: '+name;
    }
}
function setDispActionRow(id,clear,name,action)
{
    if(clear)
        document.getElementById(id).innerHTML='&nbsp;';
    else
        if(action=='False')
            document.getElementById(id).innerHTML='L&auml;gg till vin i listan: '+name;
        else
            document.getElementById(id).innerHTML='Ta bort vin fr&aring;n listan: '+name;
}




/*** *** ***/
	/* xMenu1A Object Prototype

	  Parameters:
	    triggerId   - id string of trigger element.
	    menuId      - id string of menu.
	    mouseMargin - integer margin around menu;
	                  when mouse is outside this margin the menu is hid.
	    slideTime   - integer time for menu slide (in milliseconds).
	    openEvent   - string name of event on which to open menu ('click', 'mouseover', etc).
	*/

	function xMenu1A(triggerId, menuId, mouseMargin, slideTime, openEvent)
	{
	
	  var isOpen = false;
	  var trg = xGetElementById(triggerId);
	  var mnu = xGetElementById(menuId);
	  if (trg && mnu) {
	    xHide(mnu);
	    xAddEventListener(trg, openEvent, onOpen, false);
	}
	  function onOpen()
	  {
	    if (!isOpen) {
		
		//	alert(trg.clientHeight);
	     // xMoveTo(mnu, (xPageX(trg)), (xPageY(trg)+h+2));
	      xShow(mnu);
	     // xSlideTo(mnu, xPageX(trg), xPageY(trg) + xHeight(trg), slideTime);
	      xAddEventListener(document, 'mousemove', onMousemove, false);
	      isOpen = true;
	    }
	  }
	  function onMousemove(ev)
	  {
	    var e = new xEvent(ev);
	    if (!xHasPoint(mnu, e.pageX, e.pageY, -mouseMargin) &&
	        !xHasPoint(trg, e.pageX, e.pageY, -mouseMargin))
	    {
	      xRemoveEventListener(document, 'mousemove', onMousemove, false);
	     // xSlideTo(mnu, xPageX(trg), xPageY(trg), slideTime);
	      setTimeout("xHide('" + menuId + "')", slideTime);
	      isOpen = false;
	    }
	  }
	} // end xMenu1A
	
	