/****************************************************************
*
*	Ribbon Menu
*	Author: Michael Turnwall - Digitaria Inc.
*	Created: 5.17.2007
*
*	Edit getRibbon() to change the files to the backend script being used
*
****************************************************************/
var alreadyCheckedForTicker = false;
var self;
function Ribbon(ribbonCont,nav,objName,ribbonTop)
{
	self = this;
	this.baseURL = "/content/System/Main_Navigation/";	// default URL to the scripts
	this.ribbonCont = ribbonCont;
	this.mainNavEl = document.getElementById(nav);
	this.parentEl = this.mainNavEl.parentNode;
	this.targetHeight = 192;	// the height of the ribbon
	this.targetTop = ribbonTop;
	this.ribbonLayout;
	this.ribbonTimer;
	this.div = false;		// false means a ribbon is not being shown
	this.ribbonType;
	
	// 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()
			{
				// if the ribbon container already exists we just swap the content
				if(el = document.getElementById("ribbonCont"))
				{
					var ribbonLink = document.getElementsByClassName("ribbonOn");
					if(ribbonLink)
						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[1];
					self.getRibbon();
				}
				else
				{
					this.className += " ribbonOn"
					var ribbonType = this.getAttribute("rel").split(".");
					self.ribbonType = ribbonType[1];
					self.initRibbon();
				}
				
			}
			this.links[i].onmouseout = function(){
				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");
	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(){ribbonObj.clearTimer()};
	this.div.onmouseout = function(){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(document.getElementById("homepageFlashTicker") && alreadyCheckedForTicker == false) {
		this.targetTop = parseFloat(this.targetTop)+60;
		alreadyCheckedForTicker = true;
	}
	
	this.div.style.top = this.targetTop + "px";
	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 + this.ribbonType + ".aspx";

	// if the array 
	if(this.cacheArray[this.url])
		this.showRibbonCache();
	else
		initRequest("GET",this.url,true,ribbonObj.showRibbon);	// start AJAX call
}
/*----------------------*/

// writes the ajax response to page
Ribbon.prototype.showRibbon = function()
{
	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 + 20) + "px";
		this.ribbonTimer = window.setTimeout("ribbonObj.growRibbon()",15);
	}
	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");
	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");
}
/*----------------------*/

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

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;
	}
}
/*----------------------*/