
jQuery.fn.slots = function(obj,index,options) {

	this.settings = {
		box: false,
		actions: {}
	};

	this.obj = obj;
	this.slots = {};
	this.html = {};
	this.time = 0;
	this.itime = 0;
	this.mtime = 0;
	this.tickVar = 0;
	this.user = 0;
	this.interval = 2000;

	this.updating = false;

	if (options) {
		jQuery.extend(this.settings, options);
	}

	this.dataReady = function(data) {
		this.updating = false;
		this.data = data;
		if ((this.data.time - this.time) > 1) {
			this.itime = new Date().getTime();
			this.time = this.data.time;	
			this.tickVar = 0;
		}
		this.user = this.data._user;
		$('.chips strong').html(this.data.account);
		this.mtime = false;
		$.each(this.data.slots,myScope(this.updateSlot,this));
		this.mtime = this.mtime - this.time;
		this.setInterval();
	}

	this.updateSlot = function(id,value) {
		if (typeof this.slots[id] == 'object') {
			if (this.mtime == false) {
				this.mtime = value.finish; 
			} else {
				this.mtime = Math.min(this.mtime,value.finish);
			}
			this.slots[id].updateStatus(value);
		}
	}

	this.refreshSlot = function(id,value) {
		if (typeof this.slots[id] == 'object') {
			this.slots[id].refresh();
		}
	}

	this.getSec = function(time) {
		return time - this.time - this.tickVar;	
	}

	this.registerSlot = function(id,slot) {
		this.slots[id] = slot;
		return this;
	}

	this.tick = function() {
		var xtime = new Date().getTime();
		this.tickVar = Math.ceil((xtime - this.itime) / 1000);
		//$.each(this.slots,myScope(this.refreshSlot,this));
	}

	this.getUpdate = function() {
		if (this.updating) { return false; }
		this.updating = true;
		$.getJSON(this.url,myScope(this.dataReady,this));
	}

	this.currencyMini = function(value) {
		value = value * 1;
		if (value >= 100) {
			if (value%100 == 0) {
				return Math.floor(value / 100) + this.gs('Kc');
			}
			if (value%100 > 10) {
				return Math.floor(value / 100) +','+ (value%100) + this.gs('Kc');
			}
			return Math.floor(value / 100) +','+ this.pad(value%100,2) + this.gs('Kc');

		} 

		return value + this.gs('hal');
	}

	this.currency = function(value) {
		return Math.floor(value / 100) +','+ this.pad(value%100,2) + ' ' + this.settings.currency;
	}

	this.currencyValue = function(value) {
		return Math.floor(value / 100) +','+ this.pad(value%100,2);
	}

	this.gs = function(index) { 
		if (typeof this.settings.actions[index] != 'undefined') {
			return this.settings.actions[index];
		}
		return '???';
	}

	this.pad = function(number, length) {
		var str = '' + number;
		while (str.length < length) {
			str = '0' + str;
		}
		return str;
	}

	this.getTime = function(s) {
		var t_s = s%60;
		var t_s_r = Math.floor(s/60);
		var t_m = t_s_r % 60;
		var t_m_r = Math.floor(t_s_r/60);
		var t_h = t_m_r % 24;
		var t_h_r = Math.floor(t_m_r/24);
		var t_d = t_h_r;
		if (t_d == 0) {
			if ((t_h == 0) && (t_m == 0) && (t_s < 30)) {
				return this.pad(t_h,2)+':'+this.pad(t_m,2)+':<span class="alert">'+this.pad(t_s,2)+'</span>';
			} else {
				return this.pad(t_h,2)+':'+this.pad(t_m,2)+':'+this.pad(t_s,2);
			}
		} else {
			var den = '';
			if (t_d == 1) { den = this.gs('den'); }
			if ((t_d > 1) && (t_d < 5)) { den = this.gs('dny'); }
			if (t_d > 4) { den = this.gs('dnu'); }
			return t_d + den +' ' + this.pad(t_h,2)+':'+this.pad(t_m,2)+':'+this.pad(t_s,2);
		}
	}

	this.setInterval = function() {
		var new_interval = 2000;

		if (this.mtime > 15) { new_interval = 5000; }
		if (this.mtime > 60) { new_interval = 10000; }
		if (this.mtime > 600) { new_interval = 30000; }
		if (this.mtime > 3600) { new_interval = 20000; }
		if (this.mtime > 7200) { new_interval = 40000; }

		if (this.interval != new_interval) {
			this.interval = new_interval;
			clearInterval(this.timerStatus);
			this.timerStatus = setInterval(myScope(this.getUpdate,this),this.interval);
		}
	}

	this.init = function () {
		this.url = this.settings.url;
		this.urlData = this.settings.urlData;
		this.urlAction = this.settings.urlAction;
		this.urlGuest = this.settings.urlGuest;
		this.urlPrihazovac = this.settings.urlPrihazovac;
		this.html.slots = $('.slot',this.obj);
		if (this.url && this.urlData) {
			this.html.slots.make('slot',{ 'parent': this, 'urlData' : this.urlData, 'urlAction': this.urlAction, 'urlGuest': this.urlGuest, 'urlPrihazovac': this.urlPrihazovac, 'user': this.user });
			this.getUpdate();
		} else {
			//this.html.slots.remove();
			$(this.obj).append('<div id="closeSlots"></div>');
		}

		this.timer = setInterval(myScope(this.tick,this),100);
		this.timerStatus = setInterval(myScope(this.getUpdate,this),this.interval);

		return this;
	}

	return this.init();
};