function myScope(func,obj,args) {
	return function() {
		obj.args = args;
		return func.apply(obj,arguments);
	};
};

function byteconvert(bytes) {
	var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
	var e = Math.floor(Math.log(bytes)/Math.log(1024));
	if (bytes > 0) {
		return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
	}
	return bytes +" "+s[0];
}

function getXY(oElement,reference) {
	var iReturnValue = { 'x': 0, 'y': 0 };
	while ((oElement != null) && (oElement != reference)) {
		iReturnValue.y += oElement.offsetTop;
		iReturnValue.x += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;

}

function getY(oElement,reference) {
	var iReturnValue = 0;
	while( oElement != null) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function getX(oElement,reference) {
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetLeft;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
var myURL = function(url) {
	
	this.url = ''+url;

	this.params = {};

	this.initQuery = function (all, name, value) {
		if (name) {
			this.params[name] = value;
		}
		return this;
	}

	this.setQuery = function (name, value) {
		if (name) {
			this.params[name] = value;
		}
		return this;
	}

	this.getQueryValue = function (name) {
		return this.params[name];
	}

	this.init = function() {
		//log.info(this.url);
		var reg1 = /(https?:\/\/)?([a-zA-Z0-9_\-\.]+)?(:[0-9]+)?\/?([^\?#]*)?(\?([^#]*)?)?(#(.*))?/;
		var result = this.url.match(reg1);
		this.scheme = result[1];
		this.host = result[2];
		this.port = result[3];
		this.path = result[4];
		this.query = result[6];
		this.hash = result[8];

		if (this.query) {
			var reg2 = /(?:^|&)([^&=]*)=?([^&]*)/g;
			var result = this.query.match(reg2);

			this.query.replace(reg2, myScope(this.initQuery,this));
		}
	}

	this.getScheme = function() {
		return ((this.scheme)?(this.scheme):(''));
	}

	this.getHost = function() {
		return ((this.host)?(this.host):(''));
	}

	this.getPort = function() {
		return ((this.port)?(':'+this.port):(''));
	}

	this.getPortValue = function() {
		return ((this.port)?(this.port):(''));
	}

	this.getPath = function() {
		return ((this.path)?('/'+this.path):(''));
	}

	this.getPathValue = function() {
		return ((this.path)?(this.path):(''));
	}

	this.addToString = function(k,v) {
		if (v !== false) { 
			this.queryString = ((this.queryString)?(this.queryString+'&'):('?')) + k + '=' +v;
		}
	}

	this.getQuery = function() {
		this.queryString = false;
		$.each(this.params,myScope(this.addToString,this));
		return this.queryString;
	}

	this.getHash = function() {
		return ((this.hash)?('#'+this.hash):(''));
	}

	this.getHashValue = function() {
		return ((this.hash)?(this.hash):(''));
	}

	this.getURL = function() {
		return this.getScheme() + this.getHost() + this.getPort() + this.getPath() + this.getQuery() + this.getHash();
	}

	this.init();
}


var eventArray = function(source,name) {

	this.source = source;
	this.name = name;
	this.size = 0;
	this.events = new Array();

	this.add = function(callback) {
		this.size++;
		this.events.push(callback);
		return true;
	}

	this.call = function() {
		var result = true;
		var index = 0;
		while ((result) && (this.size > index)) { 
			result = this.events[index](arguments);
			index++;
		}
		return result;
	}

	return this;
};

var log = false;
var logger = function() {

	this.last = '';
	this.span = false;
	this.count = 0;
	this.log = false; 
	this.make_log = false;
	//this.make_log = true;

	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').hide();
			//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;
		}
		*/
		return true;
	}

	this.hide = function(e) {
		this.log.hide();
		return false;
	}

	this.error = function(message) {
		this.message(message,'error');
	}

	this.infoObjectItem = function(name,value) {
		//log.info('+'+name + ':' + value);
	}

	this.infoObject = function(obj) {
		//log.info('Object:');
		$.each(obj,myScope(this.infoObjectItem,this));
		return true;
	}

	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) {
			this.log.show();
			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+' noPrint">'+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.openers = function() {

	this.windows = new Array();

	this.open = function(obj,url,w,h,s) {
		var newWindow = window.open(url, s+"_jswin", "toolbar=no,width="+w+",height="+h+",scrollbars=yes,resizable=yes");
		this.windows.push({ 'caller': obj, 'window': newWindow });
		return newWindow;
	}

	this.call = function(w,func,data) {
		for (var i = 0; i < this.windows.length; i++) {
			if (this.windows[i]['window'] == w) {
				return this.windows[i]['caller'][func](data);
			}
		}
		return false;
		
	}

	this.close = function(obj) {
		for (var i = 0; i < this.windows.length; i++) {
			if (this.windows[i]['caller'] == obj) {
				this.windows[i]['window'].close();
				this.windows.splice(i,1);
				return true;
			}
		}
		return false;
		
	}
}

var openers = new $.fn['openers']();

$.fn.collection = function(name) {

	this.name = name;
	this.layers = new Array();

	this.add = function(layer) {
		this.layers.push(layer);
	}

	this.getValue = function(func) {
		for (var i = 0; i < this.layers.length; i++) {
			if (this.layers[i][func]()) {
				return true;
			}
		}
		return false;
	}

	this.setOnlyMe = function(layer,name_on,name_off) {
		var result = false;
		for (var i = 0; i < this.layers.length; i++) {
			if (this.layers[i] == layer) {
				result = this.layers[i][name_on]();
			} else {
				this.layers[i][name_off]();
			}
		}
		return result;
	}

	this.set = function(name,args) {
		for (var i = 0; i < this.layers.length; i++) {
			this.layers[i][name](args);
		}
	}
}

var changeItems = new $.fn['collection']('changeItems');

$.fn.load = function(name, callback) {
	if (!$.fn[name]) {
	}

	return false;
};

var loaderJS = function(name,makers) {
	this.name = name;
	this.makers = makers;
	this.done = false;
	this.data = new Array();
	this.index = 0;
	this.item = false;

	this.loadDone = function() {
		this.done = true;
		this.call();
	};

	this.add = function(data) {
		this.data.push(data);
		if (this.done) {
			this.call();
		}
	}

	this.init = function() {
		//log.info('Loading: js/jquery.'+name+'.js');
		if (typeof $.fn[name] == 'undefined') {
			$.getScript('/js/jquery.'+name+'.js', myScope(this.loadDone,this));
		} else {
			this.loadDone();
		}
	};

	this.call = function() {
		var item = false;
		while (item = this.data.pop()) {
			this.initItem(this.index,item);
			this.index++;
		}
	}

	this.initItem = function(index,item) {
		var id = $(item.object).attr('id');
		if (typeof item.settings == 'undefined') {
			item.settings = {};
		}
		jQuery.extend(item.settings, settings.get(id));

		if ($.fn[name]) {
			//log.info('Create obj: ('+index+')' +name );
			var newObj = new $.fn[name](item.object,index,item.settings);
			if (typeof item.callback == 'function') {
				item.callback(newObj);
			}
			return true;
		}
		return false;
	}
	this.init();

	return this;
}

var makersJS = function() {
	
	this.names = new Array();

	this.add = function(name,data) {
		if (typeof this.names[name] == 'undefined') {
			var load = new loaderJS(name,this);
			this.names[name] = load;
		}
		this.names[name].add(data);
	}

	return this;
}

var makers = new makersJS();

$.fn.make = function(name,settings,callback) {

	if (this.length == 0) { return false; }
	$.each(this,function(index,object) { makers.add(name, { 'object': object, 'settings': settings, 'callback': callback } ); });
	
	return this;
};

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); 
		}
	}
}

