var curStyleIndex = 0;
var styleArray = new Array();
styleArray[0] = "Text small";
styleArray[1] = "Text medium";
styleArray[2] = "Text large";
styleArray[3] = "Text extra-large";

checkStylesheet();

function ChangeSrc(objToChange, strNewSrc) {
	objToChange.src = strNewSrc;
	return true;
}

function checkStylesheet()
{
	cookieStyleIndex = getCookie("text_style");

	if (cookieStyleIndex != "null")
	{
		setStylesheet(cookieStyleIndex);
		curStyleIndex = cookieStyleIndex;
	}
	return true;
}

function cookieExists(cookieName)
{
	if (document.cookie != "")
	{
		var theCookieList = document.cookie.split(";");

		for (i = 0; i < theCookieList.length; i++)
		{
			var currCookieName = theCookieList[i].split("=")[0];
			var currCookieValue = theCookieList[i].split("=")[1];

			if (currCookieName.indexOf(cookieName) != -1)
			{
				if (currCookieValue == "null")
				{
					return false;
				}

				return true;
			}
		}
	}

	return false;
}

function getCookie(cookieName)
{
	if (cookieExists(cookieName))
	{
		var theCookieList = document.cookie.split(";");

		for (var i = 0; i < theCookieList.length; i++)
		{
			if (theCookieList[i].split("=")[0].indexOf(cookieName) != -1)
			{
				return theCookieList[i].split("=")[1];
			}
		}
	}

	return "null";
}

function doStyle(cookieName, styleIndex)
{
	if ((styleIndex >= 0) && (styleIndex < styleArray.length))
	{
		setStylesheet(styleIndex);
		setCookie(cookieName, styleIndex);
		curStyleIndex = styleIndex;
	}
	checkBoundaries(styleIndex);
}

function setCookie(cookieName, cookieValue)
{
	var cookieOptions = "path=/;";

	cookieOptions += "expires=" + (new Date("December 31, 2010")).toGMTString() + ";";
	document.cookie = cookieName + "=" + cookieValue + ";" + cookieOptions;

	return;
}

function setStylesheet(styleIndex)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleArray[styleIndex])
				{				
					currTag.disabled = false;
				}
			}
		}
	}
	else
	{
		alert("You may not switch styles with this browser.");
	}
}

function checkBoundaries(styleIndex) 
{
	var textLarge = document.getElementById("text_larger");
	var textSmall = document.getElementById("text_smaller");
	textLarge.className = "";
	textSmall.className = "";
	if (styleIndex >= styleArray.length - 1) textLarge.className = "disabled";//textLarge.setAttribute("class", "disabled");
	else if (parseInt(styleIndex) <= 0) textSmall.className = "disabled";//textSmall.setAttribute("class", "disabled");
}

// front page scripts

// featured story slideshow
var intDelaySlide = 8;

function init_slide_container()
{
	// only init slide if container exists and the page is in print mode
	if (document.getElementById("slideContainer") && (document.location.pathname.lastIndexOf("print") != (document.location.pathname.length - ("print").length)))
	{
		makeSlideContainer("slideContainer", intDelaySlide);
	}
}

var chosen = Array();
var intervalIds = Array();
var intervals = Array();

function makeSlideContainer( id, delay ) {
	var container = document.getElementById( id );
	var aTags = container.getElementsByTagName('a');
	for ( var j=0; j<aTags.length; j++ ) {
		if ( aTags[j].className.indexOf('thumb') > -1 ) {
			aTags[j].onclick = switchClick;
			
		}
	}
	var slide = container.firstChild;
	while ( slide != null ) {
		var next = slide.nextSibling;
		if ( slide.nodeType != 1 ) {
				container.removeChild( slide );
		}
		slide = next;
	}
	chosen[id] = 0;
	intervals[id] = delay * 1000;
	//startAutoSwitch( id );
	setTimeout( function () { switchLoop(id); }, intervals[id] );
}

function startAutoSwitch( id ) {
	//intervalIds[id] = setInterval( function () { switchLoop(id); }, intervals[id] );
}

function stopAutoSwitch( id ) {
	/*
	clearInterval( intervalIds[id] );
	intervalIds[id] = -1;
	*/
}

function switchLoop123( id ) {
	var n = currentSlide[id] + 1;
	n = (n % slideContainers[id].length);
	return switchNth( id, n );
}

