/********* Start of windowing functions *********/

// Browser identification variables
var ns6 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5)?true:false;
var ie = (navigator.appName == "Microsoft Internet Explorer")?true:false;
var ie6 = (ie && navigator.appVersion.indexOf("6.") != -1);

// Used to determine which panel is on top
var dragPanelTopZ = 0;

// Current dragging panel
var activeDragPanel = false;

// Drag coordinate offset
var dragPanelOffsetX, dragPanelOffsetY;

// Options
var dragPanelOptions = false;

function initPanels(options) {
	if (!ns6 && !ie) {
		alert("This page requires Internet Explorer 5 or Netscape 6.");
		return;
	}
	
	// Enable event capturing in ns6
	if (ns6) window.captureEvents(Event.MOUSEMOVE);

	// Set document drag event
	document.onmousemove = dragActivePanel;
	
	// Initialize options object
	if (arguments.length == 1)
		dragPanelOptions = options;
	else
		dragPanelOptions = new DragPanelOptions;
	
	// Create display panel
	if (dragPanelOptions.showDisplayPanel === true)
		makeDisplayPanel();
	
	// Initialize panels
	for (var i=0;i<dragPanelOptions.panels.length;i++)
		makePanel(
			dragPanelOptions.panels[i][0],
			dragPanelOptions.panels[i][1],
			dragPanelOptions.panels[i][2],
			dragPanelOptions.panels[i][3],
			dragPanelOptions.panels[i][4],
			dragPanelOptions.panels[i][5],
			dragPanelOptions.panels[i][6]
		);
}

function makePanel(id, title, width, height, left, top, visible) {
	if (dragPanelOptions === false) {
		alert("Panels not initialized!");
		return;
	}
	
	var parentEl, removeEl, childEl, newPanel, table, titleBarRow, titleCell;
	var resizeCell, resizeButton, resizeImage, contentRow, contentCell, contentHeight;
	
	// Make sure the element exists in the dom tree
	if ((removeEl = document.getElementById(id)) != null) {
		parentEl = removeEl.parentNode;
		// Remove the element from the tree and rename it
		childEl = parentEl.removeChild(removeEl);
		childEl.id = id+"_data";
		childEl.style.visibility = "inherit";
		
		// Create the new containing panel and set its properties
		newPanel = document.createElement("div");
		// Attributes
		newPanel.id = id;
		newPanel.setAttribute("expanded", "true");
		newPanel.setAttribute("panelTitle", title);
		// Methods and Events
		newPanel.toggleExpanded = toggleExpanded;
		newPanel.toggleVisibility = toggleVisibility;
		newPanel.onmousedown = enableDrag;
		newPanel.onmouseup = disableDrag;
		// Styles
		newPanel.style.position = "absolute";
		newPanel.style.visibility = "hidden";
		newPanel.style.zIndex = dragPanelTopZ++;
		if (width != null) newPanel.style.width = width;
		if (height != null) newPanel.style.height = height - ((ie)?20:0); // weird bug - ie magically adds 20 pixels
		if (left != null) newPanel.style.left = left;
		if (top != null) newPanel.style.top = top;

		// Add to the document and set visibility
		parentEl.appendChild(newPanel);
		
		// Create the formatting table
		table = document.createElement("table");
		table.cellPadding = 1;
		table.cellSpacing = 0;
		table.border = 0;
		if (width != null) table.setAttribute("width", width);
		table.className = dragPanelOptions.dragPanelClass;
		
		// Insert table into drag panel
		newPanel.appendChild(table);
		
		// First row
		titleBarRow = table.insertRow(0);
		// Draggable title bar
		titleCell = titleBarRow.insertCell(0);
		titleCell.align = dragPanelOptions.titleBarTextAlign;
		titleCell.vAlign = dragPanelOptions.titleBarTextVAlign;
		titleCell.className = dragPanelOptions.titleBarClass;
		titleCell.style.cursor = dragPanelOptions.titleBarCursor;
		titleCell.innerHTML = title;
		titleCell.setAttribute("name", "dragHandle");
		titleCell.setAttribute("unselectable", "on");
		// If no buttons, don't make the button cell
		if (dragPanelOptions.showResize !== true && dragPanelOptions.showClose !== true) {
			titleCell.colSpan = 2;
		}
		else {
			// Button cell
			buttonCell = titleBarRow.insertCell(1);
			buttonCell.align = dragPanelOptions.buttonAlign;
			buttonCell.vAlign = dragPanelOptions.buttonVAlign;
			buttonCell.className = dragPanelOptions.titleBarClass;
			buttonCell.style.cursor = dragPanelOptions.titleBarCursor;
			buttonCell.setAttribute("name", "dragHandle");
			buttonCell.setAttribute("unselectable", "on");
			
			if (dragPanelOptions.showResize === true) {
				// Minimize/maximize button
				resizeButton = document.createElement("a");
				resizeButton.href = "javascript:document.getElementById('"+newPanel.id+"').toggleExpanded()";
				resizeImage = document.createElement("img");
				resizeImage.src = dragPanelOptions.resizeImageSrc;
				resizeImage.border = dragPanelOptions.resizeImageBorder;
				resizeImage.width = dragPanelOptions.resizeImageWidth;
				resizeImage.height = dragPanelOptions.resizeImageHeight;
				resizeImage.setAttribute("hspace", dragPanelOptions.buttonSpacing);
				resizeButton.appendChild(resizeImage);
				
				// Add button to table cell
				buttonCell.appendChild(resizeButton);
			}
			
			if (dragPanelOptions.showClose === true) {
				// Close button
				closeButton = document.createElement("a");
				closeButton.href = "javascript:document.getElementById('"+newPanel.id+"').toggleVisibility()";
				closeImage = document.createElement("img");
				closeImage.src = dragPanelOptions.closeImageSrc;
				closeImage.border = dragPanelOptions.closeImageBorder;
				closeImage.width = dragPanelOptions.closeImageWidth;
				closeImage.height = dragPanelOptions.closeImageHeight;
				closeImage.setAttribute("hspace", dragPanelOptions.buttonSpacing);
				closeButton.appendChild(closeImage);
				
				// Add button to table cell
				buttonCell.appendChild(closeButton);
			}
		}
		
		// Second row
		contentRow = table.insertRow(1);
		// Content cell
		contentCell = contentRow.insertCell(0);
		contentCell.align = dragPanelOptions.contentAlign;
		contentCell.vAlign = dragPanelOptions.contentVAlign;
		contentCell.colSpan = 2;
		if (height != null) {
			cssClass = getCssClass(dragPanelOptions.dragPanelClass);
			contentHeight = height;
			contentHeight -= ((ie)?titleBarRow.clientHeight:titleBarRow.offsetHeight);
			contentHeight -= parseInt(cssClass.style.borderTopWidth);
			contentHeight -= parseInt(cssClass.style.borderBottomWidth);
			contentCell.setAttribute("height", contentHeight);
		}
		contentCell.appendChild(childEl);
		
		// Add display panel element
		addToDisplayPanel(newPanel);
		
		// Set visibility
		if (visible == "true") newPanel.toggleVisibility();
	}
}


