// JavaScript Document

// Initials:
var currentPanel = 1;
var panelInterval = null;

var currentFeedback = 1;
var feedbackTimer = null;


// Rotate viewport from panel to panel:
function togglePanels() {
			
	$("div#panels div.panel").removeClass("selected");
	$("div#panels div.panel").fadeTo("fast", ".2");
	$("div#panels div.panel").eq(currentPanel - 1).addClass("selected");

 $("div#panels div.panel").each(function() {
		var absDiff = Math.abs( $("div#panels div.panel").index(this) + 1 - currentPanel );
		$(this).css("z-index", 5 - absDiff); });

	$("div#panels div.selected").fadeTo("slow", "1");
	$("div#view_port").animate({"left": currentPanel * 100 - 100+"px"}, "slow");
	
	currentPanel++;
	
	if(currentPanel > ($("div.panel").length)) {
		currentPanel = 1; } }



// Load the next feedback;
function nextFeedback() {

	$("div#featured_feedback div.feedback").hide();

	$("div#featured_feedback div.feedback p.quote").css("marginTop", "90px");
	$("div#featured_feedback div.selected").removeClass("selected");

	$("div#featured_feedback div.feedback").eq(currentFeedback - 1).addClass("selected");
 $("div#featured_feedback div.selected").show();
	
 $("div#featured_feedback p.current_author span").fadeOut(1000, function(){
 	$("div#featured_feedback p.current_author span").text($("div#featured_feedback div.selected p.author").text());
		$("div#featured_feedback p.current_author span").fadeIn("slow"); 
	 scrollFeedback(); });
	
	currentFeedback++;
	
	if(currentFeedback > ($("div.feedback").length)) {
		currentFeedback = 1; } }



// Scroll the feedback up:
function scrollFeedback() {
	
	var bottomPos = 90;
	var topPos = $("div#featured_feedback div.selected p.quote").height();
	var currentPos = $("div#featured_feedback div.selected p.quote").css("marginTop").slice(0, $("div#featured_feedback div.selected p.quote").css("marginTop").indexOf("p", 0));
	var remainingPctg = Math.abs((1 - (Math.abs(currentPos-bottomPos) / topPos)) * 100);
	var nextTimer = Math.abs((topPos)*remainingPctg);

 $("div#featured_feedback div.selected p.quote").animate({"marginTop": 78-topPos+"px"}, nextTimer);
 feedbackTimer = setTimeout("nextFeedback()", nextTimer + 4000); }



