/*
 * yui-ext
 * Copyright(c) 2006, Jack Slocum.
 */

var DOM = YAHOO.util.Dom;
function LoginDialog () {
    var dialog, txtUsername, txtPassword;
    var postBtn, redirectLink;
    var wait, error, errorMsg;
    var dialogW = 420;
    var dialogH = 275;
    
    return {
    
      show : function(linkId, rL){
        redirectLink = rL;
        var position = new Array;
          position = DOM.getXY(linkId);
          
          this.init(position);
          
          //dialog.resizeTo(dialogW, dialogH);
      //dialog.moveTo(position[0]-dialogW+37, position[1]+19);
            var linkObj = getEl(linkId);
            dialog.show(linkObj);
      },
    
        init : function(position){
             txtUsername = getEl('username');
             txtPassword = getEl('password');
             wait = getEl('post-wait');
             error = getEl('post-error');
             errorMsg = getEl('post-error-msg');
             this.createDialog(position);
        },
        
        // submit the comment to the server
        submitComment : function(){
            //postBtn.disable();
            wait.radioClass('active-msg');
            YAHOO.util.Connect.setForm(DOM.get('comment-form'));

      var commentSuccess = function(o){

                //alert(o.responseText);

              //var result = eval('new Object(' + o.responseText + ')');
              var result = YAHOO.ext.util.JSON.decode(o.responseText);

                //postBtn.enable();
                wait.removeClass('active-msg');
                if(result.link != false && result.errors.length == 0){
                    dialog.hide();
                    if(redirectLink == '' || redirectLink == null || /^index.php(.)*$/.test(redirectLink)){
            setTimeout(location = result.link, 100);
          }
                    else{
            setTimeout(location = redirectLink, 100);
          }
                    //setTimeout(location = result.link, 100);
                }else{
                  error.radioClass('active-msg');
                  var err = '';
                  var diffH = 0;
                  for(i in result.errors){
                    err += '<div class="Error" style="border: solid 0px orange;">' + result.errors[i] + '</div>\n';
                    diffH += 24;
                  }
                  //dialog.resizeTo(dialogW, (dialogH+diffH));
                  errorMsg.update(err);
                }
            };
            
            var commentFailure = function(o){
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
    
            YAHOO.util.Connect.asyncRequest('POST', 'login.php', 
                    {success: commentSuccess, failure: commentFailure});          
        },
        
        createDialog : function(position){
            dialog = new YAHOO.ext.BasicDialog("comments-dlg", {
          draggable: false,
                    modal:false,
                    width: dialogW,
                    height: dialogH,
                    x: position[0]-dialogW+37,
                    y: position[1]+19,
                    shadow:false,
                    minWidth:dialogW,
                    minHeight:dialogH,
                    autoScroll: true,
                    resizable: false
            });
      
            dialog.addKeyListener(27, dialog.hide, dialog);
            dialog.addKeyListener(13, this.submitComment, this);
            
            postBtn = getEl('post-dialog-btn');
            postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('cancel-dialog-btn');
            cancelBtn.on('click', dialog.hide, dialog);
            
            //postBtn = dialog.addButton('Sign-In', this.submitComment, this);
            //dialog.addButton('Cancel', dialog.hide, dialog);
            
            // clear any messages and indicators when the dialog is closed
            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');
                txtUsername.dom.value = '';
                txtPassword.dom.value = '';
            });
            
            dialog.on('show', function(){
              //txtUsername.value = tmpUsername;
        txtUsername.focus();

            });
        }
    };
};
var LD = new LoginDialog();
YAHOO.ext.EventManager.onDocumentReady(LD.init, LD, true);