/****************************************************************
*
*	Ribbon Menu
*	Author: Michael Turnwall - Digitaria Inc.
*	Created: 5.17.2007
*
*	Edit getRibbon() to change the files to the backend script being used
*
****************************************************************/

var self;
function Ribbon(ribbonCont,nav,objName)
{
	
	self = this;
	this.baseURL = LB_BASE_URL;	// default URL to the scripts
	this.ribbonCont = ribbonCont;
	this.mainNavEl = document.getElementById(nav);
	this.parentEl = this.mainNavEl.parentNode;
	this.targetHeight = 216;	// the height of the ribbon
	this.ribbonLayout;
	this.ribbonTimer;
	this.div = false;		// false means a ribbon is not being shown
	this.ribbonType;
	this.activeLink = "";
	
	// create custom events for the links that have ribbons
	this.links = this.mainNavEl.getElementsByTagName("a");
	this.cacheArray = new Array();
	for(var i = 0; i < this.links.length; i++)
	{
		if(this.links[i].getAttribute("rel"))
		{
			this.links[i].onmouseover = function()
			{
				addClass(this, "nav_active");
				ribbonObj.activeLink = this;
				// if the ribbon container already exists we just swap the content
				if(el = document.getElementById("ribbonCont"))
				{
					var ribbonLink = document.getElementsByClassName("ribbonOn");
					if(ribbonLink[0])
						ribbonLink[0].className = ribbonLink[0].className.replace(/ribbonOn/,"");

					while(el.firstChild)
					{
						el.removeChild(el.firstChild);
					}
					self.clearTimer();
					this.className += " ribbonOn"
					var ribbonType = this.getAttribute("rel").split(".");
					self.ribbonType = ribbonType[0];
					self.getRibbon();
				}
				else
				{
					this.className += " ribbonOn"
					var ribbonType = this.getAttribute("rel").split(".");
					self.ribbonType = ribbonType[0];
					self.initRibbon();
				}
				
			}
			this.links[i].onmouseout = function(){
				removeClass(this, "nav_active");
				self.startTimer();
			};
		}
	}
	
	// hide the select boxes in IE6-
	if(typeof document.body.style.maxHeight == "undefined")
	{
		this.ie6 = true;
		//pngFix();
		this.selectArray = new Array();
		var select = document.getElementsByTagName("select")
		for(var i = 0; i < select.length; i++)
		{
			if(select[i].id == "newsSelect" || select[i].id == "q" || select[i].id == "p" || select[i].id == "chearleaderDropdown" ||
				select[i].id == "Select1" || select[i].id == "d")
				this.selectArray.push(select[i])
		}
	}	
	
}
/*----------------------*/

Ribbon.prototype.initRibbon = function()
{
	// if a ribbon is already shown, delete
	if(this.div != false)
		this.deleteRibbon();
	
	// create the ribbon div and assign attibutes
	// this part is messy, it gets the background and inner shadow to be show as the ribbon slides down
	this.div = document.createElement("div");
	this.div.setAttribute("id","ribbonCont");
	
	// set our div location this.mainNavEl
	var xpos = lbElementGetX(this.mainNavEl);
	var ypos = lbElementGetY(this.mainNavEl) + this.mainNavEl.offsetHeight-1;
	
	this.div.style.left = xpos + "px";
	this.div.style.top = ypos + "px";
	
//	this.topDiv = document.createElement("div");
//	this.topDiv.setAttribute("id","ribbonTop");
//	this.bottomDiv = document.createElement("div");
//	this.bottomDiv.setAttribute("id","ribbonBottom");	
	
	// events so the ribbon doesn't disappear
	this.div.onmouseover = function(){
//		alert(ribbonObj.activeLink.className);
//		addClass(ribbonObj.activeLink, "nav_active");
//		alert(ribbonObj.activeLink.className);
		ribbonObj.activeLink.className = "nav_active ribbonOn";
		ribbonObj.clearTimer()}
	;
	
	this.div.onmouseout = function(){
		ribbonObj.activeLink.className = "ribbonOn";
		//removeClass(ribbonObj.activeLink, "nav_active");
		ribbonObj.startTimer()
	};
	
//	this.div.appendChild(this.topDiv);
//	this.div.appendChild(this.bottomDiv);
	this.parentEl.insertBefore(this.div,this.mainNavEl.nextSibling);
	this.div.style.height = this.targetHeight/2 + "px";		// animation trick to speed up the look of the ribbon sliding down
	this.ribbonTimer = window.setTimeout("ribbonObj.growRibbon()",500);
	if(this.ie6)
		this.hideSelect("hidden");
}
/*----------------------*/

