// $Id: mw-dynamic.js,v 1.6 2005/04/09 23:17:13 wilcoxj Exp $
//--------------------------------------------------------------------
// MaizeWeb DHTML Library & Functionality
// Copyright (C) 2005 Contributors. See http://www.maizeweb.com/ 
// Original author: Jeff Wilcox 
//--------------------------------------------------------------------
// Utilizes the DHTML reference library and samples from the 
// O'reilly book "Dynamic HTML" 2nd Edition by Danny Goodman 
//--------------------------------------------------------------------
// Contents:
//	- MaizeWeb object
//	- Dynamic search menu object
//	- MaizeWeb dhtml debug object
//
// Current Script Compatibility:
//	 - Firefox 1.0; Modern Netscape + Mozillas
//	 - Apple Safari
//	 - IE 6.0
//--------------------------------------------------------------------

//--------------------------------------------------------------------
// MaizeWeb [object] : Constructor
//--------------------------------------------------------------------
function MaizeWebObject()
{
	// The #header element of all MW pages
	this.header = getRawObject("header");
	if( ! this.header ) return; // Unable to access said elements

	// A list of all tab <li>'s at the top of the page : <ul> element
	this.tabsList = getRawObject("toolbar_tabs");

	// An Array of the actual list items (tabs)
	this.tabs = this.tabsList.getElementsByTagName("li");

	// This array contains whether or not the tab is selected
	this.tabsSelect = new Array( this.tabs.length );
	for(var i =0; i < this.tabs.length; ++i )
		this.tabsSelect[i] = (this.tabs[i].className == "selected");

	// The tabs are in the process of being hidding when this is true
	this.tabsHiding = false;

	// Percent that the tabs have been faded; 100 means 0% fade
	this.tabsFadeLevel = 100; // percent - opacity of tabs

	this.afterHideCall = null;
}

//--------------------------------------------------------------------
// MaizeWeb [object] : Member Functions
//--------------------------------------------------------------------

// protected
MaizeWebObject.prototype.tabsFadeUp = function _mwtfu()
{
	if(this.tabsFadeLevel < 76)
		this.tabsFadeLevel += 25;
	else this.tabsFadeLevel = 100;
	this.tabsFadeUpdate();
}
// protected
MaizeWebObject.prototype.tabsFadeDown = function _mwtfd()
{
	if(this.tabsFadeLevel <= 0)
		this.tabsFadeLevel = 0;
	else 	this.tabsFadeLevel -= 25;
	this.tabsFadeUpdate();	
}
// Fade the tabs all the way in
// public
MaizeWebObject.prototype.tabsFadeIn = function _mwtfoatwin() 
{
	this.tabsHiding = false;
	mw_t_fi_more(); // start all fade in	
} 
// internal private
function mw_t_fi_more()
{
	MaizeWeb.tabsFadeUp();
	if(MaizeWeb.tabsFadeLevel < 100)
		setTimeout("mw_t_fi_more()", MW_TAB_FADETIME);  
}
// Fade the tabs all the way out 
// public
MaizeWebObject.prototype.tabsFadeOut = function _mwtfoatw(
	aftercall) 
{
	if(aftercall != null)
		this.afterHideCall = aftercall;
	this.tabsHiding = true;
	mw_t_fo_more(); // start all fade out	
} 
// internal private
function mw_t_fo_more()
{
	MaizeWeb.tabsFadeDown();
	if(MaizeWeb.tabsFadeLevel > 0)
		setTimeout("mw_t_fo_more()", MW_TAB_FADETIME);  
	else 
	{
 		MaizeWeb.tabsHiding = false; // Not anymore, gone now
		hide(MaizeWeb.tabsList);
		if(MaizeWeb.afterHideCall != null)
			eval(MaizeWeb.afterHideCall);
		this.afterHideCall = null;
	}
}
// private
MaizeWebObject.prototype.tabsFadeUpdate = function _mwtfupd()
{	// Warning: Only 0,25,50,75,100 def'd in css now
	var newClass = "fade" + this.tabsFadeLevel; 
	for( var i = 0; i < this.tabs.length; ++i )
		this.tabs[i].className = newClass + ( this.tabsSelect[i] ?
			" selected" : "" );
}
// End of tabs code
//--------------------------------------------------------------------

