var DOM = YAHOO.util.Dom;

function EmailToDialog () {
    var dialog, postLink, msgText;
    var postBtn, oForm;
    var wait, error, errorMsg;
    var dialogW = 320;
    var dialogH = 245;
    
    return {
    
    	show : function(postLinkId){
			postLink = getEl('email-to-link' + postLinkId);
            oForm.friend_id.value = postLinkId;
        	var position = DOM.getXY(postLink.id);
        	dialog.resizeTo(dialogW, dialogH);
			dialog.moveTo(position[0]-dialog.width+37, position[1]+19);
            dialog.hide();
           	dialog.show(postLink);
    	},
    	
        init : function(){
        	oForm = DOM.get('email-to-form');
			msgText = DOM.get('msgText');
            wait = getEl('email-to-wait');
            error = getEl('email-to-error');
            errorMsg = getEl('email-to-error-msg');
            this.createDialog();
        },
        
        submitComment : function(){
            wait.radioClass('active-msg');
            YAHOO.util.Connect.setForm(oForm);
			var commentSuccess = function(o){
            	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-to-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-to-post-dialog-btn');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('email-to-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 = '';
				//msgText.focus();
            });
        }
    };
};

var ETD = new EmailToDialog();
YAHOO.ext.EventManager.onDocumentReady(ETD.init, ETD, true);
