// Global variables
var xMousePos = 0; // Horizontal position of the mouse on the screen
var yMousePos = 0; // Vertical position of the mouse on the screen
var currentDrag = false;
var noDrag = false;
var resizing = false;
var currentId = "";
var currentLeft = 0;
var currentTop = 0;
var currentHeight = 0;
var currentWidth = 0;
var leftOffset = 0;
var topOffset = 0;


var xPos, yPos, width, height, content,html, appended,parent;
var windowWidth, windowHeight, browserTopY;

//Tells whether the wizard is visible or not
appended = false;


//start the mouse tracking
mouseInit();

/* 	This is the constructor it is called by the including page 
		The parent parameter is the id of the parent div where the 
		wizard will exist on the including page.  The content is the 
		initial content of the wizard.
*/
function wizard(parent,xPos, yPos, width, height, content){
	this.parent = parent;
	this.xPos = xPos;
	this.yPos = yPos;
	this.width = width;
	this.height = height;
	this.content = content; 

	//We want to display a loading wheel while ajax grabs the content
	this.content = '<html><body><table width="100%" height="100%"><tr><td valign="center" align="center"><span style="color:gray;font-size:18px;font-style:bold">Loading Special Announcment Please Wait...<br><br><img src="splash/loadingWheel.gif"><br></span></td></tr></table></body></html>';

  showWizard();
  setTimeout("ajaxPost('initialPage', 'initialPage')", 3000);
/*
	if(appended){
		hideWizard();
		showWizard();
		setTimeout("ajaxPost('initialPage','initialPage')",3000);
	}else{
		showWizard();
		setTimeout("ajaxPost('initialPage','initialPage')",3000);
	}
*/
}

/* Adds the wizard div to the document */
function showWizard(){
 	var wizardDiv = document.createElement('div');
	wizardDiv.setAttribute('id', 'wizard');
 	wizardDiv.className='wizard';
 	wizardDiv.style.cssText = "z-index:3;border: 1px orange solid;background-color:white;opacity: 0.9;filter:alpha(opacity=90);position:absolute;top:" + yPos + "px;left:" + xPos + "px;width:" + width + "px;height:" + height + "px;";
 	wizardDiv.onmousedown = wizardDragStart;
 	//wizardDiv.innerHTML = getContent();
	wizardDiv.appendChild(wizardNav());
	wizardDiv.appendChild(wizardContent());
	//disableSelection(wizardDiv);
 	document.getElementById(parent).appendChild(wizardDiv);
 	appended=true;
	//document.getElementById("wizardButton").value = "Hide Wizard";
}

function showBubble(field){
	var bubbleName = field.name;
	if(bubbleName.charAt(bubbleName.length-1) != "h"){
		bubbleName = bubbleName + "h";
  }

	var bubble = document.getElementById(bubbleName);
	bubble.style.display = "";
}

function hideBubble(field){
	var bubbleName = field.name;
	if(bubbleName.charAt(bubbleName.length-1) != "h"){
		bubbleName += "h";
  }
	var bubble = document.getElementById(bubbleName);
	bubble.style.display="none";
}

function wizardContent(){
	var contentDiv = document.createElement('div');
	contentDiv.setAttribute('id', 'wizardContent');
	contentDiv.setAttribute('name', 'wizardContent');
	contentDiv.className='wizardContent';
	//contentDiv.onmousedown = wizardDragStart;
	contentDiv.innerHTML = getContent();
	disableSelection(contentDiv);
	contentDiv.style.cssText="z-index:2;overflow : auto;width:" + width + "px;height:" + (height-20) + "px;";
	return contentDiv;
}

function wizardNav(){
	var navbar = document.createElement('div');
	navbar.setAttribute('id', 'wizardNav');
	navbar.className='wizardNav';
	//navbar.onmousedown = wizardDragStart;
	navbar.style.cssText="height:20px;";
	navbar.innerHTML = '<table width="100%" height="100%"><tr><td valign="top" align="right"><a href="" onClick="javascript:hideWizard();return false;">Close</a></td></tr></table>';
  return navbar;
}

