HJ.ShareGroup = Class.create();

HJ.ShareGroup.prototype = Object.extend(new HJ.Dialog(), {

    initialize: function(handler) {
        this.HJ_Dialog_initialize("Share a Group", null, null);
        this.handler = handler;
		this.emailValidator = new HJ.EmailValidator();
        // this.width = 500;
        // this.height = 400;
    },
   
    loadTemplate: function() {
        this.showHTMLTemplate(
                basePageURL 
                + "groups/shareGroup?userUUID=" + login.userData.UUID, 
                "MainArea", 
                "shareGroup");
    },

    templateLoaded: function() {
        app.endBusy();
        // element collection class limits the look up by id within
        // the scope of the given element (in this case this.element).
        // In case there are other elements with the same id in the
        // "document" scope, this avoids id collision
        var ec = new Af.ElementCollection(this.element);

        var button = ec.getFirstElementById("successButton");
        if (button != null) {
            // share was successful, nothing to do
            button.onclick = this.successButtonClicked.bind(this);
            return;
        }
        
        this.selectPersonToShareWith 
            = ec.getFirstElementById("selectPersonToShareWith");
        this.shareToEmail 
            = ec.getFirstElementById("shareToEmail");
        this.selectGroupToShare = ec.getFirstElementById("selectGroupToShare");
        
        this.errorText = ec.getFirstElementById("errorText");

        button = ec.getFirstElementById("okButton");
        button.onclick = this.okButtonClicked.bind(this);

        var button = ec.getFirstElementById("cancelButton");
        button.onclick = this.cancelButtonClicked.bind(this);
        
        var left = 0;
        var top = 0;
        var elem = document.getElementById("MyGroupContent");
        
        if (elem.offsetParent) {
            do {
                left += elem.offsetLeft;
                top += elem.offsetTop;
            } while (elem = elem.offsetParent)
        }
        
        this.setLocation(left, top);
    },

    close: function() {
        if (this.handler != null) {
            this.handler.shareGroupDialogClosed(this);
        }
        this.hide();
        return false;
    },

    successButtonClicked: function() {
        return this.close();
    },
  
    okButtonClicked: function(ev) {

        this.clearErrorText();

        var shareTo = this.getShareToParameter();
        if (shareTo == null) {
            this.addErrorText(
                    "Please select a Jooners user from your contacts or "
                    + "enter the email address of a Jooners user.  ");
            return false;
        }
        
        var group = this.getGroupParameter();
        if (group == null) {
            this.addErrorText("Please select a group to share.  ");
            return false;
        }

        app.showBusy();

        // make sure a new template is created (tl in HtmlDialog.js)
        // XXX: Should be part of showHTMLTemplate? 
        this.tl = null;

        this.showHTMLTemplate(
                basePageURL 
                + "groups/shareGroup?userUUID=" + login.userData.UUID
                + "&" + group
                + "&" + shareTo, 
                "MainArea", 
                "shareGroup");
        

        return false;
    },

    /**
     * Return the user that has been selected either with the select widget
     * or by typing in the email address text input
     * 
     * @return a "shareToUUID=<User UUID>, "shareToEmail=<email> or null
     */
    getShareToParameter: function() {
        var email = trim(this.shareToEmail.value);
        if (email != "") {
            if (!this.emailValidator.validateEmail(email)) {
                this.addErrorText("Invalid email address " 
                                  + email + ".<br/>");
                return null;
            }
            email = this.emailValidator.extractEmailAddress(email);
            return "shareToEmail=" + email;
        }
        
        var i = this.selectPersonToShareWith.selectedIndex;
        if (i != 0) {
            return "shareToUUID=" + this.selectPersonToShareWith[i].value
        }

        // none selected
        return null;

    },

    /**
     * Return the group (UUID) that has been selected with the select widget
     * 
     * @return a "groupUUID=<Group UUID> or null
     */
    getGroupParameter: function() {
        var i = this.selectGroupToShare.selectedIndex;
        if (i == 0) {
            // none selected
            return null;
        }
        return "groupUUID=" + this.selectGroupToShare[i].value;
    },

    cancelButtonClicked: function() {
        this.close();
        return false;
    }, 

    clearErrorText: function() {
        this.errorText.innerHTML = "";
    },

    addErrorText: function(s) {
        this.errorText.innerHTML = this.errorText.innerHTML + s;
    }
    
});

