var origColor, scrollTop;
var detect = navigator.userAgent.toLowerCase();
var scrollInterval;
var stopDrag = true;
// above vars never used? - TH

/* begin global */
function load( src ) {
	var tmp = src;
	var re = new RegExp( "_", "g" );
	tmp = tmp.replace( re, "/" );
	
	if ( window.name == 'slide' && !(src.indexOf("/EML/") > -1 ) )
	{
		top.slide.location.href = tmp;
	}
	else if (src.indexOf("/EML/") > -1 )
	{
		emailWindow = window.open("http://www.brandlogic.com/themes/brandensemble.themes.default/email.aspx?l=" + escape(window.location) + "&t=" + escape(document.title), 'eml', 'scrollbars=no,menubar=no,height=254,width=247,resizable=yes,toolbar=no,location=no,status=yes');
	}
	else 
	{
		movieWidth = "So I says to the girl on the beach...";
		movieHeight = "...Sandy?";
		var foo = tmp.replace( new RegExp( "^.*\/([\\w]{3})\/([\\d]{2})(\/([\\d]{2}))?[\/]?$", "" ),"$1$2$4" );
		if (foo.length == 5) foo = foo + "01";
		switch (foo.substring(foo.length - 7,foo.length - 2))
		{
			case "CSS01":
				movieWidth = "700";
				movieHeight = "480";
				break;
			case "CSS10":
				movieWidth = "640";
				movieHeight = "480";
				break;
			case "CSS09":
				movieWidth = "700";
				movieHeight = "480";
				break;
			case "CSS12":
				movieWidth = "690";
				movieHeight = "535";
				break;
			case "CSS18":
				movieWidth = "690";
				movieHeight = "535";
				break;
			case "CSS19":
				movieWidth = "690";
				movieHeight = "535";
				break;
			case "CSS28":
				movieWidth = "690";
				movieHeight = "535";
				break;
			case "RES04":	// BrandAudit (doesn't seem to appear in the site though)
				movieWidth = "690";
				movieHeight = "561";
				break;
			case "PRF06":
				movieWidth = "569";
				movieHeight = "255";
				break;
			case "":	// home page flash
				movieWidth = "799";
				movieHeight = "285";
				break;
			default:	// standard dimensions for CSS windows
				movieWidth = "646";
				movieHeight = "380";
				break;
		/*	TO COME
			case "CSS10":
			case "CSS23":
			case "CSS27":
		*/
		}
		if (window.innerHeight && window.innerWidth ) {
			newWin = window.open( tmp, "slide", "scrollbars=no,menubar=no,height=" + movieHeight + ",width=" + movieWidth + ",resizable=yes,toolbar=no,location=no,status=no");
			newWin.innerHeight = movieHeight;
			newWin.innerWidth = movieWidth;
		}
		else {
			newWin = window.open( "", "slide", "scrollbars=no,menubar=no,height=0,width=0,resizable=no,toolbar=no,location=no,status=no");
			newWin.close();
			newWin = window.open( tmp, "slide", "scrollbars=no,menubar=no,height=" + movieHeight + ",width=" + movieWidth + ",resizable=yes,toolbar=no,location=no,status=no");
		}
		if (window.focus) newWin.focus();
		return newWin;
	}
	return window;
};

function rolloverArrow(itemID, templateColor, direction) {
	var arrow = document.getElementById("arrow" + itemID);
	arrow.src = "/images/" + templateColor + "/" + direction + "arrowhover.gif";
};

function rolloutArrow(itemID, templateColor, direction) {
	var arrow = document.getElementById("arrow" + itemID);
	arrow.src = "/images/" + templateColor + "/" + direction + "arrow.gif";
};

function positionImages( n )
{
	var node = document.getElementById( n );
	if ( node != null ) {
		var i = 1;
		var w = 799;
		var c = node.parentNode.childNodes;
		var l = 0;
		
		for ( var j = 0; c[j] != node; j++ ) {
			l = l + c[j].clientWidth + 1;
		}
		
		while ( document.getElementById('large' + i) != null ) {
			var img = document.getElementById('large' + i);
			var right = 0;
			var top = 20;
			
			if ( img.clientWidth == 170 ) {
				right = w - l - node.clientWidth;
				top = 18;
			} else if ( img.clientWidth == 227 ) {
				right = w - l - img.clientWidth - 2;
				top = 18;
			}
			
			if ( right < 0 )
				right = 0;
			
			img.style.right = right + "px";
			img.style.top = top + "px";
			i++;
		}
	}
};

