<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ ?>
<div class="admin" id="admin">
  <span class="header windowtitle">Admin</span>
  <div class="button close"><a href="?page=">X</a></div>
<?php
global $loggedin;

if($loggedin == true) {
?>
<?php
global $DATA_DIR;
  $users = new Users($DATA_DIR . "/users.xml");
  if($userid) $UID = $userid;
  else $UID = $HTTP_COOKIE_VARS["UserID"];
  $user = $users->findUser($UID);
?>
<a class="logout" href="?page=admin&amp;action=logout">Logout <span class="user"><?php echo $UID;?></span></a>
<div class="menu">
<?php
include_once($UTIL_DIR . "/modules.php");

loadAllModules();

foreach($modules as $modulename => $module) {
  if($user->checkModule($modulename) == false) continue;
  $active = "";
  if($m == $modulename) {
    $admin_module = $module;
    $admin_modulename = $modulename;
    $active = " active";
  }
  echo "<a class=\"entry$active\" href=\"?page=admin&amp;m=$modulename\">";
  echo $module->admin_title;
  echo "</a>\n";
}
?>
</div>
<div class="submenu">
<?php
if($admin_module && $user->checkModule($admin_modulename)) {
  foreach($admin_module->admin_submodules as $submodulename => $submodule) {
		$active = "";
    if($s == $submodule) {
      $admin_submodule = $s;
      $admin_submodule_name = $submodulename;
			$active = " active";
    }
    echo "<a class=\"entry$active\" href=\"?page=admin&amp;m=$m&amp;s=$submodule\">";
		echo $submodulename;
		echo "</a>\n";
  }
}
?>
</div>
<div class="form">
<?php
if($admin_submodule) {
  echo "<div class=\"header\">$admin_submodule_name</div>\n";
	include_once($UTIL_DIR . "/convert.php");
  $admin_module->admin($admin_submodule, $a, convert_array($vars));
}
?>
</div>
<?php
} else { // logged in?
?>
<div class="form">
<form action="?page=admin&amp;action=login" method="post">
<p>UserID: <input name="userid"/></p>
<p>Password: <input name="password" type="password"/></p>
<p><button type="submit">Login</button></p>
</form>
</div>
<?php
}
?>
</div>
<script language="JavaScript" type="text/javascript"> 
<!--

function ExtractNumber(value)
{
    var n = parseInt(value);
	
    return n == null || isNaN(n) ? 0 : n;
}

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

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

// this is simply a shortcut for the eyes and fingers
function $(id)
{
    return document.getElementById(id);
}

InitDragDrop();

function InitDragDrop()
{
	document.onmousedown = OnMouseDown;
	document.onmouseup = OnMouseUp;

	var x = readCookie('admin_x');
	var y = readCookie('admin_y');

  //  if(x > document.body.clientWidth) x = document.body.clientWidth - 30;
  //  if(y > document.body.clientHeight) y = document.body.clientHeight - 30;

	var _dragElement = document.getElementById('admin');

	_dragElement.style.left = x + 'px';
	_dragElement.style.top = y + 'px';
}

function OnMouseDown(e)
{
	// IE is retarded and doesn't pass the event object
	if (e == null) 
		e = window.event; 
    
	// IE uses srcElement, others use target
	var target = e.target != null ? e.target : e.srcElement;
    
	// for IE, left click == 1
	// for Firefox, left click == 0
	if ((e.button == 1 && window.event != null || e.button == 0) && target.className == 'admin') {
		// grab the mouse position
		_startX = e.clientX;
		_startY = e.clientY;
        
		// grab the clicked element's position
		_offsetX = ExtractNumber(target.style.left);
		_offsetY = ExtractNumber(target.style.top);
    
		// bring the clicked element to the front while it is being dragged
		_oldZIndex = target.style.zIndex;
    _oldOpacity = target.style.opacity;
		target.style.zIndex = 10000;
    //    target.style.opacity = 0.6;
    _setOpacity = true;

		// we need to access the element in OnMouseMove
		_dragElement = target;
		
		// tell our code to start moving the element with the mouse
		document.onmousemove = OnMouseMove;
    
		// cancel out any text selections
		document.body.focus();
		
		// prevent text selection in IE
		document.onselectstart = function () { return false; };
		// prevent IE from trying to drag an image
		target.ondragstart = function() { return false; };
    
		// prevent text selection (except IE)
		return false;
	}
}

function OnMouseMove(e)
{
	if (e == null) 
		var e = window.event; 
	
	// this is the actual "drag code"
	_dragElement.style.left = (_offsetX + e.clientX - _startX) + 'px';
	_dragElement.style.top = (_offsetY + e.clientY - _startY) + 'px';

  if(_setOpacity == true) {
    _dragElement.style.opacity = 0.6;
    _setOpacity = false;
  }
}

function OnMouseUp(e)
{
	if (_dragElement != null) {
		_dragElement.style.zIndex = _oldZIndex;
    _dragElement.style.opacity = _oldOpacity;

		// we're done with these events until the next OnMouseDown
		document.onmousemove = null;
		document.onselectstart = null;
		_dragElement.ondragstart = null;
		
		// this is how we know we're not dragging      
		_dragElement = null;
		
		if (e == null) 
			var e = window.event; 
		
		// Store new coordinate
		createCookie('admin_x', _offsetX + e.clientX - _startX, 1000);
		createCookie('admin_y', _offsetY + e.clientY - _startY, 1000);
	}
}
//-->
</script>