//functions for dragging WebAuthor
var objConsole
var ConsolePaintTimer = "";
var dragOn=0
var dragInit=0;
var dragDiv=null;
var exp = new Date(); 
exp.setTime(exp.getTime() + (1*24*60*60*1000));

function InitConsole(){

if(document.getElementById('Console') != null){

	var intConsoleXPos
	var intConsoleYPos

	if(GetCookie("ConsoleXPos") == null){
		intConsoleXPos = 10
		intConsoleYPos = 10
	} else {
		intConsoleXPos = parseInt(GetCookie("ConsoleXPos"));
		intConsoleYPos = parseInt(GetCookie("ConsoleYPos"));
	}
	
	objConsole = new Console()

	var curXpos = objConsole.window.width();
	
	if(curXpos < intConsoleXPos){
		intConsoleXPos = 10;
		intConsoleYPos = 10;
	}
	
	var curYpos = objConsole.window.height();
	
	if(curYpos < intConsoleYPos){
		intConsoleXPos = 10;
		intConsoleYPos = 10;
	}	
	
	//Check for negative values
	if(intConsoleYPos < 0){
		intConsoleXPos = 10;
		intConsoleYPos = 10;
	}
	
	if(intConsoleXPos < 0){
		intConsoleXPos = 10;
		intConsoleYPos = 10;
	}

	objConsole.ConsoleLayer.xpos(intConsoleXPos)
	objConsole.ConsoleLayer.ypos(intConsoleYPos)
	objConsole.sethover(intConsoleYPos,intConsoleXPos)
	ConsolePaintTimer = window.setInterval("objConsole.paint()",objConsole.callRate);
	}
}


function ResetConsole()
{
	if(objConsole != null)
	{
		objConsole.sethover(10,10)
	}
}

function Console(){
	this.yhov = 0;
	this.xhov = 0;
	this.div=8;
	this.callRate=5;
	this.CollapseState = 0;

	this.window = new WindowObject();
	this.ConsoleLayer = new LayerObject("Console");
	this.OptionsLayer = new LayerObject("ConsoleOptions");

	this.sethover = ConsoleSetHover;
	this.paint = ConsolePaint;
	this.collapse = ConsoleCollapse;
	this.drag = ConsoleDrag;
}

function WindowObject(){
	this.height = new Function("return document.all?(document.body.clientHeight):(window.innerHeight)");
	this.width = new Function("return document.all?(document.body.clientWidth):(window.innerWidth)");
	this.yoffset = new Function("return document.all?(document.body.scrollTop):(window.pageYOffset)");
	this.xoffset = new Function("return document.all?(document.body.scrollLeft):(window.pageXOffset)");
}

function LayerObject(strName){
	this.layer = document[document.all?"all":"layers"][strName];
	this.xpos = LayerXPos;
	this.ypos = LayerYPos;
	this.height = new Function("return parseInt((document.all?(this.layer.offsetHeight):(this.layer.document.height)))");
	this.width = new Function("return parseInt((document.all?(this.layer.offsetWidth):(this.layer.document.width)))");
}

function ConsoleSetHover(intYpos,intXpos){
	SetCookie('ConsoleXPos',intXpos,exp)
   	SetCookie('ConsoleYPos',intYpos,exp)
	this.yhov = intYpos - this.window.yoffset()
	this.xhov = this.window.xoffset() + this.window.width() - intXpos
}

function LayerXPos(intXPos) {
	if(intXPos){
		this.layer.style.pixelLeft = intXPos
	} else {
		return this.layer.style.pixelLeft
	}
}

function LayerYPos(intYPos) {
	if(intYPos){
		this.layer.style.pixelTop = intYPos
	} else {
		return this.layer.style.pixelTop
	}
}

function ConsolePaint(){
	tarY=this.window.yoffset() + this.yhov;
	curY=this.ConsoleLayer.ypos();
	difY=curY-tarY;
	decY=Math.round(difY/this.div);
	this.ConsoleLayer.ypos(this.ConsoleLayer.ypos()-decY);

	tarX=this.window.xoffset() + this.window.width()-this.xhov;
	curX=this.ConsoleLayer.xpos();
	difX=curX-tarX;
	decX=Math.round(difX/this.div);
	newX=this.ConsoleLayer.xpos()-decX
	if(newX<0){
		newX = 0
	}
	this.ConsoleLayer.xpos(newX);
}

function ConsoleCollapse(){
	if (this.CollapseState == 1){
   		this.OptionsLayer.layer.style.display="block";
   		this.CollapseState = 0;
	} else {
		this.OptionsLayer.layer.style.display="none";
   		this.CollapseState = 1;
	}
}

function ConsoleDrag(){
if (!dragInit) initDrag();
if (!dragOn) {
	dragDiv = this.ConsoleLayer
   	dragDiv.layer.onmouseout=dragf;
   }
}

function initDrag() {
if (document.layers) 
   document.captureEvents(Event.MOUSEMOVE|Event.MOUSEDOWN|Event.MOUSEUP);
   document.onmousemove=dragf;
   document.onmousedown=dragf;
   document.onmouseup=dragf;
   dragInit=1;
}

function dragf(arg) {
	ev=arg?arg:event;
if (dragDiv && ev.type=="mousedown") {
	window.clearInterval(ConsolePaintTimer);
	dragOn=1;
	dragX=(ev.pageX?ev.pageX:ev.clientX)-dragDiv.xpos();
	dragY=(ev.pageY?ev.pageY:ev.clientY)-dragDiv.ypos();
	return false;
   }
if (dragDiv && ev.type=="mouseup") {
	dragOn=0;
	objConsole.sethover(dragDiv.ypos(),dragDiv.xpos())

	window.clearInterval(ConsolePaintTimer)
	ConsolePaintTimer = window.setInterval("objConsole.paint()",objConsole.callRate);
  }
if (dragDiv && ev.type=="mousemove" && dragOn) {
   dragDiv.xpos((ev.pageX?ev.pageX:ev.clientX)-dragX);
   dragDiv.ypos((ev.pageY?ev.pageY:ev.clientY)-dragY);
   return false;
   }
if (ev.type=="mouseout") {
   if (!dragOn) dragDiv=null;
   }
}

function GetCookie (name) {  
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {    
var j = i + alen;    
if (document.cookie.substring(i, j) == arg)      
return getCookieVal (j);    
i = document.cookie.indexOf(" ", i) + 1;    
if (i == 0) break;}  
return null;
}

function SetCookie (name, value) {  
var argv = SetCookie.arguments;  
var argc = SetCookie.arguments.length;  
var expires = (argc > 2) ? argv[2] : null;  
var path = (argc > 3) ? argv[3] : null;  
var domain = (argc > 4) ? argv[4] : null;  
var secure = (argc > 5) ? argv[5] : false;  
document.cookie = name + "=" + escape (value) + 
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
((path == null) ? "" : ("; path=" + path)) +  
((domain == null) ? "" : ("; domain=" + domain)) +    
((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