// get an array of all the available parameters
function getParams()
{
	var params = new Array();
	var current = 0;

	s = location.search.substring(1,location.search.length).split('&');
	for (i = 0; i < s.length; i++)
	{
		t = s[i].split('=');
		params[ current ] = t[ 0 ];
		current++;
	}

	return params;
};

// retrieve a parameter value from the query string.
function getParam( name )
{
	var v;
	var s = location.search.substring(1,location.search.length).split('&');
	for (var i = 0; i < s.length; i++)
	{
		var t = s[i].split('=');
		if (t[0] == name)
		{
			v = unescape(t[1]).split('+').join(' ');
			i = s.length++;
		}
		else
		{
			v = '';
		}
	}
	return( v );
};

function addZero(vNumber)
{ 
    return ((vNumber < 10) ? "0" : "") + vNumber;
};
    
function formatDate(vDate, vFormat)
{ 
    var vDay = addZero(vDate.getDate()); 
    var vMonth = addZero(vDate.getMonth() + 1); 
    var vYearLong = addZero(vDate.getFullYear()); 
    var vYearShort = addZero(vDate.getFullYear().toString().substring(3, 4)); 
    // var vYear = ((vFormat.indexOf("yyyy") > -1) ? vYearLong : vYearShort) ;	// this didn't work in safari in combination with the old regex for some reason - TH
    var vHour = addZero(vDate.getHours()); 
    var vMinute = addZero(vDate.getMinutes()); 
    var vSecond = addZero(vDate.getSeconds()); 
    var vDateString = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/yyyy/g, vYearLong);
    vDateString = vDateString.replace(/yy/g, vYearShort);
    vDateString = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond);
    return vDateString;
};

// slide layout initialization
function testSlide() {
	if (window.name == 'eml')
	{
		var referringPage = getParam('t') + '\n' +getParam('l');
		var pageAddy = getParam('t');
		//document.getElementById("pageAddress").innerHTML = pageAddy.replace(/\n/, '&lt;br/&gt;');
		document.getElementById("pageAddress").innerHTML = pageAddy;
		document.getElementById("referringPage").value = referringPage;
	}
	else if ( window.name != 'slide')
	{
		var newWindow = load(location.href);
		if (newWindow)
		{
			if (history.length > 1)
			{
				//window.close();	// ??? -TH
				history.back();
			}
			else
			{
				window.location = "http://www.brandlogic.com/";
			}
		}
		newWindow.focus();
	}
	//this.window.focus();
};

