/****************************************************************
*
*	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 selfs;
function doChangeOver(menuid,havingrel){
	var div = menuid+"_1";

	var lastChar = menuid.charAt(menuid.length-1);

	newImage = "url("+MEDIA_URL+menuid+"_active.gif)";
	document.getElementById(div).style.backgroundImage = newImage;

	var items = havingrel.split(",");	
	
	var newStr = '';
	
	for(var i=0;i<items.length-1;i++){
		if(items[i] != lastChar){
			newStr  += items[i]+",";	
		}
	}

	var newStrr = newStr.split(",");	
	
	
//	alert(menuid);
//	alert(menuItem);
	newImage = "url("+MEDIA_URL+menuItem+".gif)";
	document.getElementById(menuItem+"_1").style.backgroundImage = newImage;
	
	for(var i=0;i<newStrr.length-1;i++){
		
		var divv = "menu"+newStrr[i]+"_1";		
//		if(menuItem == "menu"+newStrr[i]) {continue;}
		newImage = "url("+MEDIA_URL+"menu"+newStrr[i]+".gif)";
		document.getElementById(divv).style.backgroundImage = newImage;
	}
}
function Ribbon(ribbonCont,nav,objName,ribbonTop)
{	

	selfs = this;
	this.baseURL = "http://localhost/test/ipl_menu/";	// 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();
	var strMenuItems = '';

	for(var i = 0; i < this.links.length; i++)
	{
		var ind = i+1;
		
			
		if(this.links[i].getAttribute("rel"))
		{
			strMenuItems  += ind+","; 		
			this.links[i].onmouseover = function()
			{
				var did = this.getAttribute("id");

				//doChangeOver(did,strMenuItems);
				
				
				// if the ribbon container already exists we just swap the content
				if(el = document.getElementById("ribbonCont"))
				{

					var browser=navigator.appName;
					if(browser != "Microsoft Internet Explorer"){
						var ribbonLink = document.getElementsByClassName("ribbonOn");
						if(ribbonLink)
							ribbonLink[0].className = ribbonLink[0].className.replace(/ribbonOn/,"");
						}
					while(el.firstChild)
					{
						el.removeChild(el.firstChild);
					}
					selfs.clearTimer();
					this.className += " ribbonOn"
					var ribbonType = this.getAttribute("rel");
					selfs.ribbonType = ribbonType;
					selfs.getRibbon();
				}
				else
				{
					this.className += " ribbonOn"
					var ribbonType = this.getAttribute("rel");
					selfs.ribbonType = ribbonType;
					selfs.initRibbon();
				}
				
			}
			this.links[i].onmouseout = function(i){
				selfs.startTimer();	
				var did = this.getAttribute("id");

				
				if(did!=menuItem) {
					
					document.getElementById(did+"_inactive").style.display= 'block';
					document.getElementById(did+"_active").style.display= 'none';
					
				}
				
				document.getElementById(menuItem).style.display= 'block';	
				
			};
		}
	}

// 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;
	}

	var brow=navigator.appName;

	if(blogPage == 'y'){
		if(brow == "Microsoft Internet Explorer"){
			this.div.style.top = parseInt(this.targetTop)+8 + "px";
		}else{
			this.div.style.top = parseInt(this.targetTop)-6 + "px";
		}
	}else{
		if(brow == "Microsoft Internet Explorer"){
			if(jQuery.browser.version==6 || jQuery.browser.version==8 ){
				var ieh = parseInt(this.targetTop);
				this.div.style.top = ieh + "px";	
			}else{
				var ieh = parseInt(this.targetTop)+14;
				this.div.style.top = ieh + "px";	
			}
		}else{
			this.div.style.top = this.targetTop + "px";	
		}
	}
	this.div.style.left = jQuery('#menuContainer').offset().left+"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.ribbonType;
	

	// 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";
		ribbonObj.div.style.background = "none";
		//pngFix();
	}
}
/*----------------------*/

// writes cached version of the ribbon
Ribbon.prototype.showRibbonCache = function()
{
	this.div.innerHTML = this.cacheArray[this.url];
	this.div.style.backgroundImage = "none";
	this.div.style.background = "none";
	//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 = "#0D2242 url("+MEDIA_URL+"menu-ajax-loader.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;
	}
}

/*----------------------*/