// JavaScript Document

Event.observe( window, 'load', init, false ) ;

function init () {

	mbImport () ;

}

function $Notify ( Message, Highlight ) {
	new Effect.Appear( $('Update'), { duration: 0.3 } ) ;
	$('Update').innerHTML = Message ;
	new Effect.Highlight( $('Update'), { duration: 0.4 } ) ;
	if( Highlight )
	new Effect.Highlight( Highlight, { duration: 3.0 } ) ;

}

var _mbTree = null;			// the tree representation
var _mbList = null;			// the list representation

//
// a "simple" tree class w/ back edges
//
function mbTreeNode (icon, title, link, target, descript)
{
	this.parentNode = null;
	this.firstChild = null;
	this.lastChild = null;
	this.prevSibling = null;
	this.nextSibling = null;

	this.icon = icon;
	this.title = title;
	this.link = link;
	this.target = target;
	this.descript = descript;

	//
	// attach a node at the end of the child tree
	//
	// if childNode is null or undefined, nothing is done.
	//
	this.addChild = function (childNode)
	{
		if (!childNode)
			return;
		if (this.lastChild == null)
		{
			this.firstChild = childNode;
			this.lastChild = childNode;
			childNode.parentNode = this;
		}
		else
		{
			this.lastChild.nextSibling = childNode;
			childNode.prevSibling = this.lastChild;
			this.lastChild = childNode;
			childNode.parentNode = this;
		}
	};

	//
	// assumed that this. prevSibling, nextSibling are null
	//
	this.insertBefore = function (node)
	{
		this.parentNode = node.parentNode;
		if (node.prevSibling)
		{
			node.prevSibling.nextSibling = this;
			this.prevSibling = node.prevSibling;
		}
		else
		{
			if (node.parentNode &&
				node.parentNode.firstChild == node)
				node.parentNode.firstChild = this;
		}
		this.nextSibling = node;
		node.prevSibling = this;
	};

	//
	// assumed that this. prevSibling, nextSibling are null
	//
	this.insertAfter = function (node)
	{
		this.parentNode = node.parentNode;
		if (node.nextSibling)
		{
			node.nextSibling.prevSibling = this;
			this.nextSibling = node.nextSibling;
		}
		else
		{
			if (node.parentNode &&
				node.parentNode.lastChild == this)
				node.parentNode.lastChild = this;
		}
		this.prevSibling = node;
		node.nextSibling = this;
	};

	//
	// remove a this node from other tree structures
	//
	this.remove = function ()
	{
		if (this.parentNode)
		{
			var parentNode = this.parentNode;
			if (parentNode.firstChild == this)
				parentNode.firstChild = this.nextSibling;
			if (parentNode.lastChild == this)
				parentNode.lastChild = this.prevSibling;
		}

		if (this.prevSibling)
			this.prevSibling.nextSibling = this.nextSibling;
		if (this.nextSibling)
			this.nextSibling.prevSibling = this.prevSibling;

		this.parentNode = null;
		this.prevSibling = null;
		this.nextSibling = null;
	};
	
	//
	// generate a preorder list of nodes
	//
	this.generateList = function (list)
	{
		list.push (this);
		var child;
		for (child = this.firstChild; child != null; child = child.nextSibling)
			child.generateList (list);
	};
	
	//
	// generate a preorder list of <option> for <select>
	//
	this.generateOption = function (indent)
	{
		var str = '<option onmouseover="mbOptionHover (this)" onmouseout="mbClearStatus ()">' + indent;
		if (this.icon == _cmSplit)
			return str + '--------------</option>\n';
		str += this.title + '</option>\n';
		var child;
		for (child = this.firstChild; child != null; child = child.nextSibling)
			str += child.generateOption (indent + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		return str;
	};
	
	//
	// generate the correspond tree/menu code for JSCookMenu/JSCookTree
	//
	this.generateCode = function (indent)
	{
		var str = indent;
		
		if (this.icon == _cmSplit)
		{
			if (this.nextSibling)
				return str + '_cmSplit,\n';
			else
				return str + '_cmSplit\n';
		}
		
		//alert(this.title);
//		this.title= this.title.replace(/\'/g,"\\'");
//		this.link= this.link.replace(/\'/g,"\\'");
//		this.target= this.target.replace(/\'/g,"\\'");
		//alert(this.title);
		
		str += '[';
		str += (this.title == null) ? 'null,' : '\"' + this.title.replace(/\"/g,"&quot;") + '\",';
		str += (this.link == null) ? 'null,' : '\"' + this.link.replace(/\"/g,"&quot;") + '\",';
		str += (this.target == null) ? 'null' : '\"' + this.target.replace(/\"/g,"&quot;") + '\"';
		
		//str += '[';
		//str += (this.title == null) ? 'null,' : '\"' + this.title.replace("\"","&quot;") + '\",';
		//str += (this.link == null) ? 'null,' : '\"' + this.link.replace("\"","&quot;") + '\",';
		//str += (this.target == null) ? 'null' : '\"' + this.target.replace("\"","&quot;") + '\"';
		
		//str += '[';
		//str += (this.title == null) ? 'null,' : '\'' + this.title + '\',';
		//str += (this.link == null) ? 'null,' : '\'' + this.link + '\',';
		//str += (this.target == null) ? 'null' : '\'' + this.target + '\'';
		
		if (this.firstChild)
			str += ',\n';
		var child;
		for (child = this.firstChild; child != null; child = child.nextSibling)
			str += child.generateCode (indent + '\t');
		if (this.firstChild)
			str += indent;

		if (this.nextSibling)
			str += '],\n';
		else
			str += ']\n';
		return str;
	};
}

//
// import tree

//
function mbImport(){
	var obj = cmGetObject('importArea');
	var tmpTree = null;
	//if Side Menu is selected then the right/left radios are shown
	var menuChoice=$F('Path');
	if(menuChoice=='Menu2.xml')
	{
			$('navChoice').show();
					
	}else{
		$('navChoice').hide();
	}

	if (typeof($Request)!='undefined')
		{
			
			/*I dont know what this does, but caused JS errors in IE*/
			$Request('loadMenu', $('Path').serialize(), function(r){
			$testing=$('Path').serialize();
			if(r.responseText == ""){
					return $Notice('That menu does');
				}
				temp = r.responseText.replace(/^[^\[]+/,"");
				eval('tmpTree = ' + temp);
				tmpTree = mbConvertTree(tmpTree);
				_mbTree = tmpTree;
				mbShowTree();
			});
		}
}

//
// convert JSCookMenu/JSCookTree representation to internal representation
//
function mbLayout(){
	//if navChoice changes file is writen to the users file with text option
	//the default is set to the right

	var selected = $$('input[type=radio][name=navPosition]').find(function(el){return el.checked}).value;
	if (selected) {
		selected='selected='+selected;
		if (typeof($Request)!='undefined')
		{			
			$Request('layoutChoice',selected, function(r){
				if(r.responseText == ""){
					return $Notice('That menu does');
				}
				temp = r.responseText.replace("Dev Server","");;
				
			});
		}
	}else
	{
		alert("selected has no value");
	}
	
	
	
}
function getCheckedValue(rbform, rbname) {
	 
	chosen = ""
	len = document.rbform.rbname.length
	
	for (i = 0; i <len; i++) {
	if (document.f1.r1[i].checked) {
	chosen = document.f1.r1[i].value
	}
	}
	
	if (chosen == "") {
	alert("No Location Chosen")
	}
	else {
	alert(chosen);
	}
}
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}








function mbChecked(){
if (typeof($Request)!='undefined')
	{			
		$Request('layoutChecked','none=none',function(r){
			if(r.responseText == ""){
				return $Notice('That menu does');
			}
			temp = r.responseText.replace("Dev Server","");
			
	     if (temp == 'right') {
		 	$$("input[type=radio][name='navPosition'][value='right']")[1].writeAttribute("checked", "checked");
		 	$("right").writeAttribute("checked", "checked");
		 }else{
		 	$$("input[type=radio][name='navPosition'][value='left']")[0].writeAttribute("checked", "checked");
		 	$("left").writeAttribute("checked", "checked");
		 	
		 }  
		});
		
	}
	else
	{
		alert("no value returned");
	}
	
	
	
}

function mbConvertTree (tree)
{
	var i;
	var firstNode = null;
	var lastNode = null;
	var thisNode = null;
	for (i = 0; i < tree.length; ++i)
	{
		thisNode = mbConvertTreeSub (tree[i]);
		if (firstNode == null)
			firstNode = thisNode;
		if (lastNode)
			thisNode.insertAfter (lastNode);
		lastNode = thisNode;
	}
	return firstNode;
}

function mbConvertTreeSub (tree)
{
	if (tree == _cmSplit)
		return new mbTreeNode (_cmSplit, null, null, null, null);
	if (!tree)
		return null;
	var tmpTree = new mbTreeNode (null, tree[0], tree[1], tree[2], null);
	var i;
	for (i = 3; i < tree.length; ++i)
		tmpTree.addChild (mbConvertTreeSub (tree[i]));
	return tmpTree;
}

//
// shows the current structure from scratch
//
function mbShowTree ()
{
	_mbList = new Array ();	// clear the old tree list representation
	var str = '';

	if (_mbTree)
	{
		var tmpTree;
		for (tmpTree = _mbTree; tmpTree; tmpTree = tmpTree.nextSibling)
		{
			tmpTree.generateList (_mbList);
			str += tmpTree.generateOption ('');
		}
	}
	str = '<select id="treeArea" onchange="mbSelectOnChange ()" size="20">' + str +
		  '</select>';
	
	cmGetObject ('Builder').innerHTML = str;
	cmGetObject ('treeArea').focus() ;
}

//
// clear the tree
//
function mbClearTree ()
{
	_mbTree = null;
	mbShowTree ();
}

//
// show the code for the current tree
//
function mbOutput ()
{
	var str = '';
	if (_mbTree)
	{
		var tmpTree;
		for (tmpTree = _mbTree; tmpTree; tmpTree = tmpTree.nextSibling)
			str += tmpTree.generateCode ('\t');
	}
	str = '[\n' + str + '];\n';

	$Request( 'saveMenu', 'XML=' + encodeURIComponent( str ) + '&' + $('Path').serialize(), function (r) { $Notify( r.responseText ) } ) ;

}

//
// return the selected node in 'treeArea'
//
function mbGetSelectedIndex ()
{
	var obj = cmGetObject ('treeArea');
	if (obj.selectedIndex < 0 || obj.selectedIndex >= obj.length)
		return -1;
	return obj.selectedIndex;
}

//
// set the selected node in 'treeArea'
//
function mbSetSelectedIndex (index)
{
	var obj = cmGetObject ('treeArea');
	if (index < 0 || index >= obj.length)
		return false;
	obj.selectedIndex = index;
	return true;
}

//
// get the # of child nodes plus itself
//

function mbGetNodeLength (node)
{
	if (node == undefined)
		return 0;
	if (node == _cmSplit)
		return 1;
	var len = 1;
	if (node.length > 3)
	{
		var i;
		for (i = 3; i < node.length; ++i)
			len += mbGetNodeLength (node[i]);
	}
	return len;
}
//
// add a node
//
function mbAddNode ()
{
	var title = cmGetObject ('title').value;
	if (!title)
		return $Notify( '<b>Please enter a Name</b> for your menu item.', $('title').parentNode ) ;

	var icon = cmGetObject ('icon').value;
	var link = cmGetObject ('link').value;
	var target = cmGetObject ('target').value;
	var descript = cmGetObject ('descript').value;

	if (!icon) icon = null;
	if (!link) link = null;
	if (!link || !target) target = null;
	if (!descript) descript = null;
	
	var tmpNode = new mbTreeNode (icon, title, link, target, descript);

	var index = mbGetSelectedIndex ();
	if (!_mbTree)
	{
		_mbTree = tmpNode;
	}
	else if (index < 0)
	{
		tmpNode.insertAfter (_mbList[_mbList.length - 1]);
	}
	else
	{
		tmpNode.insertAfter (_mbList[index]);
	}
	mbShowTree ();
	mbSetSelectedIndex (index + 1);
	return $Notify( 'Item Added <b>Successfully</b>' ) ;
}

//
// add a child node
//
function mbAddChild ()
{
	var title = cmGetObject ('title').value;
	if (!title)
		return $Notify( '<b>Please enter a Name</b> for your menu item.', $('title').parentNode ) ;

	var icon = cmGetObject ('icon').value;
	var link = cmGetObject ('link').value;
	var target = cmGetObject ('target').value;
	var descript = cmGetObject ('descript').value;

	if (!icon) icon = null;
	if (!link) link = null;
	if (!link || !target) target = null;
	if (!descript) descript = null;

	var tmpNode = new mbTreeNode (icon, title, link, target, descript);

	var index = mbGetSelectedIndex ();
	if (!_mbTree)
	{
		_mbTree = tmpNode;
	}
	else if (index < 0)
	{
		tmpNode.insertAfter (_mbList[_mbList.length - 1]);
	}
	else
	{
		_mbList[index].addChild (tmpNode);
	}
	mbShowTree ();
	mbSetSelectedIndex (index);
	return $Notify( 'Sub-item Added <b>Successfully</b>' ) ;
}

//
// add a menu split
//
function mbAddSplit ()
{
	var tmpNode = new mbTreeNode (_cmSplit, null, null, null, null);

	var index = mbGetSelectedIndex ();
	if (!_mbTree)
	{
		_mbTree = tmpNode;
	}
	else if (index < 0)
	{
		tmpNode.insertAfter (_mbList[_mbList.length - 1]);
	}
	else
	{
		tmpNode.insertAfter (_mbList[index]);
	}
	mbShowTree ();
	mbSetSelectedIndex (mbGetNodeIndex (tmpNode));
}

//
// change the current node
//
function mbModify ()
{
	var index = mbGetSelectedIndex ();
	if (!_mbTree || index < 0)
		return $Notify( 'Please <b>Select an Item to Modify.</b>' ) ;

	var title = cmGetObject ('title').value;
	if (!title)
		return $Notify( '<b>Please enter a Name</b> for your menu item.', $('title').parentNode1 ) ;

	var icon = cmGetObject ('icon').value;
	var link = cmGetObject ('link').value;
	var target = cmGetObject ('target').value;
	var descript = cmGetObject ('descript').value;

	if (!icon) icon = null;
	if (!link) link = null;
	if (!link || !target) target = null;
	if (!descript) descript = null;

	var node = _mbList[index];

	node.icon = icon;
	node.title = title;
	node.link = link;
	node.target = target;
	node.descript = descript;

	mbShowTree ();
	mbSetSelectedIndex (index) ;
	
	return $Notify( 'Item Updated <b>Successfully</b>' ) ;
}

//
// move the current node up
//
function mbMoveUp ()
{
	var index = mbGetSelectedIndex ();
	if (!_mbTree || index < 0)
		return $Notify( 'Please <b>Select an Item to Move.</b>' ) ;

	var node = _mbList[index];

	if (!node.prevSibling)
		return;
	var prevSibling = node.prevSibling;
	node.remove ();
	node.insertBefore (prevSibling);

	if (_mbTree == prevSibling)
		_mbTree = node;

	mbShowTree ();
	mbSetSelectedIndex (mbGetNodeIndex (node));
	
	return $Notify( 'Item moved <b>Successfully</b>' ) ;
}

//
// move the current node down
//
function mbMoveDown ()
{
	var index = mbGetSelectedIndex ();
	if (!_mbTree || index < 0)
		return $Notify( 'Please <b>Select an Item to Move.</b>' ) ;

	var node = _mbList[index];

	if (!node.nextSibling)
		return;
	var nextSibling = node.nextSibling;
	node.remove ();
	node.insertAfter (nextSibling);

	if (_mbTree == node)
		_mbTree = nextSibling;

	mbShowTree ();
	mbSetSelectedIndex (mbGetNodeIndex (node));

	return $Notify( 'Item moved <b>Successfully</b>' ) ;
}

//
// indent the current node
//
function mbIndent ()
{
	var index = mbGetSelectedIndex ();
	if (index < 0)
		return $Notify( 'Please <b>Select an Item to Move.</b>' ) ;

	var node = _mbList[index];

	if (!node.prevSibling || node.prevSibling.icon == _cmSplit)
		return;

	var prevSibling = node.prevSibling;

	node.remove ();
	prevSibling.addChild (node);

	mbShowTree ();
	mbSetSelectedIndex (mbGetNodeIndex (node));
	
	return $Notify( 'Item moved <b>Successfully</b>' ) ;
}

//
// unindent the current node
//
function mbUnindent ()
{
	var index = mbGetSelectedIndex ();
	if (index < 0)
		return $Notify( 'Please <b>Select an Item to Move.</b>' ) ;

	var node = _mbList[index];

	if (!node.parentNode)
		return;

	var parentNode = node.parentNode;

	node.remove ();

	node.insertAfter (parentNode);

	mbShowTree ();
	mbSetSelectedIndex (mbGetNodeIndex (node));
	
	return $Notify( 'Item moved <b>Successfully</b>' ) ;
}

//
// delete the current node
//
function mbDelete ()
{
	var index = mbGetSelectedIndex ();
	if (index < 0)
		return $Notify( 'Please <b>Select an Item to Delete</b>.' ) ;

	var node = _mbList[index];

	if (_mbTree == node)
		_mbTree = node.nextSibling;

	node.remove ();
	mbShowTree ();
	if (!mbSetSelectedIndex (index))
		mbSetSelectedIndex (index - 1);
		
	return $Notify( 'Item Deleted <b>Successfully</b>' ) ;
}

//
// find the index of a node in _mbList
//
function mbGetNodeIndex (node)
{
	var i;
	for (i = 0; i < _mbList.length; ++i)
		if (_mbList[i] == node)
			return i;
	return -1;
}

//
// show the description of the current item being hovered
//
function mbOptionHover (obj)
{
	var descript;
	var node = _mbList[obj.index];
	if (node.icon == _cmSplit)
		descript = 'menu split';
	else
		descript = node.icon + ',' + node.title + ',' + node.link + ',' + node.target + ',' + node.descript;
	window.defaultStatus = descript;
}

//
// clear the status
//
function mbClearStatus ()
{
	window.defaultStatus = '';
}

//
// when user selected something in <select>
//
function mbSelectOnChange ()
{
	var index = mbGetSelectedIndex ();
	var node = _mbList[index];
	if (node.icon == _cmSplit)
		return;
	cmGetObject ('icon').value = node.icon ? node.icon : '';
	cmGetObject ('title').value = node.title ? node.title : '';
	cmGetObject ('link').value = node.link ? node.link : '';
	cmGetObject ('target').value = node.target ? node.target : '';
	cmGetObject ('descript').value = node.descript ? node.descript : '';
}

/*
	Menu Builder for JSCookMenu and JSCookTree
		v1.0. (c) Copyright 2002 by Heng Yuan

	Permission is hereby granted, free of charge, to any person obtaining a
	copy of this software and associated documentation files (the "Software"),
	to deal in the Software without restriction, including without limitation
	the rights to use, copy, modify, merge, publish, distribute, sublicense,
	and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	DEALINGS IN THE SOFTWARE.
*/
