/*----------------------------------------------------  
 *  MysetNavigation javascript control
 *  Language: Java Script 
 *  Version: 1.0
 *  Author: Cristian Gheorghe Florescu
 ----------------------------------------------------*/
var MysetNavigation = Class.create();
MysetNavigation.prototype = {
	initialize : function() {
		this.init();
	},
	init : function() {
		// alert(parseBoolean(null));
	this.id = "navigation";
	this.align = null;
	this.style = null;
	this.styleClass = "navigation-bar-menu";
	this.activeMenu = null;
	this.activeSubMenu = null;
	this.clickBased = false;
	this.clickBasedPersist = 1;
},

// Properties
	setId : function(val) {
		this.id = val;
	},
	getId : function() {
		return this.id;
	},
	setAlign : function(val) {
		this.align = val;
	},
	getAlign : function() {
		return this.align;
	},
	setStyle : function(val) {
		this.style = val;
	},
	getStyle : function() {
		return this.style;
	},
	setStyleClass : function(val) {
		this.styleClass = val;
	},
	getStyleClass : function() {
		return this.styleClass;
	},
	setClickBased : function(val) {
		this.clickBased = val;
	},
	getClickBased : function() {
		return this.clickBased;
	},
	setClickBasedPersist : function(val) {
		this.clickBasedPersist = val;
	},
	getClickBasedPersist : function() {
		return this.clickBasedPersist;
	},

	// get parent object
	getParent : function(obj) {
		return (typeof obj.parentNode != "undefined") ? obj.parentNode : obj.parentElement;
	},

	// get parent object by className
	getParentByClassName : function(obj, className) {
		if (obj != null) {
			if (obj.className == className)
				return obj;
			var parent = this.getParent(obj);
			while (parent != null && parent.className != "menu") {
				if (parent.className == className)
					return parent;
				parent = this.getParent(parent);
			}
		}
		return null;
	},

	// get child object by className
	getChildByClassName : function(obj, className) {
		if (obj != null) {
			if (obj.className == className)
				return obj;
			var child;
			for ( var i in obj.childNodes) {
				child = obj.childNodes[i];
				if (child.className == className) {
					return obj.childNodes[i];
				}
			}
		}
		return null;
	},

	// hide parents until menu
	hideParents : function(obj) {
		if (obj != null) {
			var parent = obj;
			while (parent != null && parent.className != "menu") {
				if (parent.className == "items_over") {
					parent.className = 'items';
				}
				if (parent.className == "submenu") {
					parent.style.visibility = 'hidden';
				}
				parent = this.getParent(parent);
			}
		}
	},

	handleOnMouseOver : function(e) {
		// alert('handleOnMouseOver');
	if (!e)
		e = window.event;
	var selectedObj = (typeof e.target != "undefined") ? e.target : e.srcElement
	// alert("OVER: "+selectedObj.tagName+":"+selectedObj.id+":"+selectedObj.className);
	var menuObj, activeMenu;
	this.activeObj = selectedObj

	// handle item over
	menuObj = this.getParentByClassName(selectedObj, "item");
	if (menuObj != null) {
		menuObj.className = "item_over";
	}
	menuObj = this.getParentByClassName(selectedObj, "items");

	// handle items over
	if (menuObj != null)
		menuObj.className = "items_over";
	else
		menuObj = this.getParentByClassName(selectedObj, "items_over");
	if (menuObj != null) {
		// handle popup main items over
	activeMenu = this.getChildByClassName(menuObj, "main");
	if (activeMenu != null) {
		activeMenu.style.visibility = 'visible';
		activeMenu.onmouseover = function() {
			this.style.visibility = 'visible';
		};
		activeMenu.onmouseout = function() {
			this.style.visibility = 'hidden';
		};
		if (this.activeMenu != null && this.activeMenu != activeMenu) {
			if (this.activeMenu != null) {
				this.hideParents(this.activeSubMenu);
				this.activeSubMenu = null;
				this.activeMenu.style.visibility = 'hidden';
			}
		}
		this.activeMenu = activeMenu;
	}

	// handle popup submenu items over
	activeMenu = this.getChildByClassName(menuObj, "submenu");
	if (activeMenu != null) {
		activeMenu.style.visibility = 'visible';
		activeMenu.onmouseover = function() {
			this.style.visibility = 'visible';
		};
		activeMenu.onmouseout = function() {
			this.style.visibility = 'hidden';
		};
		this.activeSubMenu = activeMenu;
	}

}

},

handleOnMouseOut : function(e) {
// alert('handleOnMouseOut');
	if (!e)
		e = window.event;
	var menuObj, selectedObj, activeMenu;
	selectedObj = (typeof e.target != "undefined") ? e.target : e.srcElement;
	// alert("OUT: "+selectedObj.tagName+":"+selectedObj.id+":"+selectedObj.className);

	// handle item out
	menuObj = this.getParentByClassName(selectedObj, "item_over");
	if (menuObj != null) {
		menuObj.className = "item";
	} else {
		// handle items out
	menuObj = this.getParentByClassName(selectedObj, "items_over");
	if (menuObj != null) {
		menuObj.className = "items";
		if (this.activeMenu != null)
			this.activeMenu.style.visibility = 'hidden';
		if (this.activeSubMenu != null)
			this.activeSubMenu.style.visibility = 'hidden';
	}
}
},

handleOnClick : function(e) {
// alert('handleOnClick');
	if (!e)
		e = window.event;
	var menuObj, menuObj1, selectedObj, activeMenu;
	selectedObj = (typeof e.target != "undefined") ? e.target : e.srcElement;
	// alert("CLICK: "+selectedObj.tagName+":"+selectedObj.id+":"+selectedObj.className);
	this.activeObj = selectedObj;

	// handle main click
	menuObj = this.getParentByClassName(selectedObj, "item");
	if (menuObj == null) {
		menuObj = this.getParentByClassName(selectedObj, "items");
		if (menuObj != null) {
			menuObj.className = "items_over";
			menuObj1 = this.getChildByClassName(menuObj, "submenu");
			if (menuObj1 != null) {
				menuObj1.style.display = "block";
			}

		} else {
			menuObj = this.getParentByClassName(selectedObj, "items_over");
			if (menuObj != null) {
				menuObj.className = "items";
				menuObj1 = this.getChildByClassName(menuObj, "submenu");
				if (menuObj1 != null) {
					menuObj1.style.display = "none";
				}
			}
		}
	}
	// save state
	// this.savestate(this.id, this.clickBasedPersist);

},

startContent : function() {
	writeLine('');
	writeLine('<!-- ' + this.styleClass + ' -->');
	write('<div class="' + this.styleClass + '"');
	if (this.align != null)
		write(' align="' + this.align + '"');
	if (this.style != null)
		write(' style="' + this.style + '"');
	if (parseBoolean(this.clickBased))
		writeLine(' onclick="' + this.id + '.handleOnClick(event)">');
	else
		writeLine(' onmouseover="' + this.id + '.handleOnMouseOver(event)" onmouseout="' + this.id + '.handleOnMouseOut(event)">');
},

endContent : function() {
	writeLine('</div>');
	// restore state
	// this.restorestate(this.id);
},

setCookie : function(name, value, days) {
	var expireDate = new Date();
	var expstring = expireDate.setDate(expireDate.getDate() + parseInt(days));
	document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString() + "; path=/";
},

getCookie : function(name) {
	var re = new RegExp(name + "=[^;]+", "i");
	if (document.cookie.match(re))
		return document.cookie.match(re)[0].split("=")[1];
	return "";
},

savestate : function(treeid, durationdays) {
	alert("savestate:"+treeid);
	if (document.getElementById(treeid) != null) {
		var ultags = document.getElementById(treeid).getElementsByTagName("ul");
		var openuls = new Array();
		for ( var i = 0; i < ultags.length; i++) {
			if (ultags[i].parentNode.className == "items_over")
				openuls[openuls.length] = i;
		}
		if (openuls.length == 0)
			openuls[0] = "nothing";
		this.setCookie(treeid, openuls.join(","), durationdays);
	}
},

restorestate : function(treeid) {
	alert("restorestate:"+treeid);
	if (document.getElementById(treeid) != null) {
		var ultags = document.getElementById(treeid).getElementsByTagName("ul");
		var openuls = new Array();
		if (this.getCookie(treeid) != "")
			openuls = this.getCookie(treeid).split(",");
		for ( var i = 0; i < ultags.length; i++) {
			for ( var j = 0; j < openuls.length; j++) {
				if (openuls[j] == i) {
					openuls[j].parentNode.className == "items_over";
					openuls[j].style.display = "block";
				}
			}
		}
	}
}

}

