function feedback() {
    this.status = false;
    this.timeout = false;
    this.init();
    this.timer();
}

(function() {

   var th;

feedback.prototype = {
    status: false,
    init: function() {
        $( 'body' ).append( '<div style="width: 30px; height: 158px; cursor: pointer; position: fixed; left: 0; top: 284px; z-index: 10; background-image: url( \'i/fb/online_small.png\' ); background-position: right;" id="feedback"></div>' );
        $( '#feedback' ).click( this.enlarge );
    },
    enlarge: function() {
        if( !ff.status ) {
            $( '#feedback' ).html( '<div id="mess" style="color: white; font-size: 18px; position: absolute; left: 24px; top: 18px; width: 288px; height 150px; line-height: 20px; ">Добрый день! :-)<br> Возможно, у Вас есть вопрос или Вы хотите что-то уточнить? <div style="font-size: 12px; margin: 10px 10px 0 0; color: black;  line-height: 16px;">Оператор контакт-центра СкайНэт готов ответить на Ваш вопрос в режиме онлайн,<br> без регистрации. <b style="color: white;"><u>Начать диалог</u> &raquo;</b></div></div><img src="i/fb/online_full.png">' );
            $( '#feedback' ).css( 'background-image', '' );
            $( '#feedback' ).css( 'left',  '-312px' );
            $( '#feedback' ).unbind();
            $( '#feedback img' ).click( ff.small );
            $( '#mess' ).click( ff.open_chat() );
            $( '#feedback' ).animate( { 'left': '0' }, 'slow' );
            ff.status = true;
            createCookie( 'user_close', 0 );
        }
    },
    small: function( e ) {
        e.stopPropagation();
        $( '#feedback' ).html( '' );
        $( '#feedback' ).css( 'background-image', 'url( \'i/fb/online_small.png\' )' );
        $( '#feedback img' ).unbind();
        $( '#feedback' ).click( ff.enlarge );
        ff.status = false;
        createCookie( 'user_close', 1 );
    },
    check: function() {
        var last_set = readCookie( 'last_set' );
        var now = new Date();
        if( last_set != null && last_set > now.getTime() - 30000 ) { //30 secons lifetime
            var elapsed = parseInt( readCookie( 'elapsed' ) );
            if( elapsed > 15*1000 && !parseInt( readCookie( 'user_close' ) ) ) { // 5 minutes
                this.enlarge();
            }
            createCookie( 'last_set', now.getTime() );
            createCookie( 'elapsed', elapsed + now.getTime() - last_set );
        }
        else {
            createCookie( 'last_set', now.getTime() );
            createCookie( 'elapsed', 0 );
            createCookie( 'user_close', 0 );
        }
    },
    timer: function() {
        if( !th ) {
            th = this;
            th.timeout = setTimeout( th.timer, 5000 );
        }
        else {
            th.check();
            th.timeout = setTimeout( th.timer, 10000 );
        }
    },
    open_chat: function() {
        return function() {
            var height = 550;
            if( jQuery.browser.safari || jQuery.browser.mozilla ) {
                height += 4;
            }
        window.open(    'http://feedback.sknt.ru/?cat=chat', 
                        'chat_feedback', 
                        'menubar=0,resizable=0,directories=no,location=no,toolbar=no,status=no,scrollbars=0,width=822,height='+height+',screenX=50,screenY=100' 
                    );
        return false;
        }
    }
}
})();