jQuery.fn.swap = function() {

	var a = this[0];
	var b = this[1];


	if (a && b) {
		var parentNode = b.parentNode;

		if (a.nextSibling == b) {
			parentNode.replaceChild(a,b);
			parentNode.insertBefore(b,a);
		} else {
			parentNode.replaceChild(b,a);
			parentNode.insertBefore(a,b);
		}
	}
 
	return this;
};

function initData(context) {
  $('.focusToggle').make('searchInput');
  $('.contentBox').make('mytabs2');
  $('div.time').make('time');
  $('ul.grantCategory li').make('imgHigh');
  $('.printMe').make('myPrint');
  $('.saveMe').make('saveMe');
  $('div.calendar').make('kalendar');
  $('#poradnaForm').make('poradenstvi');
  $('#catalogNace').make('collapse');
  $('div#footSlider').make('browseIcons');
  $('body').make('activateTab');
  $('.contentBox').make('linkAlter');
}

$(document).ready(function() {
	initData(document);
});

 /* 
$(document).ready(function() {
  
  $('div.icon-browser').make('browseIcons', {});
  
  
  $('.table-hl').make('rowHl');
  
 // $('.gallery a').lightBox({fixedNavigation:true});
  
  $('.articleRatingBox').make('rating');
  
  $('.printMe').make('printMe');
  
  $('.mailForm').make('mailForm');
  
  $('.scrollAble').make('scrollMe');
  
  $('.pollVote').make('poll');
});
  */
