/*
*************************************************

MICA
JavaScript functions

Created by the wacky troop at Happy Cog
http://www.happycog.com/

*************************************************
*/

/*-------------------------------------------    
    Global Variables
-------------------------------------------*/
var d = document;



/*-------------------------------------------    
    General Functions
-------------------------------------------*/
function stripeTables() {
	
	if(!d.getElementsByTagName) { return; }
	
	var tables = d.getElementsByTagName('table');
	
	for(var i = 0; i < tables.length; i++){
		if(tables[i].className == 'striped'){
			var rows = tables[i].getElementsByTagName('tr');
			
			for(var j = 0; j < rows.length; j++){				
				if((j%2) == 0 && rows[j].className != 'total'){
					rows[j].className = 'alt';
				}
			}
		
		}
	}
	
}

addLoadEvent(stripeTables);




function initActionNavFlyout(){

	if(!d.getElementById || !d.getElementsByTagName) { return; }
	
	if(d.getElementById('action-nav')){
		var actionNav = d.getElementById('action-nav');
	}else{
		return;
	}
	
	//alert(actionNav.getElementsByTagName('h6')[0].nextSibling.nextSibling.nodeName);
	
	var flyouts = actionNav.getElementsByTagName('h6');
	if(flyouts.length == 0){ return; }
	
	for(var i = 0; i < flyouts.length; i++){
		
		if(browser == "IE6" || browser == "IE7") {
			flyouts[i].nextSibling.className = 'flyout hidden';
		}else{
			flyouts[i].nextSibling.nextSibling.className = 'flyout hidden';
		}
		
		flyouts[i].className = 'toggle-closed';
		
		flyouts[i].onmouseover = function(){
			
			if(this.className == 'toggle-closed'){
				this.className = 'toggle-closed-over';
			}else{
				this.className = 'toggle-open-over';
			}
			
			this.style.cursor = "pointer";
			this.style.cursor = "hand";
			
		}
		
		flyouts[i].onmouseout = function(){
			
			if(this.className == 'toggle-closed-over'){
				this.className = 'toggle-closed';
			}else{
				this.className = 'toggle-open';
			}
		
		}
		
		flyouts[i].onclick = function(){
		
			if(this.className == 'toggle-closed-over'){
				
				if(browser == "IE6" || browser == "IE7") {
					this.nextSibling.className = 'flyout shown';
				}else{		
					this.nextSibling.nextSibling.className = 'flyout shown';
				}
				
				this.className = 'toggle-open-over';
				
			}else{
				if(browser == "IE6" || browser == "IE7") {
					this.nextSibling.className = 'flyout hidden';
				}else{
					this.nextSibling.nextSibling.className = 'flyout hidden';
				}
				
				this.className = 'toggle-closed-over';
			
			}
		
			return false;
		
		}
	}

}

addLoadEvent(initActionNavFlyout);




/*-------------------------------------------    
    Home Page
-------------------------------------------*/

/*
	
	Header Borders
	

*/

function createBorders(){
	
	if(!d.getElementById || !d.createElement) { return; }
	
	if(d.body.id == 'home'){
		
		if(d.getElementById('bt') && !d.getElementById('btproxy')){
	
			var bt = d.getElementById('bt');
			var btColor = parseColor(getStyle(bt.getElementsByTagName('hr').item(0), 'background-color'));
	
			var btproxy = d.createElement('div');
			btproxy.setAttribute("id", "btproxy");		
			bt.appendChild(btproxy);
			
			swfobject.embedSWF("prebuilt/s/home-header-border.swf?r="+Math.random(), "btproxy", "100%", "16", "8.0.0", "expressInstall.swf", { passedBG: btColor }, {wmode: "transparent", menu: "false", allowScriptAccess: 'always', swliveconnect: 'true'}, {});	
	
		}
	
		if(d.getElementById('bb') && !d.getElementById('bbproxy')){
	
			var bb = d.getElementById('bb');
			var bbColor = parseColor(getStyle(bt.getElementsByTagName('hr').item(0), 'background-color'));
	
			var bbproxy = d.createElement('div');
			bbproxy.setAttribute("id", "bbproxy");		
			bb.appendChild(bbproxy);		
		
			swfobject.embedSWF("prebuilt/s/home-header-border.swf?r="+Math.random(), "bbproxy", "100%", "16", "8.0.0", "expressInstall.swf", { passedBG: bbColor }, {wmode: "transparent", menu: "false", allowScriptAccess: 'always', swliveconnect: 'true'}, {});	
	
		}
	
	}	
	
}

