var DOM = YAHOO.util.Dom;

function EmailDialog () {
    var dialog, postLink, msgAddress, msgText, msgFname, msgLname;
    var postBtn;
    var wait, error, errorMsg;
    var dialogW = 420;
    
    if(!nonSignedUser || nonSignedUser == 0)
    	var dialogH = 275;
    else
	    var dialogH = 350;
    
    return {
        init : function(){
			msgAddress = DOM.get('msgAddress');
			msgText = DOM.get('msgText');
			
			if(nonSignedUser && nonSignedUser == 1){
				msgFname = DOM.get('msgFname');
				msgLname = DOM.get('msgLname');
			}
			
            postLink = getEl('email-link');
            if(!postLink) return;
            wait = getEl('email-wait');
            error = getEl('email-error');
            errorMsg = getEl('email-error-msg');
            
            this.createDialog();
			
            postLink.addHandler('click', true, function(){
	        	var position = DOM.getXY(postLink.id);
	        	dialog.resizeTo(dialogW, dialogH);
				dialog.moveTo(position[0]-dialog.width+37, position[1]+19);
                dialog.show(postLink);
             });
        },
        
        submitComment : function(){
            wait.radioClass('active-msg');
            YAHOO.util.Connect.setForm(DOM.get('email-form'));
			var commentSuccess = function(o){
			
				//alert(o.responseText);
			
            	var result = eval('new Object(' + o.responseText + ')');
                //postBtn.enable();
                wait.removeClass('active-msg');
                if(result.errors.length == 0){
                    dialog.hide();
                }else{
                    error.radioClass('active-msg');
                    var err = '';
                    for(i in result.errors){
                    	err += '<div class="Error">' + result.errors[i] + '</div>\n';
                    }
                    dialog.resizeTo(dialogW, dialogH+30);
                    errorMsg.update(err);
                }
            };

            var commentFailure = function(o){
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
            YAHOO.util.Connect.asyncRequest('POST', 'query_wrapper.php',
                    {success: commentSuccess, failure: commentFailure});
        },
        
        createDialog : function(){
            dialog = new YAHOO.ext.BasicDialog("email-dlg", {
					draggable: false,
                    modal:false,
                    width: dialogW,
                    height: dialogH,
                    shadow:true,
                    minWidth:dialogW,
                    minHeight:dialogH,
                    resizable: false
            });

            dialog.addKeyListener(27, dialog.hide, dialog);
            //dialog.addKeyListener(13, this.submitComment, this);
            
            postBtn = getEl('email-post-dialog-btn');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('email-cancel-dialog-btn');
            cancelBtn.on('click', dialog.hide, dialog);

            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
            });
            
            dialog.on('show', function(){
				msgText.value = '';
				if(nonSignedUser && nonSignedUser == 1){
					msgFname.value = '';
					msgLname.value = '';
				}
				msgAddress.value = '';
				msgAddress.focus();
            });
        }
    };
};

var ED = new EmailDialog();
YAHOO.ext.EventManager.onDocumentReady(ED.init, ED, true);
