/*
 *********************************
 *********************************
 Non modificare il seguente codice
 *********************************
 *********************************
 *Do not modify
 *********************************
 * @extension       Marlev Templates
 * @author          Lev Milicenco
 * @link            http://www.marlev.it
 * @copyright       Copyright 2017 marlev.it - Itroom srls All Rights Reserved
 */


jQuery.define_data = function define_data(element) {
    var element_class = "." + element;
    var data = {
        first: jQuery(element_class + "-0"),
        last: jQuery(element_class + "-1"),
        first_text: jQuery(element_class + "-0").text(),
        last_text: jQuery(element_class + "-1").text()
    };

    return data;
};


jQuery.sec_min = function sec_min(element, first_last) {
    var change_first = true;
    var start_prev = false;
    var data = jQuery.define_data(element);

    if (data['last_text'] == 0 && data['first_text'] == 0) {
        data['first_text'] = first_last[0];
        data['last_text'] = first_last[1];
        start_prev = true;
    } else if (data['last_text'] == 0) {
        data['last_text'] = 9;
        data['first_text'] = data['first_text'] - 1;
    } else {
        var change_first = false;
        data['last_text'] = data['last_text'] - 1;
        ;
    }

    jQuery.change_num(data['last'], data['last_text']);
    if (change_first === true) {
        jQuery.change_num(data['first'], data['first_text']);
    }
    return start_prev;

};


jQuery.change_num = function change_num(element, value) {
    jQuery(element).html(value);
}
jQuery.run_counter = function run_counter() {
    var data = {
        sec: [5, 9],
        min: [5, 9],
        hours: [2, 3],
        days: [0, 6],
        weeks: [5, 1],
        years: [0, 0]
    };
    var array = ["sec", "min", "hours", "days", "weeks", "years"];
    if (jQuery(".sec").length) {
        var get_seconds = jQuery("#ml-counter").data("seconds");
        if (get_seconds == 0) {
            return;
        }
        jQuery("#ml-counter").data("seconds", get_seconds - 1);
        var run_next = true;
        jQuery(array).each(function (key, value) {
            if (run_next === true) {
                run_next = jQuery.sec_min(value, data[value]);
            }
        });
    }
};
jQuery.timebox_width = function timebox_width() {
    if (jQuery("#ml-counter").length) {

        jQuery("#ml-counter").each(function () {
            var w = 0;
            var h = 0;
            jQuery(this).find(".time-box").each(function () {
                var t_w = jQuery(this).innerWidth();
                if (t_w > w) {
                    w = t_w;
                }
                var h_w = jQuery(this).innerHeight();
                if (h_w > h) {
                    h = h_w;
                }
            });
            jQuery(this).find(".time-box").each(function () {
                jQuery(this).css({"width": w, "height": h});

            });
        });

    }
};
jQuery.open_login_box = function open_login_box(){

       jQuery(".login-box").animate({"height": jQuery(".login-box").data("height")});
            jQuery(".login-box").addClass("opened");
            var poselement = jQuery(".offline-login").offset().top - 20;
            var positionTop = (poselement - jQuery(window).scrollTop());
            if (positionTop > 0 || positionTop < 50) {
                jQuery("html, body").animate({scrollTop: poselement}, 500);

            }
};

jQuery.offline_login = function offline_login() {
    var h = jQuery(".login-box").height();
    jQuery(".login-box").data("height", h);
    jQuery(".login-box").css({"height": 0, "display": "block"});

    jQuery(".offline-login").click(function () {
        if (jQuery(".login-box").hasClass("opened")) {

            jQuery(".login-box").animate({"height": 0},500);
            jQuery(".login-box").removeClass("opened");
            jQuery("html, body").animate({scrollTop: 0}, 500);

        } else {
            jQuery.open_login_box();
        }

    });
};
jQuery(document).ready(function () {
    jQuery.offline_login();
    jQuery.timebox_width();
    setInterval(function () {
        jQuery.run_counter();
    }, 1000);

    if(jQuery("#system-message").length) {
        setTimeout(function(){
             jQuery.open_login_box();
        },800);

    }


});

