var menus = new Array('subnav_home', 'subnav_buy');

function prodTag(tag)
{
	document.location.href = 'productsearch.ninja?tags=' + tag;
}

function imgTag(tag)
{
	$('imgTagDrop').style.display = 'none';
	$('message').innerHTML = '';
	proxy.call('showTag', tag);
}

function getPos(obj)
{
	var leftPos = obj.offsetLeft;
	var topPos = obj.offsetTop;

	/*while ((obj = obj.offsetParent) != null)
	{
		leftPos += obj.offsetLeft;
		topPos += obj.offsetTop;
	}*/
	return { 'leftPos' : leftPos, 'topPos' : topPos };	
}

function initDrop(btn, drop, align, xOff)
{
	Event.observe(btn, 'mouseover', function(e) {myNav.showMenu(btn, drop, align, xOff);Event.stop(e);});
	Event.observe(btn, 'click', function(e) {myNav.showMenu(btn, drop, align, xOff);Event.stop(e);});
	Event.observe(btn, 'mouseover', function() {$(btn).src = 'img/nav/arrOn.gif';});
	Event.observe(btn, 'mouseout', function() {$(btn).src = 'img/nav/arrOff.gif';});
}

var Nav = Class.create();

Nav.prototype =
{	
	initialize: function(ie)
	{
		if (ie) this.vOff = 2;
		else this.vOff = 0;
		Event.observe(document, 'click', function() {myNav.hideMenu()});
		Event.observe(window, 'resize', function() {myNav.hideMenu()});
	},
	
	showMenu: function(btn, drop, align, xOff)
	{
		if (this.displayed != drop && !this.running)
		{
			this.running = true;
			this.hideMenu();
			this.blindMenu(btn, drop, align, xOff);
		}
	},
	
	blindMenu: function(btn, drop, align, xOff)
	{
		this.position(btn, drop, align, xOff);
		Effect.BlindDown(drop, {duration:0.6, afterFinish: function() {myNav.running = false;}});
		this.displayed = drop;
	},
	
	hideMenu: function()
	{
		if (this.displayed) $(this.displayed).style.display = 'none';
		delete(this.displayed);
	},
	
	position: function(srcId, targetId, align, xOff)
	{
		if (!xOff) xOff = -1;
		if (align)
		{
			pos = getPos($(align));
			$(targetId).style.top = pos.topPos + this.vOff + 21 + 'px';
			$(targetId).style.left = xOff + pos.leftPos + 'px';
		}
		else
		{
			pos = getPos($(srcId));
			$(targetId).style.top = pos.topPos + this.vOff + 20 + 'px';
			$(targetId).style.left = xOff + pos.leftPos - 200 + 'px';
		}
	},
	
	keyboardAction: function(e)
	{
		if (e == null)
		{ // ie
			keycode = event.keyCode;
		}
		else
		{ // mozilla
			keycode = e.which;
		}
		key = String.fromCharCode(keycode).toLowerCase();
	},
	
	nav: function(subId)
	{
		this.hideMenu();
		for (var i = 0; i < menus.length; i++)
		{
			if (menus[i] == subId)
			{
				Element.show(menus[i]);
				Element.addClassName(menus[i].substr(3), 'current');
			}
			else
			{
				Element.hide(menus[i]);
				Element.removeClassName(menus[i].substr(3), 'current');
			}
		}
		return false;
	}
}

function initNav(ie) { myNav = new Nav(ie); }