/* Remaining code from Thomas C Sherman @ http://blog.thomascsherman.com/
functions fade() and animateFade() from http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation */

$(document).ready( function() {

	//open links in new window via js (because _blank isn't xhtml strict compliant)
	//switch this to jQuery
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  if (links.length == 0) return false;
  for (var i = 0; i < links.length; i++) {
    var relation = links[i].getAttribute("rel");
    if (relation == "external") {
      links[i].onclick = function() {
        return !window.open(this.href);
      }
    }     
  }


});
	
//this has to be moved into jQuery and use the cycle plugin.	
var TimeToFade = 1000.0;					//time to fade in and fade out
var timeToShow = 7000.0;				//how long to display each piece of content
var timeToRest = 250;						//how long to wait before displaying next piece of content
var currentContentItem = 0;			//which content item you wish to start with, zero based
var contentContainer;						//the containing div
var contentItems;  							//all the content items
var fadeOutTimer;								//timer for handling fade outs
var fadeInTimer;								//timer for handling fade ins

function CycleQuotes()
{
	//get all the divs inside the contentContainer div.  Each of these is a unique quote
	contentContainer = document.getElementById("quotes");
	contentItems = contentContainer.getElementsByTagName("div");
	contentItems[0].style.display = "block";									//all child divs are currently hidden so show the first one			
	setTimeout("FadeOut()", timeToShow);
}

function PrevClick()
{
	PausePlayClick(false);

	//make sure the current quote is displayed
	contentItems[currentContentItem].style.display = "none";
	
	//change the current quote
	if (currentContentItem == 0)
		{ currentContentItem = contentItems.length - 1; }
	else
		{ currentContentItem--; } 
	
	contentItems[currentContentItem].style.display = "block";
}

function NextClick()
{
	PausePlayClick(false);
	
	//make sure the current quote is displayed
	contentItems[currentContentItem].style.display = "none";
	
	//change the current quote
	if (currentContentItem == contentItems.length - 1)		{ currentContentItem = 0; }
	else		{ currentContentItem++; } 	
	contentItems[currentContentItem].style.display = "block";
}

function PausePlayClick(playing)
{
	//get the play/pause buttons
	var playbutton = document.getElementById('playimg');
	var pausebutton = document.getElementById('pauseimg');
		
	//start/stop the current fade effects
	if (playing) 
	{	
		//change play icon to pause
		pausebutton.style.display = "";
		playbutton.style.display = "none";

		setTimeout("FadeOut()", 0);
	}
	else
	{
		//make sure we have a quote showing
		if (contentContainer.FadeState < 0)  { fade(contentContainer.id); }
		
		//change pause icon to play icon
		pausebutton.style.display = "none";
		playbutton.style.display = "";

		//stop timers
		clearTimeout(fadeOutTimer);
		clearTimeout(fadeInTimer);
	}
}

//Fade out quote
function FadeOut()
{
	fade(contentContainer.id);
	fadeOutTimer = setTimeout("FadeIn()", TimeToFade + timeToRest);
}

//Fade new quote in
function FadeIn()
{
	//hide the current quote (the container div will already have opacity set to 100% so this quote is currently invisible)
	contentItems[currentContentItem].style.display = "none";
	
	//show the next quote
	if (currentContentItem == contentItems.length - 1) { currentContentItem = 0; } else { currentContentItem++; }
	contentItems[currentContentItem].style.display = "block";
	
	//fade in the container
	fade(contentContainer.id);
	
	//recurse
	fadeInTimer = setTimeout("FadeOut()", timeToShow);
}


function fade(eid) //function from http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null 
        || element.style.opacity == '' 
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
    
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
  
}

function animateFade(lastTick, eid) //function from http://www.switchonthecode.com/tutorials/javascript-tutorial-simple-fade-animation
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = ' 
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
  
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}
