$(document).ready(function() {
   	if($('flashcontent')){
    	swfobject.registerObject("architecture", "9.0.0"); 
    }
    externalLinks();
	try{Typekit.load();}catch(e){}
	
	if(jQuery('#game').children().size()>1){
		$('#game').addClass('jcarousel-skin-key');
		var num = ($('#game').children().size());
		$('#game').jcarousel({
		scroll: 1,
        initCallback: gamecarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
		});
		$('#gcontrols').find(':nth-child(1)').addClass('active');
	}
	
	$(featuredInit());
		
	$('ul#featured-controls li a').click(function(e){
	   e.preventDefault();
	   $(this).parent().siblings().removeClass('active');
	   $(this).parent().addClass('active');
	   var fi = $('ul#featured-controls li a').index(this);
	   var fw = $('div#featured ul.slides li').width();
	   var fp = -fi * fw;
	   $('div#featured ul.slides').animate({left: fp}, 250);
	   clearTimeout(ftime);
	   ftime = setTimeout('featuredSlide('+ fi +')', 20000);
	});
	
	$('div#featured ul.slides li').hover(function(){
	   clearTimeout(ftime);
	}, function(){
	   var fi = $('div#featured ul.slides li').index(this);
	   featuredSlide(fi);
	});
	
	mi = 0;
	
	$('a.arrow-left').click(function(e){
	   e.preventDefault();
	   var mw = $('div#mask ul li').width() + 20;
	   if ( mi > 0 ) { mi--; }
	   else { mi = $('div#mask ul li').size() - 1; }
	   var mp = -mw * mi;
	   $('div#mask ul').animate({left: mp}, 250);
	});
	
	$('a.arrow-right').click(function(e){
	   e.preventDefault();
	   var mw = $('div#mask ul li').width() + 20;
	   if ( mi < 2 ) { mi++; }
	   else { mi = 0; }
	   var mp = -mw * mi;
	   $('div#mask ul').animate({left: mp}, 250);
	});
	
	// External links.
    $('a[rel*=external]').click(function(){
        window.open(this.href);
        return false;
    });

});

function featuredInit(){
    ftime = 0;
    featuredSlide(0);
}

function featuredSlide(fn){
    var fw = $('div#featured ul.slides li').width();
    var fp = -fn * fw;
    $('ul#featured-controls li').removeClass('active');
    $('ul#featured-controls li:eq('+ fn +')').addClass('active');
    $('div#featured ul.slides').animate({left: fp}, 250);
    if ( fn < 2 ) { fn++; }
    else { fn = 0; }
    ftime = setTimeout('featuredSlide('+ fn +')', 5000);
}

function gamecarousel_initCallback(carousel) {
  $('.game-control').each(function(i){
  	$(this).find('a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(i+1));
		changeThumbs($(this).parent());
		$(this).attr("href", "#");
		return false;
    });
   //jQuery('.jcarousel-scroll select').bind('change', function() {
    //    carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
    //    return false;
    //});
	 });
};

function changeThumbs(thumb){
	$('.game-control').each(function(i){
			$(this).removeClass('active');							   
	});
	thumb.addClass('active');
}


function externalLinks() {  
 if (!document.getElementsByTagName) return;  
 var anchors = document.getElementsByTagName("a");  
 for (var i=0; i<anchors.length; i++) {  
   var anchor = anchors[i];  
   if (anchor.getAttribute("href") &&  
       anchor.getAttribute("rel") == "external")  
     anchor.target = "_blank";  
 }  
}