// Listener functions:
$(document).ready(function() {

	// Hide hidden items:
	$(".hidden").hide();
	
	// Stop the default action of empty links:
	$("a.none").click(function(event) {
		event.preventDefault(); });
	
	// Set the current page to active in CSS
 $("a[href*='" + window.location.href.substring(window.location.href.indexOf('=')+1) + "']").addClass("current_page");

 // Run the viewport function once:
	togglePanels();

 // Rotate selected panels:
	panelInterval = setInterval("togglePanels()", 10000);
	
	// Run the feedback scroll function once:
	nextFeedback();
	
	
	
 // User has moused over a toggle item:
 $(".toggle_item > *").hover(function(){
			
			// Make this item selected:
			$(this).parent().addClass("selected");
	
	// User has moved mouse off of this item:
	}, function() {
	
	 // Deselect this item:
	 $(this).parent().removeClass("selected"); });
	


	// User has hovered over a panel:
	$("div#panels div.panel").hover(function() {
 
	 // Make this the active panel:
	 currentPanel = $("div#panels div.panel").index(this) + 1;
		togglePanels(); });
	
 // User has moused over the active panel:
	$("div#panels div#view_port").hover(function(){
  clearInterval(panelInterval);
	
	// User has moved mouse off active panel:
	}, function() {
		
		// Rotate selected panels:
	 panelInterval = setInterval("togglePanels()", 10000); });
	
	// User has clicked on an active panel:
 $("div#panels div#view_port").click(function() {
		
		// Send user to panel URL:
		location.href = $("div#panels div.selected a").attr("href");	});
		

	// User has moused over a column heading:
	$("div.column h1 > *").hover(function(){

  $(this).parent("h1").addClass("selected");
	 $("div.column h1.selected span").fadeIn();
	
	// User has moved mouse away from column heading:
	}, function() {
		
		$("div.column h1.selected a:animated").stop();
		$("div.column h1.selected span").fadeOut();
		$("div.column h1.selected a").animate({"textIndent": "0px"}, "slow");
		$(this).parent("h1").removeClass("selected");
		
	});
	
	
	// Set the animation lengths for column headings:
	$("div#latest_plays h1 > *").hover(function(){
		$("div.column h1.selected a").animate({"textIndent": "-400px"}, 3000); });

	$("div#recent_posts h1 > *").hover(function(){
		$("div.column h1.selected a").animate({"textIndent": "-392px"}, 3000); });
	
	$("div#featured_feedback h1 > *").hover(function(){
		$("div.column h1.selected a").animate({"textIndent": "-335px"}, 3000); });
	
	$("div#statistics h1 > *").hover(function(){
		$("div.column h1.selected a").animate({"textIndent": "-394px"}, 3000); });
	
	$("div#featured_partners h1 > *").hover(function(){
		$("div.column h1.selected a").animate({"textIndent": "-336px"}, 3000); });
	
	
	
	// User has moused over a latest play:
	$("table.results .toggle_item > *").hover(function() {
			$(".toggle tr.selected td.arrow img").attr("src", "images/elements/arrow_selected.png");
	
	// User has moved mouse away from a latest play:
	}, function() {
			$("td.arrow img").attr("src", "images/elements/arrow.png"); });		
			
	
	// User has moused over a recent post title:
	$("ul.posts .toggle_item > *").hover(function() {
		
		var titleWidth = $(".toggle li.selected p.title > span").width();
		var clippingWidth = $(".toggle li.selected p.title").width();
		
		if(titleWidth > clippingWidth) {
			
			var textIndent = titleWidth - clippingWidth;
			
		 $(".toggle li.selected p.title").animate({"textIndent": -textIndent}, 3000); }
	
	// User has moved mouse away from a latest play:
	}, function() {
		
		$("ul.posts li.toggle_item p:animated").stop();
		$(".toggle li p.title").animate({"textIndent": "0px"}, "slow"); });
			
	
	// User has moused over current feedback:
	$("div#featured_feedback div.clipping_region").hover(function(){

		$("div#featured_feedback div.selected").css("backgroundColor", "#444");
		$("div#featured_feedback div.selected > p").css("color", "#AAA");
		
  $("div#featured_feedback div.selected p.quote").stop();
		clearTimeout(feedbackTimer);
	
	// User has moused away from current feedback:
 }, function() {
		
		$("div#featured_feedback div.selected").css("backgroundColor", "#DFE6EA");
		$("div#featured_feedback div.selected > p").css("color", "#444");
		
		scrollFeedback();
		
	});
	
	// User has moused over a partner:
	$("div#featured_partners tr.toggle_item").hover(function(){
		
		$("div#featured_partners img.logo").stop();

  var logoWidth = $("div#featured_partners tr.selected div.clipping_region img.logo").width();
		logoWidth = $("div#featured_partners tr.selected div.clipping_region").width() - logoWidth;

		$("div#featured_partners tr.selected div.clipping_region img.logo").animate({"left": logoWidth + "px"}, 5000);
		$("div#featured_partners tr.selected a.details").fadeIn(1000);
	
	// User has moused away from current feedback:
 }, function() {
		
		$("div#featured_partners img.logo").stop();
		$("div#featured_partners tr.toggle_item img.logo").animate({"left": "0px"}, 500);
		$("div#featured_partners a.details").fadeOut(500);
		
	});
	
});