function makeDisplayPanel() {
	displayPanel = document.createElement("table");
	displayPanel.id = "displayPanel";
	displayPanel.cellPadding = dragPanelOptions.displayPanelCellPadding;
	displayPanel.cellSpacing = dragPanelOptions.displayPanelCellSpacing;
	displayPanel.style.position = "absolute";
	displayPanel.style.visibility = "visible";
	displayPanel.style.left = dragPanelOptions.displayPanelLeft;
	displayPanel.style.top = dragPanelOptions.displayPanelTop;
	displayPanel.className = dragPanelOptions.displayPanelClass;
	
	document.body.appendChild(displayPanel);
}

function addToDisplayPanel(dragPanel) {
	var displayPanel;
	
	if ((displayPanel = document.getElementById("displayPanel")) != null) {
		if (dragPanelOptions.displayPanelRenderMode == "vertical" || displayPanel.rows.length == 0)
			displayPanel.insertRow(displayPanel.rows.length);
		cell = displayPanel.rows[displayPanel.rows.length-1].insertCell(displayPanel.rows[displayPanel.rows.length-1].cells.length);
		cell.setAttribute("name", dragPanel.id);
		cell.innerHTML = dragPanel.getElementsByTagName("table")[0].rows[0].cells[0].innerHTML;
		cell.className = dragPanelOptions.displayPanelOffClass;
		cell.onclick = toggleVisibilityClick;
	}
}

function toggleExpanded() {
	var panelWidth = (ie)?this.clientWidth:this.offsetWidth;
	var panelHeight = (ie)?this.clientHeight:this.offsetHeight;
	var cssClass, titleHeight;
	
	if (this.getAttribute("expanded") == "true") {
		cssClass = getCssClass(dragPanelOptions.dragPanelClass);
		titleHeight = (ie)?
			this.getElementsByTagName("table")[0].rows[0].clientHeight:
			this.getElementsByTagName("table")[0].rows[0].offsetHeight;
		if (cssClass.style.borderTopWidth != "") titleHeight += parseInt(cssClass.style.borderTopWidth);
		
		this.style.clip = "rect(0 "+(panelWidth)+" "+(titleHeight)+" 0)";
		// Netscape 6 allows elements in a div that are outside the
		// clipping area to be interacted with by the user. To fix that,
		// we turn off visibility of the content table.
		this.getElementsByTagName("table")[0].rows[1].style.visibility = "hidden";
		this.setAttribute("expanded", "false");
	}
	else {
		this.style.clip = "rect(0 "+(panelWidth)+" "+(panelHeight)+" 0)";
		this.getElementsByTagName("table")[0].rows[1].style.visibility = "inherit";
		this.setAttribute("expanded", "true");
	}
}

