var isMSIE = document.attachEvent != null;
var isGecko = !document.attachEvent && document.addEventListener;

document.popups = [];
hideAllPopups = function() {hide(document.popups); document.popups = [];}

var Types = {
	isArray: function (variable) {
		 return ((typeof variable == 'object') && (variable.constructor == Array));
	},
	isStr: function(variable) {
		return ((typeof variable == 'string') && (variable.constructor == String));
	}
}

hide = function(popups) {
	if (!Types.isArray(popups)) popups = [popups];
	for (var q = popups.length; q--;) {
		if (typeof popups[q].hide != 'undefined') popups[q].hide();
		else popups[q].style.display = 'none';
	}
}
if (isIE) {
	document.attachEvent('onclick', hideAllPopups);
}
if (isGecko) {
	document.addEventListener('click', hideAllPopups, false);
}


var isIE = document.attachEvent != null && document.all && window.navigator.userAgent.search(/opera/gi) == -1;
var isGecko = !document.attachEvent && document.addEventListener;

var currMenu = null;
var hardCodeParameter = (isIE) ? 13 : 3;

function showSubMenu(caller) {
	if (currMenu != null) {
		currMenu.subMenu.style.display = 'none';
		ClassName.remove(currMenu, 'sel');		
	}
	var coords = Geom.getCoords(caller);
	caller.subMenu = caller.getElementsByTagName('div')[0] || null;
	if (caller.subMenu == null) {
		currMenu = null;
		return;
	}
	caller.subMenu.style.left = coords.x;
	caller.subMenu.style.top  = coords.y + caller.offsetHeight - hardCodeParameter;
//	caller.subMenu.style.width = caller.offsetWidth;
	caller.subMenu.style.display = '';
	ClassName.add(caller, 'sel');
	document.popups.push(caller.subMenu);
	if (caller.subMenu.initClick) {}
	else {
		caller.subMenu.onclick = function(event) {
			event = event || window.event;
			event.cancelBubble = true;
			//event.stopPropagation();
		}
	}
	if (caller.timer) {
		clearTimeout(caller.timer);
		caller.timer = false;
	}
	currMenu = caller;
}
function hideSubMenu(caller) {
	if (caller.subMenu == null) return;
	if (!caller.timer) caller.timer = setTimeout(function() {caller.subMenu.style.display = 'none'; ClassName.remove(caller, 'sel'); caller.timer = false;}, 100);	
}

var Geom = {
	getSize: function(element) {
		return {width: element.offsetWidth, height: element.offsetHeight}
	},
	getCoords: function(element) {
		var parent = element.offsetParent;
		var coords = {x: 0, y: 0};
		while (element)	{
			coords.x += element.offsetLeft;
			coords.y += element.offsetTop;
			element = element.offsetParent;
		}
		return coords;
	}
}
/* CLASS NAME -------------------------------------------*/
var ClassName = {
    test: function(elem, name) {
        if(!elem) return false;
        if(!name) return true;
        return this._rName(name).test(elem.className);
    },
    set: function(elem, name) {
        if(!elem) return;
        if(elem.className != name) elem.className = name;
    },
    add: function(elem, name) {
        if (!elem) return;
        if (!this.test(elem, name)) elem.className += ' ' + name;
    },
    remove: function(elem, name) {
        if (!elem) return;
        if (this.test(elem, name)) elem.className = normalizeSpace(elem.className.replace(this._rName(name), '$1$2'));
    },
    assign: function(elem, name, test) {
        if (!elem) return;
        if (test) this.add(elem, name);
        else this.remove(elem, name);
    },
    replace: function(elem, find, replace) {
        if (!elem) return;
        if (this.test(elem, find)) elem.className = elem.className.replace(this._rName(find), '$1'+replace+'$2');
    },
    swap: function(elem, one, two) {
        if (!elem) return;
        if (this.test(elem, one)) this.replace(elem, one, two);
        else if (this.test(elem, two)) this.replace(elem, two, one);
        else this.add(elem, one);
    },
    _rName: function(name) {
        return new RegExp('(^|\\s+)'+escapeRegexp(name)+'(\\s+|$)','');
    }
};
function escapeRegexp(text) {
	return text.replace(/([\|\!\[\]\^\$\(\)\{\}\+\=\?\.\*\\])/g,"\\$1");
}
function normalizeSpace(str) {
	return trim(str.replace(/[\s\xA0]{2,}/g,' '));
}
function trim(str) {
	return str.replace(/^[\s\xA0]+/,'').replace(/[\s\xA0]+$/,'');
}
