
/**
*
*
*
*/

var logicremoteURL = "";
var logicnextRequest = [];
var logicengineTimer = null;

function Logicdelegate(obj, fn) { return function() { return fn.apply(obj, arguments) } }

function logicobservableData() { this.object = [] }
logicobservableData.prototype = {
	add: function(fn){
		this.object.push(fn);
	},
	remove : function(fn){
		var temp = [];
		for(var i in this.object)
			if(this.object[i] != fn)
				temp.push(this.object[i]);
		this.object = temp;
	},
	fire : function(source){
		for(var i in this.object)
			this.object[i](source);
	}
};

function logic(action, args, interval) { return this.constructor(action, args, interval); }
logic.prototype = {
    action: null,
    args: null,
    data: null,
    error: null,
    interval: null,
    last: new Date(),

    onUpdate: {},
    onError: {},
    onReset: {},

    constructor: function(action, args, interval) {
        this.action = action;
        this.args = args;
        this.interval = interval;

        this.onUpdate = new logicobservableData();
        this.onError = new logicobservableData();
        this.onReset = new logicobservableData();
    },
    exception:{
        common: function(key) {
            try {
                if( !jQuery.isNull(key) ) {
                    var exception = key.split('|'), message;
                    message = exceptionMessages[exception[0]];
                    for( var i = 1; i < exception.length; i++ ) {
                        message = message.replace('{'+(i-1)+'}', exception[i]); }
                    return message;
                } else {
                    return exceptionMessages["ERRx0000"];
                }
            } catch(e) {
                return exceptionMessages["ERRx0000"];
            }
        }
    },
    get: function() {
        var obj = {'action':this.action};
	    if(this.args) obj.args = typeof this.args == 'function' ? this.args() : this.args;
	    return obj;
    },
    set: function(result) {
        var obj = result[this.action];
	    if(typeof obj != 'undefined' && typeof obj.error == 'undefined'){
	        this.data = obj;
	        this.onUpdate.fire(this);
	    } else {
	        if (typeof obj == 'undefined' || typeof obj.error == 'undefined'){
	            this.error = {text : exceptionMessages["ERRx0001"], time : new Date()};
	        } else {
	            if ( typeof obj.error.key != 'undefined'){
                    obj.error.text = logic.prototype.exception.common(obj.error.key);
                    this.error = obj.error;
                }
	        }

            this.onError.fire(this);
	    }
    },
    reset: function(comet) {
        if(typeof comet == 'undefined'){
		    this.data = null;
		    InsiderunRequest(this);
		    this.onReset.fire(this);
	    } else {
		    this.data = comet;
		    this.onReset.fire(this);
		    this.onUpdate.fire(this);
	    }
    },
    run: function() { InsiderunRequest(this); },
    isExpired: function() {
        if(!this.interval) return false;
        var now = new Date();
        if(now - this.last > this.interval) {
            this.last = now;
            return true;
        }
        return false;
    }
};

var logicrequest = function() {
      var currentRequest = null;

        this.run = function() {
            currentRequest = logicnextRequest;
            logicnextRequest = [];

            // Temp
            if(InsideViewModuleModel.InsideView.isExpired())
              currentRequest.push(InsideViewModuleModel.InsideView);
              
            if(InsideViewModuleModel.InsideViewCurrentRate.isExpired())
              currentRequest.push(InsideViewModuleModel.InsideViewCurrentRate);
              
            // End

            var jsonArr = [];
            for(var i in currentRequest)
                  jsonArr.push(currentRequest[i].get());

            if(jsonArr.length > 0) {
              $.getJSON(logicremoteURL, { RQ:$.toJSON(jsonArr) } , callback);
            }
      }

      function callback(data) {
              for(var i in currentRequest)
                      currentRequest[i].set(data);
      }
}

var logicexceptionMessages = {
    ERRx0000 : 'Please Try Again.',
    ERRx0001 : 'Response does not exists.'
}

