/**
 * 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;
};


var Invite = {

    init: function(elem, options) {
    
        if(jQuery.cookie('invite')) {
            return this;
        }

        this.elem = elem;
        this.options = jQuery.extend({}, this.defaults, options);

        this.setWindowDims(); // Set window dimensions


        jQuery(document).ready(function() {

            var text = jQuery('#invite-text').html();

            jQuery('body').append('<div id="lb-overlay">&nbsp;</div><div id="invite"></div>');

            Invite.$overlay = jQuery('#lb-overlay');
            Invite.$elem = jQuery('#invite');

            Invite.$elem.html(text);

            Invite.$elem.css({
                 display: 'none',
                 position: 'absolute',
                 top: (Invite.windowHeight/2 - Invite.options.height/2),
                 left: (Invite.windowWidth/2 - Invite.options.width/2),
                 width: Invite.options.width,
                 height: Invite.options.height,
                 zIndex: 9999
                 })
                 .fadeTo(0, 0);

            Invite.start()
                .initEventListners();

         });
         

        return this;

    },

    defaults: {
        speed: 200,
        cookieExpiresIn: 1, // noOfDays
        width: 400,
        height: 300,
        targetURL: '/survey.php'
    },

    initEventListners: function() {

        jQuery('body').click(function() {
            Invite.close();
        });

        this.$elem.click(function(event){
            event.stopPropagation();
        });

    },

    close: function() {

        this.setCookie();
        
        this.$elem.remove();
        
        this.$overlay.remove();

    },

    start: function() {

        this.$overlay.css({
            position: 'fixed',
            width: '100%',
            height: '100%',
            left: 0,
            top: 0,
            overflow: 'hidden',
            background: '#fff',
            zIndex: 999
            })
            .animate({opacity: 0.3}, 0);

        this.$elem.show().animate({opacity: 1}, this.options.speed);

        return this;

    },

    accept: function() {

        this.setCookie();

        return this;

    },
    
    setCookie: function() {

        jQuery.cookie('invite', true, {
            expires: Invite.options.cookieExpiresIn
        });

        return this;
        
    },

    setWindowDims: function() {

        this.windowWidth = null;
        this.windowHeight = null;

        // Browser sniffing
        if(jQuery.browser.msie) {

            var docEl = document.documentElement;
            this.windowWidth = docEl.clientWidth;
            this.windowHeight = docEl.clientHeight;

        }
        else {

            var win = window;
            this.windowWidth = win.innerWidth;
            this.windowHeight = win.innerHeight;

        }

        return this;

    }

};

Invite.init();
