/**
 * @fileOverview Library zum einbinden eines inlinePopups in Projekte
 * @name popup.js
 * @author epcom it-systeme GmbH office@epcom.cc
 * @version 4.0.1
 * @description
 * Die Classen in dieser Datei werden für die Ausgabeseiten der Projekte und nicht für das CMS selbst verwendet.
 */
/**
 * @type class
 * @name inlinePopup
 * @version 4.0.1
 * @description
 * Classe zum Öffnen eines Inline Popups
 */
var inlinePopup=new Class({
	/**
	 * @function
	 * @name inlinePopup.initialize
	 * @param {Object} options
	 * 				   options.template {String} ID des HTML Blocks des zu verwendenden Templates. Default: popup
	 * 				   options.elementID {String} ID des zu erzeugenden DIVs. Default: popup
	 * 				   options.elementClass {String} CSS Classenname des zu erzeugenden DIVs. Default: ''
	 * 				   options.transID {String} ID des DIVs welches den semi-transparenten Hintergrund erstellt. Default: null (Deaktiviert den Hintergrund)
	 * 				   options.transClass {String} CSS Classenname des semi-transparenten Hintergrundes. Default: ''
	 * 				   options.transOpacity (Float) Opacity Wert bis zu dem der Hintergrund gefaded wird. Default: 0.7
	 * @description
	 * Construktor der inlinePopup Classe. Speichern der übergebenen Werte und zuweisen von Standard-Werten
	 */
	initialize: function(options){
		if(options==null){ options={}; }
		if(options.template==null){ options.template='popup'; }
		if(options.elementID==null){ options.elementID='popup'; }
		if(options.contentID==null){ options.contentID='popup_content'; }
		if(options.elementClass==null){ options.elementClass=''; }
		if(options.transID==null){ options.transID=null; }
		if(options.transClass==null){ options.transClass=''; }
		if(options.transOpacity==null){ options.transOpacity=0.7; }
		this.template=options.template;
		this.elementID=options.elementID;
		this.contentID=options.contentID;
		this.elementClass=options.elementClass;
		this.transID=options.transID;
		this.transClass=options.transClass;
		this.transOpacity=options.transOpacity;
		/**
		 * @type {Object}
		 * @name inlinePopup.fx
		 * Objekt zum Speichern laufender Effekte
		 */
		this.fx={};
		
	},
	/**
	 * @name inlinePopup.open
	 * @function
	 * @param {Object} infoObject Optionales Objekt zur Einbindung von Texten in das Popup-Template
	 * @description
	 * Funktion zum Öffen eines Popups innerhalb einer Seite
	 */
	open: function(infoObject){
		if(infoObject==null){ infoObject={}; }
		if(this.transID!=null){
			if(!document.id(this.transID)){				
				var elementTrans=new Element('div',{
					'id': this.transID
				});
				if(this.transClass!=''){
					elementTrans.addClass(this.transClass);
				}else{
					elementTrans.setStyles({
						'z-index':'9',
					 	'background-color':'#FFFFFF',
					 	'position':'absolute',
					 	'left':'0px',
					 	'top':'0px',
					 	'width':'100%',
					 	'height':'100%',
					 	'cursor':'pointer'
					});
					elementTrans.fade('hide');
				}
				elementTrans.inject(document.body);
				var thisClass=this;
				elementTrans.addEvent('click',function(){
					thisClass.close();
				});
			}else{
				var elementTrans=document.id(this.transID);
			}
			elementTrans.setStyles({
				'width':window.getScrollSize().x,
			 	'height':window.getScrollSize().y,
			 	'visibility':'visible'
			});
			elementTrans.fade(this.transOpacity);
		}
		var obj=null;
  		if(document.id(this.elementID)){
  			obj=document.id(this.elementID);
  		}else{
			obj=new Element('div',{
				'id': this.elementID,
				'class': this.elementClass,
				'styles': {
					'left':window.getSize().x/2,
					'top':window.getScroll().y.toInt() + 20,
					'opacity':0,
					'visibility':'hidden'
				}
			});
  		}
		obj.set('html',getTemplate(this.template).substitute(infoObject));
		if(!document.id(this.elementID)){
			obj.inject(document.body);
		}
		obj.setStyle('visibility',null);
		var objSize=obj.measure(function(){
			return this.getSize();
		});
		obj.setStyles({
			'left':(window.getSize().x - objSize.x)/2,
			'top':window.getScroll().y.toInt() + 20
		});
		obj.fade(1);
		this.position();
		return obj;
	},
	/**
	 * @name inlinePopup.resize
	 * @function
	 * @description
	 * Funktion zum Anpassen der DIV - Breite an den Content des Popups
	 * Dies ist notwendig, da der IE7 ansonsten die gesamte verfügbare
	 * Window-Breite verwenden würde
	 */
	resize: function(){
		if(this.contentID!=null && document.id(this.contentID)){
			var elPopup=document.id(this.elementID);
			var elContent=document.id(this.contentID);
			elPopup.setStyle('width',elContent.getSize().x);
		}else{
			devLog('contentID nicht gesetzt oder das Element ist nicht vorhanden');
		}
	},
	/**
	 * @name inlinePopup.position
	 * @function
	 * @description
	 * Positionieren des Popups in die Horizontale Mitte und 20 Pixel vom oberen Rand
	 */
	position: function(){	
		if(Browser.ie==true){
			this.resize();
		}
		var obj=document.id(this.elementID);
		if(this.fx.L==null){
			this.fx.L=new Fx.Tween(obj,{'link':'cancel'});
		}
		if(this.fx.T==null){
			this.fx.T=new Fx.Tween(obj,{'link':'cancel'});
		}
		this.fx.L.start('left',window.getSize().x/2 - obj.getSize().x/2);
		this.fx.T.start('top',window.getScroll().y.toInt() + 20);
	},
	/**
	 * @name inlinePopup.close
	 * @function
	 * @description
	 * Schliesst das aktuelle Popup
	 */
	close: function(){
		if(this.transID!=null){
			if(document.id(this.transID)){	
				var transBGElement=document.id(this.transID);
				if(this.fx.BG==null){
					this.fx.BG=new Fx.Tween(transBGElement,{
						'link':'cancel',
						'onComplete':function(){
							transBGElement.setStyle('visibility','hidden');
						}
					});
				}
				this.fx.BG.start('opacity',0);
			}
		}
		if(this.fx.L!=null){ this.fx.L.cancel(); }
		if(this.fx.T!=null){ this.fx.T.cancel(); }
		if(document.id(this.elementID)){
			document.id(this.elementID).destroy();
		}
	}
});
