///////////////////////////////////////////////////////////////////
///																///
///	tk.esterni, this is a gallery box application				///
///	developed by triknine										///
///																///
///////////////////////////////////////////////////////////////////

/// THIS IS A GALLERY MENU GLOBAL VARS
var thisisagallerymenu_Menus = new Array();

function menuElement(id,reference,title) {
	this.id = id;
	this.thisisagallerymenu_Menus_id = (thisisagallerymenu_Menus.length);
	this.reference = reference;
	this.title = title;
	this.isMenuElement = true;
	this.children = new Array();
	this.parent = null;
	this.isOpen = true;
	this.containers = new Array();
	this.parentContainer = null;
	this.haveImages = false;
	this.haveVideos = false;
	thisisagallerymenu_Menus.push(this);
	
	this.add = function (menuElement) {
	 	if (menuElement.isMenuElement==true) {
	 	 	menuElement.parent = this;
		  	this.children.push(menuElement); return true;
		} else {return false;}
	}
	
	this.render = function(obj) {
	 	var children = this.children.length;
		var br = document.createElement("br");
		var i = 0;
		for (i=0;i<children;i++) {
		 	var container = document.createElement("div");
		 	var element = document.createElement("span");
			var link = document.createElement("a");
			link.setAttribute("href","javascript:thisisagallerymenu_Menus["+this.children[i].thisisagallerymenu_Menus_id+"].openOrClose()");
			var title = document.createTextNode(this.children[i].title);
			link.appendChild(title);
			element.appendChild(link);
			container.appendChild(element);
			this.children[i].render(container);
			if (this.isOpen) obj.appendChild(container);
			this.containers.push(container);
		}
		this.parentContainer = obj;
	}
	
	this.openOrClose = function () {
	 	
	 	if (this.isOpen) {
	 	 	if (this.children.length>0) {
	 	 		this.isOpen = false;
				//return this.parentContainer.firstChild.innerHTML;
				for (j=0;j<this.containers.length;j++) {
					this.parentContainer.removeChild(this.containers[j]);
				}}	
		} else {
		 	if (this.containers.length>0) this.isOpen = true;
		 	for (j=0;j<this.containers.length;j++) { 	
			 	this.parentContainer.appendChild(this.containers[j]);
			}
		}
		if (this.haveImages == true) ajaxpage("./gallery.php?ref="+this.reference,"contentGallery");
		if (this.haveVideos == true) ajaxpage("./videogallery.php?ref="+this.reference,"contentGallery");
	}
}