var md = function () {
	/* //////////// ==== private methods ==== //////////// */
	var instance = null;
	/* ===== custom getElementsByClass ==== */
	function getElementsByClass (object, tag, className) {
		var o = object.getElementsByTagName(tag);
		for ( var i = 0, n = o.length, ret = []; i < n; i++)
			if (o[i].className == className) ret.push(o[i]);
		if (ret.length == 1) ret = ret[0];
		return ret;
	}
	/* ===== crossbrowsers addEvent ==== */
	function addEvent (o, e, f) {
		if (window.addEventListener) o.addEventListener(e, f, false);
		else if (window.attachEvent) r = o.attachEvent('on' + e, f);
	}
	
	
	/* //////////// ==== MenuDyna Constructor ==== //////////// */
	function MenuDyna(oCont, selitem, start, interval) {
		
		this.items      = [];
		this.oCont      = oCont;
		this.selItem      = selitem;
		this.oc         = document.getElementById(oCont);
		this.oc.parent  = this;
		
		//this.firstIsOpen = 0;
		this.view = null;
		
		this.time_start = start * 62.5 || 0;
		this.time_inter = interval * 62.5 || 0;
		this.time_out   = this.time_start;
		this.time       = 0;
				
		this.oc.onselectstart = function () { return false; }
		
		
		var itms   = getElementsByClass(this.oc, 'div', 'menu');
		this.NB = itms.length;
		if(typeof(this.NB)=='undefined'){
			this.NB = 1;
			this.items[0] = new Items(this, 0, itms);
			this.view = this.items[0];
		}else{
			for (var i = 0, o; o = itms[i]; i++) {
				this.items[i] = new Items(this, i, o);
				if (selitem == i){
					this.view = this.items[i];
				}
			}
		}

		
	}
	/* //////////// ==== MenuDyna prototype ==== //////////// */
	MenuDyna.prototype = {
		init : function () {
			
			for (var i = 0; i<this.NB; i++) {
				this.items[i].init();
				
			}

			//this.oc.style.visibility = 'visible';
			//this.oc.style.display = 'block';		
			
		}
	}
	/* //////////// ==== Items Constructor ==== //////////// */
	Items = function (parent, N, obj) {
		
		this.subitems      = [];
		this.parent           = parent;
		this.N                = N;
		
		this.oButton  = getElementsByClass(obj, 'div', 'menu_button');
		if (!this.oButton.className){
			this.oButton  = getElementsByClass(obj, 'div', 'menu_button_sel');
		}
		this.oButton.parent = this;
		this.oButton.onclick = function () { this.parent.open();	}
		

		this.oContener = getElementsByClass(obj, 'div', 'sub_menu_contener');
		
		
		
		/*if (this.N == this.parent.NB-1){
			this.oButton.style.marginBottom = '0px';
		}*/
		
		
						
	}
	/* //////////// ==== Items prototype ==== //////////// */
	Items.prototype = {
		/* ==== HTML rendering ==== */
		init : function () {
			if (this.oContener != ''){
				
				/*if (!this.parent.firstIsOpen){
					//this.oContener.style.display = 'block';
					this.parent.firstIsOpen = 1;
					this.parent.view = this;
				}*/
			}
		},
		open : function () {
			var ref  = getElementsByClass(this.oButton, 'a', 'menu_link');
			if (ref.href){
				window.open(ref.href, '_self');
			}
				
			
			if (this.oContener != ''){
				this.parent.view.oContener.style.display = 'none';
				this.oContener.style.display = 'block';
				
				//this.parent.view.oButton.className = "menu_button";
				//this.oButton.className = "menu_button_sel";
				this.parent.view = this;
			}
		}
		
	}


	/* //////////// ==== public methods ==== //////////// */
	return {
		/* ==== initialize script ==== */
		create : function (div, selitem, start, interval) {
			/* ---- instanciate MenuDyna ---- */
			var load = function () {
				
				var loaded = false;
				if (instance != null && instance.oCont == div) loaded = true;
				
				if (!loaded) {
					
					instance = new MenuDyna(div, selitem, start, interval);
					
					/* ---- init script (once) ---- */
					if (!md.initialized) {
						md.initialized = true;
						
						/* ---- set init ---- */
						setTimeout(function () {
							instance.init();
						}, 16);

						/* ---- set interval loop ---- */
						/*setInterval(function () {
							instance.run();
						}, 16);*/
					}
				}
				
			}
			/* ---- window onload event ---- */
			addEvent(window, 'load', function () { load(); }); 
		}
	}
}();
