function getDimension(num)
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if(num == 1)
  {
  return myWidth;
  }
  else if(num==2)
  {
  return myHeight;
  }
}

function doPopup(img, iwidth, iheight)
{
	//page and popup size configuration
	var _docWidth = getDimension(1);
	var _docHeight = getDimension(2);
	
	var width = (_docWidth)
	var height = (_docHeight-50);
	
	if(height > iheight)
	{
		height = iheight;
		width = parseInt((iwidth*height)/iheight);
	}
	width = 5+(iwidth*height)/iheight;
	if(width > iwidth)
	{
		width = iwidth;
		iheight = parseInt((width*iheight)/iwidth);
	}
	
	var popmargin = (_docWidth-width)/2;
	
	var popcontent = document.getElementById("popupcontent");
	popcontent.innerHTML = getFile('image.php?id='+img+'&height='+height+'&width='+width+'&page='+CurrentModule);
	
	var container = document.getElementById("popupcontainer");
	
	container.style.height = height+"px";
	container.style.width = width+"px";
	container.style.left = popmargin+"px";
	container.style.top = 25+"px";
	container.style.display = "block";
	
	var close = document.getElementById("floatcloseimage");
	close.style.left = (width-42)+"px";
	close.style.top = (10)+"px";
	
	var close = document.getElementById("floatsaveimage");
	close.style.left = (0)+"px";
	close.style.top = (0)+"px";
	
	var close = document.getElementById("floatnextimage");
	close.style.left = (width-30)+"px";
	close.style.top = (height-48)+"px";
	
	var close = document.getElementById("floatpreviousimage");
	close.style.left = (0)+"px";
	close.style.top = (height-48)+"px";
	
}

function undoPopup()
{
	var container = document.getElementById("popupcontainer");
	container.style.display = "none";
}

function descPopupOn()
{
	var desc = document.getElementById("popupdesc");
	desc.style.filter="alpha(opacity=100)";
	desc.style.opacity="1.0";
}

function descPopupOff()
{
	var desc = document.getElementById("popupdesc");
	desc.style.filter="alpha(opacity=20)";
	desc.style.opacity="0.2";
}

function getFile(fileName){
    oxmlhttp = null;
    try{
        oxmlhttp = new XMLHttpRequest();
        oxmlhttp.overrideMimeType("text/xml");
    }
    catch(e){
        try{
            oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e){
            return null;
        }
    }
    if(!oxmlhttp) return null;
    try{
       oxmlhttp.open("GET",fileName,false);
       oxmlhttp.send(null);
    }
    catch(e){
       return null;
    }
    return oxmlhttp.responseText;
}

function fireEvent(element,event)
{
	if (document.createEventObject)
	{
		// dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on'+event,evt);
	}
	else
	{
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
}

function galleryKeyPress(actualkey)
{
	if (actualkey=="'") //right
	{
		fireEvent(document.getElementById("floatnext"),'click');
	} 
	if (actualkey=="%") //left
	{
		fireEvent(document.getElementById("floatprevious"),'click');
	}
}


//
//
//
// Inactivity Check
//
//
//

function keyaction(e)
{
	var evtobj=window.event? event : e; //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
	galleryKeyPress(String.fromCharCode(unicode));
	setTime();
}

function mouseaction(e)
{
	setTime();
}

function setTime()
{
	var Time = new Date();
	ExpireTime = Time.valueOf()+(Inactivity*1000);
	createCookie('JavascriptInactivityTimer','1',Inactivity);
}

function doInactivityWarning()
{
	var _docWidth = getDimension(1);
	
	var width = (_docWidth-200)
	
	var popmargin = (_docWidth-width)/2;
	
	var iw = document.getElementById("inactivitywarning");
	
	iw.style.width = width+"px";
	iw.style.left = popmargin+"px";
	iw.style.top = 75+"px";
	iw.style.display = "block";

}

function undoInactivityWarning()
{
	var iw = document.getElementById("inactivitywarning");
	iw.style.display = "none";
}

function checkExpiration()
{
	var Time = new Date();
	if(Time.valueOf() > ExpireTime)
	{
		window.location = '?logoutinactive=1';
	}
	else if(Time.valueOf()+(WarnTime*1000) > ExpireTime)
	{
		setTimeout ( checkExpiration, CheckRate*1000);
		doInactivityWarning();
	}
	else
	{
		setTimeout ( checkExpiration, CheckRate*1000);
		undoInactivityWarning();
	}
}

function createCookie(name,value,seconds) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(seconds*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

document.onkeypress=keyaction;
document.onclick=mouseaction;

setTime();
if(LoggedIn == 1)
{
	checkExpiration();
}