addLoadEvent(createBorders);


/*
	
	News
	

*/

/*function createNews(){
	
	if(!d.getElementById || !d.createElement) { return; }
	
	if(d.body.id == 'home')
	{
		
		var lead = d.getElementById('lead');
		var leadProxy = d.getElementById('leadproxy');
		
		if(lead && !leadproxy){
			
			var leadproxy = d.createElement('div');
			leadproxy.setAttribute("id", "leadproxy");
			lead.parentNode.insertBefore(leadproxy, lead);
			
			var news1 = d.getElementById('news1');
			var header = lead.getElementsByTagName('h4').item(0);
			var headline = lead.getElementsByTagName('h2').item(0);
			var subheadline = lead.getElementsByTagName('h3').item(0);
			
			var headerTxt = header.innerHTML.toUpperCase();
			var headlineTxt = escape(headline.innerHTML);
			var subHeadlineTxt = lead.getElementsByTagName('h3').item(0).innerHTML.toUpperCase();
			
			var headerTxtColor = parseColor(getStyle(header, 'color'));
			var headlineTxtColor = parseColor(getStyle(headline, 'color'));
			var subHeadlineTxtColor = parseColor(getStyle(subheadline, 'color'));
			
			if(headerTxtColor.toString().length == 3) headerTxtColor = headerTxtColor.substring(0, 1)+headerTxtColor.substring(0, 1)+headerTxtColor.substring(1, 2)+headerTxtColor.substring(1, 2)+headerTxtColor.substring(2, 3)+headerTxtColor.substring(2, 3);
			if(headlineTxtColor.toString().length == 3) headlineTxtColor = headlineTxtColor.substring(0, 1)+headlineTxtColor.substring(0, 1)+headlineTxtColor.substring(1, 2)+headlineTxtColor.substring(1, 2)+headlineTxtColor.substring(2, 3)+headlineTxtColor.substring(2, 3);
			if(subHeadlineTxtColor.toString().length == 3) subHeadlineTxtColor = subHeadlineTxtColor.substring(0, 1)+subHeadlineTxtColor.substring(0, 1)+subHeadlineTxtColor.substring(1, 2)+subHeadlineTxtColor.substring(1, 2)+subHeadlineTxtColor.substring(2, 3)+subHeadlineTxtColor.substring(2, 3);
			
			var headerBgColor = parseColor(getStyle(header, 'background-color'));
			var headlineBgColor = parseColor(getStyle(news1, 'background-color'));
			
			swfobject.embedSWF(
				 "prebuilt/s/home-news-heading.swf?r="+Math.random()
				,"leadproxy"
				,"308"
				,"92"
				,"8.0.0"
				,"expressInstall.swf"
				,{
					 headerBgColor: headerBgColor
					,headlineBgColor: headlineBgColor
					,headerTxt: headerTxt
					,headlineTxt: headlineTxt
					,subHeadlineTxt: subHeadlineTxt
					,headerTxtColor: headerTxtColor
					,headlineTxtColor: headlineTxtColor
					,subHeadlineTxtColor: subHeadlineTxtColor
				 }
				,{
					 wmode: "transparent"
					,menu: "false"
					,allowScriptAccess: 'always'
					,swliveconnect: 'true'
				 }
				,{}
			);
		}
	
	}	
	
}

addLoadEvent(createNews);*/

addLoadEvent(function(){
	if(browser == "IE6" || browser == "IE7")
	{
		if (document.getElementById('spacer')) {
			document.getElementById('spacer').style.position = 'absolute';
			document.getElementById('spacer').style.top = '0';
			document.getElementById('spacer').style.right = '0';
			document.getElementById('spacer').style.left = 'auto';
		}
		if (document.getElementById('news1'))
			document.getElementById('news1').getElementsByTagName('p').item(1).style.marginRight = '187px';
	}
});


