if (typeof(BASESOFT) == 'undefined')
	BASESOFT = {};

if (typeof(BASESOFT.Doors) == 'undefined')
	BASESOFT.Doors = {};

BASESOFT.Doors.util = {

	/**
	 * getElementById cross browser wrapper
	 */
	returnObjById: function(id) {
		if (document.getElementById)
			var returnVar = document.getElementById(id);
		else if (document.all)
			var returnVar = document.all[id];
		else if (document.layers)
			var returnVar = document.layers[id];
		return returnVar;
	},

	/**
	 * Return the html elements with the given tag that are inside the given parent
	 * @param {HTMLElement} parent: element in which we look for tags
	 * @param {String} tag: the tag name we're looking for
	 * @return {Array}: list of elements with the given tag
	 */
	returnObjByTag: function(parent, tag) {
		var i, e;
		var res = [];
		for (i=0; i<parent.children.length; i++) {
			e = parent.children[i];
			if (e.tagName.toLowerCase() == tag.toLowerCase())
				res.push(e);
		}
		return res;
	},

	/**
	 * cross browser method for canceling the bubleing of an event
	 */
	cancelBubble: function(event) {
		var evt = event ? event : window.event;
		if (evt.stopPropagation)
			evt.stopPropagation();
		if (evt.cancelBubble!=null)
			evt.cancelBubble = true;
	},

	/**
	 * Method for clearing an html node
	 * @param {HTMLElement} the element that needs to be cleared
	 */
	clearNode: function(element) {
		while (element.firstChild!==null)
			element.removeChild(element.firstChild); // remove all existing content
	},

	/**
	 * hide any opened submenus
	 */
	hideSubmenus: function() {
		var menu_ul = this.returnObjById('first-level-menu');
		var i, li, a, submenu;
		for (i=0; i<menu_ul.children.length; i++) {
			li = menu_ul.children[i];
			submenu = this.returnObjByTag(li, 'ul');
			if (submenu.length > 0) {
				submenu[0].style.display = 'none';
			}
		}
	},

	/**
	 * Show the submenu for the given element
	 * @param {HTMLElement} elem: the li item that contains the submenu
	 */
	showSubmenu: function(elem) {
		this.hideSubmenus(); // hide any previously opened submenus
		var submenu = this.returnObjByTag(elem, 'ul');
		if (submenu.length > 0) {
			submenu = submenu[0];
			submenu.style.display = 'block';
		}
	},

	/**
	 * Initialize the menu events so that on hover the submenus open properly
	 */
	initMenu: function() {
		var menu_ul = this.returnObjById('first-level-menu');
		var i, li, a, submenu;
		for (i=0; i<menu_ul.children.length; i++) {
			li = menu_ul.children[i];
			submenu = this.returnObjByTag(li, 'ul');
			if (submenu.length > 0) {
				// there's a submenu, so it needs to be shown on hover
				li.onmouseover = new Function("BASESOFT.Doors.util.showSubmenu(this)");
			}
		}
	},

	/**
	 * Show a simple dialog with some information and a close button. Similar to the showDialog that displays a modal
	 * dialog, but this one adds a class to the container also, to make it smaller and non-blocking for navigation
	 * @param {String} elem_id. The id of the html element that contains the html that needs to be shown in the dialog
	 * @param {String} class_name. optional class that should be added to the dialog
	 * @param {Array} elements. Optional. a list containing element ids that we need to navigate to/from
	 */
	showSimpleDialog: function(event, elem_id, class_name, elements) {
		this.showDialog(event, elem_id, class_name, 'non-blocking', elements);
	},

	/**
	 * Show a modal dialog with some information and a close button
	 * @param {String} elem_id. The id of the html element that contains the html that needs to be shown in the dialog
	 * @param {String} class_name. optional class that should be added to the dialog
	 * @param {String} container_class_name. optional class that should be added to the dialog container
	 * @param {Array} elements. Optional. a list containing element ids that we need to navigate to/from
	 */
	showDialog: function(event, elem_id, class_name, container_class_name, elements) {
		
		// cancel the bubble. no need to propagate this further
		if (event)
			this.cancelBubble(event);

		var d = this.returnObjById('modal-dialog');
		if (!d) {
			d = document.createElement('div');
			d.id = 'modal-dialog';
		} else {
			this.clearNode(d);
		}
		if (container_class_name)
			d.className = container_class_name;

		var elem = this.returnObjById(elem_id); // element that contains what we need to show

		var b = document.createElement('div'); // body where the dialog shows the content
		if (class_name)
			b.className = class_name;

		var c = document.createElement('div'); // content with scrollbar
		c.innerHTML = elem.innerHTML;
		b.appendChild(c);
		d.appendChild(b);

		// close button
		var close = document.createElement('img');
		close.className = 'close';
		close.src = '/media/template/transp.gif';
		close.alt = 'X';
		close.title = 'Close';
		b.appendChild(close);

		// previous & next links
		if (elements) {
			var pid; // id of the current element for the previous link
			var nid; // id of the current element for the next link
			var i, cn='', ccn='';
			var eparam=""; // the param with the list of elements
			var params; // all the params that go in the prev&next links

			for (i=0; i<elements.length; i++) {
				
				if (elements[i] == elem_id) {
					if (i == 0) {
						pid = ''; // no previous link for the first element in the list
					} else {
						pid = elements[i-1];
					}

					if (i == elements.length-1) {
						nid = ''; // no next link for the last element in the list
					} else {
						nid = elements[i+1];
					}
				}
				eparam = eparam + "'" + elements[i] + "'";
				if (i<elements.length-1)
					eparam = eparam + ",";
			}

			if (class_name)
				cn = class_name;
			if (container_class_name)
				ccn = container_class_name;

			// prev link
			if (pid) {
				var prev = document.createElement('div');
				prev.className = 'previous-link';
				params = "'" + pid + "','"+cn+"','"+ccn+"',["+eparam+"]";
				prev.onclick = new Function("BASESOFT.Doors.util.showDialog(null, "+ params +")");
				b.appendChild(prev);
			}

			// next link
			if (nid) {
				var next = document.createElement('div');
				next.className = 'next-link';
				params = "'" + nid + "','"+cn+"','"+ccn+"',["+eparam+"]";
				next.onclick = new Function("BASESOFT.Doors.util.showDialog(null, "+ params +")");
				b.appendChild(next);
			}
			
			// pages
			var pages = document.createElement('div');
			pages.className = 'pages';
			b.appendChild(pages);
			pages.style.paddingLeft = 570 - ((elements.length+2) * 20) + 'px';
			
			var x = document.createElement('div');
			x.className = 'box';
			pages.appendChild(x);
			
			if (pid) {
				var a = document.createElement('a');
				a.innerHTML = '&lt;';
				a.href = '#';
				params = "'" + pid + "','"+cn+"','"+ccn+"',["+eparam+"]";
				a.onclick = new Function("BASESOFT.Doors.util.showDialog(null, "+ params +"); return false;");
				x.appendChild(a);
			} else {
				var a = document.createElement('span');
				a.innerHTML = '&lt;';
				x.appendChild(a);
			}
			
			for (var i=0; i<elements.length; i++) {
				
				var str = i+1;
				
				var x = document.createElement('div');
				x.className = 'box';
				pages.appendChild(x);
				
				if (elements[i] == elem_id) {
					var a = document.createElement('span');
					a.innerHTML = str;
					x.appendChild(a);
				} else {
					var a = document.createElement('a');
					a.innerHTML = str;
					a.href = '#';
					params = "'" + elements[i] + "','"+cn+"','"+ccn+"',["+eparam+"]";
					a.onclick = new Function("BASESOFT.Doors.util.showDialog(null, "+ params +"); return false;");
					x.appendChild(a);
				}
			}

			var x = document.createElement('div');
			x.className = 'box';
			pages.appendChild(x);
			
			if (nid) {
				var a = document.createElement('a');
				a.innerHTML = '&gt;';
				a.href = '#';
				params = "'" + nid + "','"+cn+"','"+ccn+"',["+eparam+"]";
				a.onclick = new Function("BASESOFT.Doors.util.showDialog(null, "+ params +"); return false;");
				x.appendChild(a);
			} else {
				var a = document.createElement('span');
				a.innerHTML = '&gt;';
				x.appendChild(a);
			}
			
			
			
		}

		var holder = this.returnObjById('container');
		close.onclick = new Function("BASESOFT.Doors.util.closeDialog()");

		// extra layer for closing the popup
		var closeLayer = document.createElement('div');
		closeLayer.className = 'close-layer';
		closeLayer.onclick = new Function("BASESOFT.Doors.util.closeDialog()");
		b.appendChild(closeLayer);

		holder.appendChild(d);
	},

	/**
	 * Hide the modal dialog
	 */
	closeDialog: function(event) {
		var dialog = this.returnObjById('modal-dialog');
		if (dialog) {
			dialog.parentNode.removeChild(dialog);
		}
	},

	/**
	 * Shows the content of a hidden element and hide the link
	 * @param {Object} elem_id: id of the element that needs to be shown
	 * @param {Object} link_elem: element with the link. if given, it will remove the element from the dom
	 */
	showHidden: function(elem_id, link_elem) {
		var elem = this.returnObjById(elem_id); // element that contains what we need to show
		elem.className = ''; // TODO: maybe change this later in case we have some classes that we want to keep

		// check if we need to remove the paragraph that contains the read more link
		if (link_elem)
			link_elem.parentNode.parentNode.removeChild(link_elem.parentNode);
	},

	/**
	 * show or hide the given elements
	 * @param {Array} list of element ids
	 * @param {Bool} true if the visibility of each item should be checked. otherwise only the first one is used
	 * @param {String} the style of the visibility. if not specified, default is 'block'
	 */
	toggleVisibility: function(elements, toggle_each, visible_style) {
		if (elements.length === 0)
			return;

		toggle_each = toggle_each || false;
		visible_style = visible_style || 'block';

		var i, elem, visible;
		var first = this.returnObjById(elements[0]);

		if (first.style.display == 'none') {
			visible = visible_style;
		} else {
			visible = 'none';
		}

		for (i=0; i<elements.length; i++) {
			elem = this.returnObjById(elements[i]);

			// check the toggle_each param. if false, we use the visibility of the first elem
			if (toggle_each && i>0) {
				if (elem.style.display == 'none') {
					visible = visible_style;
				} else {
					visible = 'none';
				}
			}

			elem.style.display = visible;
		}
	},

	/**
	 * Initialize the menu
	 * needs to be called at body load if we want menus with javascript
	 * (currently switched back to css)
	 */
	init: function() {
		this.initMenu();
		document.body.onclick = new Function("BASESOFT.Doors.util.hideSubmenus()");
	}
};

