/*////////////////////////////////////////////////////////////////////////////
// Author:              Nzime, John Knowles, Darren Horner-Glister
// Client:              Rare Ltd
// Description:         A box of magic tricks for transition effects, loading
//                      flash content, gallery functionality.
// Version:             1.0
// Last Updated:        29/07/2010
// Required Includes:   jquery.min.js, jquery.easing.js, jquery.cycle.min.js,
//                      jquery.swfobject.min.js, jquery.ui.min.js, jquery.jcarousel.min.js
*/////////////////////////////////////////////////////////////////////////////
$(document).ready(function()
{
    var base = '/';
    
    //make sure these bits happen quickly
    $("#game-nav-tab").html('');
    $(".noLabels label").hide();
    var homeFlashImage = $("#homeFlashImage").html();
    var homeFlashVideo = $("#homeFlashVideo").html();   
    
    //flash vars switch to load colour game nav
    if ($("#green").length!=0)
        {
            var color = 'green';    
        }
    if ($("#orange").length!=0)
        {
            var color = 'orange';   
        }
    if ($("#grey").length!=0)
        {
            var color = 'grey'; 
        }
    if ($("#pink").length!=0)
        {
            var color = 'pink'; 
        }
    if ($("#purple").length!=0)
        {
            var color = 'purple';   
        }

    $(".accordion").accordion({
        active: false,
        autoHeight: false,
        collapsible: true
    });
        
    //register ajax
    $('#regSubmit').click(function(){
        var name = $('#regFirstName').attr('value');
        var email = $('#regEmail').attr('value');
        
        
        $('#regFirstName').removeClass('errors');
        $('#regEmail').removeClass('errors');
        
        if (name == "First Name"){
            $("#regFirstName").addClass('errors');
            var eError = 1;
        }
        
        if (email == "Email Address"){
            $("#regEmail").addClass('errors');
            var eError = 1;
        }
        
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
        if(reg.test(email) == false) {
            $("#regEmail").addClass('errors');
            var eError = 1;
        }

        
        if (eError == 1){
            return false;   
        }
        
        var dataString = 'firstName='+ name + '&email=' + email; 
        $.ajax({  
            type: "GET",  
            url: "/ajax/register",  
            data: dataString,  
            success: function() {
                $('#poppupRegister form').hide();
                $('#poppupRegister').addClass('success');
            }
        });  
        return false;
    });
        
    //expander
    $('.read-more').toggle(function(){
        var rel = $(this).attr('rel');
        $('#'+rel+' .hidden').fadeIn(500);
        if ($.browser.msie<8) {
            this.style.removeAttribute('filter');
            $('.bottom').css('bottom', 0);
        }
        $(this).html('Show Less');
    },function(){
        var rel = $(this).attr('rel');
        $('#'+rel+' .hidden').slideUp(500);
        if ($.browser.msie<8) {
            $('.bottom').css('bottom', 0);
        }
        $(this).html('Show More');
    });
    
    //home page intro flash
    $('#introFlash').flash({swf:base+'content/flash/home_intro_panel.swf',height:269,width:810,flashvars: {
                source_file: homeFlashVideo,image_file: homeFlashImage,videoIntroPath: base+'content/flash/video_intro3.png',default_resize_mode: 'fillScreen',default_thumb_height: '80',prevent_xml_cache: 'false',auto_start: 'false'
            },params:{wmode:'transparent',scale: 'noScale',allowFullScreen: 'true'}});
    
    //remove the dodgy browser dotted line box from links
    $('a', this).click(function(){
        $(this).blur();
    });
    
    //load the game navigation
    //embed script and vars, params, attributes
    if(swfobject.hasFlashPlayerVersion("8.0.0")){
    
        $(".navGame").click(function(){
            if ($('#gameNavClose').is(':visible')){
                $('#game-nav-tab').html('');
                $('#introFlash').stop(true, true).show(1000);
                $("#gameNavClose").hide();
                return false;
            } else {
                var bgImgUrl = base+'templates/rare/swf/gamespanel'+color+'.png';
                img = new Image();
                img.src = bgImgUrl;
                $('#game-nav-tab').flash({swf: base+'templates/rare/swf/carousel.swf',height:175,width:785,flashvars: {
                    dataPath: base+'xml/games-navigation',carouselBackgroundPath: bgImgUrl},params:{wmode:'transparent'}});
                $('#introFlash').stop(true, true).hide();
                $("#gameNavClose").fadeIn(100);
                return false;
            }
        });
    }
    else
    {
        //Do nothing
    } 


    //toggle the register panel at the top of the page
    $("#registerToggle").toggle(function(){
        $("#poppupRegister").fadeIn(100);
        $("#register").addClass('active');
        return false;
    }, function(){
        $("#poppupRegister").fadeOut(100);
        $("#register").removeClass('active');
        return false;
    });
    
    /* Game Gallery JS */
    
    //game gallery navigation
    $('#gameGalleryNav').jcarousel({
        easing: 'easeInOutExpo',
        scroll: 1,
        auto: 0,
        animation: 500
    });
    
    //johns simple but magical game gallery function
    $('#gameGalleryNav a').click(function(){
        var href = $(this).attr('href');
        var largeDetail = $(this).attr('rel');
        var detail = $('#gameGalleryImage img');
        img = new Image();
        img.src = href;
        if (detail.attr('src')!=href)
            {
                $('#gameGalleryNav a').fadeTo(0, 1);
                $(this).fadeTo(0, 0.7);
                detail.stop(true, true).fadeOut(500);
                detail.attr('src', href);
                $('#gameGalleryImage a').attr('href', largeDetail);
                detail.stop(true, true).fadeIn(500);
            }
        return false;
    });
    
    $("#gameGalleryNav a").each(function() {
        img = new Image();
        img.src = $(this).attr('href');
    });

    
    /* Game Gallery JS End */
    
    //home page sliding panels carousel
    $('#slide').jcarousel({
        easing: 'easeInOutExpo',
        scroll: 1,
        auto: 5,
        animation: 500,
        wrap: 'circular'
    });
    
    $('.tweet').cycle({
        fx: 'fade',
        pause: 1,
        delay: 0,
        timeout: 6000
    });
    
  /* disabled carousel on company hdr
    $('#companyIntro').cycle({
        fx: 'fade',
        pause: 1,
        delay: 0,
        timeout: 6000
    }); */

    /* News zoom */
    $("a.pirobox").fancybox({
        'overlayShow'	: true,
        'transitionIn'	: 'elastic',
        'transitionOut'	: 'elastic'
    });
    
    $('.toggleHidden').toggle(function(){
        $(this).next('.hidden').slideDown(300);
        if ($.browser.msie<8) {
            this.style.removeAttribute('filter');
            $('.bottom').css('bottom', 0);
        }
        return false;
    },
    function(){
        $(this).next('.hidden').slideUp(300);
        //Because IE7 has more bugs than the amazon rainforest apply some hax
        if ($.browser.msie<8) {
            $('.bottom').css('bottom', 0);
        }
        return false;
    });

    
    //external link
    $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
    
    //form input handeling hidden labled    
    $(".noLabels input[type='text']").focus(function() {
        if ($(this).attr("value") == $(this).attr("rel")){
            $(this).attr("value", "");
        }
    });
    
    $(".noLabels input[type='text']").blur(function() {
        if ($(this).attr("value") == "")
            {
                var val = $(this).attr("rel");
                $(this).attr("value", val);
            }
    });
    
    $(".noLabels input[type='text']").load(function() {
        if ($(this).attr("value") == "")
            {
                var val = $(this).attr("rel");
                $(this).attr("value", val);
            }
    });
    
    $(".noLabels textarea").focus(function() {
        $(this).text("");
    });
    
    //password field handeling hidden lables
    $(".password input[type='password']").hide();
    $(".password input[type='text']").show();
    
    $(".password input[type='text']").focus(function(){
        $(this).hide();
        $(this).next("input[type='password']").show().focus();
    });
    
    $(".password input[type='password']").blur(function(){
        if ($(this).attr("value") == "")
            {
                $(this).hide();
                var pass = $(this).prev("input[type='text']").show();
                var value = pass.attr("rel");
                pass.attr("value", value)
            }
    });
    
});
