$(document).ready(function(){
 doBinds();
 activateInterval();
 $.historyInit(pageload);
 
});


	function pageload(hash) {
		if(hash) {
			if(location.href.indexOf("post") == -1) ajaxfx(hash);
		} else {
		    if($("#videoCenter").length == 0 && location.href.indexOf("?post") == -1) ajaxfx('index.php');
		}
	}
	
	

function doBinds() {
      $('#hover IMG:first').addClass('top').css({opacity:1.0});
      $("#hover").hover(function() {
	var $active = $('#hover IMG:first'); 
        $active.css({}).stop().animate({opacity:0.0},1000);
      }, function() {
	var $active = $('#hover IMG:first');
        $active.css({}).stop().animate({opacity:1.0},1000);
      });
	  

 $("a").unbind().click(function(){
    if(location.href.indexOf("?post") == -1) {
		var hash = this.href;
		hash = (hash +"").replace(/^.*\//, '').replace('#','');	
		$.historyLoad(hash);
	} else {
		location.href = this.href;
	}
  return false;
 });

}

function slideSwitch() {
    var $active = $('#contentRight IMG.active');

    if ( $active.length == 0 ) $active = $('#contentRight IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#contentRight IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
			$active.css({opacity: 0.0});
        });
}

var interval;

function activateInterval() {
   deactivateInterval();
   interval = setInterval( "slideSwitch()", 4000 );
}

function deactivateInterval() {
   window.clearInterval(interval);
}

function ajaxfx(url){
	deactivateInterval();

   $("#content").fadeOut('fast', function(){
      $(this).load(url + '?bodywrap', function(){		  
		  doBinds();
         $(this).fadeIn('fast', function(){		
			activateInterval();
         });
      });
   });
}; 
