var rotatorCurrent = 1;
var rotatorLast = 0;
var rotatorCount = 0;
var rotatorTimer = null;
var KCA2009WINNERS = {"winners":[{"category":"Movie Actor",
"categoryAlias":"favorite-movie-actor",
"winner":"Will Smith",
"imageAlias":"will-smith-movies"},

{"category":"Male Singer",
"categoryAlias":"favorite-male-singer",
"winner":"Jesse McCartney",
"imageAlias":"jesse-mccartney-music"},

{"category":"TV Actor",
"categoryAlias":"favorite-tv-actor",
"winner":"Dylan Sprouse",
"imageAlias":"dylan-sprouse-biography"},

{"category":"Cartoon",
"categoryAlias":"favorite-cartoon",
"winner":"SpongeBob SquarePants",
"imageAlias":"spongebob-squarepants-cartoon"},

{"category":"Song",
"categoryAlias":"favorite-song",
"winner":"Single Ladies (Put a Ring On It) (Beyonce)",
"imageAlias":"beyonce-single-ladies"},

{"category":"Movie Actress",
"categoryAlias":"favorite-movie-actress",
"winner":"Vanessa Hudgens",
"imageAlias":"vanessa-hudgens-movies"},

{"category":"Reality Show",
"categoryAlias":"favorite-reality-show",
"winner":"American Idol",
"imageAlias":"american-idol-tv-show"},

{"category":"Female Singer",
"categoryAlias":"favorite-female-singer",
"winner":"Miley Cyrus",
"imageAlias":"miley-cyrus-music"},

{"category":"Male Athlete",
"categoryAlias":"favorite-male-athlete",
"winner":"Peyton Manning",
"imageAlias":"peyton-manning-bio"},

{"category":"Female Athlete",
"categoryAlias":"favorite-female-athlete",
"winner":"Candace Parker",
"imageAlias":"candace-parker-bio"},

{"category":"TV Actress",
"categoryAlias":"favorite-tv-actress",
"winner":"Selena Gomez",
"imageAlias":"selena-gomez-biography"},

{"category":"Video Game",
"categoryAlias":"favorite-video-game",
"winner":"Guitar Hero World Tour",
"imageAlias":"guitar-hero-world-tour-game"},

{"category":"Book",
"categoryAlias":"favorite-book",
"winner":"Twilight Series",
"imageAlias":"twilight-books"},

{"category":"Animated Movie",
"categoryAlias":"favorite-animated-movie",
"winner":"Madagascar: Escape 2 Africa",
"imageAlias":"madagascar-2-movie"},

{"category":"Music Group",
"categoryAlias":"favorite-music-group",
"winner":"The Jonas Brothers",
"imageAlias":"the-jonas-brothers-music"},

{"category":"Movie",
"categoryAlias":"favorite-movie",
"winner":"High School Musical 3: Senior Year",
"imageAlias":"high-school-musical-3-movie"},

{"category":"Voice from an Animated Movie",
"categoryAlias":"favorite-voice-animated-movie",
"winner":"Jack Black (Po, Kung Fu Panda)",
"imageAlias":"jack-black-movies"},

{"category":"TV Show",
"categoryAlias":"favorite-tv-show",
"winner":"iCarly",
"imageAlias":"icarly-tv-show"}
]};


$(document).ready(function(){ 
  $("#mainmenu>li").hover(
    function() {
      $(this).addClass("hover"); 
    },
    function() {
      $(this).removeClass("hover");
    }
  );
  $("#mainmenu").find( ("li."+kcaTimeZone()) ).show();
  
  KCA_setupAinits();
  
});

// override from /common/javascript/window/timezoneDetection.js
timezone = function() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	return std_time_offset;
}

// override from /common/javascript/window/timezoneDetection.js
kcaTimeZone = function() {
	if (timezone() == -5)
		return "east";
	else return "west";
}

function KCA_setupAinits(){
  if ( $("ul#ainit").size() > 0 ) {
    $("#ainitnav li:first").addClass("selected");
  
    var elements = $("#ainit").children();
  
    if ( elements.size() > 1 ) {
      for( var i = 0; i < elements.size(); i++ ){
        $(elements[i]).css("z-index", elements.length - 1);
      }
    
      runRotator();
      $(elements[0]).show();
      
      $("#ainitnav li").hover(
        function(){
          if ( !$(this).hasClass("selected") ) {
            $(this).addClass("hover");
            $("#ainitmsg").html( $(this).html() );
          }
        },
        function() {
          $(this).removeClass("hover");
          $("#ainitmsg").html("");
        }
      ).click(
        function() {
          if ( !$(this).hasClass("selected") ) {
            moveRotator( $("#ainitnav li").index( $(this) ) );
            $("#ainitmsg").html("");
          }
        }
      );
    } else {
      $("#ainitnav").css("display", "none");
      $("#ainit li").fadeIn(1);
    }
  }
}

function runRotator() {
  rotatorTimer = setTimeout(
    function() {
      $("#ainitnav li").eq(rotatorLast).removeClass("selected");
      $("#ainitnav li").eq(rotatorCurrent).addClass("selected");

      var elements = $("#ainit li");

      $(elements[rotatorLast]).fadeOut(750);
      $(elements[rotatorCurrent]).fadeIn(750);

      if ( (rotatorCurrent + 1) < elements.length ) {
        rotatorCurrent = rotatorCurrent + 1;
        rotatorLast    = rotatorCurrent - 1;
      } else {
        rotatorCurrent = 0;
        rotatorLast    = elements.length - 1;
      }

      runRotator();
    }, 4000
  );
}

function moveRotator(index) {
  if ( rotatorTimer != null ) {
    clearTimeout(rotatorTimer);
  }

  $("#ainitnav li").eq(rotatorLast).removeClass("selected");
  $("#ainitnav li").eq(index).addClass("selected");

  $("#ainit li").eq(rotatorLast).fadeOut(750);
  $("#ainit li").eq(index).fadeIn(750);

  rotatorLast = index;
}