/****************************************************************
*
*	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 = "/";	// default URL to the scripts
	this.ribbonCont = ribbonCont;
	this.mainNavEl = document.getElementById(nav);
	this.parentEl = this.mainNavEl.parentNode;
	this.targetHeight = 615;	// the height of the ribbon
	this.targetWidth = 758;	// the width of the ribbon
	this.ribbonLayout;
	this.ribbonTimer;
	this.div = false;		// false means a ribbon is not being shown
	this.ribbonType;
	this.categoryid;
	
	this.url = LB_BASE_URL + "ajax_index.html"
	
	// create custom events for the links that have ribbons
	this.links = this.mainNavEl.getElementsByTagName("div");
	this.cacheArray = new Array();
	
	
	
	for(var i = 0; i < this.links.length; i++)
	{
		if(this.links[i].getAttribute("categoryid"))
		{
			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.categoryid = this.getAttribute("categoryid");
					//self.ribbonType = ribbonType[1];
					
					self.getRibbon();
				}
				else
				{
					this.className += " ribbonOn"
					//var ribbonType = this.getAttribute("rel").split(".");
					//self.ribbonType = ribbonType[1];
					self.categoryid = this.getAttribute("categoryid");
					self.initRibbon();
				}
				
				swapClasses(document.getElementById('category_'+ribbonObj.categoryid), 'category_navigation_item_on');
				
				
				
			}
			this.links[i].onmouseout = function(){
				
				swapClasses(document.getElementById('category_'+ribbonObj.categoryid), 'category_navigation_item');
				
				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(); swapClasses(document.getElementById('category_'+ribbonObj.categoryid), 'category_navigation_item_on');};
	this.div.onmouseout = function(){ribbonObj.startTimer(); swapClasses(document.getElementById('category_'+ribbonObj.categoryid), 'category_navigation_item');};
	this.div.appendChild(this.topDiv);
	this.div.appendChild(this.bottomDiv);
	
	// set our location
	
	if(this.ie6){
		var left_val = parseInt(lbElementGetX(this.mainNavEl)) + parseInt(this.mainNavEl.offsetWidth) - 1 - 125;
	}else{
		var left_val = parseInt(lbElementGetX(this.mainNavEl)) + parseInt(this.mainNavEl.offsetWidth) - 1;
	}
	
	
	var top_val = parseInt(lbElementGetY(this.mainNavEl)) - 28;
	this.div.style.left = left_val + "px";
	this.div.style.top = top_val + "px";
		
	
	this.parentEl.insertBefore(this.div,this.mainNavEl.nextSibling);
	this.div.style.width = this.targetWidth/2 + "px";
	//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 + this.ribbonType + ".aspx";

	// if the array 
	if(this.cacheArray[this.categoryid]){
		this.showRibbonCache();
	}else{
		//this.showRibbon();
		
		this.div.innerHTML = "<div style=\"text-align:center; padding-top:200px; font-size:11px; color:#dde7ed;\"><img src=\"images/loader.gif\" /><br />Loading....</div>";
		
		new Ajax.Request(
			this.url, 
			{
				method: 'post', 
				parameters: 'page=display_category&categoryid='+ribbonObj.categoryid, 
				onComplete: ribbonObj.showRibbon,
				asynchronous: false
			});
	}
		//initRequest("GET","",true,ribbonObj.showRibbon);	// start AJAX call
}
/*----------------------*/

// writes the ajax response to page
Ribbon.prototype.showRibbon = function(ajax)
{
	if(ajax.readyState == 4 && ajax.status == 200)
	{
		ribbonObj.cacheArray[ribbonObj.categoryid] = ajax.responseText;
		
		ribbonObj.delayedShow(ajax.responseText);
		
//		var width = ribbonObj.div.offsetWidth;
//		if(width < ribbonObj.targetWidth){
//			ribbonObj.delayedShow(ajax.responseText);
//		}else{
//			ribbonObj.div.innerHTML = ajax.responseText;	
//		}
		
		
		//ribbonObj.cacheArray[ribbonObj.url] = ajax.responseText;	// write the response cache array
		//ribbonObj.div.innerHTML = ajax.responseText;
		//ribbonObj.div.innerHTML = "WORKED YEAH<br /><br /><br /><br /><br /><br /><br /><br /><br />";
		//var ribbon = document.getElementsByClassName("ribbonCont",ribbonObj.div.id);
		//ribbonObj.div.style.backgroundImage = "none";
		//pngFix();
	}
}

Ribbon.prototype.delayedShow = function(html){
	
//	var width = this.div.offsetWidth;
//	if(width < this.targetWidth){
//		window.setTimeout('ribbonObj.delayedShow(\''+html+'\')', 100);
//	}else{
		//alert(html);
		this.div.innerHTML = html;
//	}
	
}

/*----------------------*/

// writes cached version of the ribbon
Ribbon.prototype.showRibbonCache = function()
{
	this.delayedShow(this.cacheArray[this.categoryid]);
	//this.div.innerHTML = this.cacheArray[this.categoryid];
	
	
	
	//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";

	//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();
	//}
		
//	var width = this.div.offsetWidth;
//	if(width < this.targetWidth){
//		
//		this.div.style.width = (width + 15) + "px";
//		this.ribbonTimer = window.setTimeout("ribbonObj.growRibbon()",5);
//		
//	}else{
		
		this.div.style.width = this.targetWidth + "px";
		this.ribbonTimer = window.setTimeout("ribbonObj.getRibbon()",50);
//	}
	
}
/*----------------------*/

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.removeRibbon = function()
{
	if(this.div)
		this.parentEl.removeChild(this.div);
	this.div = false;
	
	switchOff = 1;
	
}
/*----------------------*/

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


