var SECOND_USER = null;
var SECOND_USER_DATA_COOKIE_ID = "confluence.seconduser";
var SECOND_USER_DATA_COOKIE_SESSION_ID = "confluence.seconduser.session";

var SecondUser = function() {};
SecondUser.prototype = {
    dlg: null,
    settings: null,
    enabled_config: true,
    enabled_session: true,

    create: function() {
        var enable_session_cookie = this.getCookie(SECOND_USER_DATA_COOKIE_SESSION_ID); 
        
        if (enable_session_cookie == "" || enable_session_cookie == null) {
            this.enabled_session = true;
            this.setSessionCookie(SECOND_USER_DATA_COOKIE_SESSION_ID, true);
        }
        else {
            this.enabled_session = enable_session_cookie == "true";
        }

        var enable_cookie = this.getCookie(SECOND_USER_DATA_COOKIE_ID);
        
        if (enable_cookie == "" || enable_cookie == null) {
            var self = this;
            // only call back to the server if the cookie is not present at all. 
            AJS.$.getJSON(contextPath + "/plugins/seconduser/getEnabled.action", function(data) {
                self.setCookie(SECOND_USER_DATA_COOKIE_ID, data.enabled, 365);
                self.enabled_config = data.enabled;
            });
        }
        else {
            this.enabled_config = enable_cookie == "true";
        }
    },
    
    loadSettingsAndShowDialog: function() {
        var self = this;
        AJS.$.getJSON(contextPath + "/plugins/seconduser/getSettings.action", function(data) {
            if(data.enabled == true) {
                self.settings = data;
                self.showDialog();
            }
        });
    },
    
    showDialog: function() {
        if (this.dlg == null) {
            //Create the dialog
            this.dlg = new AJS.Dialog(parseInt(this.settings.width), parseInt(this.settings.height), "second-user-dialog");
            if((typeof this.settings.url != "undefined") && this.settings.url != "" && this.settings.url != null) {
			    this.dlg.addPanel("Welcome", "<iframe src=' " + this.settings.url +"' width='100%' height='100%' frameborder='0' scrolling='no'>", "panel1");
            } else {
                this.dlg.addPanel("Welcome", "<div>The target page could not be accessed. Please contact your administrator.</div>", "panel1");
            }
			this.dlg.addHeader("Welcome to Confluence");
			this.dlg.getCurrentPanel().setPadding(0);
			this.dlg.getCurrentPanel().body.css("overflow", "hidden");
			this.dlg.addButton("Close", function(dlg) {dlg.doHide(dlg);}, "button1");
            this.dlg.doHide = function(){
                SECOND_USER.setSessionCookie(SECOND_USER_DATA_COOKIE_SESSION_ID, false);
                this.hide();
            }
	        var checked_string = this.isOnByDefault() ? "checked" : "";
            
	        this.dlg.page[this.dlg.curpage].buttonpanel.prepend(AJS.$("<div class='checkbox' style='float:left;'><input type='checkbox' id='hide-seconduser' onchange='SECOND_USER.setUserEnable(this.checked)' " + checked_string + "/><label for='hide-seconduser'>Show this Welcome Screen on log in</label></form>"));
		}
        this.dlg.show();
    },
    
    isOnByDefault: function() {
        var enable_cookie = this.getCookie(SECOND_USER_DATA_COOKIE_ID);
        return enable_cookie == "true";
    },
    
    setUserEnable: function(enable) {
        this.setUserEnabled(enable, 365);
    },

    
    setCookie: function (c_name, value, expiredays) {
        if (typeof(expiredays) == "undefined") {
            expiredays = null;
        }
    
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        var cookie = c_name + "=" + escape(value) +
                          ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + 
                          ";path=/";
        document.cookie = cookie;
    },
    
    setUserEnabled: function(value, expiredays) {
        this.setCookie(SECOND_USER_DATA_COOKIE_ID, value, expiredays);
        AJS.$.getJSON(contextPath + "/plugins/seconduser/setEnabled.action?enabled=" + value);
    },
    
    setSessionCookie: function (c_name, value) {
        this.setCookie(c_name, value, null);
    },
    
    getCookie: function (c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }
};


AJS.$(document).ready(function() {
    AJS.$(".sherpa-enable-checkbox").click(function () {
        if( null == SECOND_USER) {
            SECOND_USER = new SecondUser();
        }
        SECOND_USER.setUserEnabled(this.checked, 365);
        SECOND_USER.setSessionCookie(SECOND_USER_DATA_COOKIE_ID, this.checked);
    });
});

AJS.$(document).ready(function()
{
    var lightbox = AJS.$("#show-sherpa-lightbox");
    var finish = function() {
        lightbox[0].href = "#";
        lightbox.click(function ()
        {
            SECOND_USER.create();
            SECOND_USER.loadSettingsAndShowDialog();
        });
    }
    
    if(typeof lightbox == "undefined" || typeof lightbox[0] == "undefined")
    {
        var list = AJS.$("#header-menu-bar")[0];
        if(typeof list == "undefined") {
            return;
        }
        
        AJS.$.getJSON(contextPath + "/plugins/seconduser/getSettings.action", function(data) {
            var enabled = data.enabled;
            if(enabled == true) {
                var extraHtml = '<li class="ajs-menu-item normal"><a class="user-item" href="#" id="show-sherpa-lightbox"><span>Welcome Screen</span></a></li>';
                list.innerHTML = list.innerHTML + extraHtml;
                lightbox = AJS.$("#show-sherpa-lightbox");
                finish();
            }
        });
    }
    else {
        finish();
    }
    
});

// Init function
AJS.toInit(function() {
	if (null == SECOND_USER){
		SECOND_USER = new SecondUser();
		SECOND_USER.create();
        if(SECOND_USER.enabled_config && SECOND_USER.enabled_session)
        {
            SECOND_USER.loadSettingsAndShowDialog();
        }
	}
});

