var Teras = {};
Teras.Window = new Class({
	initialize: function(width) {
		this.width = (width>0) ? width : 500;
		this.overlay = new Element('div').setProperty('id', 'mb_overlay').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'mb_center').setStyles({width: '1px', height: '1px', display: 'none'}).injectInside(document.body);
		this.contents = new Element('div').setProperty('id', 'mb_contents').setStyles({width: this.width+'px', height: '300px'}).injectInside(this.center);
		this.bottom = new Element('div').setProperty('id', 'mb_bottom').setStyles({width: this.width+'px', display: 'none'}).injectInside(document.body);
		this.bclose = new Element('a').setProperties({id: 'mb_close_link', href: '#'}).injectInside(this.bottom).onclick = this.close.bind(this);
		var contentsEffect = this.contentsEffect.bind(this);
		var bottomEffect = this.bottomEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', { duration: 400,onComplete: contentsEffect }).hide(),
			center: this.center.effects({ duration: 600, onComplete: bottomEffect}),
			bottom: this.bottom.effects({duration: 600})
		};	
		window.addEvent('scroll', function(){ $('mb_center').setStyle('top', Window.getScrollTop()+'px'); $('mb_bottom').setStyle('top', (Window.getScrollTop()+$('mb_contents').clientHeight)+'px');});
	},
	open: function() {
		this.overlay.setStyles({width: Window.getScrollWidth()+'px', height: Window.getScrollHeight()+'px',display: ''});
		this.center.setStyles({display: '',height:'0'});
		this.center.setStyles({width:this.contents.scrollWidth+'px',height:'0',top: this.top+'px',left: ((Window.getWidth()-this.width)/2)+'px'});
		this.fx.overlay.start(0.8);
	},
	contentsEffect: function() {
		this.fx.center.custom({height: [0, this.contents.scrollHeight]});
	},
	bottomEffect: function() {
		this.bottom.setStyles({left: ((Window.getWidth()-this.width)/2)+'px', top:0,display: ''});
		this.fx.bottom.custom({top: [(this.contents.scrollHeight/2), (Window.getScrollTop()+this.contents.clientHeight)]});
	},
	close: function() {
		this.overlay.style.display = this.center.style.display = this.bottom.style.display = 'none';
		return false;
	}	
});

Teras.Menu = new Class({
	options: {method:'post'},
	initialize: function(width) {
		this.stato = 1;
		this.overlay = new Element('div').setProperty('id', 'md_overlay').setStyles({display: 'none'}).injectInside(document.body);
		this.contents = new Element('div').setProperty('id', 'md_contents').setStyles({}).injectInside(this.overlay);
		
		this.overlay.addEvent('mouseenter', this.mouseenter.bind(this));
		this.overlay.addEvent('mouseleave', this.onClose.bind(this));
		
		$each($$('a.ddMenu'), function(el){this.click($(el))},this);
		
	},
	click: function(el){
		var href = 	el.getProperty('href');
		el.addEvent('click', function(e) {
			e.stop();
			this.open(e,href);
		}.bindWithEvent(this));
	},
	start: function(el,href){
		alert(href);
	},
	locate: function(){
		var event = this.event;
		var win = {'x': window.getWidth(), 'y': window.getHeight()};
		var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
		var tip = {'x': this.overlay.offsetWidth, 'y': this.overlay.offsetHeight};
		var prop = {'x': 'left', 'y': 'top'};
		for (var z in prop){
			var pos = event.page[z] + 10;
			if ((pos + tip[z] - scroll[z]) > win[z]) pos = event.page[z] - 10 - tip[z];
			this.overlay.setStyle(prop[z], pos + 'px');
		};
		this.closing = this.close.bind(this).delay(3000);
	},
	open: function(event,url,anim) {
		$clear(this.closing);
		this.event = event;
		this.overlay.setStyles({display: ''});
		this.options.request = false;
		this.options.anim = 'onSimple';
		this.updatenow = new Teras.Updater(this.contents, url,this.options);
		this.updatenow.addEvent('onSimple',this.locate.bind(this));
		this.updatenow.request();
	},
	mouseenter:function() {
		$clear(this.closing);
	},
	onClose: function() {
		this.closing = this.close.bind(this).delay(1000);
	},
	close: function() {
		this.overlay.setStyles({display: 'none'});
		this.contents.innerHTML = "";
	}	
});

