$(document).ready(function(){
	var url = window.location.protocol + '//' + window.location.hostname + '/user/updateAccountDetails';
	var element = $('.p4fnav-block .email');
	element.css('cursor', 'pointer');
	element.click(function(event) {
		var url = window.location.protocol + '//' + window.location.hostname + '/user/updateAccountDetails';
		window.location.href = url;
		event.stopPropagation();
	});
});

$(document).ready(function() {
	// Start a new game
    $('#game .new-game').click(function() {
        
        // Hide Recommended Server icon
        $('#game .recommended-games ul.server-list .server-icon').hide();
    
        // Fade out Icons
        $('#game ul.server-list .server-icon:not(:animated)').fadeOut('fast', function(){
            
        });
        
        // Hide My games
            $('#game .world-played:not(:animated)').slideUp('1000');
            // Show new games
            $('#game .world-new:not(:animated)').slideDown('fast', function(){
                // Show Recommended Server icon
                $('#game .recommended-games ul.server-list .server-icon').hide();
                $('#game .recommended-games ul.server-list .server-icon').fadeIn();
            });
        
        return false;
    });
    
    // Go back to My games
    $('#game .old-game').click(function(){                
        //$('#game .world-new').fadeOut('50000');
        $('#game ul.server-list .server-icon').fadeOut();
        $('#game .world-played').slideDown('slow');
        
        $('#game div.recommended-games ul li.new').stop(true, true);
        
        $('#game .world-new').slideUp('slow', function() {
            
            //$('#game ul.server-list .server-icon').show();
            
            $('#game ul.server-list .server-icon').fadeIn();
                        
            $('#game div.recommended-games ul li.new').hide();
            $('#game div.recommended-games a.more-worlds').show();
            $('#game a.why-recommended').show();
            $('#game div.info-box div.info-text').hide();
        });
        
        
        return false;
    });
    
    // Show me more worlds
    $('#game .more-worlds').click(function(){
        
        $('#game div.recommended-games ul.server-list .server-icon').show();

        $('#game div.recommended-games a.more-worlds').fadeOut('fast', function() {
            // Check if browser supports opacity (also known as: check that user isn't using IE)
            if (jQuery.support.opacity == true) {
                fadeAnother();
            } else {
                $('div.recommended-games .recommended-game-list li').hide();
                $('div.recommended-games .recommended-game-list li').fadeIn('slow');
            }
        });
        //$('#game .world-new').slideUp();
        return false;
    });
    
    // Why is this server recommended?
    $('#game a.why-recommended')
        .mouseenter(function() {
            $('#game div.info-box div.info-text').fadeIn('1000');
            return false;
        })
        .mouseleave(function() {   // out
            $('#game div.info-box div.info-text').fadeOut('1000');
            return false;
        })
        .click(function(e) {
            e.preventDefault();
        });
    
	
		$(function () {
	        $('.menu_bubble').each(function () {
	            var distance = 10;
	            var time = 250;
	            var hideDelay = 50;
	
	            var hideDelayTimer = null;
	
	            var beingShown = false;
	            var shown = false;
	            var trigger = $('.info_trigger', this);
	            var info = $('.menu_sheet', this).css('opacity', 0);
				
	
	
	            $([trigger.get(0), info.get(0)]).mouseenter(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                if (beingShown || shown) {
	                    // don't trigger the animation again
	                    return;
	                } else {
	                    // reset position of info box
	                    beingShown = true;
	
	                    info.css({
	                        top: -40,
	                        left: 2,
	                        display: 'block'
	                    }).animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 1
	                    }, time, 'swing', function() {
	                        beingShown = false;
	                        shown = true;
	                    });
	                }
	
	                return false;
	            }).mouseleave(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                hideDelayTimer = setTimeout(function () {
	                    hideDelayTimer = null;
	                    info.animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 0
	                    }, time, 'swing', function () {
	                        shown = false;
	                        info.css('display', 'none');
	                    });
	
	                }, hideDelay);
	
	                return false;
	            });
	        });
	    });	
    
	
	
	
	// Check if browser supports opacity (also known as: check that user isn't using IE)
    if (jQuery.support.opacity == true) {
		
        $('#game ul.server-list li').mouseenter(function() {
            if ($(this).find('.lou-button_grey').length == 0) {
                $(this).find('a.lou-button span').css('color', '#F0E8D6');
				
                $(this).find('div').animate(
                    {
                        
                    }, 
                    {
                        queue: false, 
                        duration: 250,
                        easing: 'swing'
                    }
                );
            }
        });
        
        $('#game ul.server-list li').mouseleave(function() {
            $(this).find('a.lou-button span').css('color', '');

            $(this).find('div').animate(
                {
                    
                }, 
                {
                    queue: false, 
                    duration: 500,
                    easing: 'swing'
                }
            );
        });
    }
    
    // Join existing game button
    $(".my-server, .continuegame").click(function(e) {
        e.preventDefault();
        
        $(this).parent("form").submit();
    });
    
    // Join server button
    $("#startgame").click(function(e) {
        e.preventDefault();
        
        selected_server = $("#servers select option:selected");
        
        if ($(selected_server).hasClass("high")) {
            alert("This server is unavailable!");
        }
        else {
        	$("#sessionId").val(selected_server.attr("id"));         	
            selected_server_action = $(selected_server).val();
            $("#servers").attr("action", selected_server_action).submit();
        }        
    });
	
     // Join new server button
    $("#startgame_new").click(function(e) {
        e.preventDefault();
        
        selected_server = $("#servers_new select option:selected");
        
        if ($(selected_server).hasClass("high")) {
            alert("This server is unavailable!");
        }
        else {
        	$("#sessionId_new").val(selected_server.attr("id"));         	
            selected_server_action = $(selected_server).val();
            $("#servers_new").attr("action", selected_server_action).submit();
        }        
    });
	
	$(".msg_game_panel_close").click(function() {
         $(".msg_game_panel").fadeTo("slow", 0.01, function() {
             $(".msg_game_panel").slideUp("slow", function() {
                 $(".msg_game_panel").remove();
             });
         });
	});
});

