
var modulControllers = new Object;

modulControllers.switchFilter = function(modul) {
	for(var m in this) {
		if(m=='STD' || m=='BOOK' || m=='EVT' || m=='GAST' || m=='SUPPL' || m=='SIGHT') {
			if(m!=modul) {
				this[m].switchFilter("off");
			}
		}
	}
	this[modul].switchFilter("on");
}

modulControllers.search = function() {
	var modul = null;
	for(var m in this) {
		if(m=='STD' || m=='BOOK' || m=='EVT' || m=='GAST' || m=='SUPPL' || m=='SIGHT') {
			if(this[m].opened) {
				modul = m;
				break;
			}
		}
	}
	if(modul!=null) {
		this[modul].search();
	}
}

modulControllers.moreSearch = function() {
	var modul = null;
	for(var m in this) {
		if(m=='STD' || m=='BOOK' || m=='EVT' || m=='GAST' || m=='SUPPL' || m=='SIGHT') {
			if(this[m].opened) {
				modul = m;
				break;
			}
		}
	}
	if(modul!=null) {
		this[modul].moreSearch();
	}
}

modulControllers.popup = function() {
	if($('tisSearchBoxPopup')) {
		$('tisSearchBoxPopup').show();
	}
}

modulControllers.popdown = function() {
	if($('tisSearchBoxPopup')) {
		$('tisSearchBoxPopup').hide();
	}
}

modulControllers.close = function() {
	modulControllers.popdown();
	setTimeout("$('tisSearchBoxPopupContainer').onmouseout = modulControllers.popdown;",100);
}

modulControllers.popfix = function() {
	if($('tisSearchBoxPopupContainer')) {
		$('tisSearchBoxPopupContainer').onmouseout = null;
	}
}