Teras.Updater = Ajax.extend({
	initialize: function(container, url, options) {
		this.container = container;
		options = Object.extend({
			headers: {'X-Request': 'html'},
			request:true,
			anim:'onSimple' 
		}, options || {});
		this.parent(url,options);
		this.addEvent('onSimple', this.onSimple);
		this.addEvent('onFade', this.onFade);
		this.addEvent('onBlindUp', this.onBlindUp);


		if(this.options.request)
			this.request();
	},
	onComplete: function(responseText){
		this.responseText = responseText;
		this.fireEvent(this.options.anim);
	},
	onSimple: function(){
		$(this.container).setHTML(this.responseText);
		mooRemote($(this.container));
		mooAnim($(this.container));
	},
    onFade: function(){
	    var onComplete = this.Appear.bind(this);
	    $(this.container).effect('opacity',{duration: 500, fps: 100, onComplete: onComplete}).custom(0.1);
    },
  	Appear: function(){
	    this.onSimple();
	    $(this.container).effect('opacity',{duration: 500, fps: 100}).custom(1);
  	},	
    onBlindUp: function(){
	    var onComplete = this.BlindDown.bind(this);
	    this.scrollHeight = $(this.container).scrollHeight;
	    var myEffect = new Fx.Style(this.container, 'height', {duration:300,onComplete: onComplete});
		myEffect.start(this.scrollHeight,1);
    },
  	BlindDown: function(){
	    this.onSimple();
	    this.scrollHeight = $(this.container).scrollHeight;
	    var myEffect = new Fx.Style(this.container, 'height', {duration:800});
		myEffect.start(1,this.scrollHeight); //will close the element if open, and vice-versa.
  	}	
});

Teras.Request = Ajax.extend({
	options: {
		request:true,
		onComplete: Class.empty,
		onSimple: Class.empty,
		onFade: Class.empty,
		onBlindUp: Class.empty,
		anim: 'onSimple'
	},
	initialize: function(url, options) {
		this.setOptions(this.options,options);
		this.parent(url,this.options);
		this.jsonScript='';
		if(this.options.request)
			this.request();
  	},
  	bindreplace: function(key,value){
		switch(key) { 
			case '_forward':
			window.location = value;
			break;
			
			case 'script':
			eval(value);
			break;
			
			case 'squeeze':
			SqueezeBox.setContent('string', value);
			break;

			case 'squeezeConfirm':
			SqueezeBox.setContent('string', value);
			break;
  	
  			default:
  			$(key).innerHTML=value;
  			mooRemote($(key));
  			mooDetail($(key));
  			//ajaxparse($(key));
  		}
  	},
	onComplete: function(responseText){
		var json = Json.evaluate(responseText.replace("}splitbody{",","));
		for (key in json)
			this.bindreplace(key,json[key]);
	}
	
});


Teras.PeriodicalUpdater = new Class({
	initialize: function(container, url, options,frequency,anim) {
		this.container = container;
		this.url = url;
		this.options = options;
		this.anim = anim;
		this.onTimerEvent();
		this.onTimerEvent.periodical(1000*this.options.frequency,this);
	},
	onTimerEvent: function(pe) {
	    this.updater = new Teras.Updater(this.container, this.url, this.options,this.anim);
  	}
	
});


function gdf(url,parametri,formName){
	var parametri = (parametri || {});
	new Teras.Request(url, $merge({postBody: $(formName).toQueryString()}, parametri, {method: 'post'})).request();
}

function gdp(url,parametri){
	var parametri = (parametri || {});
	new Teras.Request(url, parametri);
}
function gdpr(container, url, frequency, parametri,anim){
	var options = {parameters: parametri || {}};
	options.anim = "on"+(anim || 'Simple');
	var ajaxdddd = new Teras.Updater(container, url,options);
}
function remoteNow(parent){
	var arEl = $ES('a.mooRemote',(parent || document));
	$each(arEl, function(el){
	
		var href = 	$(el).getProperty('href');
		var miostile = $(el).getProperty('style');
		if($type(miostile)=='object')
			miostile = miostile['cssText'];
		var contents = new Element('div').setProperties({'id': $(el).getProperty('id'),'style':miostile,'class': $(el).getProperty('class')});
		alert(href);
		var ajax = new Teras.Updater(contents, href);
		$(el).replaceWith(contents);
		//ajax.addEvent('onSimple', function(){$(el).replaceWith(contents);});
	},this);
}
function mooRemote(parent){
	var arEl = $ES('a.mooRemote',(parent || document));
	$each(arEl, function(el){
		var options = {};
		if ($(el).rel) options = $merge(options, Json.evaluate($(el).rel));
		var href = 	$(el).getProperty('href');
		var miostile = $(el).getProperty('style');
		if($type(miostile)=='object')
			miostile = miostile['cssText'];
		var contents = new Element('div').setProperties({'id': $(el).getProperty('id'),'style':miostile,'class': $(el).getProperty('class')});
		var ajax = (options.frequency>0) ? new Teras.PeriodicalUpdater(contents, href,options) : new Teras.Updater(contents, href,options);
		$(el).replaceWith(contents);
	},this);
}