// initialize all the javascript stuffs
function init(section)
{
	if (debugJS)
	{
		debugInit();
	}
	if (text_fadeJS)
	{
		fadeText();
	}
	
	if ( document.getElementById('large1') != null )
	{
		//positionImages( section );
		var done = false;
		var i = 1;
		var item;
		while ( (item = document.getElementById(section + i)) != null && !done )
		{
			if ( item.className.indexOf("highlight") > -1 )
			{
				item.onmouseover();
				done = true;
			}
			i++;
		}

		if ( section != '' )
		{
			i = 1; 
			while ( (item = document.getElementById(section + i)) != null )
			{
				if ( document.getElementById('large'+i) != null )
				{
					var offset = 1;
					if ( section != 'NEW' && section != 'BRF' && document.getElementById('large' + i).src.indexOf("FEL") > -1)
					{
						offset += 57;
					}
					if ( section == 'BRF' )
					{
						document.getElementById('large' + i).style.right = "0px";
						document.getElementById('large' + i).style.left = "auto";
					}
					else
					{
						document.getElementById('large' + i).style.left = (document.getElementById(section).offsetLeft - offset) + "px";
						document.getElementById('large' + i).style.right = "auto";
					}
					document.getElementById('large' + i).style.top = "auto";
					document.getElementById('large' + i).style.bottom = "-134px";
					document.getElementById('large' + i).style.display = "block";
				}
				i++;
			}
		}
		else
		{
			document.getElementById('large1').style.display = "block";
		}
		
	}
	
	if (scrollbarsJS)
	{
		if (document.getElementById('scrollbar') != null)
		{
			if (document.getElementById('contentText').clientHeight < document.getElementById('contentBody').clientHeight)
			{
				document.getElementById('scrollbar').style.visibility = 'hidden';
			}
			else
			{
				document.getElementById('scrollbar').style.height = document.getElementById('contentBody').clientHeight + "px";
				document.getElementById('scrollLine').style.height = document.getElementById('contentBody').clientHeight + "px";
				initScroll();

				// Hopping to a specific anchor tag in the contentText
				var hopTo = location.search.substr( 1 );
				if ( hopTo != '' )
				{
					var anchors = document.getElementById('contentText').getElementsByTagName('a');
					var anchor = null;
					for( i=0; i < anchors.length && anchor == null; i++ )
					{
						if ( anchors[i].name.indexOf( hopTo ) > -1 )
						{
							anchor = anchors[i];
						}
					}
					if ( anchor != null )
					{
						anchor.style.color = "red";
						anchor.style.fontWeight = "bold";
						var contentBody = document.getElementById('contentBody'); 
						var offset = 16 + 
								((document.getElementById('scrollLine').offsetHeight - 22)
								* (anchor.offsetTop)
								/ (anchor.offsetParent.offsetHeight 
								- contentBody.offsetHeight));
						var myE = { layerY: offset, layerX:0, offsetY: offset, offsetX:0 };
						document.getElementById('scrollLine').onmousedown(myE);
					}
				}
				//document.getElementById('downArrow').style.visibility = 'visible';
			}
		}
	}
    
    if (formsJS)
    {
		// add in comments
		setComments();
    }

    // set todays date in NEWS
	 var date = new Date();
     var today = document.getElementById( 'todaysDate' );
     if ( today != null ) {
        today.appendChild( document.createTextNode( formatDate( date, "MM/dd/yyyy" ) ) );
     }
     
     if (suckerfish_menusJS)
     {
		startList();
     }
     
	// college mode
	var path = document.location.pathname;
	// allow an rcb object to be highlighted by its id?
	//var highlight = getParam("cid");
	switch (path)
	{
			case "/":
				switch (getParam("for"))
				{
					case "college":
						// add custom content
						var contentContainer = document.getElementById("content");
						var newDiv = document.createElement("div");
						newDiv.id = "collegeLink";
						//newDiv.style.position = "static";
						newDiv.style.marginTop = "15px";
						newDiv.style.marginLeft = "15px";
						//newDiv.style.top = "140px";
						newDiv.style.width = "365px";
						newDiv.innerHTML = "<p><strong>Battle of the brands in higher education</strong></p>";
						newDiv.innerHTML += "<p style=\"margin: 15px 0;\">The days when the biggest rivalries among colleges and universities were fought on the gridiron and in the gymnasium are gone. Today, the main clashes take place within the admissions arena, where schools are locked in intense competition for the best and brightest students. Schools employ many strategies to win these battles, but none are more effective, especially over the long term, than projecting the right image. <!-- The value of a college or university brand can exceed that of the institution's physical assets, even more so than in the world of consumer products, where companies routinely spend millions developing and promoting their brands. --> See case study at right about how the new St. John's University brand/identity program helped them save communications dollars and exceed their marketing goals.</p>";
						newDiv.innerHTML += "<p>Free BrandLogic Journal:<br />";
						newDiv.innerHTML += "<a href=\"/CCT/?infotype=Journals&currentjournal=the+current+journal\" style=\"color: #FFF;\">Bringing branding to higher education marketing</a></p>";
						if (document.getElementById("landing-menu"))
						{
							document.getElementById("landing-menu").style.display = "none";
						}
						contentContainer.appendChild(newDiv);

						// highlight the SJU case study
						// TODO: add function that will highlight an rcb item based on its content id - TH
						// highlight CSS/07 in the rcb
						rollOverText('CSS', 2, 'CSS');
						break;
					case "ibm":
						// highlight the SJU case study
						// TODO: add function that will highlight an rcb item based on its content id - TH
						// highlight CSS/07 in the rcb
						rollOverText('CSS', 1, 'CSS');
						// add custom content
						//var contentContainer = document.getElementById("content");
						//contentContainer.innerHTML += "<div id=\"ibmLink\" style=\"position: absolute; width: 140px; left: 229px; top: 140px;\"><a href=\"/RES/01/\" style=\"color: #FFF;\">Free BrandLogic Journal: The Battle of the Brands in Higher Education</a></div>";
						break;
				}
				break;
	}
};
var globalJS = 1;