function toggleVisibility() {
	var displayPanel, tableRows;

	this.style.zIndex = dragPanelTopZ++;
	this.style.visibility = (this.style.visibility == "visible")?"hidden":"visible";

	if ((displayPanel = document.getElementById("displayPanel")) != null) {
		tableRows = displayPanel.rows;
		
		for (var i=0;i<tableRows.length;i++) {
			for (var x=0;x<tableRows[i].cells.length;x++) {
				if (tableRows[i].cells[x].getAttribute("name") == this.id) {
					tableRows[i].cells[x].className = (this.style.visibility == "visible")?
						dragPanelOptions.displayPanelOnClass:
						dragPanelOptions.displayPanelOffClass;
					break;
				}
			}
		}
	}
}

function enableDrag(e) {
	if ((ie && event.srcElement.name == "dragHandle") || (ns6 && ((e.target.nodeType == 3 && e.target.parentNode.getAttribute("name") == "dragHandle") || (e.target.nodeType == 1 && e.target.getAttribute("name") == "dragHandle")))) {
		dragPanelOffsetX = (ns6)?e.layerX:event.clientX-(this.offsetLeft+this.clientLeft);
		dragPanelOffsetY = (ns6)?e.layerY:event.clientY-(this.offsetTop+this.clientTop);
		
		dragPanelOffsetX = (dragPanelOffsetX<5)?5:(this.clientWidth-dragPanelOffsetX<5)?this.clientWidth-5:dragPanelOffsetX;
		dragPanelOffsetY = (dragPanelOffsetY<5)?5:(this.clientHeight-dragPanelOffsetY<5)?this.clientHeight-5:dragPanelOffsetY;
		
		this.style.zIndex = dragPanelTopZ++;
		activeDragPanel = this;
		return false;
	}
}

function disableDrag() {
	window.status = "";
	activeDragPanel = false;
}

function dragActivePanel(e) {
	if (ns6) event = e;

	if (activeDragPanel !== false) {
		window.status = activeDragPanel.getAttribute("panelTitle")+": "+(event.clientX - dragPanelOffsetX)+" x "+(event.clientY - dragPanelOffsetY);
		activeDragPanel.style.left = event.clientX - dragPanelOffsetX + ((ie6)?document.body.scrollLeft:(ns6)?window.scrollX:0);
		activeDragPanel.style.top = event.clientY - dragPanelOffsetY + ((ie6)?document.body.scrollTop:(ns6)?window.scrollY:0);
	}
}

function togglePanelExpanded(panelId) {
	document.getElementById(panelId).toggleExpanded();
}

function toggleVisibilityClick(e) {
	var panelId;
	if (ie) panelId = event.srcElement.name;
	if (ns6) panelId = (e.target.nodeType == 3)?e.target.parentNode.getAttribute("name"):e.target.getAttribute("name");
	document.getElementById(panelId).toggleVisibility();
}

function DragPanelOptions() {
	// Initial drag panel array
	this.panels = new Array();
	
	// Display Panel Options
	this.showDisplayPanel = true;
	this.displayPanelRenderMode = "horizontal";
	this.displayPanelCellPadding = 0;
	this.displayPanelCellSpacing = 0;
	this.displayPanelLeft = 0;
	this.displayPanelTop = 0;
	this.displayPanelClass = "";
	this.displayPanelOnClass = "";
	this.displayPanelOffClass = "";
	
	// Drag Panel Options
	this.dragPanelClass = "";
	
	// Title Bar Options
	this.titleBarClass = "";
	this.titleBarCursor = "move";
	this.titleBarTextAlign = "left";
	this.titleBarTextVAlign = "middle";
	
	// Button Cell Options
	this.buttonAlign = "right";
	this.buttonVAlign = "middle";
	this.buttonSpacing = 2;

	// Resize Button Options
	this.showResize = true;
	this.resizeImageSrc = "expand.gif";
	this.resizeImageWidth = 9;
	this.resizeImageHeight = 14;
	this.resizeImageBorder = 0;
	
	// Resize Button Options
	this.showClose = true;
	this.closeImageSrc = "close.gif";
	this.closeImageWidth = 9;
	this.closeImageHeight = 14;
	this.closeImageBorder = 0;
	
	// Content Cell Options
	this.contentAlign = "left";
	this.contentVAlign = "top";
	
	// Method to add initial panels
	this.addPanel = function() {
		this.panels[this.panels.length] = arguments;
	}
}

function getCssClass(className) {
	for (var i=0;i<document.styleSheets.length;i++) {
		if (ie)
			for (var x=0;x<document.styleSheets[i].rules.length;x++)
				if (document.styleSheets[i].rules[x].selectorText == "."+className)
					return document.styleSheets[i].rules[x];
		if (ns6)
			for (var x=0;x<document.styleSheets[i].cssRules.length;x++)
				if (document.styleSheets[i].cssRules[x].selectorText == "."+className)
					return document.styleSheets[i].cssRules[x];
	}
}

/********* End of windowing functions *********/
