

window.addEvent('domready', function() {
	load_menu();
	fix_pngs();
	loadRolloverImages();
});



function load_menu() {
	var t = $('mainmenu');
	if(!t) return;

	var alist = t.getElementsByTagName('a');
	for(var i=0; i<alist.length; ++i) {
		if(alist[i].getAttribute('showmenu')) {
			new Menu(alist[i]);
		}
		else {
			alist[i].onmouseover = hideAllMenus;
		}
	}
}

function hideAllMenus() {
	for (var i=0; i<window._MENUS_.length; ++i) {
		window._MENUS_[i].hideMenu();
	}
}



function Menu(a) {
	this.anchor = $(a);
	this.anchortd = $(a.parentNode);
	this.submenudiv = $(this.anchor.getAttribute('showmenu'));;
	this.timer = null;
	this.fx = null;

	if(!window._MENUS_) window._MENUS_ = new Array();
	window._MENUS_.push(this);
	this.index = window._MENUS_.length -1;
	var index = this.index;

	this.anchortd.onmouseover = function() {
		window._MENUS_[index].showMenu();	

	}
	this.anchortd.onmouseout = function() {
		window._MENUS_[index].startHideMenu();
	}

	var lst = this.submenudiv.getElementsByTagName('td');
	for(var i=0; i<lst.length; ++i) {
		lst[i].onmouseover = function() {
			this.className = 'hover';
			window._MENUS_[index].clearTimer();
		}
		lst[i].onmouseout = function() {
			this.className = '';
			window._MENUS_[index].startHideMenu();
		}
	}



	this.showMenu = function() {
		this.clearTimer();
		if(this.submenudiv.getStyle('display') == 'block') return;
		if(this.fx) this.fx.cancel();


		this.submenudiv.setStyle('top', ( this.anchortd.getTop() + this.anchortd.offsetHeight + 1 ) );
		this.submenudiv.setStyle('left', ( this.anchortd.getLeft() + 0 ) );

		this.submenudiv.setStyle('opacity', 0);
		this.submenudiv.setStyle('display', 'block');

		this.fx = new Fx.Morph( this.submenudiv, {
			duration: 200

		}).start({
			opacity: 1
		});
		
	}

	this.startHideMenu = function() {
		this.timer = setTimeout('window._MENUS_['+this.index+'].hideMenu();', 500);

	}

	this.hideMenu = function() {
		this.clearTimer();
		if(this.submenudiv.getStyle('display') == 'none') return;
		if(this.fx) this.fx.cancel();

		this.fx = new Fx.Morph( this.submenudiv, {
			duration: 200,
			onComplete: function() {
				this.element.setStyle('display', 'none');
			}

		}).start({
			opacity: 0
		});
	}


	this.clearTimer = function() {
		clearTimeout(this.timer);
	}


}


function fix_pngs() {
	if(navigator.appName.indexOf('Microsoft') == -1) return;

	var imgs = document.getElementsByTagName('img');
	for(var i=0; i<imgs.length; ++i) {
		if(imgs[i].src.indexOf('.png') > 0 && imgs[i].src.indexOf('xparent') == '-1') {

			var src = imgs[i].src;
			var w = imgs[i].offsetWidth;
			var h = imgs[i].offsetHeight;
			imgs[i].src = 'images/xparent.png';
			imgs[i].width = w;
			imgs[i].height = h;
			imgs[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale');";

		}
	}
}



function loadRolloverImages() {

	var allimgs = document.getElementsByTagName('img');
	for(var i=0; i<allimgs.length; ++i) {
		if(allimgs[i].getAttribute('hoversrc')) {
			_loadRolloverImage(allimgs[i]);
		}
	}

	var allinputs = document.getElementsByTagName('input');
	for(var i=0; i<allinputs.length; ++i) {
		if(allinputs[i].getAttribute('hoversrc')) {
			_loadRolloverImage(allinputs[i]);
		}
	}
}

function _loadRolloverImage(img) {
	preloadImage(img.getAttribute('hoversrc'));

	img.onmouseover = function() {
		var src = this.getAttribute('src');
		if(src) {
			this.setAttribute('__src_orig', src);
			this.setAttribute('__filter_orig', this.style.filter);

			if(this.style.filter && navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0) {
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.getAttribute('hoversrc')+"', sizingMethod='scale')";
			} else {
				this.setAttribute('src', this.getAttribute('hoversrc'));
			}
		}
	};
	img.onmouseout = function() {
		var src = this.getAttribute('__src_orig');
		if(src) {
			if(this.style.filter && navigator.userAgent.toUpperCase().indexOf('MSIE') >= 0) {
				this.style.filter = this.getAttribute('__filter_orig');
			} else {
				this.setAttribute('src', src);
			}
		}
	};

}


function preloadImage(href) {
	if(!window.preloadImageList) window.preloadImageList = new Array();
	var i = window.preloadImageList.length;

	window.preloadImageList[i] = new Image();
	window.preloadImageList[i].src = href;
}



