var img=null;
var section=null;
var my_interval=null;
var jsTortellino=Class.create();
jsTortellino.prototype= {
	initialize:function() {
		
		var options = Object.extend({
				
		}, arguments[1] || {});
		this.options=options;
		this.opened='home';
		this.moving=false;
		this.loading_img=false;
		this.sections={
			home:{w:897,x:0},
			azienda:{w:770,x:897},
			valeggio:{w:770,x:1667},
			punto_vendita:{w:770,x:2437},
			laboratorio:{w:770,x:3207},
			prodotti:{w:770,x:3977},
			eventi:{w:770,x:4747},
			contatti:{w:770,x:5517}
		};
		//alert($(document.body).getHeight());
		
		var b_h=($(document.body).getHeight()-$("contents").getHeight())/2-20;
		if(b_h>0) {
			$('centered').setStyle({
				marginTop:b_h+"px"
			});
		}
		$("menu").setStyle({
			position:"absolute"
		});
		$("menu").hide();
		
		$("menu").select("li a").each(function(el){
				//console.log("adding event to "+el.readAttribute("rel"));
				el.observe("click",function(e){
					//console.log(el.readAttribute("rel"));
					this.go(el.readAttribute("rel"));
					return false;
				}.bind(this));
		}.bind(this));
		
		$("mclose").observe("click",function(e){
			this.closeMenu();
		}.bind(this));
		$("mopen").observe("click",function(e){
			this.openMenu();
		}.bind(this));
		$("sections").select(".overlay:not(.first)").each(function(el){el.show();});
	},
	closeMenu:function() {
		if (!this.moving) {
			this.moving=true;
			new Effect.Move("menu",{y:-210,mode:'absolute',afterFinish:function(e){
					this.moving=false;
					$('mopen').show();
				}.bind(this)
			});
		}
	},
	openMenu:function() {
		if (!this.moving) {
			this.moving=true;
			new Effect.Move("menu",{y:0,mode:'absolute',afterFinish:function(e){
					this.moving=false;
					$('mopen').hide();
				}.bind(this)
			});
		}
	},
	setMenu:function() {
		$('mopen').hide();
		$('mclose').show();
		$('menu').setStyle({top:"0px"});
	},
	go:function(s) {
		if (this.opened!=s && !this.moving) {
			delta=(s!='home')?50:0;
			this.moving=true;
			new Effect.Move("sections",{x:-this.sections[s].x+delta,mode:'absolute',afterFinish:function() {
					
					$(this.opened).down("div.overlay").show();
					this.opened=s;
					
					
					if(s!='home') {
						new Effect.Fade($(s).down("div.overlay"));
						//if ($('menu').visible())
							new Effect.Appear('menu')
					} else {
						$(s).down("div.overlay").hide();
						new Effect.Fade('menu',{afterFinish:function(e){
								this.setMenu();
							}.bind(this)
						})
					}
					this.moving=false;
			}.bind(this)});
		}
	},
	openBox:function(s) {
		if (!this.moving) {
			var box=$(s).down(".box")
			this.moving=true;
			new Effect.Move(box,{y:-330,afterFinish:function(){
				box.down(".open").hide();
				//box.down(".close").show();
				var cl=box.down(".close");
				new Effect.Appear(cl);
				var bc=box.down(".b_content");
				new Effect.Appear(bc);
				this.moving=false;
			}.bind(this)});
		}
		return false;
	},
	closeBox:function(s) {
		if (!this.moving) {
			this.moving=true;
			var box=$(s).down(".box");
			box.down(".b_content").hide();
			new Effect.Move(box,{y:330,afterFinish:function(){
				//box.down(".open").show();
				var op=box.down(".open");
				new Effect.Appear(op);
				box.down(".close").hide();
				
				this.moving=false;
			}.bind(this)});
		}
		return false;
	},
	changeBackground:function(i,section) {
		if(!this.loading_img) {
			this.loading_img=true;
			section=section;
			img=new Image();
			img.src="http://www.pastificioremelli.it/img/pics/"+section+i+".jpg";
			//console.log("click!");
			$(section).down(".th"+i).insert($('loader'));
			//new Effect.Appear('loader',{duration:0.2});
			$('loader').show();

			if (img.complete) {
				//console.log("loaded "+section+"("+img.naturalWidth+")");
				$(section).setStyle({
					backgroundImage:"url(img/pics/"+section+i+".jpg)"
				});
				
				new Effect.Fade('loader',{duration:0.4});
				this.loading_img=false;
			} else {
				img.onload = function() {
					//console.log("loaded "+section);
					$(section).setStyle({
						backgroundImage:"url(img/pics/"+section+i+".jpg)"
					});
					new Effect.Fade('loader',{duration:0.4});
					this.loading_img=false;
				}.bind(this);
			}
		}
		
		
		
		return false;
	}
};
var onloads = new Array();

document.observe("dom:loaded",function() {
	bodyOnLoad();
});

function bodyOnLoad() {
	for ( var i = 0 ; i < onloads.length ; i++ )
		onloads[i]();
}
var tortellino={};
onloads.push(function() {
		tortellino=new jsTortellino();
});