// construct the URL to the back-end script
Ribbon.prototype.getRibbon = function()
{
	
	// default URL to the scripts
	this.url = this.baseURL + "ajax_call.php?page=" + this.ribbonType + "";
	// if the array 
	if(this.cacheArray[this.url])
		this.showRibbonCache();
	//else
	
	new Ajax.Request(
			this.url, 
			{
				method: 'post', 
				parameters: '', 
				onComplete: ribbonObj.showRibbon,
				asynchronous: true
			});
	
}
/*----------------------*/

// writes the ajax response to page
Ribbon.prototype.showRibbon = function(ajax)
{
	
	if(ajax.readyState == 4 && ajax.status == 200)
	{
		ribbonObj.cacheArray[ribbonObj.url] = ajax.responseText;	// write the response cache array
		ribbonObj.div.innerHTML = ajax.responseText;
		var ribbon = document.getElementsByClassName("ribbonCont",ribbonObj.div.id);
		//ribbonObj.div.style.backgroundImage = "none";
		//pngFix();
	}
}
/*----------------------*/

// writes cached version of the ribbon
Ribbon.prototype.showRibbonCache = function()
{
	this.div.innerHTML = this.cacheArray[this.url];
	//pngFix();
}
/*----------------------*/

// grow/slide down effect
Ribbon.prototype.growRibbon = function()
{
	if(this.ribbonTimer)
		clearTimeout(this.ribbonTimer);
	// make the div visible if it's the first time around
	
	if(this.div.style.display != "block")
		this.div.style.display = "block";

	//ribbonObj.getRibbon();
	var height = this.div.offsetHeight;
	
	if(height < this.targetHeight)
	{
		this.div.style.height = (height + 5) + "px";
		this.ribbonTimer = window.setTimeout("ribbonObj.growRibbon()",5);
	}
	else
	{
		// in case the ribbon too tall, correct to desired height
		this.div.style.height = this.targetHeight + "px";
		// loading graphic
		//this.div.style.background = "#2d2d2d url(/media/desmm_load_w.gif) no-repeat center center";
		this.ribbonTimer = window.setTimeout("ribbonObj.getRibbon()",50);
		//this.getRibbon();
	}
}
/*----------------------*/

Ribbon.prototype.shrinkRibbon = function()
{
	if(this.ribbonTimer)
		clearTimeout(this.ribbonTimer);
	var height = this.div.offsetHeight;
	if(this.div.offsetHeight > 0)
	{
		this.div.style.height = (this.div.offsetHeight - 10) + "px";
		this.ribbonTimer = window.setTimeout("ribbonObj.shrinkRibbon()",10);
	}
	else
	{
		this.deleteRibbon();
	}
}
/*----------------------*/

// remove the ribbon from the DOM
Ribbon.prototype.deleteRibbon = function()
{
	clearTimeout(this.ribbonTimer);
	clearTimeout(this.hideTimer);
	
	var ribbonLink = document.getElementsByClassName("ribbonOn");
	if(ribbonLink[0]){
		ribbonLink[0].className = ribbonLink[0].className.replace("ribbonOn","");
	}
	if(this.div)
		this.parentEl.removeChild(this.div);
	this.div = false;
	if(this.ie6)
		this.hideSelect("visible");
}
/*----------------------*/

document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

Ribbon.prototype.startTimer = function()
{
	this.hideTimer = window.setTimeout("ribbonObj.deleteRibbon()",400);
}
/*----------------------*/

Ribbon.prototype.clearTimer = function()
{
	clearTimeout(this.hideTimer);
}
/*----------------------*/

Ribbon.prototype.hideSelect = function(display)
{
	for(var i = 0; i < this.selectArray.length; i++)
	{
		this.selectArray[i].style.visibility = display;
	}
}
/*----------------------*/


