﻿var timer = 1000;
var calling = 0;

$(document).ready(function () {

//    setInterval(resetTime(), 5000);

    initCancelClick();

//    function sessionBeat() {
//        $.ajax({
//            type: "POST",
//            url: $("#theLocalPath").val() + "scripts/SessionCheck.ashx",
//            success: function (returnObj) {
//                if (returnObj != "")
//                    location.href = returnObj;
//            }
//        });
//    }

    function initCancelClick() {
        $('.prizeButton').click(function () {

            if (this.className.indexOf('clicked') > 0)
                return false;

            this.className += ' clicked';
            $(this).removeAttr("href");

            var position = $(this).parent().parent()[0].className;
            var split = position.split(' ');
            return Mine(this.className, split[5].split('_')[1]);
        });
    }

    function Mine(classname, position) {

        var split = classname.split(' ');
        var SplitIndex1 = 3;
        var SplitIndex2 = 4;
        if (classname.indexOf("animatedButton") != -1) {
            SplitIndex1 = 4;
            SplitIndex2 = 5;
        }

        if (calling == 1)
            return false;
        calling = 1;
        var priceId = split[SplitIndex1];
        var profileId = split[SplitIndex2];

        var theData = "priceid=" + priceId + "&profileid=" + profileId + "&position=" + position;

        $.ajax({
            type: "POST",
            url: $("#theLocalPath").val() + "scripts/MinePrice.ashx",
            data: theData,
            success: function (returnObj) {

                var url = location.href;
                if (url.indexOf('?stat=') > -1)
                    url = url.substring(0, url.indexOf('?stat='));
                else if (url.indexOf('&stat='))
                    url = url.substring(0, url.indexOf('&stat='));

                $('#layer').val(returnObj);
                document.forms[0].submit();
            }
        });
        return false;
    }

    $('.prizeTime').each(function (index, value) {
        var split = $(this).parent()[0].className.split(' ');
        if (split.length > 6) {
            $(value)[0].mineTime = parseInt(split[5].split('_')[1]);

        }
        else
            $(value)[0].mineTime = 0;


        if (!$(value).hasClass('hidden')) {

            setInterval(function () {
                // als de mijn tijd groter wordt dan 1 minuut dan verversen van de pagina, immers is dan of al gemijnd of het systeem heeft de prijs gehouden
                //if ($(value)[0].mineTime > 0)
                //    document.title = $(value)[0].mineTime;
                if ($(value)[0].mineTime > 60000) {
                    $(value)[0].minceTime = 0;
                    //  window.location = $("#theLocalPath").val() + "Prijzenpot.html";
                }
                else {
                    if ($(value).html() != "00:00:00" && $(value).html().indexOf('-') < 0) {
                        var hours = $(value).html().substring(0, 2) * 1;

                        var minutes = $(value).html().substring(3, 5) * 1;
                        var seconds = $(value).html().substring(6, 8) * 1;
                        seconds--;
                        if (seconds < 0) {
                            minutes--;
                            seconds = 59;
                        }
                        if (minutes < 0) {
                            hours--;
                            minutes = 59;
                        }
                        var hourPart = hours;
                        var minutesPart = minutes;
                        var secondsPart = seconds;
                        if (hourPart < 10) hourPart = "0" + hourPart;
                        if (minutesPart < 10) minutesPart = "0" + minutesPart;
                        if (secondsPart < 10) secondsPart = "0" + secondsPart;
                        if (isNaN(hourPart)) hourPart = "00";
                        if (isNaN(minutesPart)) minutesPart = "00";
                        if (isNaN(secondsPart)) secondsPart = "00";
                        $(value).html(hourPart + ":" + minutesPart + ":" + secondsPart);
                    }
                    else {
                        $(value).html("00:00:00");
                        if ($(value).text() == "00:00:00" && $(value).parent().hasClass('activePrice'))
                            $(value)[0].mineTime += timer;
                    }
                }
            }, timer);
        }
    });
});

