// JavaScript Document
var Dialog = new Class({
				Implements: Options,
				options: {
					speed: 500,
					maskColor: '#000000',
					backgroundColor: '#ffffff',
					width: 400,
					height: 'auto',
					onComplete: Class.empty,
					onStart: Class.empty
				},
				initialize: function(options){
					this.setOptions(options);
				},
				show: function(el) {

					// setup the message
					switch ($type(el)) {
						case 'element':
							this.el = $(el).clone();
							if (this.el.tagName == 'img') this.preload;
							break;
						case 'string':
							this.el = new Element('div',{'styles':{'padding':10,'width':this.options.width,'height':this.options.height}}).setHTML(el)
							break;
						default:
							return false;
							break;
					}

					// create elements: mask, pop, message and close
					this.mask = new Element('div',{id:'maske','class':'maske','onclick':'calculatorFadeOut()','styles':{
						
						'position':'absolute',
						'top':0,
						'left':$('main').getFirst().getCoordinates().left,
						'right':$('main').getFirst().getCoordinates().right,
						'opacity':0,
						'height': (window.getHeight() > window.getScrollHeight())?window.getHeight():window.getScrollHeight(),
						'width':'290px',
						'background':'transparent',
						'z-index':9999
					}});
					//this.mask.addEvent('click',this.hide.bind(this));
					this.pop = new Element('div',{id:'poplayer','class':'poplayer','styles':{
						
						'position':'absolute',
						'visibility':'hidden',
						//'width':'100%',
						'margin':0,
						'left':$('main').getFirst().getCoordinates().left+5,
						'right':$('main').getFirst().getCoordinates().right,
						'z-index':10000
					}});
					this.message = new Element('div',{'styles':{
						
						'margin':'0 auto'
					}});
					this.close = new Element('span',{'styles':{
						//'display':'none',
						//'background':'#000000',
						'text-align':'right',
						'color':'#ffffff',
						//'padding':3,
						'cursor':'pointer'
					}}).set('html','<img src="fileadmin/templates/img/calculator_close.png" onclick="calculatorFadeOut()" />');

					// add events to close the dialog box
					//this.close.addEvent('click',this.hide.bind(this));

					// in case there are objects in our message,
					// we hide objects BEFORE we add the message to the dom
					//$$('object','select').setStyle('visibility','hidden');

					// add elements to the DOM
					document.body.appendChild(this.mask);
					document.body.appendChild(this.pop);
					this.close.inject(this.message,'inside')
					this.el.inject(this.message,'inside');
					this.message.inject(this.pop,'inside');
					this.message.setStyle('width',this.el.getCoordinates().width);

					// position the dialog outside (above) the visible window
					this.pop.setStyles({
						'top': window.getScrollTop() - this.pop.getCoordinates().height,
						'visibility':'visible'
					});

					// start the animation
					this.fade = new Fx.Tween(this.mask, {property:'opacity', duration:this.options.speed});
					
					this.slide = new Fx.Tween(this.pop, {property:'top', duration:this.options.speed});

					this.fade.start(.8);
					this.slide.start(window.getScrollTop()+135);
					this.periodical = this.update.periodical(100,this);
				},
				update: function() {
					this.slide.start(window.getScrollTop()+135);
					var h = (window.getHeight() > window.getScrollHeight())?window.getHeight():window.getScrollHeight();
					this.mask.setStyle('height',h);
				},
				hide: function() {
					//$$('select').setStyle('visibility','visible');
					$clear(this.periodical);
					this.fade.start(0);
					this.slide.start(window.getScrollTop()-this.pop.getCoordinates().height).chain(function(){
						this.pop.destroy();
						this.mask.destroy;
						$$('body').setStyle('overflow','auto');

					}.bind(this));
				}
			});


			
			/*
			$('SimpleMessage').addEvent('click',function(e){
				var e = new Event(e);
				e.stop();
				dlg.show('This is a <b>simple message</b> with HTML formatting');
			});
			*/
			
/*
	Class:    	ScrollSpy
	Author:   	David Walsh
	Website:    http://davidwalsh.name/js/scrollspy
	Version:  	1.0.0
	Date:     	5/19/2009
	Built For:  MooTools 1.2.2
*/

/* scroll spy plugin / class */
var ScrollSpy = new Class({
	
	/* implements */
	Implements: [Options,Events],

	/* options */
	options: {
		min: 0,
		mode: 'vertical',
		max: 0,
		container: window,
		onEnter: $empty,
		onLeave: $empty,
		onTick: $empty
	},
	
	/* initialization */
	initialize: function(options) {
		/* set options */
		this.setOptions(options);
		this.container = $(this.options.container);
		this.enters = this.leaves = 0;
		this.max = this.options.max;
		
		/* fix max */
		if(this.max == 0) 
		{ 
			var ss = this.container.getScrollSize();
			this.max = this.options.mode == 'vertical' ? ss.y : ss.x;
		}
		/* make it happen */
		this.addListener();
	},
	
	/* a method that does whatever you want */
	addListener: function() {
		/* state trackers */
		this.inside = false;
		this.container.addEvent('scroll',function() {
			/* if it has reached the level */
			var position = this.container.getScroll();
			var xy = this.options.mode == 'vertical' ? position.y : position.x;
			/* if we reach the minimum and are still below the max... */
			if(xy >= this.options.min && xy <= this.max) {
					/* trigger Enter event if necessary */
					if(!this.inside) {
						/* record as inside */
						this.inside = true;
						this.enters++;
						/* fire enter event */
						this.fireEvent('enter',[position,this.enters]);
					}
					/* trigger the "tick", always */
					this.fireEvent('tick',[position,this.inside,this.enters,this.leaves]);
			}
			else {
				/* trigger leave */
				if(this.inside) 
				{
					this.inside = false;
					this.leaves++;
					this.fireEvent('leave',[position,this.leaves]);
				}
			}
		}.bind(this));
	}
});

/// JSON.P
var JsonP = Class.refactor(Request.JSONP, {
	initialize: function() {
		var params = Array.link(arguments, {url: String.type, options: Object.type});
		options = (params.options || {});
		options.url = options.url || params.url;
		if (options.callBackKey) options.callbackKey = options.callBackKey;
		this.previous(options);
	},
	getScript: function(options) {
		var queryString = options.queryString || this.options.queryString;
		if(options.url && queryString) options.url += (options.url.indexOf("?") >= 0 ? "&" : "?") + queryString;
		var script = this.previous(options);
		if ($chk(options.globalFunction)) {
			window[options.globalFunction] = function(r){
				JsonP.requestors[index].handleResults(r)
			};
		}
		return script;
	},
	request: function(url) {
		this.send({url: url||this.options.url});
	}
});
