$(function () {

    function inUrl(text) {
        var url = window.location.href;
        return (url.indexOf(text) != -1);
    }


    // difference in milliseconds

    function msToTime(s) {
        var time = {};

        time.ms = s % 1000;
        s = (s - time.ms) / 1000;
        time.secs = s % 60;
        s = (s - time.secs) / 60;
        time.mins = s % 60;
        time.hrs = (s - time.mins) / 60;

        return time;//hrs + ':' + mins + ':' + secs;
    }

    function pad(num, size) {
        var s = "000000000" + num;
        return s.substr(s.length - size);
    }

    var time = $("#time_started").data('time');
    var timer = $("div.timer")
    var d2 = new Date(time);  // start date


    var showButton = $("button#showbutton");
    showButton.prop("disabled", true);

    var dukazImg = $("div#dukaz");
    dukazImg.hide();

    showButton.click(function () {
        dukazImg.show();
    });


    showTime();

    // its showtime babe
    function showTime() {

        if ($("#time_ended").length) {
            d1 = new Date($("#time_ended").data('time'));
        } else
            d1 = new Date();

        var diff = Math.abs(d1 - d2);

        var time = msToTime(diff);

        timer.text(time.hrs + ":" + pad(time.mins, 2) + ":" + pad(time.secs, 2));


            if (diff > 3000000 && diff < 3600000) // 50 minut
            {
                timer.addClass('orange');
            } else if (diff > 4500000) // hodina a čtvrt
            {
                timer.addClass('red');
            }

        if (diff > 1800000)
        {
            showButton.prop("disabled", false);
        }


        // console.log(diff>3600000);
        setTimeout(showTime, 500);
    }

    $('#frm-firstForm').submit(checkTimeSubmit);

    function checkTimeSubmit(e) {

        var comparement = 2700000;
        var time = "45";

        d1 = new Date();

        var diff = Math.abs(d1 - d2);

        if (diff < comparement) {
            e.preventDefault();
            alert("Submit is possible after " + time + " mins");
        }

    }
});