var scriptController = {
	links:		new Array,
	scripts:	new Array,
	groups:		new Object,
	callbacks:	new Object,
	locks:		new Object,

	reset: function() {
		this.scripts	= new Array;
		this.groups		= new Object;
		this.callbacks	= new Object;
		this.locks		= new Object;
	},

	registerCallback: function(callback,group) {
		this.locks[group] = false;
		this.callbacks[group] = callback;
	},

	registerScript: function(src,group) {
		// init srcs array
		if(!this.scripts) {
			this.scripts = new Array;
		}
		// if src not registered
		var i = this.scripts.indexOf(src);
		if(i==-1) {
			i = this.scripts.length;
			this.scripts.push(src);
		}
		if(!this.groups[group]) {
			this.groups[group] = new Object;
		}
		this.groups[group][src] = false;
	},

	registerLink: function(src,group) {
		// init srcs array
		if(!this.links) {
			this.links = new Array;
		}
		// if src not registered
		var i = this.links.indexOf(src);
		if(i==-1) {
			i = this.links.length;
			this.links.push(src);
		}
		if(!this.groups[group]) {
			this.groups[group] = new Object;
		}
		this.groups[group][src] = true;
	},

	run: function(group) {
		if(!group) {
			for(var i=0; i<this.scripts.length; i++) {
				this.load(this.scripts[i],"scriptController.process('"+this.scripts[i]+"')",'sc_'+i);
			}
			for(var i=0; i<this.links.length; i++) {
				this.loadLink(this.links[i],"scriptController.process('"+this.links[i]+"')",'li_'+i);
			}
		}
		else {
			for(var src in this.groups[group]) {
				if(!this.groups[group][src]) {
					var i = this.getScriptIndex(src);
					if(i>-1) {
						this.load(this.scripts[i],"scriptController.process('"+this.scripts[i]+"')",'sc_'+i);
					}
					else {
						i = this.getLinkIndex(src);
						if(i>-1) {
							this.loadLink(this.links[i],"scriptController.process('"+this.scripts[i]+"')",'li_'+i);
						}
					}
				}
			}
		}
	},

	getScriptIndex: function(src) {
		var j = -1;
		for(var i=0; i<this.scripts.length; i++) {
			if(this.scripts[i]==src) {
				j = i;
				break;
			}
		}
		return j;
	},

	getLinkIndex: function(src) {
		var j = -1;
		for(var i=0; i<this.links.length; i++) {
			if(this.links[i]==src) {
				j = i;
				break;
			}
		}
		return j;
	},

	load: function(src,callback,id,nocache) {
		if(src.substr(0,5)!="http:") {
			src = BASE_PATH + src;
		}
	
		var scriptObj = document.createElement( 'script' );
		scriptObj.setAttribute( 'type', 'text/javascript' );
		scriptObj.setAttribute( 'charset', 'utf-8' );
		scriptObj.setAttribute( 'id', id );
		scriptObj.setAttribute( 'onload', callback );
		// IE uglyhack.
		scriptObj.onreadystatechange = function() {
			if ( this.readyState == "complete" || this.readyState == "loaded" ) {
				eval( callback );
			}
		}
		if(nocache) {
			var rnd = Math.random()*9999999;
			src = src+'&r='+rnd;
		}
		scriptObj.setAttribute( 'src', src);
		if ( $(id) != null ) {
			$(id).replace( scriptObj );
		} else {
			document.body.appendChild( scriptObj );
		}
	},

	loadLink: function(src,callback,id,nocache) {
		if(src.substr(0,5)!="http:") {
			src = BASE_PATH + src;
		}
		var scriptObj = document.createElement( 'link' );
		scriptObj.setAttribute( 'type', 'text/css' );
		scriptObj.setAttribute( 'rel', 'stylesheet' );
		scriptObj.setAttribute( 'id', id );
		scriptObj.setAttribute( 'onload', callback );
		if(nocache) {
			var rnd = Math.random()*9999999;
			src = src+'&r='+rnd;
		}
		scriptObj.setAttribute( 'href', src);
		if ( $(id) != null ) {
			$(id).replace( scriptObj );
		} else {
			document.getElementsByTagName("head")[0].appendChild(scriptObj)
		}
	},

	process: function(src) {
		for(var grp in this.groups) {
			var all = true;
			for(var key in this.groups[grp]) {
				if(key==src) {
					this.groups[grp][src] = true;
				}
				if(!this.groups[grp][key]) {
					all = false;
				}
			}
			// if all script loaded in group
			if(all && !this.locks[grp]) {
				this.locks[grp] = true;
				setTimeout(this.callbacks[grp],0);
			}
		}
	},

	requestForm: function(f,callback) {

		var PostText = "";
		var amp = "";
		for(i=0; i<f.elements.length; i++){
			if(f.elements[i].type == "text" || f.elements[i].type == "textarea" || f.elements[i].type == "hidden") {
				PostText += amp+f.elements[i].name+"="+encodeURIComponent(f.elements[i].value);
			}
			else if (f.elements[i].type == "checkbox") {
				PostText += amp+f.elements[i].name+"="+(f.elements[i].checked ? '1' : 0);
			}
			else if (f.elements[i].type == "select-one") {
				PostText += amp+f.elements[i].name+"="+f.elements[i].options[f.elements[i].selectedIndex].value;
			}
			amp = "&";
		}
		PostText += amp+'__json__=1';
		this.load('/index.php?'+PostText,callback,'sc_response',true);
	}

};