/* Removes the wizard div from the document */
function hideWizard(){
	var handle = document.getElementById("wizard");
	document.getElementById(parent).removeChild(handle);
	appended=false;
	currentDrag = false;
	//document.getElementById("wizardButton").value = "Show Wizard";
}

/* This function moves everything into the dragging state to get
	 ready for the wizard to be dragged.
*/
function wizardDragStart(){
	//Get a handle on the wizard
	var handle = document.getElementById("wizard");
	currentWidth = parseInt(handle.style.width.replace("px",""));
	currentLeft = parseInt(handle.style.left.replace("px",""));
	scrollSafeArea = (xMousePos < ((currentWidth + currentLeft) - 16));
	
	if(appended && scrollSafeArea){
		currentTop = parseInt(handle.style.top.replace("px",""));
		currentHeight = parseInt(handle.style.height.replace("px",""));

		leftOffset = xMousePos - parseInt(currentLeft);
		topOffset = yMousePos - parseInt(currentTop);
	
		currentDrag = true;
		handle.style.cursor='hand';

		disableSelection(document.body);
	}
}


/* This function gets called when the user is done dragging the 
	 wizard, it preforms some clean up to move out of the dragging state
*/
function wizardDragEnd(){
	
	if(appended && currentDrag){
		var handle = document.getElementById("wizard");
	  newX = xMousePos - leftOffset;
	 	newY = yMousePos - topOffset;
	 	if(((newX + currentWidth) < windowWidth) && (newX > 0)){
	   	handle.style.left=newX + "px";
	 	}
	 	if(((newY + currentHeight) < windowHeight) && (newY > browserTopY)){
     	handle.style.top=newY + "px";
	 	}
		currentDrag = false;
    enableSelection(document.body);
		handle.style.cursor='default';
  }


}

/* This function moves the wizard to a location where it can
	 Be seen on the screen in case the user scrolls the wizard 
	 off the screen 
*/
function moveWizard(){
	if(appended){
  	var handle = document.getElementById("wizard");
		currentY = parseInt(handle.style.top.replace("px",""));
    if(currentY < browserTopY){
    	handle.style.top = browserTopY + "px";
		}else if((currentY + currentHeight) > windowHeight){
     	handle.style.top = (windowHeight - currentHeight) + "px"
		}

	}


}


/* This function is used to initialize the mouse events */
function mouseInit(){
  window.onscroll = moveWizard; 
	
	if (document.layers) { // Netscape
		document.captureEvents(Event.MOUSEMOVE);
   	document.captureEvents(Event.MOUSEUP);
   	document.onmouseup = wizardDragEnd;
   	document.onmousemove = captureMousePosition;
	}else if (document.all) { // Internet Explorer
  	document.onmouseup = wizardDragEnd;
  	document.onmousemove = captureMousePosition;
	}else if (document.getElementById) { // Netcsape 6
  	document.onmouseup = wizardDragEnd;
  	document.onmousemove = captureMousePosition;
	}
}

