/*
 * Popups
 * @version: $Id: popup.js 659 2010-12-16 00:11:55Z jberzak $
 */

var popup = (function(){

	//Sprache
	var _i18n = 'de';

	//Breite des Popups
	var _width = 687;

	//Breite des Inhalts-Wrappers
	var _wrWidth = 640;

	//Abstand des Popups oben
	var _tpMargin = 40;

	//Höhe der Kopfgraphik (mit Schatten)
	var _hdHeight = 69;

	//Höhe der Fussgraphik (nur Schatten)
	var _ftHeight = 40;

	//Höhe der Knopfleiste unten (mit Schatten)
	var _btHeight = 61;

	//Overlay Element (halbtransparent)
	var _overlay;

	//Objekte und Maße
	var _win = $nolte(window);
	var _doc = $nolte(document);
	var _body = $nolte('body');
	var _view = [_win.width(),_win.height()];
	var _all = [_doc.width(),_doc.height()];

	//Overlay hinzufügen
	var _addOverlay = function() {
		_overlay = $nolte('<div>').attr('id','popup_overlay').css({
			'height': _all[1],
			'width': _all[0]
		});
		_body.append(_overlay);
		_body.append($nolte('<div>').attr('id','popup_wait'));
	};

	//Popup öffnen
	var _addPopup = function(html,args) {
		//Gesamthöhe des Popups
		var bHeight;
		/*
		 * Im statischen Modus müssen wir die Höhe des Inhalts vorberechnen, indem wir
		 * ein "unsichtbares" Element mit dem Inhalt erzeugen und diese vermessen.
		 */
		if(args.mode == 'static') {
			var tmpEl = $nolte('<div>').attr('id','popup_tmp').width(_wrWidth).html(html);
			_body.append(tmpEl);
			bHeight = tmpEl.height()+_hdHeight+_btHeight;
			tmpEl.remove();
			/*
			 * Falls das statische Fesnter höher ist als das halbtransparante Overlay,
			 * muss das Overlay so hoch werden wie das Popup + 2mal der Abstand zum
			 * Fensterrand
			 */
			var scrHeight = (bHeight+(2*_tpMargin));
			if(_overlay.height() < scrHeight) _overlay.height(scrHeight);
		/*
		 * Im dynamischen Modus wird das Popup so hoch, das es exakt mit den Abständen oben und
		 * unten in das Fenster passt.
		 */
		} else { bHeight = (_view[1]-(2*_tpMargin)); };

		var bEl = $nolte('<div>').attr('id','popup').css({
			'height': bHeight,
			'left': ((_view[0]/2)-(_width/2)),
			'top': _tpMargin,
			'width': _width
		});

		bEl.append($nolte('<div>').attr('id','popup_head'));
		bEl.append($nolte('<div>').attr('id','popup_fill').css({
			'height': (bHeight-_hdHeight-_ftHeight),
			'top': _hdHeight
		}));
		bEl.append($nolte('<div>').attr('id','popup_footer'));
		bEl.append($nolte('<div>').attr('id','popup_content').css('height',bHeight).html(html));
		var clFunc = function(e) {
			e.preventDefault();
			_removePopup();
			_removeOverlay();
		};
		bEl.append($nolte('<div>').attr('id','popup_close').append(
			$nolte('<a>').attr('href','#').text('X').bind('click',clFunc)
		));
		var btnEl = $nolte('<div>').attr('id','popup_buttons');
		btnEl.append($nolte('<div>').attr('id','popup_buttons_close').append(
			$nolte('<a>').attr('href','#').text('X').bind('click',clFunc)
		));
		var aLft = $nolte('<a>').attr('href','#').text('X');
		if(args.prvSl) {
			aLft.bind('click',function(e) {
				clFunc(e);
				_getI18nContent(_getFile(args.prvSl),_addOverlay,_addPopup,function(obj,page) {
					_removeOverlay();
					_redirect(page);
				},_getArgs(args.prvSl));
			});
		} else {
			aLft.addClass('inactive').bind('click',function(e) {
				e.preventDefault();
			});
		};
		btnEl.append($nolte('<div>').attr('id','popup_buttons_left').append(aLft));
		var aRgt = $nolte('<a>').attr('href','#').text('X');
		if(args.nxtSl) {
			aRgt.bind('click',function(e) {
				clFunc(e);
				_getI18nContent(_getFile(args.nxtSl),_addOverlay,_addPopup,function(obj,page) {
					_removeOverlay();
					_redirect(page);
				},_getArgs(args.nxtSl));
			});
		} else {
			aRgt.addClass('inactive').bind('click',function(e) {
				e.preventDefault();
			});
		};
		btnEl.append($nolte('<div>').attr('id','popup_buttons_right').append(aRgt));
		bEl.append(btnEl);

		_body.append(bEl);
	};

	var _getArgs = function(el) {
		var args = {
			'mode': 'static',
			'prvSl': false,
			'nxtSl': false,
			//Die beiden folgenden Werte sind bisher nicht implementiert
			'width': 0,
			'height': 0
		};

		var tmp = $nolte(el).attr('rel').split(',');

		if(tmp[1]) args.mode = tmp[1];
		if(tmp[2]) {
			var els = $nolte('*[rel^=popup,'+args.mode+','+tmp[2]+']');
			for(var i=0;i<els.length;i++) {
				if(els[i] == el) {
					if(i>0) args.prvSl = els[i-1];
					if(i<(els.length-1)) args.nxtSl = els[i+1];
					break;
				};
			};
		};
		if(tmp[3]) args.width = tmp[3];
		if(tmp[4]) args.height = tmp[4];

		return args;
	};

	var _getFile = function(el) {
		var parts = el.href.split('/');
		var index = parts.length-1;
		if(parts[index] == '') index--; //Falls ein Slash am Ende enthalten war
		return parts[index];
	};

	//Gibt die Sprache zurück
	var _getI18n = function() {
		return _i18n;
	};

	//Holt den Content per AJAX mit Callbacks
	var _getI18nContent = function(page,wait,success,error,args) {
		wait(args);
		$nolte.ajax({
			url: '/ajax.php',
			data: 'param='+_getI18n()+'/'+page, //URL bauen
			dataType: 'html',
			success: function(html) { //Erfolg
				if(typeof(success) != 'undefined') success(html,args);
			},
			error: function(obj) { //Fehler
				if(typeof(error) != 'undefined') error(obj,page,args);
			}
		});
	};

	//Umleitung
	var _redirect = function(obj,page) {
		location.href = page;
	};

	//Overlay entfernen
	var _removeOverlay = function() {
		$nolte('#popup_wait').remove();
		$nolte('#popup_overlay').remove();
		return this;
	}

	//Popup entfernen
	var _removePopup = function() {
		$nolte('#popup').remove();
		return this;
	};

	//Sprache setzen
	var setI18n = function(i18n) {
		_i18n = i18n;
		return this;
	};

	//Initialisierung
	$nolte('a[rel^=popup],area[rel^=popup]').each(function() {
		$nolte(this).click(function(e) {
			e.preventDefault();
			_getI18nContent(_getFile(this),_addOverlay,_addPopup,function(obj,page) {
				_removeOverlay();
				_redirect(page);
			},_getArgs(this));
		});
	});

	//Öffentliche Methoden zurückgeben
	return {
		setI18n: setI18n
	};
});
