/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


/* stickyfloat - jQuery plugin for verticaly floating anything in a constrained area
* 
* Example: jQuery('#menu').stickyfloat({duration: 400});
* parameters:
*         duration     - the duration of the animation
*        startOffset - the amount of scroll offset after it the animations kicks in
*        offsetY        - the offset from the top when the object is animated
*        lockBottom    - 'true' by default, set to false if you don't want your floating box to stop at parent's bottom
* $Version: 05.16.2009 r1
* Copyright (c) 2009 Yair Even-Or
* vsync.design@gmail.com
*/
function close_layer() {
    //jQuery("#we_chat_support").css({"display" : "none"});
    jQuery("#we_chat_support").animate({"opacity": "0"}, "slow");
    var wedate = new Date();
    wedate.setTime(wedate.getTime() + (30 * 60 * 1000));
    jQuery.cookie("chat_window", "disable", { expires: wedate });
}
jQuery.fn.stickyfloat = function(options, lockBottom) {
    var $obj                 = this;
    var parentPaddingTop     = parseInt($obj.parent().css('padding-top'));
    var startOffset         = $obj.parent().offset().top;
    var opts                 = jQuery.extend({ startOffset: startOffset, offsetY: parentPaddingTop, duration: 200, lockBottom:true }, options);
    
    $obj.css({ position: 'absolute' });
    
    if(opts.lockBottom){
        var bottomPos = $obj.parent().height() - $obj.height() + parentPaddingTop; //get the maximum scrollTop value
        if( bottomPos < 0 )
            bottomPos = 0;
    }
    
    jQuery(window).scroll(function () { 
        $obj.stop(); // stop all calculations on scroll event

        var pastStartOffset            = jQuery(document).scrollTop() > opts.startOffset;    // check if the window was scrolled down more than the start offset declared.
        var objFartherThanTopPos    = $obj.offset().top > startOffset;    // check if the object is at it's top position (starting point)
        var objBiggerThanWindow     = $obj.outerHeight() < jQuery(window).height();    // if the window size is smaller than the Obj size, then do not animate.
        
        // if window scrolled down more than startOffset OR obj position is greater than
        // the top position possible (+ offsetY) AND window size must be bigger than Obj size
        if( (pastStartOffset || objFartherThanTopPos) && objBiggerThanWindow ){ 
            var newpos = (jQuery(document).scrollTop() -startOffset + opts.offsetY );
            if ( newpos > bottomPos )
                newpos = bottomPos;
            if ( jQuery(document).scrollTop() < opts.startOffset ) // if window scrolled < starting offset, then reset Obj position (opts.offsetY);
                newpos = parentPaddingTop;

            $obj.animate({ top: newpos }, opts.duration );
        }
    });
};

jQuery(document).ready(function(){
    
    var showchatwindow = jQuery.cookie("chat_window");
    if(! showchatwindow) {
        if(we_online != 2) {
            jQuery("#we_chat_support").css("display", "block");
            jQuery('#we_chat_support').stickyfloat({ duration: 400 });
            jQuery("#we_chat_support").animate({"opacity": "0"}, "slow");
            jQuery("#we_chat_support").animate({"opacity": "100"}, "slow");
            //$('#we_chat_support').slideToggle();
        }
    }
});
