//------------------------------------------------------------------------
// Written by Ray Wilson
// Copyright Music From Outer Space LLC 2010.
// http://www.musicfromouterspace.com/mfosweb/home.action
//------------------------------------------------------------------------
function commonSetup(o)
{
	o.style.cursor = "pointer";
	o.style.backgroundImage = "url(images/utilitymenuimages.gif)";
	o.style.backgroundRepeat = "no-repeat";
}

function UtilityMenu(container)
{
	this.container = container;
	
	this.cartid = getNewId();
	this.cartdiv = UtilityMenu.makeContainer(this, this.cartid);
	var o = this.cartdiv;
	commonSetup(o);
	o.title = "Go to the catalog for this item...";
	o.style.backgroundPosition = "-320px 0px";
	var that = this;
	o.onclick = function(){UtilityMenu.goBuy(that);};
	this.container.appendChild(o);
	this.buyshown = false;
	
	this.favid = getNewId();
	this.favdiv = UtilityMenu.makeContainer(this, this.favid);
	o = this.favdiv;
	commonSetup(o);
	o.title = "Add this link to \"My MFOS\"";
	o.style.backgroundPosition = "-340px 0px";
	o.onclick = function(){UtilityMenu.goFav(that);};
	this.container.appendChild(o);
	this.favshown = false;
	
	this.shareid = getNewId();
	this.sharediv = UtilityMenu.makeContainer(this, this.shareid);
	o = this.sharediv;
	commonSetup(o);
	o.title = "Share This Page...";
	o.style.left = "100px";
	o.style.top = "5px";
	o.style.backgroundPosition = "-360px 0px";
	o.onclick = function(){UtilityMenu.goShare(that);};
	o.style.display = "block";
	o.style.visibility = "visible";
	this.container.appendChild(o);
	
	this.searchid = getNewId();
	this.searchdiv = UtilityMenu.makeContainer(this, this.searchid);
	o = this.searchdiv;
	commonSetup(o);
	o.title = "Go Search...";
	o.style.left = "292px";
	o.style.top = "5px";
	o.style.backgroundPosition = "-380px 0px";
	o.onclick = function(){UtilityMenu.goSearch(that);};
	o.style.display = "block";
	o.style.visibility = "visible";
	this.container.appendChild(o);
	
	this.searchTermId = "mainpagesearchtext";
	this.searchTerm = UtilityMenu.makeFormControl("text", "", "utilitysearchtext", this.searchTermId);
	o = this.searchTerm;
	o.title = "Search term entry field";
	o.style.fontFamily = "Verdana";
	o.style.backgroundColor = "#cccccc";
	o.style.fontSize = "8pt";
	o.style.position = "absolute";
	o.style.left = "136px";
	o.style.top = "6px";
	o.style.width = "150px";
	o.style.display = "block";
	o.style.visibility = "visible";
	addEvent(o, "keydown", searchKeyPress);
	this.container.appendChild(o);
	
	this.favtype = "";
	this.favuri = "";
	this.favlinktitle = "";
	this.favpartno = "";
	
	this.buypartno = "";
}

UtilityMenu.CATALOGICON = 0;
UtilityMenu.FAVICON = 1;

UtilityMenu.goShare = function(o)
{
	showShareWindow(o.sharediv);
};

UtilityMenu.goSearch = function(o)
{
	if(o.searchTerm.value.length > 0)
	{
		openSearchWindowAutoSearch(o.searchTerm.value);
	}
	else
	{
		openSearchWindow();
	}
};

UtilityMenu.goFav = function(o)
{
	var dao = new Object();
	dao.favtype   = o.favtype;
	dao.uri       = o.favuri;
	dao.linktitle = o.favlinktitle;
	dao.partno    = o.favpartno;
	storeMyMFOSLink(dao);
};

UtilityMenu.goBuy = function(o)
{
	launchpage("CATALOG", "", o.buypartno, false);
};


UtilityMenu.prototype.setFavButton = function(showbutton, favtype, favuri, favlinktitle, favpartno)
{
	this.favtype = favtype;
	this.favuri = favuri;
	this.favlinktitle = favlinktitle;
	this.favpartno = favpartno;
	if(showbutton)
	{
		this.showIcon(UtilityMenu.FAVICON);
	}
	else
	{
		this.hideIcon(UtilityMenu.FAVICON);
	}
};

UtilityMenu.prototype.setBuyButton = function(showbutton, buypartno)
{
	this.buypartno = buypartno;
	if(showbutton)
	{
		this.showIcon(UtilityMenu.CATALOGICON);
	}
	else
	{
		this.hideIcon(UtilityMenu.CATALOGICON);
	}
};

UtilityMenu.prototype.showIcon = function(icon)
{
	var o;
	if(icon == UtilityMenu.FAVICON)
	{
		o = document.getElementById(this.favid);
		o.style.left = "70px";
		o.style.top = "5px";
		o.style.display = "block";
		o.style.visibility = "visible";
		this.favshown = true;
	}
	else if(icon == UtilityMenu.CATALOGICON)
	{
		o = document.getElementById(this.cartid);
		o.style.left = "40px";
		o.style.top = "5px";
		o.style.display = "block";
		o.style.visibility = "visible";
		this.buyshown = true;
	}
};

UtilityMenu.prototype.hideIcon = function(icon)
{
	var o;
	if(icon == UtilityMenu.FAVICON)
	{
		o = document.getElementById(this.favid);
		o.style.display = "none";
		o.style.visibility = "hidden";
		this.favshown = false;
	}
	else if(icon == UtilityMenu.CATALOGICON)
	{
		o = document.getElementById(this.cartid);
		o.style.display = "none";
		o.style.visibility = "hidden";
		this.buyshown = false;
	}
};

UtilityMenu.mouseOver = function(e)
{
	e = getEvent(e);
	var o = getEventSource(e);
	var cn = o.className; 
	o.className = (cn == "utilitybaricon") ? "utilitybariconhi" : "utilitybaricon";
};

UtilityMenu.makeContainer = function(ptr, id)
{
	var o = document.createElement("div");
	o.id = id;
	o.onmouseover = UtilityMenu.mouseOver; 
	o.onmouseout = UtilityMenu.mouseOver;
	o.className = "utilitybaricon";
	o.style.zIndex = "500";
	o.style.height = (ie) ? "23px" : "21px";
	o.style.width = (ie) ? "23px" : "21px";
	o.style.display = "none";
	o.style.visibility = "hidden";
	return o;
};

UtilityMenu.makeFormControl = function(type, value, name, id) 
{
    var element = document.createElement("input");
    element.id = id;
    element.setAttribute("type",  type);
    element.setAttribute("value", value);
    element.setAttribute("name",  name);
    return element;
};

