var cUniverse = Class.create({
	initialize: function() {
		this.idPool = 0;
		this.active = null;
		this.views = new Array();
		this.keresek = new Array();
		this.activeAlert = 0;
		Event.observe(window, 'load', this.onReady.bind(this));
	},
	id: function(prefix){
        prefix = prefix || "";
        return prefix + (++this.idPool);
	},
	addView: function(view) {
		this.views.push(view);
		if (this.active == null) {
			this.activateViewByName(view.name);
		}
	},
	activeView: function() {
		return this.active;
	},
	activateViewByName: function(melyik) {
		if (this.active != null && this.active.name == melyik && !menuer.isActive()) {
			return;
		}
		var i=0;
		for (i=0; i<this.views.length; i++) {
			if (this.views[i].name == melyik) {
				if (this.active != null) this.active.deactivateView();
				menuer.hideAllTartalom();
				this.active = this.views[i];
				this.active.activateView();
				break;
			}
		}
	},
	getViewByName: function(melyik) {
		var i=0;
		for (i=0; i<this.views.length; i++) {
			if (this.views[i].name == melyik) {
				return this.views[i];
			}
		}
		return null;
	},
	deactivateAll: function() {
		if (this.active == null) return;
		this.active.deactivateView();
	},
	hasRequest: function(tipus) {
		for (ir=0; ir<this.keresek.length; ir++) {
			if (this.keresek[ir][0] == tipus) {
				return true;
			}
		}
		return false;
	},
	killRequest: function(tipus) {
		for (ir=0; ir<this.keresek.length; ir++) {
			if (this.keresek[ir][0] == tipus) {
				this.keresek[ir][1].transport.abort();
				this.keresek.splice(ir, 1);
			}
		}
	},
	addRequest: function(tipus, keres, unique, name) {
		// TODO: egyes nezeteke kinyirtak egymast
		//if (unique) this.killRequest(tipus);
		this.keresek[this.keresek.length] = new Array(tipus, keres, name);

		this.showIndicator(name);

		//progress1.add(1);
	},
	showIndicator: function(name) {
		if (this.activeAlert)
			return;

		var pi = $('userbarContainer');
		pi.innerHTML = '<p style="color: white; font-weight: bold; font-size: 20px;">Töltés... </p>';
		//$('userbarContainer').show();
		pi.show();
	},
	hideIndicator: function(name) {
		if (this.activeAlert > 0)
			return;
		$('userbarContainer').hide();
		//$('userbar').hide();
	},
	showDebugger: function(szoveg) {
		if (document.location.href.indexOf('debug') > -1) {
			userer.showEgyeb(szoveg);
		}
	},
	userAlert: function(uzenet, pri, time, type) {
		this.userAlertHide();
		if (!pri && this.activeAlert > 0)
			return;

		this.activeAlert = pri || 1;
		// header
		var pi = $('userbarContainer');
		var background = 'green';
		var color = 'white';
		if (type) {
			switch (type) {
				case 'success':
					background = 'green';
					color = 'white';
					break;
				case 'faild':
					background = 'red';
					color = 'white';
					break;
				case 'message':
				default:
					background = '#092e50';
					color = 'white';
					break;
			}
		}
		pi.innerHTML = '<p style="color: ' + color + '; font-weight: bold; font-size: 20px;">'+uzenet+'</p>';
		pi.style.backgroundColor = background;
		//$('userbarContainer').show();
		pi.show();
		//blabla.alert()
		var time = time || 2000;
		window.setTimeout(universe.userAlertHide.bind(universe), time);
	},
	userAlertHide: function() {
		this.activeAlert = 0;
		//new Effect.Move($('message'), { x: 0, y: 100 });
		$('userbarContainer').hide();
		//$('userbar').hide();
		/*$('userbarfooter').hide();*/
	},
	userConfirmShow: function() {
		return;
	},
	showDataSheet: function(type, azon) {
		var caller = '';
		if (this.active) caller = this.active.name;
		this.getViewByName('datasheet').showDataSheet(type, azon, caller);
	},
	/* link example datasheet-movie-856-recoms */
	bindLinks: function(items, fn, scope, link, param) {
		if (!Object.isArray(items)) return;
		for (var i=0; i<items.length; i++) {
			this.bindLink(items[i], fn, scope, link, param);
		}
	},
	bindLink: function(item, fn, scope, link, param) {
		if (item) {
			item.href = "#" + link;
			item.observe('click', function() {
				historical.add(link);
			});
			item.observe('click', fn.bind(scope, param));
		}
	},
	bindDatasheet: function(item, type, id) {
		if (item) {
			item.observe('click', function() {
				historical.add(type + '-' + id);
			});
			item.href = "#" + type + '-' + id;
			item.observe('click', this.showDataSheet.bind(this, type, id));
		}
	},
	bindDatasheets: function(items, type, id) {
		if (!Object.isArray(items)) return;
		for (var i=0; i<items.length; i++) {
			if (items[i]) {
				this.bindDatasheet(items[i], type, id);
			}
		}
	},
	bindEntities: function(items) {
		if (!Object.isArray(items)) return;
		for (var i=0; i<items.length; i++) {
			if (items[i]) {
				var id = this.getFilmxId(items[i]);
				if (items[i].hasClassName('assessorDatasheetLink')) {
					this.bindDatasheet(items[i], 'ppl', id);
				} else if (items[i].hasClassName('movieDatasheetLink')) {
					this.bindDatasheet(items[i], 'movie', id);
				} else if (items[i].hasClassName('creativeDatasheetLink')) {
					this.bindDatasheet(items[i], 'creative', id);
				}
			}
		}
	},
	getElementIndex: function(element, key) {
		var pos = element.className.indexOf(key);
		if (pos == -1) return null;
		var end = element.className.indexOf(' ', pos);
		if (end == -1) end = element.className.length;
		return element.className.substr(pos + key.length, end - (pos + key.length));
	},
	getFilmxId: function(element) {
		return this.getElementIndex(element, 'filmxid');
	},
	processLinking: function(params) {
		if (params.length > 0) {
			if (params[0] == 'datasheet') {
				params.shift();
				this.getViewByName('datasheet').processLinking(params);
			}
			// datasheet sortcuts
			else if (params[0] == 'movie') {
				this.getViewByName('datasheet').processLinking(params);
			}
			else if (params[0] == 'creative') {
				this.getViewByName('datasheet').processLinking(params);
			}
			else if (params[0] == 'ppl') {
				this.getViewByName('datasheet').processLinking(params);
			}
			else if (params[0] == 'search') {
				params.shift();
				universe.activateViewByName('kereso');
				menuer.changeIt('szabad');
			}
			else if (params[0] == 'movies') { /*browser*/
				params.shift();
				universe.activateViewByName('valaszto');
				menuer.changeIt('kategoria');
			}
			else if (params[0] == 'series') {
				universe.activateViewByName('sorozatvalaszto');
				menuer.changeIt('sorozatok');
			}
			else if (params[0] == 'conrec') {
				universe.activateViewByName('conversation');
				menuer.changeIt('kerdezes');
			}
			else if (params[0] == 'community') {
				universe.activateViewByName('emberek');
				menuer.changeIt('ember');
			}
			else if (params[0] == 'rss') {
				universe.activateViewByName('rss');
				menuer.changeIt('hirek');
			}
			else if (params[0] == 'adverts') {
				universe.activateViewByName('assessoradvert');
				menuer.changeIt('reklamok');
//				menuer.showTartalom('ads');
			}
			else if (params[0] == 'trailertv') {
				universe.activateViewByName('trailertv');
				window.scrollTo(0, 0);
			}
		}
	},
	initLinking: function(link) {
		if (link) {
			var params = link.split('-');
			if (params.length > 0) {
				this.processLinking(params);
			} else {
				this.activateViewByName('kereso');
				menuer.changeIt('szabad');
			}
		}/* else {
			this.activateViewByName('kereso');
			menuer.changeIt('szabad');
		}*/
		return false;
	},
	onReady: function() {
		var a = document.location.href.indexOf('#');
		if (a > -1) {
			var b = document.location.href.substring(a+1, document.location.href.length);
			this.initLinking(b);
		}
		Event.observe(window.document, 'click', function() {
			tooltipper.mouseOut();
			return false;
		});
		this.bindLinks($$('.menu_search_link'), this.initLinking, this, 'search', 'search');
		this.bindLinks($$('.menu_browser_link'), function() {
			this.initLinking('movies');/*browser*/
			Select_show( 'mufaj1_options_spec', 'b_mufaj_select' );
		}, this, 'movies', 'movies');/*browser, browser*/
		this.bindLinks($$('.menu_series_link'), function() {
			this.initLinking('series');
			Select_show( 's_mufaj1_options_spec', 's_mufaj_select' );
		}, this, 'series', 'series');
		this.bindLinks($$('.menu_convrecom_link'), this.initLinking, this, 'conrec', 'conrec');
		this.bindLinks($$('.menu_community_link'), this.initLinking, this, 'community', 'community');
		this.bindLinks($$('.menu_rss_link'), this.initLinking, this, 'rss', 'rss');
		this.bindLinks($$('.menu_adverts_link'), this.initLinking, this, 'adverts', 'adverts');
		this.bindLinks($$('.menu_trailertv_link'), this.initLinking, this, 'trailertv', 'trailertv');
		historical.on('change', this.initLinking, this);
	}
});
