NGMP.utils.clone=function(C){
	if("object"!==typeof C){
		return C;
	}
	var D="function"===typeof C.pop?[]:{} ;
 	
	var B,A;
 	
	for(B in C){
		A=C[B];
		if("object"===typeof A){
			D[B]=NGMP.util.clone(A);
			} else {
				D[B]=A;
	   		}
    }
	return D;
};

NGMP.utils.EventQueue=function(B,A){
    if(!B){
        throw"Handler is required.";
    }
    this.handler=B;
    this.scope=A||window;
    this.queue=[];
    this.is_processing=false;
    this.postEvent=function(C,D){
        D=D||null;
        this.queue.push({
            event:C,data:D
        });
        if(!this.is_processing){
            this.process();
        }
        
    };
    this.flushEventQueue=function(){
        this.queue=[];
    };
	this.process=function(){
        while(this.queue.length> 0){
            this.is_processing=true;
            var C=this.queue.shift();
            this.handler.call(this.scope,C.event,C.data);
        }
        this.is_processing=false;
    };
};
NGMP.utils.FSA=function(C,B,A){
    this.current_state=C;
    this.trans_table=B||{
       } ;
    this.trans_table_scope=A||window;
    NGMP.utils.FSA.superclass.constructor.call(this,this.processEvent,this);
};
Ext.extend(NGMP.utils.FSA,NGMP.utils.EventQueue,{
    current_state:null,trans_table:null,trans_table_scope:null,state:function(){
        return this.current_state;
    },
    processEvent:function(A,D){
        var H=this.currentStateEventTransitions(A);
        if(!H){
            throw"State '"+this.current_state+"' has no transition for event '"+A+"'.";
        }
        for(var E=0,F=H.length;E<F;E++){
            var G=H[E];
            var I=G.predicate||G.p||true;
            var C=G.action||G.a||Ext.emptyFn;
            var B=G.state||G.s||this.current_state;
            var J=G.scope||this.trans_table_scope;
            if(this.computePredicate(I,J,D,A)){
                this.callAction(C,J,D,A);
                this.current_state=B;
                return;
            }
            
        }
        throw"State '"+this.current_state+"' has no transition for event '"+A+"' in current context";
    },
    currentStateEventTransitions:function(A){
        return this.trans_table[this.current_state]?this.trans_table[this.current_state][A]||false:false;
    },
    computePredicate:function(C,E,G,F){
        var B=false;
        switch(Ext.type(C)){
            case"function":B=C.call(E,G,F,this);
            break;
            case"array":B=true;
            for(var D=0,A=C.length;
            B&&(D<A);D++){
                if(Ext.type(C[D])=="function"){
                    B=C[D].call(E,G,F,this);
                }
                else{
                    throw ["Predicate: ",C[D],' is not callable in "',
					this.current_state,'" state for event "',F].join("");
                }
                
            }
            break;
            	case"boolean":B=C;
            break;
            default:throw ["Predicate: ",C,' is not callable in "',
			this.current_state,'" state for event "',F].join("");
        }
        return B;
    },
    callAction:function(F,C,E,D){
        switch(Ext.type(F)){
            case"array":for(var B=0,A=F.length;B<A;B++){
                if(Ext.type(F[B])=="function"){
                    F[B].call(C,E,D,this);
                }
                else{
                    throw ["Action: ",F[B],' is not callable in "',this.current_state,'" state for event "',D].join("");
                }
                
            }
            break;
            case"function":F.call(C,E,D,this);
            break;
            default:throw ["Action: ",F,' is not callable in "',this.current_state,'" state for event "',D].join("");
        }
        
    }
    
});

NGMP.utils.GUID=function(A){
    if(!A){
        try{
            this.id=(new ActiveXObject("Scriptlet.TypeLib").GUID).substr(1,36);
        }
        catch(B){
            this.id=this.createGUID();
        }
        
    }
    else{
        Ext.Ajax.request(A);
    }
    
};
NGMP.utils.GUID.prototype.valueOf=function(){
    return this.id;
};
NGMP.utils.GUID.prototype.toString=function(){
    return this.id;
};
NGMP.utils.GUID.prototype.createGUID=function(){
    var D=new Date(1582,10,15,0,0,0,0).getTime();
    var G=new Date().getTime();                        
    var I=(D<
    0)?Math.abs(D)+G:G-D;
    var B="-";
    var J=NGMP.utils.GUID.getIntegerBits(I,0,31);
    var H=NGMP.utils.GUID.getIntegerBits(I,32,47);
    var F=NGMP.utils.GUID.getIntegerBits(I,48,59)+"1";
    var C=NGMP.utils.GUID.getIntegerBits(Math.randRange(0,4095),0,7);
    var E=NGMP.utils.GUID.getIntegerBits(Math.randRange(0,4095),0,7);
    var A=NGMP.utils.GUID.getIntegerBits(Math.randRange(0,8191),0,7)+NGMP.utils.GUID.getIntegerBits(Math.randRange(0,8191),8,15)+NGMP.utils.GUID.getIntegerBits(Math.randRange(0,8191),0,7)+NGMP.utils.GUID.getIntegerBits(Math.randRange(0,8191),8,15)+NGMP.utils.GUID.getIntegerBits(Math.randRange(0,8191),0,15);
    return J+B+H+B+F+B+C+E+B+A;
};
NGMP.utils.GUID.getIntegerBits=function(F,G,B){
    var A=NGMP.utils.GUID.returnBase(F,16);
    var D=A.split("");
    var E="";
    var C=0;
    for(C=Math.floor(G/4);C<=Math.floor(B/4);C++){
        if(!D[C]||D[C]===""){
            E+="0";
        }
        else{
            E+=D[C];
        }
        
    }
    return E;
};
NGMP.utils.GUID.returnBase=function(A,B){
    return A.toString(B).toUpperCase();
};
Ext.applyIf(Math,{
    randRange:function(B,A){
        return Math.max(Math.min(Math.round(Math.random()*A),A),B);
    }
});