function fadeAnother() {

    var firstItem = $('#game div.recommended-games ul li.new:hidden:first');
    
    if (firstItem.length > 0) {
        // Hide item
        $(firstItem).animate({scale: '0.1', opacity: '0'}, {queue: true, duration: 0});
        
        // Fade in item
        $(firstItem).show(200, fadeAnother).animate(
            {
                scale: '1.0', 
                opacity: '1.0'
            }, 
            {
                queue: false, 
                duration: 400,
                easing: 'swing'
            }
        );
    }
}(function ($) {
    // Monkey patch jQuery 1.3.1+ css() method to support CSS 'transform'
    // property uniformly across Webkit/Safari/Chrome and Firefox 3.5.
    // 2009 Zachary Johnson www.zachstronaut.com
    function getTransformProperty(element)
    {
        // Try transform first for forward compatibility
        var properties = ['transform', 'WebkitTransform', 'MozTransform'];
        var p;
        while (p = properties.shift())
        {
            if (typeof element.style[p] != 'undefined')
            {
                return p;
            }
        }
        
        // Default to transform also
        return 'transform';
    }
    
    var proxied = $.fn.css;
    $.fn.css = function (arg)
    {
        // Find the correct browser specific property and setup the mapping using
        // $.props which is used internally by jQuery.attr() when setting CSS
        // properties via either the css(name, value) or css(properties) method.
        // The problem with doing this once outside of css() method is that you
        // need a DOM node to find the right CSS property, and there is some risk
        // that somebody would call the css() method before body has loaded or any
        // DOM-is-ready events have fired.
        if
        (
            typeof $.props['transform'] == 'undefined'
            &&
            (
                arg == 'transform'
                ||
                (
                    typeof arg == 'object'
                    && typeof arg['transform'] != 'undefined'
                )
            )
        )
        {
            $.props['transform'] = getTransformProperty(this.get(0));
        }
        
        // We force the property mapping here because jQuery.attr() does
        // property mapping with jQuery.props when setting a CSS property,
        // but curCSS() does *not* do property mapping when *getting* a
        // CSS property.  (It probably should since it manually does it
        // for 'float' now anyway... but that'd require more testing.)
        if (arg == 'transform')
        {
            arg = $.props['transform'];
        }
        
        return proxied.apply(this, arguments);
    };
})(jQuery);
(function ($) {
    // Monkey patch jQuery 1.3.1+ to add support for setting or animating CSS
    // scale and rotation independently.
    // 2009 Zachary Johnson www.zachstronaut.com
    var rotateUnits = 'deg';
    
    $.fn.rotate = function (val)
    {
        var style = $(this).css('transform') || 'none';
        
        if (typeof val == 'undefined')
        {
            if (style)
            {
                var m = style.match(/rotate\(([^)]+)\)/);
                if (m && m[1])
                {
                    return m[1];
                }
            }
            
            return 0;
        }
        
        var m = val.toString().match(/^(-?\d+(\.\d+)?)(.+)?$/);
        if (m)
        {
            if (m[3])
            {
                rotateUnits = m[3];
            }
            
            $(this).css(
                'transform',
                style.replace(/none|rotate\([^)]*\)/, '') + 'rotate(' + m[1] + rotateUnits + ')'
            );
        }
    }
    
    // Note that scale is unitless.
    $.fn.scale = function (val, duration, options)
    {
        var style = $(this).css('transform');
        
        if (typeof val == 'undefined')
        {
            if (style)
            {
                var m = style.match(/scale\(([^)]+)\)/);
                if (m && m[1])
                {
                    return m[1];
                }
            }
            
            return 1;
        }
        
        $(this).css(
            'transform',
            style.replace(/none|scale\([^)]*\)/, '') + 'scale(' + val + ')'
        );
    }

    // fx.cur() must be monkey patched because otherwise it would always
    // return 0 for current rotate and scale values
    var curProxied = $.fx.prototype.cur;
    $.fx.prototype.cur = function ()
    {
        if (this.prop == 'rotate')
        {
            return parseFloat($(this.elem).rotate());
        }
        else if (this.prop == 'scale')
        {
            return parseFloat($(this.elem).scale());
        }
        
        return curProxied.apply(this, arguments);
    }
    
    $.fx.step.rotate = function (fx)
    {
        $(fx.elem).rotate(fx.now + rotateUnits);
    }
    
    $.fx.step.scale = function (fx)
    {
        $(fx.elem).scale(fx.now);
    }
    
    /*
    
    Starting on line 3905 of jquery-1.3.2.js we have this code:
    
    // We need to compute starting value
    if ( unit != "px" ) {
        self.style[ name ] = (end || 1) + unit;
        start = ((end || 1) / e.cur(true)) * start;
        self.style[ name ] = start + unit;
    }
    
    This creates a problem where we cannot give units to our custom animation
    because if we do then this code will execute and because self.style[name]
    does not exist where name is our custom animation's name then e.cur(true)
    will likely return zero and create a divide by zero bug which will set
    start to NaN.
    
    The following monkey patch for animate() gets around this by storing the
    units used in the rotation definition and then stripping the units off.
    
    */
    
    var animateProxied = $.fn.animate;
    $.fn.animate = function (prop)
    {
        if (typeof prop['rotate'] != 'undefined')
        {
            var m = prop['rotate'].toString().match(/^(([+-]=)?(-?\d+(\.\d+)?))(.+)?$/);
            if (m && m[5])
            {
                rotateUnits = m[5];
            }
            
            prop['rotate'] = m[1];
        }
        
        return animateProxied.apply(this, arguments);
    }
})(jQuery);

