$(document).ready(function() {
  //-- Fix the scroll bars
  $('html').css('overflow-y', 'scroll');			

  function applyLinks() {
	  $("#logo").click(function(){
      hasMoved = false;
		  $('#content').hide();
      $('#navigation li').stop().fadeTo("slow", 1.00);
      $('#navigation .sub-nav div')
        .html('');
      $('#navigation .sub-nav')
        .stop()
        .animate({height: 95});
      if($.browser.mozilla)
        $('#home').stop().animate({backgroundPosition:"(50% -90px)"},{duration: 500});
      else
        $('#home').stop().animate({backgroundPositionY:"-90"},{duration: 500});
      $('#main').stop().animate( {top: 0}, {duration: (500)});
	  });

	  $(".pagelink").click(function(){
		  $('#homecontent').hide();
		  $('.content-section').hide();
		  
		  slideBackgroundAndContent($(this), true);
      $(this).parent('li').siblings('li[class!=nav-date]').stop().fadeTo("slow", 1.00);
      $(this).parent('li').stop().fadeTo("slow", 0.35);
		  
		  // Force the browser to stay at the top of the content
		  // after the animation
		  $(window).scroll(function(){ return false; });	
      return false;
	  });
  }
  applyLinks();
	
	$(".newslink").click(function(){
		$('#homecontent').hide();
		$('.content-section').hide();
		$('.news-item').hide();
		
		slideBackgroundAndContent($('#news'), false);
		
		$($(this).attr('href')).stop().fadeIn();
	});
	
	var hasMoved = false;
	
  function slideBackgroundAndContent(target) {
      target = $(target).attr('href');
    // Slide the background upward. Mozilla is picky about its backgroundPosition
    // property. Safari and IE are not.
    if($.browser.mozilla)
      $('#home').stop().animate({backgroundPosition:"(50% -345px)"},{duration: 500});
    else
      $('#home').stop().animate({backgroundPositionY:"-260px"},{duration: 500});
    
    // Slide the main content upward
    // NOTE: If the content has already shifted upward,
    // 		 shorten the animation time

    //-- For BuenoThin
    $('#navigation .sub-nav').stop().animate({height: 174});
    //-- For BuenoBold
    //$('#navigation .sub-nav').stop().animate({height: 155});

    $(".content").css("top", 0);
    $('#main').css({position: 'relative'});
    $('#main').stop().animate(
      {top: -250},
      {duration: (hasMoved?5:500), complete: function(){
        // Fix for nasty Safari bug which hides non-block content...
        if($.browser.safari) {
          $('#navigation .sub-nav .other-pages').stop().fadeOut('fast', function () {
              $(this).html('');
              $('#navigation .sub-nav .current-page').stop().fadeOut('fast', function () {
                  $("#content").load(target, '', function() { onAjaxLoad(); } );
              });
          });
          //$(target).show();
        } else {
          $('#navigation .sub-nav .other-pages').stop().fadeOut('fast', function () {
              $(this).html('');
              $('#navigation .sub-nav .current-page').stop().fadeOut('slow', function () {
              //$(target).stop().fadeIn();
              $("#content").stop().fadeOut().hide().load(target, '', function() { onAjaxLoad() });
              });
          });
        }
        try {
          pageTracker._trackPageview("/"+target);
        } catch(e) {
          //nothing here
        }
      }}
    );

    hasMoved = true;
  }

  //---------------------------
  //-- Start Scroll Functions -
  //---------------------------
  var scrollTimerId;
  var scrollSpd = 10; // Smaller = Faster
  var scrollJmp = 2;  // Bigger = Bigger jump
  var scrollbarHeight = 120; // Set default
  var scrollAltFunction = false;

  function scroll_dn() {
      var h=$("#content .content").height();
      if ( h == 0 ) { 
          h=$('#content .subpagediv:visible').height(); 
          scrollAltFunction = false; 
      } else { 
          scrollAltFunction = true; 
      }
      var topVal = $("#content .scrollcontainer .content").css("top").replace(/px/,'')*1-scrollJmp;

      if( ($.browser.msie && $.browser.version < 8.0) || scrollAltFunction == true ){
        if ( h==0 || (parseInt(h)+parseInt(topVal)+parseInt(scrollbarHeight)) > $("#content .scrollcontainer").height()) {
            $("#content .content").css("top", topVal);
            scrollTimerId=setTimeout(function() { scroll_dn() }, scrollSpd);
        }
      } else {
        if ( h==0 || (parseInt(h)+parseInt(topVal)+parseInt(scrollbarHeight)) > 0 ) {
            $("#content .content").css("top", topVal);
            scrollTimerId=setTimeout(function() { scroll_dn() }, scrollSpd);
        }
      }
      return false;
  }
  function scroll_up() {
      var topVal = $("#content .scrollcontainer .content").css("top").replace(/px/,'')*1+scrollJmp;
      if ( topVal <= 0 ) {
          $("#content .content").css("top", topVal);
      }
      scrollTimerId=setTimeout(function() { scroll_up() }, scrollSpd);
      return false;
  }
  function scroll_apply() {
      //-- Add Scroll Box buttons and apply their functions
      if ( null != $('#content .scrollcontainer').attr('class') ) {
          var h=$("#content .content").height();
          if ( h == 0 ) { h=$('#content .content-section').height(); }
          var topVal = $("#content .scrollcontainer .content").css("top").replace(/px/,'')*1-scrollJmp;

          if ( h==0 || h+topVal+scrollbarHeight > $("#content .scrollcontainer").height()) {
              $(".scrollcontainer").prepend(
                  '<div class="scrollbuttons" style="display: none;">' +
                  '<a href="#" class="dn"></a>' +
                  '<a href="#" class="up"></a>' +
                  '</div>'
              );
              $(".scrollcontainer .scrollbuttons").stop().show();
              $("a.dn")
                  .click( function() { return false; } )
                  .bind("mouseover", function(e){ scroll_dn(); return false; })
                  .bind("mouseout", function(e){ clearTimeout( scrollTimerId ); return false; });
              $("a.up")
                  .click( function() { return false; } )
                  .bind("mouseover", function(e){ scroll_up(); return false; })
                  .bind("mouseout", function(e){ clearTimeout( scrollTimerId ); return false; });
          } else {
              if ( null != $('.scrollbuttons') ) {
                  $('.scrollbuttons').fadeOut('fast', function() {
                      $(".scrollbuttons").remove();
                  });
              }
          }
      }
  }
  //--------------------------
  //-- End Scroll Functions -
  //--------------------------

  function onAjaxLoad() {
      //-- Apply Players
      applyAudioPlayer();
      applyLinks();

      //-- Setup Thickbox
      tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
      imgLoader = new Image();// preload image
      imgLoader.src = tb_pathToImage;

      //-- News Setter
      $('a.news_link').click(function(e) {
          $("#news .scrollcontainer .content").css("top", 0);
          $('#news .scrollcontainer .content').stop().fadeOut('fast', function () {
              $("#news .scrollcontainer .content").load(e.target.href, function () {
                  scroll_apply();
                  $("#news .scrollcontainer .content").stop().fadeIn("slow");
	                try {
                    pageTracker._trackPageview(e.target.href);
   	              } catch(e) {
   		              //nothing here
	                }
              });
          });
          return false;
      });

      //-- Set the mouse overs for the Commercial Thumbnails
	    $('.thumbframe a').css('visibility', 'hidden');			
	    $('.thumbnail').hover(function() {
		    $(this).find('img').css('opacity',.8);
		    $(this).find('a').css('visibility', 'visible');
	    }, function () {
		    $(this).find('img').css('opacity',1);
		    $(this).find('a').css('visibility', 'hidden');
	    });

      //-- Set the links up top
      var title = "";
      title = title + $('#content .content-section').attr('id');
      title = title.toLowerCase().replace(/ /, '_');
      $('#navigation .sub-nav .current-page').html('<div id="current_page_'+ title +'"></div>');
      $('#navigation .sub-nav .current-page').stop().fadeIn('slow', function () {
          if(  $('#content .subnav').html() != null ) {
              $('#navigation .sub-nav .other-pages')
                  .html("<ul>"+ $('#content .subnav').html() +"</ul>")
              $('#navigation .sub-nav .other-pages')
                  .stop().fadeIn('slow');
              $('#navigation .sub-nav .other-pages li:first').stop().fadeTo("slow", 0.35);
              $('#navigation .sub-nav .other-pages li:first').siblings('li').stop().fadeTo("slow", 1.00);
              //-- Make the links do something!
              $('#navigation .sub-nav a').click(function() {
                  //-- Hide the current page and fade in the new one
                  $('.subpagediv').hide();
                  $('#sub_'+$(this).parent('li').attr('class')).stop().fadeIn();
        
                  //-- Make the current link faded, and show the others
                  $(this).parent('li').siblings('li').stop().fadeTo("slow", 1.00);
                  $(this).parent('li').stop().fadeTo("slow", 0.35);
                  $(".content").css("top", 0);
                  return false;
              });
          }
      });

      scroll_apply();
      if($.browser.safari) {
          $('#content').show();
      }
      $('#content').stop().fadeIn("slow");
  }

});
