/*
 * yui-ext
 * 
 */

var DOM = YAHOO.util.Dom;
function ZipDialog () {
    var dialog, txtUsername, txtPassword;
    var postBtn, redirectLink;
    var wait, error, errorMsg;
    var dialogW = 260;
    var dialogH = 220;
    
    return {
	    iSubmit: 0,
      show : function(linkId, rL){
        if(dialog.isVisible()){
		     dialog.hide();
	    }
        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);
            if(linkId=='')linkId='ZipLink';
            if(redirectLink=='')redirectLink='index.php?';
            var linkObj = getEl(linkId);
            dialog.show(linkObj);
      },
    
        init : function(position){
             txtZip = getEl('zip');
             wait = getEl('post-wait-zip');
             error = getEl('post-error-zip');
             errorMsg = getEl('post-error-msg-zip');
             this.createDialog(position);
        },
        
        // submit the comment to the server
        submitComment : function(){
            //postBtn.disable();
           	if(this.iSubmit==0){	
	            wait.radioClass('active-msg');
    	        YAHOO.util.Connect.setForm(DOM.get('comment-form-zip'));
    	     }

      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);
			         }
			          //location.href = redirectLink;
                    //setTimeout(location = result.link, 100);
                }else{
                  error.radioClass('active-msg');
                  var err = '';
                  for(i in result.errors){
                    err += '<div class="Error" style="border: solid 0px orange; margin:0px; padding:0px">' + result.errors[i] + '</div>';
                  }
                  errorMsg.update(err);
                }
            };
            
            var commentFailure = function(o){
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
	    	if(this.iSubmit==0){
	            this.iSubmit=1;
    	        YAHOO.util.Connect.asyncRequest('POST', 'login_ajax.php', 
        	            {success: commentSuccess, failure: commentFailure});          
        	 }
        },
        
        createDialog : function(position){			
           var _shim=false;
  	       if(navigator.appMinorVersion) _shim=true; 
           dialog = new YAHOO.ext.BasicDialog("comments-dlg-zip", {             
          			draggable: false,
                    modal:false,
					closed:true,
                    width: dialogW,
                    height: dialogH,
                    x: position[0]+37,
                    y: position[1]+19,
                    shadow:false,
                    minWidth:dialogW,
                    minHeight:dialogH,
                    autoScroll: true,
                    resizable: false,
                    shim:  _shim
            });
      	 	dialog.toFront();
            dialog.addKeyListener(27, dialog.hide, dialog);
            //dialog.addKeyListener(13, this.submitComment, this);
            
            //postBtn = getEl('post-dialog-btn-zip');
            //postBtn.on('click', this.submitComment, this);

            cancelBtn = getEl('cancel-dialog-btn-zip');
            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 ZD = new ZipDialog();
YAHOO.ext.EventManager.onDocumentReady(ZD.init, ZD, true);
if(document.getElementById('ZipLink'))
	document.getElementById('ZipLink').style.display = '';