function switchLoop( id ) {
	try {
		var slide = document.getElementById( id ).firstChild;
		if ( slide != null ) {
			switchSlide( slide );
		}
	}
	catch( e ) {
		alert( e.message );
	}
	setTimeout( function () { switchLoop(id); }, intervals[id] );
}

function switchClick( e ) {
	var a;
	if ( e != null ) {
		// Firefox
		a = e.target;
		if ( a.tagName != 'A' ) {
			// Safari
			a = a.parentNode;
		}
	}
	else {
		// IE
		a = window.event.srcElement.parentNode;
	}
	var slide = a.parentNode;
	var container = slide.parentNode;
	var id = container.id;
	if ( container.firstChild != slide || chosen[ id ] != 1 ) {
		stopAutoSwitch( id );
		chosen[ id ] = 0;
		if ( container.firstChild != slide ) {
			switchSlide( a.parentNode );
		}
		chosen[ id ] = 1;
		startAutoSwitch( id );
	}
	return false;
}

function switchSlide( selected ) {
	if ( selected ) {
		var container = selected.parentNode;
		var newContainer = container.cloneNode( false );
		var slide = container.firstChild;;
		if ( chosen[ container.id ] == 1 ) {
			newContainer.appendChild( slide.cloneNode( true ) );
			slide = slide.nextSibling;
		}
		else {
			if ( slide != selected ) {
				newContainer.appendChild( selected.cloneNode( true ) );
			}
		}
		newContainer.appendChild( slide.cloneNode( true ) );
		while ( (slide = slide.nextSibling) != null ) {
			if ( slide != selected ) {
				newContainer.insertBefore( slide.cloneNode( true ), newContainer.lastChild );
			}
		}
		
		// resetting the classnames for the divs that are direct children
		// of the container.
		var counter = 0;
		slide = newContainer.firstChild;
		while ( slide != null ) {
			if ( slide.tagName == 'DIV' ) {
				if ( counter == 0 ) {
					slide.className = 'selected';
				}
				else if ( counter == 1 ) {
					slide.className = 'highlighted';
				}
				else if (counter == 2 ) {
					slide.className = 'thumb1';
				}
				else if (counter == 3 ) {
					slide.className = 'thumb2';
				}
				counter++;
			}
			slide = slide.nextSibling;
		}
		
		// unfortunately, actions are lost when cloning nodes.
		// re-associating onclick with the switchClick() function.
		var aTags = newContainer.getElementsByTagName('a');
		for ( var j=0; j<aTags.length; j++ ) {
			if ( aTags[j].className.indexOf('thumb') > -1 ) {
				aTags[j].onclick = switchClick;
			}
		}
		var p = container.parentNode;
		p.replaceChild( newContainer, container );
		
		/*
		var containerID = container.id;
		newContainer.style.zIndex = 0;
		newContainer.id = "SlideContainerReplacement";
		container.style.zIndex = 0;
		container.parentNode.appendChild( newContainer );
		newContainer.style.zIndex = 100;
		container.parentNode.removeChild( container );
		newContainer.id = containerID;
		*/
		return false;
	}
	else {
		return true;
	}
}

//var onload_before_init_slide_container = (window.onload) ? window.onload : alert("false");;
		$.fn.equalHeight = function(){
			var max_height = 0;
			
			this.each(function(){
				var bottom = this.offsetTop + $(this).height();
				if (bottom > max_height) max_height = bottom;
			});
			
			this.each(function(){
				var cur_height = $(this).height();
			    
				// we don't want to touch the div we are trying to match up against, so test to see if the height of the div is smaller than what we are trying to match
				if (max_height - this.offsetTop > cur_height)
				{
					// remove bottom padding since the div will grow anyway, then get current height again
					this.style.paddingBottom = 0;	    
					cur_height = $(this).height();
			    
					// remove top padding and calculate what it was so we can add it later. keeping padding will skew the height.
					this.style.paddingTop = 0;
					var pad_top = cur_height - $(this).height();
		    	    
					var new_height = max_height - this.offsetTop - pad_top;
					$(this).css("height", new_height);
					$(this).css("padding-top", pad_top);
				}
			});
		};
		
$(document).ready(function (e) 
	{ 
		//onload_before_init_slide_container(); 
		checkStylesheet();
		checkBoundaries(curStyleIndex);
		$("div.equal-last > div:last-child, div.equal-all > div, div.equal").equalHeight();
		init_slide_container(); 
		
	});