/*
	
	Resize News
	

*/
/*function resizeNews(newSize)
{
	if(!d.getElementById || !d.createElement) { return; }
	
	if(d.body.id == 'home'){
		
		if(d.getElementById('leadproxy')){
			d.getElementById('leadproxy').style.position = 'absolute';
			d.getElementById('leadproxy').style.top = Math.ceil(-newSize)+'px';
			d.getElementById('leadproxy').style.left = '0';
			d.getElementById('leadproxy').width = 308;
			d.getElementById('leadproxy').height = Math.ceil(newSize);
		}
		
		if(d.getElementById('spacer')){
			d.getElementById('spacer').style.marginTop = Math.floor(-newSize)+'px';
		}
		
		if(d.getElementById('news1')){
			if(browser == "IE6" || browser == "IE7")
			{
				d.getElementById('news1').style.marginTop = Math.floor(newSize-2)+'px';
			}
			else
			{
				d.getElementById('news1').style.marginTop = Math.floor(newSize)+'px';
			}
		}
		
	}
}*/


/*
	
	Swap Student Work
	

*/

function getsIFRVar(varToGet, vars)
{
	for(var i=0; len=vars.length,i<len; i++)
	{
		var regex = new RegExp('^'+varToGet+'=');
		if(vars[i].match(regex))
		{
			return unescape(vars[i].replace(regex, '').replace(/\+/g,  " "));
		}
	}
}

