//Login functionality
var loginToServer = function() {
	dojo.xhrPost({
    url : '/names.nsf?login',
    handleAs : "text",
    preventCache : true,
    content: {
    	"UserName": dojo.byId("loginusername").value ,
    	"Password": dojo.byId("loginpassword").value
    },
    load : function(response, ioArgs) {
    		if (response.indexOf('reasonType')==-1) {
    				window.location.reload();
    			
    		} else {
    			dojo.byId('loginMessage').innerHTML = "Du har angitt et ugyldig brukernavn eller passord, eller mangler tilstrekkelig tilgangsrettigheter.";
    		}
    },
    error : function(response, ioArgs) {
        dojo.byId('loginMessage').innerHTML = "Du har angitt et ugyldig brukernavn eller passord, eller mangler tilstrekkelig tilgangsrettigheter.";
    }
});
}

//Funksjon som viser e-post adresse kryptet
function showAdr( linkTxt , pre , post , dom )
{
	document.write('<a href=mailTo:' + pre + '@' + post + '.' + dom + '>');
	if ( linkTxt == "" )
	{
    	document.write( pre + '@' + post + '.' + dom + '</a>');
    } else {
    	document.write( linkTxt + '</a>');
    }
}

//Open Dojo dialog
function showImage( img , content )
{
	var img = dojo.byId( img );
	var dialog = new dijit.Dialog({
            title: img.title ,
            style: "width: 820px" ,
            duration: 500 ,
            href: content
        });
	dialog.show();
}	

//Open Dojo dialog
function showInDialog( img , src , cssClass , w )
{
	var cont = "";
	if ( src == "" )
	{
		cont = "<img src='" + img.src + "' class='" + cssClass + "' alt='" + img.title + "' title='" + img.title + "' />" 
	} else {
		cont = "<img src='" + src + "' class='" + cssClass + "' alt='" + img.title + "' title='" + img.title + "' />"
	}
	var dialog = new dijit.Dialog({
            title: img.title ,
            style: "width: " + w + "px" ,
            duration: 500 ,
            content: cont
        });
	dialog.show();
}	
//Initialize onclick events to submenus (keep open)
function initMenu()
{
	var param = getUrlPara( "page" );
	if ( param != "" )
	{
		var m = dojo.byId( param );
		if ( m != null )
		{
			var parent = m.parentNode;
			dojo.addClass( parent.parentNode , 'active' );
			dojo.addClass( m , "selected" );
		}
	}
}

//Function to extract url parameter
function getUrlPara( p ) 
{
	var regexS = "[\\?&]" + p + "=([^&#]*)";
	var regex = new RegExp( regexS );
	var res = regex.exec( window.location.href );
	if( res == null )
	{
		return "";
  	} else {
		return res[1];
	}
}

//Set current menu item active, remove active from others
function setActive2( e )
{
	e = e || window.event;
	var node = e.target || e.srcElement;
	if( node.className == "levTop" )
	{
		dojo.query( "#navigator .active" ).removeClass( "active" );
		dojo.addClass( node , "active" );
	}
}

//Set current menu item active, remove active from others
function setActive( e )
{
	e = e || window.event;
	//var node = e.target || e.srcElement;
	var node = this;
	if( node.className == "levTop" )
	{
		var nodeChild = dojo.query( '> ul' , node )[0];
		var aNode = dojo.query( "#navigator .active" )[0];
		
		//Open selected
		var explode = dojo.animateProperty({node : nodeChild,
            properties : {
               height: { end: nodeChild.scrollHeight , start:0 }
            }
        });
        
        //If other open, close this
		if( aNode != null )
		{
			var aNodeChild = dojo.query( '> ul' , aNode )[0];
			
			var implode = dojo.animateProperty({node : aNodeChild,
    	        properties : {
        	      height: { end: 0, start: aNodeChild.scrollHeight }
            	}
        	});
    		var openClose = dojo.fx.chain([explode,implode]);
    		openClose.play()
    		dojo.removeClass( aNode , "active" );
    	} else {
    		explode.play();	
    	}
	
		dojo.addClass( node , "active" );
	}
}

//Map sorting functions
function loadMenu()
{
	dojo.query( '#navigator .levTop' ).onclick( setActive );
	//Set active menu
	var param = getUrlPara( "page" );
	if ( param != "" )
	{
		var m = dojo.byId( param );
		if ( m != null )
		{
			var parent = m.parentNode;
			parent.style.height=parent.scrollHeight+'px';
			dojo.addClass( parent.parentNode , 'active' );
			dojo.addClass( m , "selected" );
		}
	}
}

//Animation of boxes, expand - collapse
function animate(elementID, newLeft, newTop, newWidth, newHeight, time, callback)
{
  var el = document.getElementById(elementID);
  if(el == null)
    return;
 
  var cLeft = parseInt(el.style.left);
  var cTop = parseInt(el.style.top);
  var cWidth = parseInt(el.style.width);
  var cHeight = parseInt(el.style.height);
 
  var totalFrames = 1;
  if(time > 0)
    totalFrames = time/40;

  var fLeft = newLeft - cLeft;
  if(fLeft != 0)
    fLeft /= totalFrames;
 
  var fTop = newTop - cTop;
  if(fTop != 0)
    fTop /= totalFrames;
 
  var fWidth = newWidth - cWidth;
  if(fWidth != 0)
    fWidth /= totalFrames;
 
  var fHeight = newHeight - cHeight;
  if(fHeight != 0)
    fHeight /= totalFrames;
   
  doFrame(elementID, cLeft, newLeft, fLeft, cTop, newTop, fTop,
      cWidth, newWidth, fWidth, cHeight, newHeight, fHeight, callback);
}

function doFrame(eID, cLeft, nLeft, fLeft, cTop, nTop, fTop,
    cWidth, nWidth, fWidth, cHeight, nHeight, fHeight, callback)
{
  var el = document.getElementById(eID);
  if(el == null)
    return;

  cLeft = moveSingleVal(cLeft, nLeft, fLeft);
  cTop = moveSingleVal(cTop, nTop, fTop);
  cWidth = moveSingleVal(cWidth, nWidth, fWidth);
  cHeight = moveSingleVal(cHeight, nHeight, fHeight);

  el.style.left = Math.round(cLeft) + 'px';
  el.style.top = Math.round(cTop) + 'px';
  el.style.width = Math.round(cWidth) + 'px';
  el.style.height = Math.round(cHeight) + 'px';
 
  if(cLeft == nLeft && cTop == nTop && cHeight == nHeight
    && cWidth == nWidth)
  {
    if(callback != null)
      callback();
    return;
  }
   
  setTimeout( 'doFrame("'+eID+'",'+cLeft+','+nLeft+','+fLeft+','+cTop+','
      +nTop+','+fTop+','+cWidth+','+nWidth+','+fWidth+','+cHeight+','
      +nHeight+','+fHeight+','+callback+')', 40);
}

function moveSingleVal(currentVal, finalVal, frameAmt)
{
  if(frameAmt == 0 || currentVal == finalVal)
    return finalVal;
 
  currentVal += frameAmt;
  if((frameAmt > 0 && currentVal >= finalVal)
      || (frameAmt < 0 && currentVal <= finalVal))
  {
    return finalVal;
  }
  return currentVal;
}
