/**
 * @author Brendan Smith <brendan.s@crazydomains.com>
 * @version 1.0
 */
/**
 * rebuilded by Dmytro K
 */
function session_timeout_init(cart_type) {
    SessionTimeout.init(cart_type);
}

(function() {
    var session_timeout_interval,
        session_title,
        session_time_left,
        session_name = window.is_reseller_storefront ? 'reseller_sessid' : 'PHPSESSID',
        cart_type,
        old_title,
        is_showing = false,
        kill_session = false;

    var SessionTimeout = {
        init: function(type) {
            cart_type = type;

            $.ajaxSetup({ cache: false });

            clearInterval(session_timeout_interval);

            var session_id = SessionTimeout.session_id();

            session_timeout_interval = setInterval(function () {
                $.get(
                    '/members/ajax/get-session-expiry/',
                    'session_id=' + encodeURIComponent(session_id),
                    SessionTimeout.show
                );
            },30000);
        },
        kill: function() {
            return kill_session;
        },
        session_id: function() {
            var i,
                cookies = document.cookie.split(';'),
                cookie;

            for (i = 0; i < cookies.length; i++) {
                cookie = cookies[i];

                while (cookie.charAt(0) == ' ') {
                    cookie = cookie.substring(1, cookie.length);
                }

                if (cookie.indexOf(session_name) == 0) {
                    session_id = cookie.substring(session_name.length + 1, cookie.length); //+1 for the =

                    return session_id;
                }
            }
        },
        show: function(seconds_left) {
            if($('#session_timeout').is(':visible') || is_showing) return;
            if(window.parent !== window) return;
            seconds_left = parseInt(seconds_left);
            if (seconds_left > 180 || typeof seconds_left != 'number') {
                return false;
            }
            if(!seconds_left) {
                kill_session = true;
                SessionTimeout.cancel();
                return false;
            }
            is_showing = true;

            $('#session_timeout_countdown_wrap').show();

            SessionTimeout.countdown(seconds_left);
            SessionTimeout.title();

            CrazyPopUp.hide({kill:false});

            if(cart_type) {
                $('#timeout_' + cart_type).show();
            } else {
                $('#timeout_general').show();
            }

            setTimeout(function() {
                CrazyPopUp.show('session_timeout_wrap', {
                    width: 640,
                    scroll: false,
                    addClass: 'session',
                    closeOnOut: false,
                    onShow: function() {

                        clearInterval(session_timeout_interval);

                        $('.crazyPopUp_container.session').css({'z-index': 10000010});

                        $(document).on('click', '#session_timeout_countdown_ok_button', function keep() {

                            SessionTimeout.update();
                            SessionTimeout.hide();

                            $(document).unbind('click', keep);
                        });

                        $('.fakeClose').click(function() {
                            SessionTimeout.hide();
                        });
                    },
                    onHide: function() {
                        SessionTimeout.hide();
                    }
                });
            }, 350);
        },
        /**
         * {TO_FIX} need to reinit hidden popup, cause can fall on loader
         */
        hide: function() {
            if(!$('#session_timeout').is(':visible') || !is_showing) return;
            is_showing = false;
            clearTimeout(session_title);
            clearInterval(session_time_left);
            if(old_title) document.title = old_title;

            SessionTimeout.init(cart_type);

            if(SessionTimeout.kill()) {
                window.location = login_url;
                return
            }

            CrazyPopUp.showHidden();
        },
        cancel: function() {
            $(document).click(function() {

                window.location = login_url;
            });

            setTimeout(function() {
                window.location = login_url;
            }, 180000);

            $.get('/members/ajax/cancel-session/');
            /*
             $('#session_timeout_countdown_wrap').hide();
             $('#session_timeout_expired').show();
             */
            CrazyPopUp.showHidden();
            clearInterval(session_timeout_interval);
            clearTimeout(session_title);
            document.title = old_title;
        },
        title: function() {
            old_title = document.title;

            document.title = 'WARNING: Session Timeout';

            session_title = setTimeout(function() {
                document.title = old_title;

                session_title = setTimeout(SessionTimeout.title, 1000);
            }, 1000);
        },
        countdown: function(seconds_left) {

            clearInterval(session_time_left);

            session_time_left = setInterval(function() {
                seconds_left -= 1;

                if (seconds_left <= 0) {
                    clearInterval(session_time_left);
                    document.title = old_title;
                    kill_session = true;

                    SessionTimeout.cancel();
                }

                var min = Math.floor(seconds_left / 60),
                    sec = seconds_left % 60,
                    minutes = (min < 10) ? '0' + min : min,
                    seconds = (sec < 10) ? '0' + sec : sec;

                $('#session_timeout_countdown').html(minutes + ':' + seconds);
            }, 1000);
        },
        update: function() {
            var session_id = SessionTimeout.session_id();

            $.get(
                '/members/ajax/extend_session/',
                'session_id=' + encodeURIComponent(session_id)
            );
        }
    }

    window.SessionTimeout = SessionTimeout;
})();