function swapStudentWork(){
	if(!d.getElementById || !d.createElement) { return; }
	
	if(d.body.id == 'home'){
	
		// find the images to toggle
		if(d.getElementById('pieces') && d.getElementById('account-work')) {
			var pieces = d.getElementById('pieces')
			var account_work = d.getElementById('account-work');
		}else{
			return;
		}
		
		
		/* 
			Dependencies:
				- each image's parent <li> is named "piece" + #
		
		*/
		
		var piecesLength = pieces.getElementsByTagName('li').length;
		
		for(var i = 0; i < piecesLength; i++){
			
			pieces.getElementsByTagName('a')[i].num = i;
			
			pieces.getElementsByTagName('a')[i].onclick = function(){
				
				this.getElementsByTagName('span').item(0).onclick = function()
				{
					window.location = this.parentNode.href;
				}
				
				// clear current class name off
				for(var j = 0; j < piecesLength; j++){
					if(pieces.getElementsByTagName('li')[j].className.indexOf('current') != -1){
						if(j == 0){
							pieces.getElementsByTagName('li')[j].className = 'first';
						}else{
							pieces.getElementsByTagName('li')[j].className = '';
						}
					}
				}
				
				// make clicked one current
				if(this.num == 0)
				{
					this.parentNode.className = 'first current';
				}
				else
				{
					this.parentNode.className = 'current';
				}
				
				
				
				/*if (document.cookie.indexOf("edu-mica-selected-piece")!=null && document.cookie.indexOf("edu-mica-custom-colors")!=null) {
						
						this.getElementsByTagName('span').item(0).onclick = function(){

						
			
						
						var headID = document.getElementsByTagName("head")[0];         
						var cssNode = document.getElementsByClass("custom-css");
						headID.removeChild(cssNode);
						
						}
						
						}*/
				
				// update custom stylesheet
				var custom_css = getElementsByClass('custom-css');
				for(var j = 0; len=custom_css.length,j<len; j++)
				{
					custom_css[j].disabled=true;					
					
				}
				/*if (document.cookie.indexOf("edu-mica-selected-piece")!=null && document.cookie.indexOf("edu-mica-custom-colors")!=null) 		{
				var headID = document.getElementsByTagName('head')[0];         
				var currentcss = getElementsByClass('custom-css currentsheet');
				if (currentcss = null) {
					return false;
				}
				else {
				headID.removeChild(currentcss);
				}
				
				}	*/

			
				document.getElementById('custom-css'+(this.num+1)).disabled=false;
				// update cookie
				
				/*if (document.cookie.indexOf("edu-mica-selected-piece")!=null && document.cookie.indexOf("edu-mica-custom-colors")!=null) {
				deleteCookie('edu-mica-selected-piece');
				deleteCookie('edu-mica-custom-colors');
				}*/
				createCookie('edu-mica-custom-colors', document.getElementById('custom-css'+(this.num+1)).href);
				createCookie('edu-mica-selected-piece', document.getElementById('piece'+(this.num+1)).id);
				
				/**
				 * Update sIFR
				 *
				 * @todo somehow get computed :hover in there?
				 **/
				for(var j=0; len=sIFR.replacements.length,j<len; j++)
				{
					var newStyles = [];
					
					var sifr = sIFR.replacements[j];
					var css = sIFR.util.convertCssArg(getsIFRVar('css', sifr.vars));
					var alternate = sifr.getAlternate();
					
					var color = parseColor(getStyle(alternate, 'color'));
					css['.sIFR-root'].color = '#'+color;
					
					if(alternate.getElementsByTagName('a').length > 0)
					{
						var aColor = parseColor(getStyle(alternate.getElementsByTagName('a').item(0), 'color'));
						css['a'].color = '#'+aColor;
					}
					
					sifr.changeCSS(css);
				}
				
				// update borders
				if(thisMovie('btproxy')) thisMovie('btproxy').drawBorder('0x'+parseColor(getStyle(document.getElementById('bt').getElementsByTagName('hr').item(0), 'background-color')));
				if(thisMovie('bbproxy')) thisMovie('bbproxy').drawBorder('0x'+parseColor(getStyle(document.getElementById('bb').getElementsByTagName('hr').item(0), 'background-color')));
				
				// update news
				/*if(thisMovie('leadproxy'))
				{
					var lead = d.getElementById('lead');
					var news1 = d.getElementById('news1');
					var header = lead.getElementsByTagName('h4').item(0);
					var headline = lead.getElementsByTagName('h2').item(0);
					var subheadline = lead.getElementsByTagName('h3').item(0);

					var headerBgColor = parseColor(getStyle(header, 'background-color'));
					var headlineBgColor = parseColor(getStyle(news1, 'background-color'));

					var headerTxtColor = parseColor(getStyle(header, 'color'));
					var headlineTxtColor = parseColor(getStyle(headline.getElementsByTagName('a').item(0), 'color'));
					var subHeadlineTxtColor = parseColor(getStyle(subheadline, 'color'));

					if(headerTxtColor.toString().length == 3) headerTxtColor = headerTxtColor.substring(0, 1)+headerTxtColor.substring(0, 1)+headerTxtColor.substring(1, 2)+headerTxtColor.substring(1, 2)+headerTxtColor.substring(2, 3)+headerTxtColor.substring(2, 3);
					if(headlineTxtColor.toString().length == 3) headlineTxtColor = headlineTxtColor.substring(0, 1)+headlineTxtColor.substring(0, 1)+headlineTxtColor.substring(1, 2)+headlineTxtColor.substring(1, 2)+headlineTxtColor.substring(2, 3)+headlineTxtColor.substring(2, 3);
					if(subHeadlineTxtColor.toString().length == 3) subHeadlineTxtColor = subHeadlineTxtColor.substring(0, 1)+subHeadlineTxtColor.substring(0, 1)+subHeadlineTxtColor.substring(1, 2)+subHeadlineTxtColor.substring(1, 2)+subHeadlineTxtColor.substring(2, 3)+subHeadlineTxtColor.substring(2, 3);

					thisMovie('leadproxy').drawBackground(headerBgColor, headlineBgColor, headerTxtColor, headlineTxtColor, subHeadlineTxtColor); 
				}*/
				
				return false;
			}
		}
		
	}
	
}

addLoadEvent(swapStudentWork);

