﻿//include common.js
//document.write("<script src=\"" + "common.js" + "\" type=\"text/javascript\"></script>");

this.ascent = this.ascent || {};
this.ascent.cms = this.ascent.cms || {};
this.ascent.cms.library = this.ascent.cms.library || {};

if (!this.ascent.cms.library.Quota) {
    this.ascent.cms.library.Quota =
	function (quota) {
	    var minQuotaState;
	    if (!quota) {
	        // если передается пустая квота, то это запрос
	        minQuotaState = 0;
	    }
	    else {
	        switch (quota.constructor) {
	            case Array:
	            case String:
	                // квота передана, как строка в формате \d:\d ( | \d:\d )? 
	                // или квота передана, как массив таких строк
	                var quotaArray = quota.constructor == String ? [quota] : quota; ;

	                var quotaStateArray = new Array();
	                var minQuotaState = this.getQuotaState(quotaArray[0]);
	                for (var i = 1; i < quotaArray.length; i++) {
	                    var quotaState = this.getQuotaState(quotaArray[i]);
	                    minQuotaState = (quotaState < minQuotaState) ? quotaState : minQuotaState;
	                }
	                break;

	            case Number:
	                // квота передана, как индекс массива Quota.charterQuotas
	                minQuotaState = this.charterQuotas[quota];
	                break;
	        }
	    }

	    switch (minQuotaState) {
	        case -1:
	            this.code = -1;
	            this.cssClass = 'stop-quota';
	            this.img = '/images/icons/plane-icon-down.png';
	            break;
	        case 0:
	            this.code = 0;
	            this.cssClass = 'request-quota';
	            this.img = '/images/icons/plane-icon-up.png';
	            break;
	        case 1:
	            this.code = 1;
	            this.cssClass = 'available-quota';
	            this.img = '/images/icons/plane-icon-up.png';
	            break;
	    }
	};


	// --- Quota Class -----------------------------
	this.ascent.cms.library.Quota.prototype.getQuotaState = function (/*String*/quota) {
	    if (quota == null) {
	        // если передается пустая квота, то это запрос
	        return 0;
	    }
	    else {
	        var quotaArray = quota.split('|');
	        var quotaState = new Array();
	        for (var i = 0; i < quotaArray.length; i++) {
	            var quotaValue = parseInt(quotaArray[i].split(':')[0], 10);
	            if (quotaValue == 0) {
	                // stop
	                quotaState[i] = -1;
	            }
	            if (quotaValue < 0) {
	                // request
	                quotaState[i] = 0;
	            }
	            if (quotaValue > 0) {
	                // request
	                quotaState[i] = 1;
	            }
	        }
	        // get max of the state array
	        var maxValue = quotaState[0];
	        for (var i = 1; i < quotaState.length; i++) {
	            maxValue = (quotaState[i] > maxValue) ? quotaState[i] : maxValue;
	        }
	        return maxValue;
	    }
	};

	this.ascent.cms.library.Quota.prototype.isStopped = function () {
	    return this.code == -1;
	};

	this.ascent.cms.library.Quota.prototype.charterQuotas = [
	    -1, // None - 0
	    1, // Yes - 1
	    -1, // No - 2
	    1, // AFew - 3
	    0, // Request - 4
	    -1, // NoFlights - 5
	    0, // OnlyAgent - 6
	    1, // All - 7
    ];


}
    


