// JavaScript Document

function MenuDropDown (instance,menuElemId,dropdownElemId,SectionIDKey,override_width,offset_left) {
	
	this.instance = instance;
	this.sectionKey = SectionIDKey;
	this.menu_id = menuElemId;
	this.menu = null;
	this.dropdown_id = dropdownElemId;
	this.dropdown = null;
	this.sections = new Array;
	this.hideTimer = null;
	this.or_width = override_width;
	this.or_left = offset_left;
	this.activeMenuItem = null;
	this.currentMenuItem = null;
	
	this.isBrowserCompatible = function() {
		var browser = navigator.appName;
		var b_version = navigator.appVersion;
		if ((browser=="Microsoft Internet Explorer") && (parseFloat(b_version.split("MSIE")[1])<7)) {
			return false;
		} else {
			return true;
		}
	}
	
	this.initialize = function () {
		if (this.isBrowserCompatible()) {
			if(this.menu == null){
				this.configureMenu();
			}
			if(this.dropdown == null){
				this.configureDropDownBox();
			}
		}
	}
	
	this.configureMenu = function () {
		this.menu = document.getElementById(this.menu_id);
		var menuLists = this.menu.getElementsByTagName('UL');
		var menuItems = 0;
		for (i=0; menuLists.length>i; i++) {
			menuItems = menuLists[i].getElementsByTagName('li');
			for (ii=0; menuItems.length>ii; ii++) {
				if (menuItems[ii].getAttribute("class") != null && menuItems[ii].getAttribute("class").search(/active/i) >= 0 && menuItems[ii].getAttribute("class").search(/inactive/i) < 0 ) {
					this.activeMenuItem = menuItems[ii];
					break;
				}
			}
		}
	}
	
	this.configureDropDownBox = function () {
		if (this.or_width != null) {
			var width = this.or_width;
		} else {
			var width = this.menu.clientWidth;
		}
		var offsetObj = this.menu;
		var top = this.menu.offsetTop + this.menu.offsetHeight;
		var left = this.menu.offsetLeft;
		if (this.or_left != null) {
			var left = left + this.or_left;
		}
		this.dropdownbox = document.getElementById(this.dropdown_id);
		this.dropdownbox.style.width = width + 'px';
		this.dropdownbox.style.position = 'absolute';
		this.dropdownbox.style.top = top + 'px';
		this.dropdownbox.style.left = left + 'px';
		this.dropdownbox.style.margin = '0 auto 0 auto';
		for (i=0; this.dropdownbox.childNodes.length>i ;i++) {
			if(this.dropdownbox.childNodes[i].className == "section"){
				this.sections = this.sections.concat(this.dropdownbox.childNodes[i]);
			}
		}
		this.hideDropDownBox;
	}
	
	this.showDropDownBox = function () {
		if (this.isBrowserCompatible()) {
			clearTimeout(this.hideTimer);
			this.initialize();
			this.dropdownbox.style.display = 'block';
			this.deactivateAllMenuItems();
			this.activateCurrentMenuItem();
		}
	}
	
	this.hideDropDownBox = function () {
		if (this.isBrowserCompatible()) {
			this.initialize();
			this.hideTimer = setTimeout(this.instance + ".performDropDownBoxHide()",100);
		}
	}
	
	this.performDropDownBoxHide = function () {
		this.dropdownbox.style.display = 'none';
		this.deactivateAllMenuItems();
		if(this.activeMenuItem != null) {
			if (this.activeMenuItem.className.indexOf("inactive") != -1) {
				this.activeMenuItem.className = this.activeMenuItem.className.replace(" active", "");
				this.activeMenuItem.className = this.activeMenuItem.className.replace(" inactive", "");
			}
			this.activeMenuItem.className += ' active';
		}
	}
	
	this.showSection = function (section_id, elem) {
		if (this.isBrowserCompatible()) {
			var showBox = false;
			if(elem != null) {
				this.currentMenuItem = elem;
			}
			this.initialize();
			for (i=0; this.sections.length>i ;i++) {
				if(this.sections[i].id == this.sectionKey + section_id){
					this.sections[i].style.display = 'block';
					showBox = true;
				} else {
					this.sections[i].style.display = 'none';
				}
			}
			if (showBox) {
				this.showDropDownBox();
			}
		}
	}
	
	this.activateCurrentMenuItem = function () {
		if (this.currentMenuItem != null) {
			if (this.currentMenuItem.className.indexOf("inactive") != -1) {
				this.currentMenuItem.className = this.currentMenuItem.className.replace(" active", "");
				this.currentMenuItem.className = this.currentMenuItem.className.replace(" inactive", "");
			}
			this.currentMenuItem.className += ' active';
		}
	}
	
	this.deactivateAllMenuItems = function () {
		var menuLists = this.menu.getElementsByTagName('UL');
		var menuItems = 0;
		for (i=0; menuLists.length>i; i++) {
			menuItems = menuLists[i].getElementsByTagName('LI');
			for (ii=0; menuItems.length>ii; ii++) {
				if (menuItems[ii].className.indexOf("active") != -1) {
					menuItems[ii].className = menuItems[ii].className.replace(" active", "");
					menuItems[ii].className = menuItems[ii].className.replace(" inactive", "");
				}
				menuItems[ii].className += ' inactive';
			}
		}
	}
	
}