/* INSTRUCTIONS:
1. Setup area to use
*/

var codaTimer;
var theCodaBtn;

// when the DOM is ready...
$(document).ready(function () {

  // Check page has coda on it
  var element = $('#coda')[0];
  if(element){
    // Create button for each coda item
   	$(".coda_container").after("<ul class='coda_nav'></ul>");
   	var current_height = 0;
   	codaNumber = 1;
   	$(".coda_panel").each(function(){
   	  if(current_height < $(this).height()){
 	      current_height = $(this).height();
 	    }
   	  var coda_title = $(this).find(".microtitle").text();
   	  coda_id = $(this).attr("id");
   	  $(".coda_nav").append("<li id='btn_"+coda_id+"'><a href='#" + coda_id + "'>" + coda_title + "</a></li>");
   	  codaNumber++;
   	});
   	$(".coda_panel").css("height", current_height);


   	// hide first nav button when there is a flash frame as first slide
   	$('.hide_first').children('.coda_nav').children('li:first').css("display", "none");


  	// add classes for styling first and last buttons
  	$('.coda_nav').children('li:first').addClass('selected').addClass('first_btn');
  	$('.coda_nav').children('li:last').addClass('last_btn');
  	$('.hide_first').children('.coda_nav').children('li').each(function(b){
  		if(b == 1){
  			$(this).addClass('first_btn');
  		}
  	});

  	/* Setup CODA_FADE */
  	$("#coda_nojavascript").hide();
  	// If the url has a hash then show the relevant coda
  	the_hash = self.document.location.hash;
  	if(the_hash != ""){
  	   $("ul.coda_nav li a").each(function(){
        if($(this).parent().attr("id").substr(4) == the_hash.substr(2)){
          switchCoda("ul.coda_nav li#btn_coda_" + the_hash.substr(7) + " a", "load hash");
        }
      });
  	}else{
       switchCoda("ul.coda_nav li#btn_coda_0 a", "load no hash");
  	}

  	$("ul.coda_nav li a").click(function(){
      switchCoda($(this), "click");
      return false;
  	});
  }



});


// The below is for video players, ensuring that if a video is playing the coda will not animate
function playpause(obj) {
    playerState = obj.newstate;
}
var swfobjects = new Array();
var playerState;
function playerReady(obj){
  video_id = obj['id'].substring(10);
  swfobjects[obj['id']] = document.getElementById("swfobject-" + video_id);
  swfobjects[obj['id']].addModelListener("STATE", "playpause");
}


// Function to switch to the next coda
function switchCoda(theCodaBtn, codaSwitchTrigger){
  if((playerState == "PLAYING" || playerState == "BUFFERING") && (codaSwitchTrigger == "rollout timer" || codaSwitchTrigger == "normal timer")){
  }else{
  try{

    the_old_coda = $("li.selected").attr("id").substr(4);
    the_new_coda = $(theCodaBtn).parent().attr("id").substr(4);

    // Stop video playing (fix for IE only)
    if(jQuery.browser.msie && codaSwitchTrigger != "load hash" && codaSwitchTrigger != "load no hash"){
      //player1 = window.document["swfobject-1"];
      try{
        player = $("#" + the_old_coda + " #swfobject-1")[0];
        player.sendEvent("PLAY","false");
      }catch(err){}
    }


    // Calculate next coda to display
    currentCoda = $(theCodaBtn).parent().attr("id").substr($(theCodaBtn).parent().attr("id").length - 1, 1);
    codaTotal = $("ul.coda_nav li").size();
    if(currentCoda < (codaTotal - 1) ){
      nextCoda = parseInt(currentCoda) + 1;
    }else{
      nextCoda = 0;
    }


    // Change the coda being displayed
    if(the_old_coda != the_new_coda){
      $("li.selected").removeClass("selected");
      $("#coda a.active").removeClass("active");
      $(theCodaBtn).addClass("active").parent().addClass("selected");
      $("#"+the_old_coda).fadeOut(400,function(){
        $("#"+the_new_coda).fadeIn(400);
      });
      self.document.location.hash = "#n" + the_new_coda;
    }else{
      $("#"+the_new_coda).show();
      $(theCodaBtn).addClass("active").parent().addClass("selected");
    }


    // Setup Auto-Animate Timeout for next transition
    if($('#coda').children("div").hasClass("auto_animate")){
      timerInterval = 10000;
      codaBtn = $("ul.coda_nav li#btn_coda_" + nextCoda + " a");
      if(codaTimer){
        clearTimeout(codaTimer);
      }
      if(codaSwitchTrigger != "click"){
        codaTimer = setTimeout(function(){switchCoda(codaBtn, "normal timer");}, timerInterval);
      }

      $("#coda").hover(function(){
        clearTimeout(codaTimer);
      }, function(){
        if(codaTimer){
          clearTimeout(codaTimer);
        }
        codaTimer = setTimeout(function(){switchCoda(codaBtn, "rollout timer");}, timerInterval);
      });
    }
  }
  catch(err){}
  }
}