/*
Class Infobox
	
Titlefunction wird 

*/
var closeicon="close_info.gif";
var imgpath="/res/gfx/";

var Infobox = new Class({
	
	options : {
		draggable:true,
		ready:new Class(),
		close:new Class(),
		overlay:false,
		appear:0,
		space:0,
		center:true,
		firstcenter:true,
		loading:true,
		autocenter:true
	},

	initialize : function(id,options){
		this.id=id;
		this.setOptions(options);
		this.doneOnce=0;
	},
	init: function() {
		$$("a.smoothbox").each(function(el){el.onclick=this.bind.bind(this) });
	},
	show: function(url2) {
		this.buildBox();
		
		var efunc= function() {
			try {
				this.options.ready();
			} catch (e) { alert(e); }
			this.position();
			this.showWindow();
		}.bind(this);

		if (url2)
			new Request.HTML({ url: url2,update: $("TB_ajaxContent"+this.id),onComplete: efunc }).get();
		else
			this.showWindow();

		if (this.options.autocenter)
			window.onresize=function(){ this.position; this.load_position; this.overlaySize;}.bind(this); 
		
		document.onkeyup = function(event){ 	
			var event = new Event(event);
			if(event.code == 27){this.removeC();}	
		}.bind(this);
	},
	setTitle: function(title) {
		$('TB_ajaxWindowTitle'+this.id).innerHTML=title;
	},
	setContent: function(content) {
		$("TB_ajaxContent"+this.id).innerHTML=content;
	},
	buildBox : function(){
		if (!$('TB_overlay'+this.id) && this.options.overlay) {
			new Element('iframe').setProperty('id', 'TB_HideSelect'+this.id).injectInside(document.body);
			$('TB_HideSelect'+this.id).addClass('TB_HideSelect');
			$('TB_HideSelect'+this.id).setOpacity(0);
			new Element('div').setProperty('id', 'TB_overlay'+this.id).injectInside(document.body);
			$('TB_overlay'+this.id).addClass('TB_overlay');
			$('TB_overlay'+this.id).setOpacity(0);
			this.overlaySize();
			$('TB_overlay'+this.id).onclick=this.removeC.bind(this);
			
			
			$('TB_overlay'+this.id).get('tween', {property:'opacity',duration: 400, transition: Fx.Transitions.Sine.easeInOut}).start(0.6);
			
//			new Fx.Style($('TB_overlay'+this.id), 'opacity',{duration: 400, transition: Fx.Transitions.Sine.easeInOut}).start(0,0.6);
		}

		if ( !$('TB_load'+this.id) && this.options.loading)	{		
			new Element('div').setProperty('id', 'TB_load'+this.id).injectInside(document.body);
			$('TB_load'+this.id).innerHTML = "<img src='"+imgpath+"loading.gif' />";
			$('TB_load'+this.id).addClass('TB_load');
			this.load_position(this);
		}		


		if ( !$('TB_window'+this.id) ) {
			new Element('div').setProperty('id', 'TB_window'+this.id).injectInside(document.body);
			$('TB_window'+this.id).addClass('TB_window');
			$('TB_window'+this.id).setOpacity(0);
			$('TB_window'+this.id).innerHTML = "<div id='TB_title2"+this.id+"' class='TB_title2'><div id='TB_ajaxWindowTitle"+this.id+"' class='TB_ajaxWindowTitle'>Wird geladen...</div></div><div id='TB_Content"+this.id+"' class='TB_Content'><div id='TB_ajaxContent"+this.id+"' class='TB_ajaxContent'></div></div>";
			if (this.options.draggable)	$('TB_window'+this.id).makeDraggable();
			new Element('img',{'styles':{'border': 'none','display':'block','visibility': 'visible','float':'right','margin':'0','height':'24px','width':'24px','z-index':'1000','position': 'absolute','cursor':'pointer'}}).setProperty('id','TB_closeimage'+this.id).injectInside($('TB_window'+this.id));
			$('TB_closeimage'+this.id).src=imgpath+closeicon;
			$('TB_closeimage'+this.id).onclick =this.removeC.bind(this);
		}
	
		if (this.options.autocenter)	
			window.onscroll=this.positionEffect.bind(this);		

	},
	setSize: function(width,height) {
		this.WIDTH=width;
		this.HEIGHT=height;
		if ( $('TB_window'+this.id)) {
			var ajaxContentW = width - 30, ajaxContentH = height - 30;
			$('TB_ajaxContent'+this.id).setStyle('width',ajaxContentW+'px');
			$('TB_ajaxContent'+this.id).setStyle('height',ajaxContentH+'px');
		}
	},
	position : function () {
		if (this.options.center || (this.doneOnce==0 && this.options.firstcenter)) {
			$('TB_window'+this.id).setStyles({left: (window.getScrollLeft() + (window.getWidth() - this.WIDTH)/2)+'px',
			top: (window.getScrollTop() + (window.getHeight() - this.HEIGHT)/2)+'px'});
		}
		
		$('TB_window'+this.id).setStyles({width: (this.WIDTH+this.options.space)+'px'});
		$('TB_closeimage'+this.id).setStyle('left',(this.WIDTH+this.options.space)-12);
		$('TB_closeimage'+this.id).setStyle('top',-12);	
		
	},
	positionEffect: function () {
		new Fx.Morph($('TB_window'+this.id), {duration: 75, transition: Fx.Transitions.Sine.easeInOut}).start({
		'left':(window.getScrollLeft() + (window.getWidth() - this.WIDTH)/2),
		'top':(window.getScrollTop() + (window.getHeight() - this.HEIGHT)/2)
		});
	},
	overlaySize: function() {
		if (!this.options.overlay) return; 
		// we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
		$('TB_overlay'+this.id).setStyles({"height": '0px', "width": '0px'});
		$('TB_HideSelect'+this.id).setStyles({"height": '0px', "width": '0px'});
		$('TB_overlay'+this.id).setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});
		$('TB_HideSelect'+this.id).setStyles({"height": window.getScrollHeight()+'px',"width": window.getScrollWidth()+'px'});
	},
	load_position: function() {
		if ($('TB_load'+this.id)) { $('TB_load'+this.id).setStyles({left: (window.getScrollLeft() + (window.getWidth() - 56)/2)+'px', top: (window.getScrollTop() + ((window.getHeight()-20)/2))+'px',display:"block"}); }
	},
	showWindow: function() {
		this.setSize(this.WIDTH,this.HEIGHT); 	
		this.position(this);
		
		if (this.doneOnce==0) {
			this.doneOnce = 1;
			
			if (this.options.appear==1) { // Slidein
				var windowHeight=window.getScrollTop() + window.getHeight();
				var top=windowHeight-this.HEIGHT;
				$('TB_window'+this.id).setStyle('top',windowHeight);
				$('TB_window'+this.id).setStyle('opacity',1);
				new Fx.Morph($('TB_window'+this.id), {duration: 1700, transition: Fx.Transitions.Bounce.easeOut}).start({'top': [window.getHeight(), top]});
			} else
				$('TB_window'+this.id).get('tween', {property:'opacity',duration: 250, transition: Fx.Transitions.Sine.easeInOut, onComplete:function(){ if ($('TB_load'+this.id)) { $('TB_load'+this.id).dispose();}}.bind(this) }).start(1);
		} else {
			$('TB_window'+this.id).setStyle('opacity',1);
			
			if ($('TB_load'+this.id)) { $('TB_load'+this.id).dispose();}
		}
	},
	removeC: function() {
		if (!$('TB_window'+this.id)) return;
		if ($('TB_overlay'+this.id)) $('TB_overlay'+this.id).onclick=null;
		document.onkeyup=null;
		document.onkeydown=null;
		
		if ($('TB_imageOff'+this.id)) $("TB_imageOff"+this.id).onclick=null;
		if ($('TB_closeWindowButton'+this.id)) $("TB_closeWindowButton"+this.id).onclick=null;
		
		
		$('TB_window'+this.id).get('tween', {property:'opacity',duration: 250, transition: Fx.Transitions.Sine.easeInOut, onComplete:function(){$('TB_window'+this.id).dispose(); }.bind(this) }).start(0);
		if($('TB_overlay'+this.id)) {
			$('TB_overlay'+this.id).get('tween', {property:'opacity',duration: 400, transition: Fx.Transitions.Sine.easeInOut, onComplete:function(){ $('TB_overlay'+this.id).dispose();}.bind(this) }).start(0);
			$('TB_HideSelect'+this.id).dispose();
		}
			
	
		window.onscroll=null;
		window.onresize=null;	
		
//		this.init();
		this.doneOnce = 0;
		if ($('TB_load'+this.id)) $('TB_load'+this.id).dispose();

		if (this.options.close !="undefined")
			this.options.close();
		return false;
	},
	bind: function(event) {
		var event = new Event(event);
		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
		this.show(caption, this.href, group);
		this.onclick=this.bind.pass(this);
		return false;
	}
	
	
});
Infobox.implement(new Options);