$(document).ready(function(){
    $("#signin-menu").hide();
    $("#global-header > .header-content").show();
    $("#main-content").show();
    $("#global-footer").show();
    $("#language-menu").hide();

    $("#busy-dialog").dialog({
        minHeight:75,
        resizable: false,
        modal: true,
        title: "VT Arena",
        open: function(){$(this).parent().children(':first').children('a').remove();},
        autoOpen: false
    });
    
    $("#warning-dialog").dialog({
        minHeight:75,
        resizable: false,
        modal: true,
        autoOpen: false,
        title: "VT Arena",
        open: function(){$(this).parent().children(':first').children('a').remove();},
        buttons: {
            "Ok": function() {$(this).dialog("close");}
        }
    });

    if ($.Q.show == "vtcoins"){
        $("#warning-dialog").dialog({
            buttons: {
                "Ok": function() {$(this).dialog("close");},
                "Cancel": function() {$(this).dialog("close"); history.back(); }
            }
        });
        
        $("#warning-dialog p").html(RSC_PAYMENT_GATEWAY_WARNING);
        $('#warning-dialog').dialog('open');
    }
    
    $('body').click(function() {
        $("#signin-menu").hide();
        $("#language-menu").hide();
        $(".signin").removeClass("menu-open");
     });

    $("#signin").click( function (event) {
        event.stopPropagation();
    });

    $("#login-box a").click( function (event) {
        event.stopPropagation();
        $("#signin-menu").toggle( 0, function () {
            if ($("#login-toggle").hasClass("menu-open")){
                $("#login-toggle").removeClass("menu-open");
            }else{
                $("#login-toggle").addClass("menu-open");
            }
        });
    });

    $("#language-box a").click( function (event) {
        event.stopPropagation();
        $("#language-menu").toggle( 0, function () {
            if ($("#language-toggle").hasClass("menu-open")){
                $("#language-toggle").removeClass("menu-open");
            }else{
                $("#language-toggle").addClass("menu-open");
            }
        });
    });

    var params = { allowScriptAccess: "always",  wmode: 'transparent' };
    var atts = { id: "myytplayer" }
    
    swfobject.embedSWF(
        "http://www.youtube.com/v/Rzir0XQqIoc&enablejsapi=1&playerapiid=ytplayer&loop=1&autoplay=1&disablekb=1&showsearch=0&hd=1",
        "ytplayer",
        "920",
        "680",
        "8",
        null,
        null,
        params,
        atts
    );
    
    $(".ui-accordion").accordion({
        autoheight: false,
        header: 'dt'
    });

    $("#game-scroller").scroller({
        intervalTimer:5000,
        visibles:3
    });

    $("#slideshow").slideShow({
        url: 'http://' + window.location.hostname + '/json/vtarena.json.php',
        method: "get_slideshow_data"
    });
    
    $("#featured").blockRoller("init");

    $("#last-topics-block").lastTopics({
        intervalTimer:0,
        visibles:3
    });

    $(".language-option").click(function(){
        var language = $(this).attr('name');
        $.ajax({
            url:'../json/user.json.php',
            dataType:'json',
            processData:true,
            data:{'function':'set_language', 'language':language},
            error:function(xhr, err){
                $("#warning-dialog p").html("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText);
                $('#warning-dialog').dialog('open');
            },
            complete: function(xhr){
                window.location.reload();
            }
        });
    });
    
    if ($.Q.show == "download"){
        $('body').css('background-image', 'url(/img/download_tol2.jpg)');
    }

    //sample();
});

$(window).load(function() {
    if ($.Q.show == "download"){
        if (!$.browser.msie)
            window.location.href = "vtatol2://launch";            
    }
});

jQuery.extend({
    'Q' : window.location.search.length <= 1 ? {} : function(a){
        var i = a.length,
        r = /%25/g,  // Ensure '%' is properly represented
        h = {};      // (Safari auto-encodes '%', Firefox 1.5 does not)
        while(i--) {
            var p = a[i].split('=');
            h[ p[0] ] = r.test( p[1] ) ? decodeURIComponent( p[1] ) : p[1];
        }

        return h;
    }(window.location.search.substr(1).split('&'))
});

function display(myimage, width, height) {
    var posX = (screen.width - width) / 2;
    var posY = (screen.height - height) / 2;
    if (posX < 0) posX = 0;
    if (posY < 0) posY = 0;

     var html = "<HTML><HEAD><TITLE>Imágen</TITLE>" +
      "</HEAD><BODY LEFTMARGIN=0 "
      + "MARGINWIDTH=0 TOPMARGIN=0 MARGINHEIGHT=0><CENTER>"
      + "<IMG SRC='" + myimage + "' BORDER=0 NAME=image "
      + "</CENTER>"
      + "</BODY></HTML>";
     popup = window.open('', 'image', 'width=' + width + ', height=' + height + ', top=' + posY +', left='+ posX + ', toolbar=0, location=0, directories=0, menuBar=0, scrollbars=0, resizable=1');
     popup.document.write(html);
     popup.document.focus();
 }
 
 function consoleLog(data){
    if(typeof (console) != 'undefined' && typeof (console.log) != 'undefined')
        console.log(data)
}
/*var container, stats;
var camera, scene, renderer, objects;
var particleLight, pointLight;
var dae, skin;
var loader;

function sample(){

    loader = new THREE.ColladaLoader();
    loader.load( 'js/models/monster.dae', function colladaReady( collada ) {
        dae = collada.scene;
        skin = collada.skins[ 0 ];

        dae.scale.x = dae.scale.y = dae.scale.z = 0.002;
        dae.rotation.x = -Math.PI/2;
        dae.updateMatrix();

        init();
        //animate();
    });
}

function init() {

    container = document.createElement( 'div' );
    document.body.appendChild( container );

    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
    camera.position.set( 2, 2, 3 );

    scene = new THREE.Scene();

    // Add the COLLADA

    scene.add( dae );

    particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
    scene.add( particleLight );

    // Lights

    scene.add( new THREE.AmbientLight( 0xcccccc ) );

    var directionalLight = new THREE.DirectionalLight(0xeeeeee );
    directionalLight.position.x = Math.random() - 0.5;
    directionalLight.position.y = Math.random() - 0.5;
    directionalLight.position.z = Math.random() - 0.5;
    directionalLight.position.normalize();
    scene.add( directionalLight );

    pointLight = new THREE.PointLight( 0xffffff, 4 );
    pointLight.position = particleLight.position;
    scene.add( pointLight );

    renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );

    container.appendChild( renderer.domElement );

    stats = new Stats();
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.top = '0px';
    container.appendChild( stats.domElement );

}

var t = 0;
function animate() {

    requestAnimationFrame( animate );

    if ( t > 30 ) t = 0;

    if ( skin ) {

        // guess this can be done smarter...

        // (Indeed, there are way more frames than needed and interpolation is not used at all
        //  could be something like - one morph per each skinning pose keyframe, or even less,
        //  animation could be resampled, morphing interpolation handles sparse keyframes quite well.
        //  Simple animation cycles like this look ok with 10-15 frames instead of 100 ;)

        for ( var i = 0; i < skin.morphTargetInfluences.length; i++ ) {

            skin.morphTargetInfluences[ i ] = 0;

        }

        skin.morphTargetInfluences[ Math.floor( t ) ] = 1;

        t += 0.5;

    }

    render();
    stats.update();

}

function render() {

    var timer = Date.now() * 0.0005;

    camera.position.x = Math.cos( timer ) * 10;
    camera.position.y = 2;
    camera.position.z = Math.sin( timer ) * 10;

    camera.lookAt( scene.position );

    particleLight.position.x = Math.sin( timer * 4 ) * 3009;
    particleLight.position.y = Math.cos( timer * 5 ) * 4000;
    particleLight.position.z = Math.cos( timer * 4 ) * 3009;

    renderer.render( scene, camera );

}*/
