$(document).ready(function(){
    setTimeout(initSlide, 10)
});

function initSlide(){
    var _tab_box = $('#tab-box').show(),
        _tab_box_small = $('#tab-box-min').hide(),
        _speed = 500,
        _info = _tab_box.find('.info').eq(0),
        _popup = $('.popup-chat').eq(0).hide(),
        _popup_close = _popup.find('.close>a'),
        _close_link = _tab_box.find('.no-thanks').eq(0),
        _height = _tab_box.outerHeight();

    if(_tab_box_small.is(':visible')){
        $('.footer-area').addClass('collapse');
    }

    _close_link.click(function(){
        _tab_box.slideUp(_speed, function(){
            $('.footer-area').addClass('collapse');
            _tab_box_small.show();
        });
        return false;
    });

    _tab_box_small.click(function(){
        $(this).hide();
        $('.footer-area').removeClass('collapse');
        positionBox();
        _tab_box.slideDown(_speed);
        return false;
    });

    _info.click(function(){
        _popup.show();
        return false;
    });

    _popup_close.click(function(){
        _popup.hide();
        return false;
    });

    function positionBox(){
        if($.browser.msie && $.browser.version < 7){
            _tab_box.css({
                right: 'auto',
                left: parseInt($('.footer-area').offset().left + $('.footer-area').outerWidth() - _tab_box.outerWidth() - $(window).scrollLeft() + 3) + 'px'
            })
            if(_tab_box.is(':visible')){
                if(parseInt($(document).height() - $(window).height() - $(window).scrollTop()) > 200){
                    _tab_box.css({
                        top: $(window).scrollTop() + $(window).height() - _height + 'px'
                    })
                } else {
                    _tab_box.css({
                        top: 'auto',
                        bottom:parseInt($(window).height() + $(window).scrollTop() - ($(document).height() - 272) - 1) + 'px'
                    })
                }
            }
        } else {
            _tab_box.css({
                right: 'auto',
                left: parseInt($('.footer-area').offset().left + $('.footer-area').outerWidth() - _tab_box.outerWidth() - $(window).scrollLeft() + 3) + 'px'
            })

            if(parseInt($(document).height() - $(window).height() - $(window).scrollTop()) > 200){
                _tab_box.css({
                    bottom:0
                })
            } else {
                _tab_box.css({
                    bottom:parseInt($(window).height() + $(window).scrollTop() - ($(document).height() - 272)) + 'px'
                })
            }
        }
    }

    positionBox();
    jQuery(window).resize(positionBox);
    jQuery(window).scroll(positionBox);
}

