/**
 * Functions for performing Dynamic HTML related actions.
 * 
 * Author: Todd King
**/
// Causes the window to reload contents - like clicking the "refresh" button
function refresh() 
{
   window.location.replace(window.location.href);
}

// Set the page title in the browser
function docSetTitle(title)
{
	document.title = title;
}

// Perform tasks after a page loads.
function docOnLoad() 
{
 	// If local grabFucus function is defined
   if(window.myGrabFocus != null) myGrabFocus();
   
   // Fix-up forms
   if(window.formOnLoad) formOnLoad();

   // Hide Progress layer - if it exists
   if(document.getElementById('Progress')) {
		document.getElementById('Progress').style.visibility = "hidden";
	}
	
 	// If local onLoad function is defined
   if(window.menuLoad != null) menuLoad();   
   
 	// If local onLoad function is defined
   if(window.myOnLoad != null) myOnLoad();
}

// Set cursor focus to an element in a form
function docGrabFocus(form, field) 
{
   window.focus();
   eval("document." + form + "." + field + ".focus()");
}

// Set an anchor item to have the class 'selected'
function itemSelect(menuID, itemName)
{
	if(document.getElementById(menuID)) {
		elem = document.getElementById(menuID);
		if(elem) {
			list = elem.getElementsByTagName('a');
			for(i=0; i<list.length; i++) {
				if(list[i].name == itemName) list[i].className = 'selected';
			}
		}
	}
}

// Show the elements with the IDs given. 
// This function accepts a variable list of arguments.
function itemShow() 
{
	var elem;
	for(var i=0; i < itemShow.arguments.length; i++) {
		elem = document.getElementById(itemShow.arguments[i]);
		if(elem)	{
			elem.style.visibility = 'visible';
			/* Now show all <select> items in this item */
			/* Required to with with pop-down menus */
			list = elem.getElementsByTagName('select');
			for(var j=0; j < list.length; j++) {
				list[j].style.visibility = 'visible';
			}
		}
	}
}
	
// Hide the elements with the IDs given. 
// This function accepts a variable list of arguments.
function itemHide() 
{
	var list = Array();
	var elem;
	for(var i=0; i < itemHide.arguments.length; i++) {
		elem = document.getElementById(itemHide.arguments[i]);
		if(elem)	{
			elem.style.visibility = 'hidden';
			/* Now hide all <select> items in this item */
			/* Required to with with pop-down menus */
			list = elem.getElementsByTagName('select');
			for(var j=0; j < list.length; j++) {
				list[j].style.visibility = 'hidden';
			}
		}
	}
}

// Shift the position of the element 'itemID' so that 
// is lays on top of element 'withID' and show the item.
function itemAlign(itemID, withID) 
{
	itemElem = document.getElementById(itemID);
	withElem = document.getElementById(withID);
	
	if(itemElem != null && withElem != null) {
		itemElem.style.left = withElem.style.left;
		itemElem.style.top = withElem.style.top;
	}
	itemShow(itemID);
}


// Editing functions
// Global things
var mEditServiceURL = null;
var mEditEditing  = false;
var mEditLastDiv = null;

var mEditDivID = null;
var mEditDivName = null;
var mEditDivClass = null;
var mEditDivBackground = null;

// Create a button to re-use when editing

if (document.getElementById && document.createElement) {
	var mEditButton = document.createElement('BUTTON');
	var bText = document.createTextNode('Done!');
	mEditButton.appendChild(bText);
	mEditButton.onclick = editSave;
}

function editCatchClick(e) {
	if (!mEditEditing) return;
	
	// Check if DHTML is supported
	if (!document.getElementById || !document.createElement) return;
	
	// Get event target (browser dependence)
	if (!e) var obj = window.event.srcElement;
	else var obj = e.target;
	while (obj.nodeType != 1) { obj = obj.parentNode; }
	if(!obj) return;
	
	// Find enclosing DIV
	var topDiv = document.getElementById('content');
	if(!topDiv) return;
	
	if (obj.tagName == 'TEXTAREA' || obj.tagName == 'A') return;
	while (obj.nodeName != 'DIV' && obj.nodeName != 'HTML') {   // Find enclosing DIV or top of document
		obj = obj.parentNode;
	}
	if (obj == topDiv) return; // Went too far
	if (obj.nodeName == 'HTML') return; // Went too far
	
	// Save attributes, create textarea for editing and populate with content
	mEditDivID = obj.id;
	mEditDivName = obj.name;
	mEditDivClass = obj.className;
	mEditDivBackground = obj.style.background;
	
	var content = obj.innerHTML;
	var editArea = document.createElement('TEXTAREA');
	var node = obj.parentNode;
	
	editArea.cols = 40;
	editArea.rows = content.split("\n").length + 2;
	
	node.insertBefore(editArea, obj);
	node.insertBefore(mEditButton, obj);
	node.removeChild(obj);
	editArea.value = content;
	editArea.focus();
	mEditEditing = true;
}

// Convert current TEXTAREA into a DIV, fill DIV with content
function editSave() {
	var area = document.getElementsByTagName('TEXTAREA')[0];
	var div = document.createElement('DIV');
	
	div.id = mEditDivID;
	div.name = mEditDivName;
	div.className = mEditDivClass;
	div.style.background = mEditDivBackground;
	
	var node = area.parentNode;
	ajaxPost(mEditServiceURL, "div=" + mEditDivID, "content=" + encodeURI(area.value));
	div.innerHTML = area.value;
	node.insertBefore(div, area);
	node.removeChild(area);
	node.removeChild(document.getElementsByTagName('button')[0]);
	mEditEditing = false;
	mEditLastDiv = div;
}

function editInsertBefore() 
{
   if(mEditLastDiv == null) { alert('Select an area and try again'); return; }
   var newDiv = document.createElement('DIV');
   var newText = document.createTextNode("Click to edit");
   newDiv.appendChild(newText);      
   mEditLastDiv.parentNode.insertBefore(newDiv, mEditLastDiv);
}

function editInsertAfter() 
{
   if(mEditLastDiv == null) { alert('Select an area and try again'); return; }
   var newDiv = document.createElement('DIV');
   var newText = document.createTextNode("Click to edit");
   newDiv.appendChild(newText);      
   mEditLastDiv.parentNode.insertBefore(newDiv, mEditLastDiv.nextSibling);
}

function editMakeMenu()
{
   document.write('<input type=button value="Save Page" onclick=alert(document.getElementById("content").innerHTML);>');
   document.write('<input type=button value="Insert Before" onclick="insertBefore();">');
   document.write('<input type=button value="Insert After" onclick="insertAfter();">');
   document.write('<br>');
}

function editSetServiceURL(serviceURL)
{
	mEditServiceURL = serviceURL;
}

// Checks if an object is contained somewhere inside an DIV with ID "content"
function editInContent(obj)
{
	// Find enclosing DIV
	var topDiv = document.getElementById('content');
	if(obj == topDiv) return false;	// Content div is not editable.
	while(obj.nodeName != 'HTML') {   // Find top of document
		// alert(obj.id + "?= " + topDiv.id);
		if(obj == topDiv) return true;
		obj = obj.parentNode;
		if(obj == null) break;
	}
	return false;
}

function editEnable(enable)
{
	if(enable) {
		document.onclick = editCatchClick;
		// Set background for divisions
		var div = document.getElementsByTagName('DIV');
		for(var i = 0; i < div.length; i++) {
			if(editInContent(div[i])) {
				div[i].style.background = 'url(/theme/images/editable.jpg)';
			}
		}
	} else {
		document.onclick = null;
	}
}
