App = {};

// Navigation
App.Navigation = {

    init: function()
    {
        this.addHoverListener();
    },

    addHoverListener: function()
    {
        $("div.navigation ul li").mouseover(function() {

            $(this).addClass('hover');

        }).mouseout(function() {

            $(this).removeClass('hover');
            
        });
    }
},

App.Tinyurl = {

    checking: false,
    ready: true,

    init: function()
    {

        this.deployListener();
    },

    deployListener: function()
    {

        $("#hash").keyup(function() {

            var text = $(this).val();

            if (text.length < 3) {

                App.Tinyurl.ready = false;
                $("#tinyurlState").text('Kürzel muss 3 Zeichen lang sein!');

            } else if (text.length > 10) {

                App.Tinyurl.ready = false;
                $("#tinyurlState").text('Kürzel darf max. 10 Zeichen lang sein!');

            } else {

                if (!App.Tinyurl.checking) {

                    App.Tinyurl.checking = true; 
                    window.setTimeout("App.Tinyurl.checkAvailability()", 500);
                }

            }
        });
    },

    checkAvailability: function()
    {
        $.ajax({
            type: "GET",
            url: basepath + "tinyurl/checkhash/hash/" + $("#hash").val(),
            dataType: "json",
            success: function(response) {
                if (response.available == false) {

                    App.Tinyurl.ready = false;
                    $("#tinyurlState").text('Kürzel ist schon vergeben!');

                } else {

                    App.Tinyurl.ready = true;
                    $("#tinyurlState").text('Kürzel ist top!');
                }
            }
        });

        App.Tinyurl.checking = false;
    },

    submit: function()
    {
        var $url = $("#url");
        var $hash = $("#hash");
        var $expires = $("#expires");

        if (!$url.val()) {

            $("#tinyurlState").text('Keine URL angegeben!');

        } else if (!App.Tinyurl.ready) {

            $("#tinyurlState").text('Das Kürzel ist nicht korrekt!');

        } else {

            $.ajax({
                type: "POST",
                url: basepath + "tinyurl/register",
                data: "url=" + $url.val() + "&hash=" + $hash.val() + "&expires=" + $expires.val(),
                dataType: "json",
                success: function(response) {

                    if (response.success) {

                        $("#tinyurlInput").slideUp();
                        $("#tinyurlRegisterSuccessText").text('http://u.aimbot.net/' + $hash.val());
                        $("#tinyurlRegisterSuccessSheet").slideDown();
                        
                    } else {

                        $("#tinyurlState").text('Oops, nochmal probieren bitte!');
                    }
                }
            });
        }
    }
},

App.Speedchecker = {

    results: [],

    init: function()
    {
        this.deployListener();
    },

    deployListener: function()
    {
        $("#startSpeedtest").click(function() {

            App.Speedchecker.runTests();
        });
    },

    info: function(text)
    {
        $("#speedtestState").text(text);
    },

    runTests: function()
    {
        var startTime = new Date();
        var passed = false;
        var tests = [1,5,10,25];
        var i = 0;
        var result = 0;
        var testsCount = tests.length;

        while (!passed) {

            this.info('Running Test ' + (i+1) + ' of ' + testsCount);
            result = this.benchmark(tests[i]);

            App.Speedchecker.results[i] = (tests[i] * 1024) / result;

            var currentTime = new Date();
            if (startTime.getTime() - currentTime.getTime() > 60000 || i == (testsCount-1)) {

                passed = true;
                this.info('Tests completed');
            }

            i += 1;
        }

        this.calculateResults(); 
    },
    
    benchmark: function(filesize)
    {
        var startTime = new Date(); 
        var result = 0;
        
        $.ajax({
            async: false,
            type: "GET",
            url: basepath + "data/" + filesize + "mb.data",
            dataType: "text",
            success: function(msg) {

                var stopTime = new Date();
                result = stopTime.getTime() - startTime.getTime();
            }
        });

        return result;
    },

    calculateResults: function()
    {
        var overall = 0;
        for (key in App.Speedchecker.results) {

            overall += App.Speedchecker.results[key];
        }

        this.info('Your Download Speed is about ' + Math.round(overall) + ' Mbit/s');
    }

};