/*	This function takes care of grabbing the mouse position and also
		recording some other screen values 
*/
function captureMousePosition(e) {
	if (document.layers) {  //Netscape
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	}else if (document.all) {//IE
		xMousePos = window.event.x+document.body.scrollLeft;
		yMousePos = window.event.y+document.body.scrollTop;
		xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
		yMousePosMax = document.body.clientHeight+document.body.scrollTop;
	}else if (document.getElementById) {
		// Netscape 6 behaves the same as Netscape 4 in this regard
		xMousePos = e.pageX;
		yMousePos = e.pageY;
		xMousePosMax = window.innerWidth+window.pageXOffset;
		yMousePosMax = window.innerHeight+window.pageYOffset;
	}

	if ( document.documentElement && (document.documentElement.scrollTop)) {
		browserTopY = document.documentElement.scrollTop;
	}else {
		browserTopY = document.body.scrollTop;
	}

	if( typeof( window.innerHeight ) == 'number' ) {
		windowHeight = window.innerHeight + browserTopY;
	} else {
 	  windowHeight = document.documentElement.clientHeight + browserTopY;	
	}
	
	if( typeof( window.innerWidth ) == 'number' ) {
		windowWidth = window.innerWidth;
	} else {
 	  windowWidth = document.documentElement.clientWidth;	
	}

	if(currentDrag && appended){
  	var handle = document.getElementById("wizard");
  	if(resizing){
   		handle.style.width=(xMousePos-parseInt(handle.style.left));
    	handle.style.height=(yMousePos-parseInt(handle.style.top));
  	}else{
			newX = xMousePos - leftOffset;
			newY = yMousePos - topOffset;

			if(((newX + currentWidth) < windowWidth) && (newX > 0)){
	     	handle.style.left=newX + "px";
			}
			if(((newY + currentHeight) < windowHeight) && (newY > browserTopY)){
      	handle.style.top=newY + "px";
			}
		}

	}
	   moveWizard();
		//window.status = "wizardDragStart x-> " + xMousePos + " y-> " + yMousePos;
}

/* Enables the selection of text on the target, in our case document.body */
function enableSelection(target){
	if (typeof target.onselectstart!="undefined"){ //IE route
 		target.onselectstart=null;
	}else if (typeof target.style.MozUserSelect!="undefined"){ //Firefox route
		target.style.MozUserSelect=null;
		target.onmousedown=null;
	}else{ //All other route (ie: Opera)
		target.onmousedown=null;
	}
	target.style.cursor = "auto";
}

/* Disables the selection of text on the target, in our case document.body */
function disableSelection(target){
	if (typeof target.onselectstart!="undefined"){ //IE route
 		target.onselectstart=function(){return false;}
	}else if (typeof target.style.MozUserSelect!="undefined"){ //Firefox route
		target.style.MozUserSelect="none";
		target.onmousedown=function(){return false;}
	}else{ //All other route (ie: Opera)
		target.onmousedown=function(){return false;}
	}
	target.style.cursor = "default";
}

/* This function takes care of ajax calls to the wizard.asp */
function ajaxPost(command, contents){
	var xmlHttpReq = false;
	var host = window.location.hostname

	//Mozilla/Safari
	if(window.XMLHttpRequest){
		xmlHttpReq = new XMLHttpRequest();
	}else if(window.ActiveXObject){  //IE
  	xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	try{
  	xmlHttpReq.open('GET', 'splashContents.html', false);
	}catch(e){
  	window.status=e;
  	
	}

	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
	try{
	  //need to leave command=test so that firefox works correctly
  	xmlHttpReq.send("command=test");
		
		response = xmlHttpReq.responseText;
		setContent(response);
	}catch(e){
		window.status = e;
		//Also let the user know that wizard is offline
		setContent('<html><body><table width="100%" height="100%"><tr><td valign="center" align="center"><span style="color:white;font-size:18px;font-style:bold">Wizard Offline<br><br><br></span></td></tr></table></body></html>');
	}

	
}

/* All the gets and sets */
function getxPos(){
	return xPos;
}
function getyPos(){
	return yPos;
}
function getWidth(){
 	return width;
}
function getHeight(){
 	return height;
}
function getContent(){
	//closeButton = '<form><input type="button" value="Close Wizard" onClick="javascript:hideWizard()"></form>';
 	return content;
}
function setxPos(what){
	xPos = what;
}
function setyPos(what){
	yPos = what;
}
function setWidth(what){
	width = what;
}
function setHeight(what){
	height = what;
}
function setContent(what){
	content = what;
 	var handle = document.getElementById("wizardContent");
	handle.innerHTML = content;
}

