/*
 * based on 
 * Smoothbox v20080623 by Boris Popoff (http://gueschla.com)
 * To be used with mootools 1.2 or 1.1
 */
var cmsso_base_url = 'http://users.clubmetropol.hu/';

// check if mootools is present
if (typeof(MooTools)=='undefined') {
	document.write('<'+'scr'+'ipt ty'+'pe="tex'+'t/jav'+'ascript" src="' + cmsso_base_url + 'cmsso_with_moo.php"><'+'/s'+'cri'+'pt>');
} else {
	
	// prevent javascript error before the content has loaded
	TB_WIDTH = 0;
	TB_HEIGHT = 0;
	var TB_doneOnce = 0;
	
	// add smoothbox to href elements that have a class of .smoothbox
	function TB_init(){
		if (typeof(TB_inited)=='undefined') {
			$$("a.smoothbox").each(function(el){
				el.onclick = TB_bind
			});
		}
		TB_Inited = true;
	}
	
	function TB_bind(event){
		var event = new Event(event);
		// stop default behaviour
		event.preventDefault();
		// remove click border
		this.blur();
		// get caption: either title or name attribute
		var caption = this.title || this.name || "";
		// get rel attribute for image groups
		var group = this.rel || false;
		// display the box for the elements href
		TB_show(caption, this.href, group);
		this.onclick = TB_bind;
		return false;
	}
	
	// called when the user clicks on a smoothbox link
	function TB_show(caption, url, rel){
	
		// create iframe, overlay and box if non-existent
		
		if (!$("TB_overlay")) {
			new Element('iframe').setProperty('id', 'TB_HideSelect').injectInside(document.body);
			$('TB_HideSelect').setOpacity(0);
			new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
			$('TB_overlay').setOpacity(0);
			TB_overlaySize();
			new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
			$('TB_load').innerHTML = "<img src='" + cmsso_base_url + "loading.gif' />";
			TB_load_position();
			
			if (('TB_overlay').tween) {
				$('TB_overlay').set('tween', {
					duration: 400
				});
				$('TB_overlay').tween('opacity', 0, 0.6);
			} else if (Fx.Style) {
				new Fx.Style('TB_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
			} else {
				$('TB_overlay').setStyle('opacity',0.6);
			}
		}
		
		if (!$("TB_load")) {
			new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
			$('TB_load').innerHTML = "<img src='" + cmsso_base_url + "loading.gif' />";
			TB_load_position();
		}
		
		if (!$("TB_window")) {
			new Element('div').setProperty('id', 'TB_window').injectInside(document.body);
			$('TB_window').setOpacity(0);
		}
		
		$("TB_overlay").onclick = TB_remove;
		//	window.onscroll = TB_position;
		
		// check if a query string is involved
		var baseURL = url.match(/(.+)?/)[1] || url;
		
		//code to show html pages
		var queryString = url.match(/\?(.+)/)[1];
		var params = TB_parseQuery(queryString);
		
		TB_WIDTH = (params['width'] * 1) + 30;
		TB_HEIGHT = (params['height'] * 1) + 40;
		
		var ajaxContentW = TB_WIDTH - 30, ajaxContentH = TB_HEIGHT - 45;
		
		urlNoQuery = url.split('TB_');
		$("TB_window").innerHTML += "<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + caption + "</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>Bezárás</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' onload='TB_showWindow()'> </iframe>";
		
		$("TB_closeWindowButton").onclick = TB_remove;
		
		TB_position();
		if (frames['TB_iframeContent'] == undefined) {//be nice to safari
			$(document).keyup(function(e){
				var key = e.keyCode;
				if (key == 27) {
					TB_remove()
				}
			});
			TB_showWindow();
		}
		
		window.onresize = function(){
			TB_position();
			TB_load_position();
			TB_overlaySize();
		}
		
		document.onkeyup = function(event){
			var event = new Event(event);
			if (event.code == 27) { // close
				TB_remove();
			}
		}
	}
	
	//helper functions below
	
	function TB_showWindow(){
		//$("TB_load").dispose();
		//$("TB_window").setStyles({display:"block",opacity:'0'});
		
		if (TB_doneOnce == 0) {
			TB_doneOnce = 1;
			
			if ($('TB_window').tween) {
				$('TB_window').set('tween', {
					duration: 250,
					onComplete: function(){
						if ($('TB_load')) {
							if (MooTools.version>='1.2') {
								$('TB_load').dispose();
							} else {
								$('TB_load').remove();
							}
						}
					}
				});
				$('TB_window').tween('opacity', 0, 1);
			} else {
				var myFX = new Fx.Style('TB_window', 'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){if ($('TB_load')) { $('TB_load').remove();}} }).start(0,1);
			}
		}
		else {
			$('TB_window').setStyle('opacity', 1);
			if ($('TB_load')) {
				if (MooTools.version>='1.2') {
					$('TB_load').dispose();
				} else {
					$('TB_load').remove();
				}
			}
		}
	}
	
	function TB_remove(){
		$("TB_overlay").onclick = null;
		document.onkeyup = null;
		document.onkeydown = null;
		
		if ($('TB_imageOff')) 
			$("TB_imageOff").onclick = null;
		if ($('TB_closeWindowButton')) 
			$("TB_closeWindowButton").onclick = null;
		if ($('TB_prev')) {
			$("TB_prev").onclick = null;
		}
		if ($('TB_next')) {
			$("TB_next").onclick = null;
		}
		
		if ($('TB_window').tween) {
			$('TB_window').set('tween', {
				duration: 250,
				onComplete: function(){
					$('TB_window').dispose();
				}
			});
			$('TB_window').tween('opacity', 1, 0);
			
			
			
			$('TB_overlay').set('tween', {
				duration: 400,
				onComplete: function(){
					$('TB_overlay').dispose();
				}
			});
			$('TB_overlay').tween('opacity', 0.6, 0);
		} else {
			new Fx.Style('TB_window', 'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){$('TB_window').remove();} }).start(1,0);
			new Fx.Style('TB_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut, onComplete:function(){$('TB_overlay').remove();} }).start(0.6,0);
		}
		
		window.onscroll = null;
		window.onresize = null;
		
		if (MooTools.version>='1.2') {
			$('TB_HideSelect').dispose();
		} else {
			$('TB_HideSelect').remove();
		}
		TB_init();
		TB_doneOnce = 0;
		return false;
	}
	
	function TB_position(){
		top_pos = (window.getScrollTop() + (window.getHeight() - TB_HEIGHT) / 2);
		if (top_pos < 0) top_pos = 0;
	
		if ($('TB_window').morph) {
			$('TB_window').set('morph', {
				duration: 75
			});
			$('TB_window').morph({
				width: TB_WIDTH + 'px',
				left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH) / 2) + 'px',
				top: top_pos + 'px'
			});
		} else {
			$('TB_window').setStyles({width: TB_WIDTH+'px', 
						 left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH)/2)+'px',
						 top: top_pos+'px'});
		}
	}
	
	function TB_overlaySize(){
		// we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
		$("TB_overlay").setStyles({
			"height": '0px',
			"width": '0px'
		});
		$("TB_HideSelect").setStyles({
			"height": '0px',
			"width": '0px'
		});
		$("TB_overlay").setStyles({
			"height": window.getScrollHeight() + 'px',
			"width": window.getScrollWidth() + 'px'
		});
		$("TB_HideSelect").setStyles({
			"height": window.getScrollHeight() + 'px',
			"width": window.getScrollWidth() + 'px'
		});
	}
	
	function TB_load_position(){
		if ($("TB_load")) {
			$("TB_load").setStyles({
				left: (window.getScrollLeft() + (window.getWidth() - 56) / 2) + 'px',
				top: (window.getScrollTop() + ((window.getHeight() - 20) / 2)) + 'px',
				display: "block"
			});
		}
	}
	
	function TB_parseQuery(query){
		// return empty object
		if (!query) 
			return {};
		var params = {};
		
		// parse query
		var pairs = query.split(/[;&]/);
		for (var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			if (!pair || pair.length != 2) 
				continue;
			// unescape both key and value, replace "+" with spaces in value
			params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
		}
		return params;
	}
	
	
	function loadCSSFile(filename) {
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
		if (typeof fileref!="undefined")
			document.getElementsByTagName("head")[0].appendChild(fileref);
	}
	
	loadCSSFile(cmsso_base_url + 'smoothbox.css');
	
	document.write('<ifr'+'ame src="'+cmsso_base_url+'index.php" width="0" height="0" style="visibility:hidden;"></iframe>');

	// on page load call TB_init
	window.addEvent('domready', function() {
		TB_init();
	});
}
