var $j = jQuery.noConflict();
$j(document).ready(function(){
  
  // Season infographic
  
  $j("#games li a").css({ opacity: 0 }).tooltip({track: true, delay: 0, showURL: false, showBody: " - ", opacity: 0.85 });
  $j("#games li a").hover(function () {
    $j(this).stop().animate({ opacity: .5 })
  }, function () {
    $j(this).stop().animate({ opacity: 0 });
  });
  
  $j("#figure").prepend("<div id=\"exhibition-shadow\"></div>");
  $j("#figure").prepend("<div id=\"season-shadow\"></div>");
  $j("#figure").prepend("<div id=\"big12-shadow\"></div>");
  $j("#figure").prepend("<div id=\"ncaa-shadow\"></div>");
  $j("#figure").prepend("<div id=\"championship-shadow\"></div>");

  $j(".chart").append("<img id=\"exhibition-pointer\" class=\"pointer\" src=\"http://media.ljworld.com/img/specials/retrospect/img/pointer.png\" />");
  $j(".chart").append("<img id=\"season-pointer\" class=\"pointer\" src=\"http://media.ljworld.com/img/specials/retrospect/img/pointer.png\" />");
  $j(".chart").append("<img id=\"big12-pointer\" class=\"pointer\" src=\"http://media.ljworld.com/img/specials/retrospect/img/pointer.png\" />");
  $j(".chart").append("<img id=\"ncaa-pointer\" class=\"pointer\" src=\"http://media.ljworld.com/img/specials/retrospect/img/pointer.png\" />");
  $j(".chart").append("<img id=\"championship-pointer\" class=\"pointer\" src=\"http://media.ljworld.com/img/specials/retrospect/img/pointer.png\" />");
  $j(".pointer").css({ opacity: 0 });


  /* This won't work, but if someone out there knows how to genericize this function by manipulating the "this" keyword, I'll buy you a beer
  $j(".note").hover(function () {
    $j(this + "-shadow").fadeIn(500);
    $j(this + "-pointer").animate({ bottom: "-5px", opacity: 1 });
  }, function() {
    $j(this + "-shadow").fadeOut(500);
    $j(this + "-pointer").animate({ bottom: "-10px", opacity: 0 });    
  }); */
  
  $j("#exhibition").hover(function () {
    $j("#exhibition-shadow").fadeIn(500);
    $j("#exhibition-pointer").animate({ bottom: "-5px", opacity: 1 });
  }, function () {
    $j("#exhibition-shadow").fadeOut(500);
    $j("#exhibition-pointer").animate({ bottom: "-10px", opacity: 0 });
  });

  $j("#season").hover(function () {
    $j("#season-shadow").fadeIn(500);
    $j("#season-pointer").animate({ bottom: "-5px", opacity: 1 });
  }, function () {
    $j("#season-shadow").fadeOut(500);
    $j("#season-pointer").animate({ bottom: "-10px", opacity: 0 });
  });

  $j("#big12").hover(function () {
    $j("#big12-shadow").fadeIn(500);
    $j("#big12-pointer").animate({ bottom: "-5px", opacity: 1 });
  }, function () {
    $j("#big12-shadow").fadeOut(500);
    $j("#big12-pointer").animate({ bottom: "-10px", opacity: 0 });
  });

  $j("#ncaa").hover(function () {
    $j("#ncaa-shadow").fadeIn(500);
    $j("#ncaa-pointer").animate({ bottom: "-5px", opacity: 1 });
  }, function () {
    $j("#ncaa-shadow").fadeOut(500);
    $j("#ncaa-pointer").animate({ bottom: "-10px", opacity: 0 });
  });

  $j("#championship").hover(function () {
    $j("#championship-shadow").fadeIn(500);
    $j("#championship-pointer").animate({ bottom: "-5px", opacity: 1 });
  }, function () {
    $j("#championship-shadow").fadeOut(500);
    $j("#championship-pointer").animate({ bottom: "-10px", opacity: 0 });
  });
  
  $j(".accordion").accordion({
    header: '.team',
    selectedClass: 'open',
    autoHeight: false
  });

  $j(".team").hover(function() {
    $j(this).css({ cursor: "hand", textDecoration: "none" });
  }, function () {
    $j(this).css({ cursor: "pointer", textDecoration: "underline" });
  });
  
  // Video gallery links
  
  $j("#player").empty();
  $j("#gallery").append("<div id=\"instructions\"></div>").children("#instructions").css({ opacity: .67 });
  $j("#instructions").append("<p>Select a video above</p>").children().css({ opacity: 1 });
  
  function scroll(e, that) {
    var offset = $j("#youtube p").offset().top;
    $j("html,body").animate({ scrollTop: offset - 40 }, 500);
    e.preventDefault();
    $j("#youtube a").removeClass("active");
    $j(that).addClass("active");
  }

  $j("#big").click(function(e) {
    $j("#instructions").remove();
    $j("#player").empty().css({ position: "absolute", top: "0", left: "0", width: "960px", height: "575px" });
    $j("#player").flash( {
      src: 'http://media.ljworld.com/img/specials/retrospect/videos/leade.swf', width: 960, height: 575,
      quality: 'high', wmode: 'transparent',
      menu: 'false', loop: 'false',
      allowFullScreen: 'true',
      flashvars: { xmlfile: 'http://media.ljworld.com/img/specials/retrospect/videos/big12/index.xml', initialURL: 'escape(document.location)' }
    }, { version: '9.0.115' });
    scroll(e, this);
  });

  $j("#nc").click(function(e) {
    $j("#instructions").remove();
    $j("#player").empty().css({ position: "absolute", top: "0", left: "0", width: "960px", height: "575px" });
    $j("#player").flash( {
      src: 'http://media.ljworld.com/img/specials/retrospect/videos/leade.swf', width: 960, height: 575,
      quality: 'high', wmode: 'transparent',
      menu: 'false', loop: 'false',
      allowFullScreen: 'true',
      flashvars: { xmlfile: 'http://media.ljworld.com/img/specials/retrospect/videos/ncaa/index.xml', initialURL: 'escape(document.location)' }
    }, { version: '9.0.115' });
    scroll(e, this);
  });
  
  $j("#champions").click(function(e) {
    $j("#instructions").remove();
    $j("#player").empty().css({ position: "absolute", top: "0", left: "0", width: "960px", height: "575px" });
    $j("#player").flash( {
      src: 'http://media.ljworld.com/img/specials/retrospect/videos/leade.swf', width: 960, height: 575,
      quality: 'high', wmode: 'transparent',
      menu: 'false', loop: 'false',
      allowFullScreen: 'true',
      flashvars: { xmlfile: 'http://media.ljworld.com/img/specials/retrospect/videos/championship/index.xml', initialURL: 'escape(document.location)' }
    }, { version: '9.0.115' });
    scroll(e, this);
  });
  
  $j("#game").click(function(e) {
    $j("#instructions").remove();
    $j("#player").empty().toggleClass("size-original").css({ position: "absolute", top: "50px", left: "90px", width: "780px", height: "445px" }).prepend("<embed src=\"http://www.youtube.com/cp/vjVQa1PpcFM4JCArNQBX-wyEqSQAO40s5mGzppxPLqA=\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"780\" height=\"445\" />");
    var offset = $j("#youtube p").offset().top;
    $j("html,body").animate({ scrollTop: offset - 40 }, 500);
    e.preventDefault();
    $j("#youtube a").removeClass("active");
    $j(this).addClass("active");
  });
  
  $j("#celebration").click(function(e) {
    $j("#instructions").remove();
    $j("#player").empty().toggleClass("size-original").css({ position: "absolute", top: "50px", left: "90px", width: "780px", height: "445px" }).prepend("<embed src=\"http://www.youtube.com/cp/vjVQa1PpcFM4JCArNQBX-3j0DGnidFpkO5s4JZYxNzs=\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"780\" height=\"445\" />");
    var offset = $j("#youtube p").offset().top;
    $j("html,body").animate({ scrollTop: offset - 40 }, 500);
    e.preventDefault();
    $j("#youtube a").removeClass("active");
    $j(this).addClass("active");
  });
  
  // Wallpaper gallery rollover
  $j("#wallpaper").cycle({ speed: 1, timeout: 0, pager: '#nav', pagerEvent: 'mouseover' });
  $j("#nav a").attr("href", "http://www2.kusports.com/mens_basketball/ncaa_tournament/2008/wallpapers/");
  
});
