var DOM = YAHOO.util.Dom;

function FriendAddDialog () {
    var dialog, postLink;
    var postBtn, oForm, redirectLink;
    var wait, error, errorMsg;
    var dialogW = 300;
    var dialogH = 135;
    
    return {
    
    	show : function(postLinkId, rL){
    		redirectLink = rL;
	    	oForm.friend_id.value = postLinkId;
			postLink = getEl('friend-link' + postLinkId);
        	var position = DOM.getXY(postLink.id);
        	dialog.resizeTo(dialogW, dialogH);
			dialog.moveTo(position[0]-dialog.width+37, position[1]+19);
           	dialog.show(postLink);
    	},
    	
        init : function(){
        	oForm = DOM.get('friend-form');
            wait = getEl('friend-wait');
            error = getEl('friend-error');
            errorMsg = getEl('friend-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 + ')');
                wait.removeClass('active-msg');
                if(result.errors.length == 0){
                    dialog.hide();
                    if(redirectLink == '' || redirectLink == null){
						DOM.get("frd_link").style.display = "none";
					}
                    else{
						setTimeout(location = redirectLink, 100);
					}
	                    
                }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("friend-dlg", {
					draggable: false,
                    modal:false,
                    width: dialogW,
                    height: dialogH,
                    shadow:true,
                    autoScroll:false,
                    minWidth:dialogW,
                    minHeight:dialogH,
                    resizable: false
            });

            dialog.addKeyListener(27, dialog.hide, dialog);
            dialog.addKeyListener(13, this.submitComment, this);
            
            postBtn = getEl('friend-post-dialog-btn');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('friend-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(){
				postBtn.focus();
            });
        }
    };
};

var FRAddD = new FriendAddDialog();
YAHOO.ext.EventManager.onDocumentReady(FRAddD.init, FRAddD, true);
