
function myScope(func,obj) {
	return function() {
		return func.apply(obj, arguments);
	};
};

var eventArray = function(source,name) {

	this.source = source;
	this.name = name;
	this.size = 0;
	this.events = new Array();

	this.add = function(callback,scope) {
		this.size++;
		this.events.push({func: callback, scope: scope});
		return true;
	}

	this.call = function(event) {
		var result = true
		var index = 0;
		while ((result) && (this.size > index)) { 
			$.extend(event,{target: this.source, name: this.name});
			result = this.events[index].func.apply(this.events[index].scope,arguments);
			index++;
		}
		return result;
	}

};

var log = false;
var logger = function() {

	this.last = '';
	this.span = false;
	this.count = 0;
	this.log = false; 
	this.make_log = false;

	this.init = function() {
		if ($('#log').length == 0 && this.make_log) {
			$('head').append('<link rel="stylesheet" type="text/css" href="/js/jquery.utils.css" media="screen" />');
			$('body').append('<ul id="log"></ul>');
			this.log = $('#log');
			//this.log.hide();
			this.log.dblclick(myScope(this.hide,this));
			$(document).keypress(myScope(this.show,this));
		}
	}

	this.show = function(e) {
		if (e.keyCode == 112) {
			this.log.toggle();
			return false;
		}
	}

	this.hide = function(e) {
		this.log.hide();
		return false;
	}

	this.error = function(message) {
		this.message(message,'error');
	}

	this.info = function(message) {
		this.message(message,'info');
	}

	this.obj = function(obj,message) {
		this.message('[' + obj.name + ']: '+message,'info');
	}

	this.message = function(message,type) {
		if (this.log) {
			if ((message == this.last) && (this.span)) {
				this.count += 1;
				this.span.html(this.count+'x');
			} else {
				this.count = 1;
				this.log.prepend('<li class="'+type+'">'+message+'<span></span></li>');
				this.span = $('li:first span',this.log);
			}
			this.last = message;
		}
	}

	this.init();
};

var log = false;
$(document).ready(function() {
	log = new logger();
});

$.fn.settings = function() {

	this.values = new Object;

	this.add = function(name,values) {
		this.values[name] = values;
	}

	this.get = function(name) {
		if (this.values[name]) {
			return this.values[name];
		} 
		return false;
	}
};

var settings = new $.fn['settings']();

$.fn.collection = function(name) {

	this.name = name;
	this.layers = new Array();

	this.add = function(layer) {
		this.layers.push(layer);
	}

	this.setOn = function(layer,name_on,name_off) {
		for (var i = 0; i < this.layers.length; i++) {
			if (this.layers[i] == layer) {
				this.layers[i][name_on]();
			} else {
				this.layers[i][name_off]();
			}
		}
	}
}

var layers = new $.fn['collection']('layers');


$.fn.load = function(name, callback) {
	if (!$.fn[name]) {
		log.info('Loading: js/jquery.'+name+'.js');
		$.getScript('/js/jquery.'+name+'.js', callback);
	}

	return false;
};

jQuery.fn.makers = function() {
	
	this.size = 0;
	this.names = new Object();
	this.ms = new Object();

	this.add = function(name,maker) {
		if (this.names[name] == true) {
			return 	maker.create();
		} 

		if (this.names[name] == false) {
			this.ms[name].push(maker);
			return false;
		}

		this.ms[name] = new Array();
		this.ms[name].push(maker);
		this.names[name] = false;

		return true;
	}

	this.call = function(name) {
		if (this.ms[name]) { 
			this.names[name] = true;
			var i = 0;
			while (maker = this.ms[name].pop()) {
				maker.create();
				i++;
			}
		}
	}
}

var makers = new $.fn['makers']();

jQuery.fn.maker = function(data,name,local_settings) {

	this.data = data;
	this.name = name;
	this.settings = local_settings;

	this.create = function() {
		this.data.each(myScope(this.creator,this));
	}

	this.make = function() {
		makers.call(this.name);
	}

	this.creator = function(i,v) {
		var id = $(v).attr('id');
		var s = this.settings;
		jQuery.extend(s, settings.get(id));
		log.info('Create obj: ('+i+')' +this.name);
		var newObj = new $.fn[this.name](v,i,s);
		return true;
	}

	makers.add(this.name,this);
}

$.fn.make = function(name,local_settings) {

	if (this.length == 0) { return false; }
	
	var maker = new $.fn['maker'](this,name,local_settings);

	if (!$.fn[name]) {
		$.fn[name] = true;
		log.info('Loading: js/jquery.'+name+'.js');
		$.getScript('/js/jquery.'+name+'.js',myScope(maker.make,maker));
	}
};

function make_calls(calls,local_settings,obj) {
	if (calls) {
		for (var i = 0; i < calls.length; i++) {
			log.message(calls[i].selector + ': '+$(calls[i].selector,obj).length);
			log.message(calls[i].name);
			$(calls[i].selector,obj).make(calls[i].name,local_settings); 
		}
	}
}


$(document).ready(function() {

	$('div.calendar').make('calendar');
	$('.graf').make('graf');
	$('.noteText').make('noteText');

	$('.datePick').make('datePick');

	$('select.sSelect').make('sSelectMake' , { call: [ ] } );
	$('.krajmesto').make('krajmesto');

	$('.activeInput').make('activeInput');

	$('div.calendarX').make('calendarX' , { call: [ ] } );
	$('div.time').make('time' , { call: [ ] } );


});
