
var DropDownNav = Class.create({
	initialize: function (ul, opts) {
		this.ul = $(ul);
		this.opts = {
			img_over_suffix: false
		};
		this.items = [];
		Object.extend(this.opts, opts || { });
		this.ul.childElements().each(function (li) {
			this.items.push(new DropDownNavItem(li, this));
		}.bind(this));
		this.ul.setStyle({
			zIndex: '99'
		});
	},
	ul: null,
	items: null
});
var DropDownNavItem = Class.create({
	initialize: function (li, nav) {
		this.li = $(li);
		this.nav = $(nav);
		this.a = this.li.down('a');
		if (this.li.down('ul')) {
			this.subul = this.li.down('ul');
			this.subul.observe('mouseover', function () {
				this.into_nav();
			}.bind(this));
			this.subul.observe('mouseout', function () {
				this.out_of_nav();
			}.bind(this));
		}
		new PeriodicalExecuter(function () {
			this.maybe_hide();
		}.bind(this), .25);
		this.a.observe('mouseover', function () {
			this.into_nav();
		}.bind(this));
		this.a.observe('mouseout', function () {
			this.out_of_nav();
		}.bind(this));

		// Preload
		if (this.nav.opts.img_over_suffix && this.a.down('img') && !this.a.innerHTML.match(/AlphaImageLoader/)) {
			this.image_mouseover = true;
			this.image = this.a.down('img');
			this.src_out = this.image.src;
			this.src_over = this.src_out.replace(/^(.*)\/([^\/]+)(\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG))$/g, "$1/$2"+this.nav.opts.img_over_suffix+"$3"); // ^(.*)/([^/]+)(\.(gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG))$
//			  alert(this.src_over);
			var preload = new Image;
			preload.src = this.src_over;
		}
	},
	nav: null,
	li: null,
	subul: null,
	in_nav: false,
	opts: null,
	src_over: null,
	src_out: null,
	image: null,
	image_mouseover: false,
	into_nav: function () {
		this.show();
		this.in_nav = true;
	},
	out_of_nav: function () {
		this.in_nav = false;
	},
	show: function () {
		if (this.image_mouseover) {
			this.image.src = this.src_over;
		}
		if (this.subul) {
			this.subul.show();
			this.nav.items.each(function (item) {
				if (item.subul && this != item) {
					item.subul.hide();
				}
			}.bind(this));
		}
	},
	hide: function() {
		if (this.image_mouseover) {
			this.image.src = this.src_out;
		}
		if (this.subul) {
			this.subul.hide();
		}
	},
	maybe_hide: function() {
		if (this.in_nav == false) {
			this.hide();
		}
	}
});