var requirePasswordClass = {
    tplURL : "/global/form/requirepassword/requirepassword.html",
    tplReference : "#requirepassword",
    datasource: "requirepassword_tpl",

    init:function(_callback){
    	var self = this;
    	this._password = $(this.tplReference+" #password");
    	this._passwordD = $( this.tplReference+' #password-description' ); 
    	this._passwordE = $( this.tplReference+' #password-error' );       
        this._internalE=$( this.tplReference+' #internal-error' );
        this._allFields = $( [] ).add(this._password);
        this._allDescription = $([]).add(this._passwordD);
        this._allError=$([]).add(this._passwordE).add(this._internalE);       
        
        if($.client.os=="Mac") { $(this.tplReference+" .close").css({'right': '','left': -'22'}); }
        
        $(this.tplReference + " button").button();
        
        $(document).keypress(function(e) {
    		if (e.keyCode == 27) { requirePasswordClass._close(); }   // esc
    		
    		if (e.keyCode == 13) { requirePasswordClass.save(_callback); }   // enter
    	});
		
		$("#maskId").click(function(){
        	self._close();
        });
        $(this.tplReference + " #save").click(function(){
    		self.save(_callback);
    	});
    	
    	$(this.tplReference + " #close").click(function(){
    		self._close();
    	});
    	$(this.tplReference + " #cancel").click(function(){
    		self._close();
    	});
    },
    _close:function(){
    	$(this.tplReference).hide();
    	$.unmask();
    	$(this.tplReference).remove();
    },
    save:function(_callback){
    	this._allError.removeClass( 'ui-state-error-text' );
        this._allError.text("");
        this._allFields.removeClass( 'ui-state-error' );
        var self=this;
        
        $.post('/account/verifypassword',
	    { 	
	        password : this._password.val()
	    },
	    function(data){        
	    	$.trace(data);
	        if(data.success){
	        	$.trace('success');
	        	me.hasVerifiedPassword = true;
	        	self._close();
	        	_callback();
	        }else{
	        	for(var i in data.data){
	                $(self.tplReference + ' #'+i).addClass( 'ui-state-error' );
	                $(self.tplReference + ' #'+i+"-error").addClass( 'ui-state-error-text' ).text(data.data[i]);     
	            }	            	
	        }
	    },'json');
    }
};