// Dynamic MaizeWeb Search Menu
function MWSearchMenu( linkO )
{
	this.linkObject = linkO;
	this.dynamicMenu = null;
	this.textField = null;
	this.opening = false;
	this.closing = false;
	this.header = getRawObject("header");
	this.init();
}
// -- class defs
MWSearchMenu.prototype.init = function _mwsmInit()
{
return;

	if(this.linkObject.addEventListener)
        	this.linkObject.addEventListener("mouseover", this.eventSearchOver, false);
        else  	this.linkObject.onmouseover = this.eventSearchOver;

	var tabHome = getRawObject("tab_home");

	this.dynamicElement = document.createElement("div");
        this.dynamicElement.style.position="absolute";
        this.dynamicElement.style.top=0;
        this.dynamicElement.style.right=0;
        this.dynamicElement.style.width=0;
        this.dynamicElement.style.height=(getObjectHeight(this.header)-4)+"px";
        this.dynamicElement.style.display = 'block';
	this.dynamicElement.style.border=0;//"1px solid red";	

	var form = document.createElement("form");
	form.method = "post";
	form.action = "/search/";
	form.id = "searchdynamic";

	var form_p = document.createElement("p");
	form_p.style.fontWeight = "bold";
	form_p.style.fontSize = "90%";
	form_p.appendChild(document.createTextNode("Search: "));

	var txt = document.createElement("input");
	txt.type = "text";
	txt.size = 20;
	txt.name = "q";
	txt.style.fontWeight = "normal";
	txt.className = "text";
	this.textField = txt;
	form_p.appendChild(txt);

	var sub = document.createElement("input");
	sub.type = "submit";
	sub.name = "sa";
	sub.style.fontWeight = "normal";
	sub.value = "Search";
	form_p.appendChild(sub);

	form.style.position = "absolute";
	form.style.bottom="8px";
	form.style.display="block";
	form.style.right="5px";
	form.style.height="22px";
	form.style.color = "white";

	form.appendChild(form_p);
	
	this.dynamicElement.appendChild(form);
	this.header.appendChild(this.dynamicElement);
	hide(this.dynamicElement);

	var approxWidth = getObjectWidth(this.linkObject);
	approxWidth += getObjectWidth(getRawObject("mw_top_account"));	
	approxWidth += ( 3.2 * getObjectWidth(getRawObject("mw_top_account2")) );

	this.dynamicElement.style.width =approxWidth+"px";
	this.eventSlow = 0;

}
MWSearchMenu.prototype.Show = function()
{
	show(this.dynamicElement);

	getRawObject("header").onmouseover = searchMenu.nothing;
	getRawObject("container").onmouseover = searchMenu.Hide;
}

MWSearchMenu.prototype.nothing = function(evt) {}
 
MWSearchMenu.prototype.Hide = function()
{
	// Don't hide, no matter what, if they entered text
	if(searchMenu)
	{
		var sa = searchMenu.textField.value;
		if(sa.length > 0) return;
	}
	// Try to allow them time to enter or try again - 
	// so give them ~15 leeway attempts; not great 
	// but will work for now.
	searchMenu.eventSlow++;	
	if(searchMenu.eventSlow < 15) return;

	searchMenu.eventSlow = 0;
	hide(searchMenu.dynamicElement);
	show(MaizeWeb.tabsList);
	MaizeWeb.tabsFadeIn();
	searchMenu.dynamicElement.onmouseover = searchMenu.nothing;
	getRawObject("header").onmouseover = searchMenu.nothing;
	document.onmouseover = null;
}
MWSearchMenu.prototype.status = function _mwsmStatus() 
{
	MWDebug.displayError("Opening: " + this.opening + "<br />Closing: " + this.closing
		+ "<br /><br /><br />" + "Dynamic Div: " + this.dynamicElement.innerHTML
	);
}
MWSearchMenu.prototype.eventSearchOver = function _mwsmeventSearchOver(evt)
{
        evt = (evt) ? evt : ((event) ? event : null);
        if (evt )
        {
                var elem = (evt.target) ? evt.target :
                        ((evt.srcElement) ? evt.srcElement : null);
                if( elem )
                {
                        // act on element receiving event

                }
        }
	MaizeWeb.tabsFadeOut("searchMenu.Show()");

	// fade back in for now automatically, just testing really
	//setTimeout("mw_t_fi_more()", MW_TAB_FADETIME * 15);

	//if(searchMenu)
	//       searchMenu.status();           
}
// -- end class defs for dynamic search menu


function MWDebugObject()
{
	this.div = null;
	this.init();
}
MWDebugObject.prototype.init = function _mwdInit()
{
	this.div = document.createElement("div");
	this.div.style.position="absolute";
	this.div.style.top=getInsideWindowHeight() - 151;
	this.div.style.left=0;
	this.div.style.width="350px";
	this.div.style.height="150px";
	this.div.style.background="#fff";
	this.div.style.border="1px solid black";
	this.div.style.display = 'none';
	var container = getRawObject("container");
	container.appendChild(this.div);

	this.visible = false;
}
MWDebugObject.prototype.show = function _mwdShow()
{
	this.div.style.display = 'block';
	this.visible = true;
}
MWDebugObject.prototype.displayError = function _mwdDEr(msg)
{
	this.div.innerHTML = "<p>" + msg + "</p>";
	this.show();
	setTimeout("debugHide()", 2000);
}
function debugHide(){ // not part of object
	MWDebug.hide();
}
MWDebugObject.prototype.hide = function _mwdHide()
{
	this.visible = false;
	this.div.style.display = 'none';
}


//------------------------------------
// Global MaizeWeb JavaScript Objects
//------------------------------------
var searchMenu;
var MaizeWeb; // global MW functions via object
var MWDebug;

var MW_TAB_FADETIME = 80;

// Call comes after page has been loaded, by the DHTML api: 
// we're all ready to go at this point.
function initMaizeWebAPI()
{
	// Only going for W3C-style DHTML APIs for now; modern browsers
	if( document.getElementById && document.createElement )
	{
		MWDebug = new MWDebugObject( );
		MaizeWeb = new MaizeWebObject( );
	    searchMenu = new MWSearchMenu( getRawObject("mw_top_search") );
	}
}

//--------------------------------------------------------------------
// end of mw-dynamic.js
// $Id: mw-dynamic.js,v 1.6 2005/04/09 23:17:13 wilcoxj Exp $
//--------------------------------------------------------------------
