//  externalinterface functions for flash

function showChallengePreview(){
    tb_show(null,'/challenges/preview?width=955&height=585',false);
}

function addChallengeItemFromFlash(type,id){
    document.getElementById('InTheChallengeFlash').addChallengeItem(type,id);
    return false;
}

function showPS3InfoFromFlash(){

    var rand = Math.ceil(Math.random()*4);

    // preload bg png
    bgPNG = new Image();
    bgPNG.src = '/img/structure/ps3-popout-4.png';

    $('#Footer').after('<div id="TB_overlay" class="TB_overlayBG"></div>');
    $("body").append("<div id='TB_load'><img src='/img/loadingAnimation.gif' /></div>");
    $('#TB_load').show();

    $('#HeaderPS3Info').load('/the_game/ps3_popout/'+rand,function(){
        $('#TB_load').remove();
        $('#HeaderPS3Info').fadeIn('fast');
    });
}

// --

jQuery.fn.fadeToggle = function(speed, easing, callback) { 
    return this.animate({
        opacity: 'toggle'
    }, speed, easing, callback);
};

// homepage carousels
function popular_paging(carousel) {
    jQuery('.popular-paging a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        highlightCarousel(this, '.popular-paging a', 'current');
        return false;
    });
};

function featured_paging(carousel) {
    jQuery('.featured-paging a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        highlightCarousel(this, '.featured-paging a', 'current');
        return false;
    });
};

$(document).ready(function(){
	
    //IE7 Fix for Login Form
    $(function(){
        $('#UserLoginForm #UserPassword').keydown(function(e){
            if (e.keyCode == 13) {
                $(this).parents('form').submit();
                return false;
            }
        });
    });        
	
    // Latest Activity
    $("#Main .activity-listing ul").hide();
    $("#Main .activity-listing ul.list-1").show();
    
    var activityCount = $("#Main .activity-listing ul").children().length;
    if(activityCount < 25) {
        $('#Main .show-more').css("display","none");
    }
    
    var showMoreCounter = 2;
	
    $('#Main .show-more').click(function(){
        $("#Main .activity-listing ul.list-" + showMoreCounter + "").slideDown("slow");
        showMoreCounter++;
        if ($("#Main .activity-listing ul.list-" + showMoreCounter + "").length < 1) {
            $('#Main .show-more').css("display","none");
        }
        return false;
    });
    

	
    // External links open in new window
    $('A[rel="external"]').attr("target","_blank");
        	
    $('.tb_close').live('click',function(){
        tb_remove();
        return false;
    });

    $('.tb_close').live('click',function(){
        tb_remove();
        return false;
    });
    
    // send challenge menus   
    $('#Main h3.following').click(function(){
        $(this).next('ul').slideToggle("slow");
        $(this).find('a').toggleClass('active');
        $(this).attr({
            title: titleText
        });
        return false;
    });

    // faqs accordian  
    $('#FAQs h2').click(function(){
        $(this).next('.answer').slideToggle("slow");
        $(this).find('a').toggleClass('active');
        return false;
    });
	
	
    // find facebook popup links thickbox links
    // and append a <body> class to affect the thickbox styling
    var facebookPopupLinks = $("a.facebook-popup.thickbox").length;
    if(facebookPopupLinks > 0){
        $('body').addClass('facebook-popup');
    }

    CarouselArrowClick();

    // add to challenge links
    $('.add-link a, .add-this a').click(function(){
        document.getElementById('InTheChallengeFlash').addChallengeItem($(this).attr('class'),$(this).attr('id'));
        return false;
    });

    // add to challenge droppables
    $('body.create .question-listing li, body.create .minigame-listing li').draggable({
        revert:true,
        start:function(event, ui){
            $(this).find('a.thickbox').unbind(); // stop thickbox
            $(this).css('z-index','110');
        },
        stop:function(event,ui){
            tb_init($(this).find('a.thickbox')); // restore thickbox
            $(this).css('z-index','');
            $(this).css('visibility','visible');
        }
    });

    $("#InTheChallenge").droppable({
        drop: function(event, ui) {
            document.getElementById('InTheChallengeFlash').addChallengeItem(
                ui.draggable.find('.add-link a').attr('class'),
                ui.draggable.find('.add-link a').attr('id')
                );
            ui.draggable.css('visibility','hidden');
        }
    });
    
    // highlight entire facebook friend on click
    $('#FacebookFriends li').live('click',function(){
        var isChecked = $(this).find('input:checked').length;
        if(isChecked > 0){
            $(this).addClass('active');
        } else{
            $(this).removeClass('active');
        }
    });
    
    if( document.getElementById('InTheChallenge')){
        document.getElementById('InTheChallenge').offsetHeight;
    }    
    
    // my stats popup info box
    $("body.my-stats #StatsHeader ul li a").hover(function(){
        $(this).find('.popup-info').show();
    }, function(){
        $(this).find('.popup-info').hide();
    });
    
    // footer language select - launches custom thickbox
    $('#Footer ul li#LanguageSelect a:first').click(function(){
        $('#Footer').after('<div id="TB_overlay" class="TB_overlayBG language-select"></div>');
        $('#LanguageSelectOptions').show();

        $('.TB_overlayBG.language-select').click(function(){
            $('.TB_overlayBG.language-select, #LanguageSelectOptions').hide();
        });

        document.onkeyup = function(e){
            if (e == null) { // ie
                keycode = event.keyCode;
            } else { // mozilla
                keycode = e.which;
            }
            if(keycode == 27){ // close
                $('.TB_overlayBG.language-select, #LanguageSelectOptions').hide();
            }
        };

        return false;
    });
    
    // ps3 info
    $(".close-info").live('click',function(){
        $('#TB_overlay').remove();
        $('#HeaderPS3Info').hide();
        return false;
    });

    // clear cookie on name challenge thanks page, needs timeout for swfobject
    if($('body').hasClass('name-challenge-thankyou')){
        setTimeout("document.getElementById('InTheChallengeFlash').clearCookie()",1000);
    }

    $('a.back-to-search').click(function(){
        history.back();
        return false;
    });
    
});

function highlightCarousel(item, element, cssClass){
    $(element).removeClass(cssClass);
    $(item).addClass(cssClass);
}

function CarouselArrowClick(){

    // when the user clicks the next/previous arrow
    // (provided they're not disabled) find the
    // 'four dots' naviagtion and move the highlight class
    // onto the next/previous one
	
    if($('.jcarousel-next').length == 0 || $('.jcarousel-prev').length == 0){
        return;
    }
	
    var currentTab;

    $('.jcarousel-next').live('click', function(){
        if(!$(this).hasClass('jcarousel-next-disabled')){
            currentTab = $(this).parent().parent().parent().find('div.paging a.current').removeClass('current');
            $(currentTab).next().addClass('current');
        }
    });
	
	
    // previous click
    $('.jcarousel-prev').live('click', function(){
        if(!$(this).hasClass('jcarousel-prev-disabled')){
            currentTab = $(this).parent().parent().parent().find('div.paging a.current').removeClass('current');
            $(currentTab).prev().addClass('current');
        }
    });
}