var tisController = {

	data:		new Object,
	info:		new Object,
	locked:		false,

	start: function() {

		var js = /tiscontroller\.js(\?.*)?$/;
		$$('head script[src]').findAll(function(s) {
			return s.src.match(js);
		}).each(function(s) {
			var tplmatch = s.src.match(/\?.*tpl=([a-z_]*)/);
			if (tplmatch) tpl = tplmatch[1];
			else tpl = '';
		});
		this.locked = true;
		scriptController.load('/index.php?page=Layer_AjaxPage&tpl='+tpl,'tisController.autoLoadModules();','sc_tiscontroller',true);

		var redirect = this.getParameter('tiscontroller');
		if(redirect) {
			this.redirectResponse(redirect);
		}

	},

	autoLoadModules: function(cmd,params) {
		if($('tisSearchBoxPopupContainer')) {
			$('tisSearchBoxPopupContainer').onmouseover = modulControllers.popup;
			$('tisSearchBoxPopupContainer').onmouseout = modulControllers.popdown;
			$('tisSearchBoxPopupContainer').onclick = modulControllers.popfix;
		}
		if(this.info.modules) {
			scriptController.reset();
			for(var modul in this.info.modules) {
				this.info.modules[modul].element = $(this.info.modules[modul].div);
				if(this.info.modules[modul].element) {
					this.loadModul(modul,true);
				}
			}
			scriptController.run();
		}
		this.locked = false;
	},

	loadModul: function(modul,block) {

		if(this.locked&&!block) return false;
		this.locked = true;

		if(this.info.modules[modul].loaded) {
			setTimeout(this.info.modules[modul].callback,0);
		}
		else {
			if(!block) {
				scriptController.reset();
			}
			scriptController.registerCallback(this.info.modules[modul].callback,modul);
			for(var i=0;i<this.info.modules[modul].includes.length;i++) {
				scriptController.registerScript(this.info.modules[modul].includes[i],modul);
			}
			for(var i=0;i<this.info.modules[modul].links.length;i++) {
				scriptController.registerLink(this.info.modules[modul].links[i],modul);
			}
			this.info.modules[modul].loaded = true;
			if(!block) {
				scriptController.run();
				this.locked = false;
			}
		}
		return true;
	},

	getParameter: function(p) {
		var re = new RegExp('&'+p+'=([^&]*)','i');
		var c = window.location.search;
		return (c=c.replace(/^\?/,'&').match(re)) ?c=c[1] : c=null;
	},

	showMap: function() {
		loadModul('map');
	},

	showSuitcase: function() {
		loadModul('suitcase');
	},

	showSearch: function(partner) {
		loadModul('search_'+partner);
	},

	initResponseDiv: function() {
		if(!$('tisResponse')) {
			var overlay = document.createElement( 'div' );
			overlay.setAttribute( 'id', 'tisOverlay' );
			document.body.appendChild(overlay);
			var size = this.getPageSize();
			$('tisOverlay').setStyle({ height: size[1] + 'px' });

			var container = document.createElement( 'div' );
			container.setAttribute( 'id', 'tisContainer' );
			var response = document.createElement( 'div' );
			response.setAttribute( 'id', 'tisResponse' );
			container.appendChild(response);
			document.body.appendChild(container);
			this.updateSelect(true);
			new Effect.Appear($('tisOverlay'), { duration: 0, from: 0.0, to: 0.7 });
		}
		this.showLoader();
	},

	updateResponse: function(response) {
		$('tisResponse').innerHTML = response;
		this.updateLinks();
		var size = this.getPageSize();
		var divSize6 = $('tisContainer').clientHeight;
		var s = size[1];
		s = Math.max(s,divSize6);
		s = s + 40;
		$('tisOverlay').setStyle({ height: s + 'px' });
		scroll(0,0);
	},

	showLoader: function() {
		$('tisResponse').innerHTML = '<div id="loading"><a href="#" id="loadingLink"><img src="'+BASE_PATH+'/gallery/img/loader.gif" /></a></div>';
		scroll(0,0);
	},

	redirectResponse: function(href) {
		this.initResponseDiv();
		this.showLoader();
		href = href + '?__json__=1';
		scriptController.load(href,'tisController.updateResponse(__RESPONSE);','sc_response',true);
		return false;
	},

	updateLinks: function() {

		var links = $$('#tisResponse a');
		for(var i=0;i<links.length;i++) {
			if(!links[i].onclick && links[i].href.substr(0,7)!='mailto:') {
				links[i].onclick = function() {
					return tisController.redirectResponse(this.href);
				};
			}
		}
		var _forms = $$('#tisResponse form');
		for(var i=0;i<_forms.length;i++) {
			if(!_forms[i].onsubmit) {
				_forms[i].submit = function() {
					tisController.initResponseDiv();
					tisController.showLoader();
					scriptController.requestForm(this,"tisController.updateResponse(__RESPONSE);");
					return false;
				};
			}
		}

	},
	updateSelect: function(hide) {
		var select = $$('select');
		for (var j=0; j<select.length; j++){
			select[j].style.display = hide ? 'none' : '';
		}
	},


	close: function() {
		$('tisContainer').remove();
		$('tisOverlay').remove();
		this.updateSelect(false);
		scroll(0,0);
	},

	print: function() {
		printDiv('tisResponse',675);
	},

	getPageSize: function() {

		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight];

	}
};


document.observe('dom:loaded', function () { tisController.start(); });
