﻿$(document).ready(function() {
	if($.browser.msie && parseFloat($.browser.version) < 7){
		$('#nav ul ul a').each(function(){
			$(this).css('display', 'inline-block');
		});
	}
  if ($('#quotes')) {
  	var prevA = document.createElement('a');
    prevA.href = '#';
    prevA.innerHTML = '&lt;';
    prevA.className = 'goto';
    $(prevA).appendTo($('#rotator'));
    $(prevA).click(function(e){
    	e.preventDefault();
    	prevQuote();
    });
    
    $('#quotes').find('.quote').each(function(index) {
      var newA = document.createElement('a');
      newA.href = '#';
      newA.innerHTML = (index+1);
      newA.className = 'box';
      if(index == 0){
      	$(newA).addClass('select');
      	$(this).addClass('select');
      	$('#quotes').animate({'height':$(this).height()}, 0);
      }
	    else {
	    	$(this).fadeOut(0);
	    }
	    $(newA).click(function(e){
	    	e.preventDefault();
	    	playQuote(index);
	    });
      $(newA).appendTo($('#rotator'));
    });
    
    var nextA = document.createElement('a');
    nextA.href = '#';
    nextA.innerHTML = '&gt;';
    nextA.className = 'goto';
    $(nextA).appendTo($('#rotator'));
    $(nextA).click(function(e){
    	e.preventDefault();
    	nextQuote();
    });
    
    //$('#quotes').css('display', 'block');
  }
});

var currentQuote = 0;
var quoteLength = 5; //Quote length in seconds

function nextQuote(){	
	$('#quotes .quote').each(function(index){
		if(index != currentQuote){
			$($('#quotes .quote')[currentQuote]).stop(true);
			$($('#quotes .quote')[currentQuote]).fadeOut(0);
		}
	});
	
	$($('#quotes .quote')[currentQuote]).fadeOut(0, function(){
		$($('#rotator .box')[currentQuote]).removeClass('select');
		currentQuote = currentQuote+1;
		if(currentQuote >= $('#quotes .quote').length){
			currentQuote = 0;
		}
		
		$($('#rotator .box')[currentQuote]).addClass('select');
		$($('#quotes .quote')[currentQuote]).fadeTo('slow', 1);
		$('#quotes').stop(true);
		$('#quotes').animate({'height':$($('#quotes .quote')[currentQuote]).height()},'slow');
	});
}

function prevQuote(){	
	$('#quotes .quote').each(function(index){
		if(index != currentQuote){
			$($('#quotes .quote')[currentQuote]).stop(true);
			$($('#quotes .quote')[currentQuote]).fadeOut(0);
		}
	});
	
	$($('#quotes .quote')[currentQuote]).fadeOut(0, function(){
		$($('#rotator .box')[currentQuote]).removeClass('select');
		currentQuote = currentQuote-1;
		if(currentQuote < 0){
			currentQuote = ($('#quotes .quote').length-1);
		}
		
		$($('#rotator .box')[currentQuote]).addClass('select');
		$($('#quotes .quote')[currentQuote]).fadeTo('slow', 1);
		$('#quotes').stop(true);
		$('#quotes').animate({'height':$($('#quotes .quote')[currentQuote]).height()},'slow');
	});
}

function playQuote(quote){	
	$('#quotes .quote').each(function(index){
		if(index != currentQuote){
			$($('#quotes .quote')[currentQuote]).stop(true);
			$($('#quotes .quote')[currentQuote]).fadeOut(0);
		}
	});
	
	$($('#quotes .quote')[currentQuote]).fadeOut(0, function(){
		$($('#rotator .box')[currentQuote]).removeClass('select');
		currentQuote = quote;
		
		$($('#rotator .box')[currentQuote]).addClass('select');
		$($('#quotes .quote')[currentQuote]).fadeTo('slow', 1);
		$('#quotes').stop(true);
		$('#quotes').animate({'height':$($('#quotes .quote')[currentQuote]).height()},'slow');
	});
} 