/*function checkColors() {

if (document.cookie.indexOf("edu-mica-custom-colors")!=null) {
	

	
				var customCSS = readCookie('edu-mica-custom-colors');

				var headID = document.getElementsByTagName("head")[0];         
				var cssNode = document.createElement('link');
				cssNode.type = 'text/css';
				cssNode.rel = 'stylesheet';
				cssNode.href = customCSS;
				cssNode.media = 'screen';
				headID.appendChild(cssNode);
				
}
}

addLoadEvent(checkColors);

function checkPiece() {
		if(!d.getElementById || !d.getElementsByTagName) { return; }

	if (document.cookie.indexOf("edu-mica-selected-piece")!=null) {
		
						if(d.getElementById('pieces') && d.getElementById('account-work')) {
			var pieces = d.getElementById('pieces')
			var account_work = d.getElementById('account-work');
		}else{
			return;
		}
		
		
		var piecesLength = pieces.getElementsByTagName('li').length;
					
					for(var j = 0; j < piecesLength; j++){
					if(pieces.getElementsByTagName('li')[j].className.indexOf('current') != -1){
						if(j == 0){
							pieces.getElementsByTagName('li')[j].className = 'first';
						}else{
							pieces.getElementsByTagName('li')[j].className = '';
						}
					}
				}
				
				
				var selectedPiece = readCookie('edu-mica-selected-piece');
			
				var piece = document.getElementById(selectedPiece);
			
				d.getElementById(selectedPiece).className = 'current';		
			
				
			}
				
					
		}
addLoadEvent(checkPiece);*/


/*function newTheme() {
	
	if(!d.getElementById || !d.getElementsByTagName) { return; }

	
		if(d.body.id == 'home'){
	
		if(d.getElementById('pieces') && d.getElementById('account-work')) {
			var pieces = d.getElementById('pieces')
			var account_work = d.getElementById('account-work');
		}else{
			return;
		}
		
	
		var piecesLength = pieces.getElementsByTagName('li').length;
		
		for(var i = 0; i < piecesLength; i++) {
			
			pieces.getElementsByTagName('a')[i].num = i;
			
			pieces.getElementsByTagName('a')[i].onclick = function() {
				
				this.getElementsByTagName('span').item(0).onclick = function() {
					window.location = this.parentNode.href;
				
			if (document.cookie.indexOf("edu-mica-selected-piece")!=null && document.cookie.indexOf("edu-mica-custom-colors")!=null) {
			
			deleteCookie(document.cookie("edu-mica-selected-piece") && document.cookie("edu-mica-custom-colors"));
			swapStudentWork();
			
					}
			
				}
		
			}
	
		}
		
	}	
	
}

addLoadEvent(newTheme);*/

/*
	
	Go to, if they have flash
	

*/

function getURL(url)
{
	if(swfobject.hasFlashPlayerVersion('8.0.0'))
	{
		window.location = url;
		return false;
	}
	
	return true;
}


/*-------------------------------------------    
    Borrowed Functions
-------------------------------------------*/
/*

	addLoadEvent
	by Simon Willison
	http://simonwillison.net/2004/May/26/addLoadEvent/

*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/*

	getElementsByClass
	by Dustin Diaz
	http://www.dustindiaz.com/getelementsbyclass/

*/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/*

	Read/Write Cookies

*/

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=mica.edu";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

/*function deleteCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()-(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else expires = "";
document.cookie = name+"="+value+expires+"; path=/; domain=mica.edu";
}*/


/*

	Get Computed Style
	by Robert Nyman
	http://www.robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/

*/

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}


/*

	RGB -> HEX Conversion
	http://www.javascripter.net/faq/rgbtohex.htm

*/

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

function parseColor(color)
{
	if(color.indexOf('rgb') >= 0)
	{
		color = color.replace(/^\s*rgb\((.*?)\)/, '$1');
		color = RGBtoHex(color.split(',')[0], color.split(',')[1], color.split(',')[2])
	}
	if(color.indexOf('#') >= 0)
	{
		color = color.replace(/^#/, '');
	}
	
	return color;
}

/*

	Get Flash Movie
	http://livedocs.adobe.com/flash/8/main/00002201.html

*/

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}