var cisSite=Class.create();
cisSite.prototype= {
	initialize:function() {
		var options = Object.extend({
				menu_container:'menu_container',
				azienda_container:'azienda',
				come_raggiungerci_container:'come_raggiungerci',
				contatti_container:"contatti",
				vendita_dettaglio_container:"vendita_dettaglio",
				rete_vendita_container:"rete_vendita",
				resellers_container:"punti"
				
		}, arguments[1] || {});
		this.options=options;
		
		this.options.IE6=((window.IE6_OPACITY!=null)?window.IE6_OPACITY:0);
		
		Event.observe(window,"resize",function(e) {
				this.centerSite();
		}.bind(this))
		this.centerSite();
		
		this.menu=new cisMenu(this.options.menu_container,{parent:this});
		this.products=new cisProducts({parent:this});
		this.azienda=new cisAzienda(this.options.azienda_container,{parent:this});
		this.come_raggiungerci=new cisComeRaggiungerci(this.options.come_raggiungerci_container,{parent:this});
		this.contatti=new cisContatti(this.options.contatti_container,{parent:this});
		this.vendita_dettaglio=new cisVenditaDettaglio(this.options.vendita_dettaglio_container,{parent:this});
		this.rete_vendita=new cisReteVendita(this.options.rete_vendita_container,{parent:this});
		
		
		this.main_section="home";
		this.section=null;
		this.moving=false;
		
		
		
	},
	centerSite:function() {
		var screen_h=document.viewport.getHeight();
		var wrapper_h=$('centered').getHeight();
		$('centered').setStyle({
				top:Math.round(screen_h/2-wrapper_h/2)+"px",
				display:"block"
		});
	},
	openSection:function(section,main_section,menu) {
		if (!this.moving) {
			if (this.products.opened!=null && section!='prodotti')
				this.products.hide(this.products.opened);
			//console.log(section,main_section,menu);
			if (menu) {
				if(!this.menu.moving) {
					this.menu.goForward(section,function() {
							//alert(section+","+main_section+","+menu)
							
							if ($(this.main_section) && (this.main_section!=main_section))
								$(this.main_section).hide();
							if ($(this.section) && (this.section!=section))
								$(this.section).hide();
	
							if (this.main_section!=main_section)
								$(main_section).show();
							if (this.section!=section) {
								this.moving=true;
								$('prod_overlay').show();
								$(section).show();
								var url="cisjax.php";
								new Ajax.Updater(
									{success:$(section).down("div.picture")},
									url,
									{
										method: 'post',
										parameters: {
											a:"prod_pic",
											p:section,
											nocache:new Date().getTime()
										},
										onFailure:function(request) {
											alert("Error!!!!!");
										},
										onComplete: function(originalRequest) {
											var img=$(section).down("div.picture img")
											if (img.complete) {
												new Effect.Fade('prod_overlay',{
													duration:0.5,
													afterFinish:function(){
														this.main_section=main_section;
														this.section=section;
														this.moving=false;
														this.menu.moving=false;
														//if (section=='insaccati_freschi' || section=='insaccati_stagionati')
															this.products.opened=section;
													}.bind(this)
												});
											} else {
												img.onload = function() {
													new Effect.Fade('prod_overlay',{
														duration:0.5,
														afterFinish:function(){
															this.moving=false;
															this.main_section=main_section;
															this.section=section;
															this.moving=false;
															this.menu.moving=false;
															//if (section=='insaccati_freschi' || section=='insaccati_stagionati')
																this.products.opened=section;
														}.bind(this)
													});
												}.bind(this);
											}
										}.bind(this)
									}
								);
							} else {
								this.moving=false;
								this.menu.moving=false;
							}
					}.bind(this));
				}
			} else {
				
				if ($(this.main_section) && (this.main_section!=main_section)) {
					$(this.main_section).hide();
					if (this.main_section=="home")
						$(this.main_section).remove();
					if (this.main_section=="contatti") {
						$('contacts_f').reset();
						$('contatti_div').show();
						$('grazie').hide();
					}
				}
				if ($(this.section) && (this.section!=section))
					$(this.section).hide();
				
				if (this.main_section!=main_section) {
					this.moving=true;
					new Effect.Appear(main_section,{queue:'end',afterFinish:function() {
							this.main_section=main_section;
							this.section=section;
							this.moving=false;
							switch(main_section) {
								case 'come_raggiungerci':
									this.come_raggiungerci.show();
								break;
								case 'vendita_dettaglio':
									this.vendita_dettaglio.show();
								break;
								case 'rete_vendita':
									this.rete_vendita.show();
								break;
								default:
								break;
							}
						}.bind(this)
					});
				}
			}
		}
	}
}
var cisComeRaggiungerci=Class.create();
cisComeRaggiungerci.prototype= {
	initialize:function(containerId) {
		var options = Object.extend({
				parent:null
		}, arguments[1] || {});
		this.options=options;
		
		this.container=$(containerId);
		
		this.opened=null;
		this.moving=false;
	},
	show:function() {
		if (!this.moving) {
			this.moving=true;
			var url="cisjax.php";
			new Ajax.Updater(
				{success:'come_contents'},
				url,
				{
					method: 'post',
					parameters: {
						a:"come",
						nocache:new Date().getTime()
					},
					onFailure:function(request) {
						alert("Error!!!!!");
					},
					onComplete: function(originalRequest) {
						var img=$("come_contents").down("img");
						this.container.setStyle({
								backgroundImage:'url('+img.src+')'
						});
						if (img.complete) {
							new Effect.Fade('come_overlay',{
								duration:0.5,
								afterFinish:function(){
									this.moving=false;
								}.bind(this)
							});
						} else {
							img.onload = function() {
								new Effect.Fade('come_overlay',{
									duration:0.5,
									afterFinish:function(){
										this.moving=false;
									}.bind(this)
								});
							}.bind(this);
						}
					}.bind(this)
				}
			);
		}
	}
}
var cisVenditaDettaglio=Class.create();
cisVenditaDettaglio.prototype= {
	initialize:function(containerId) {
		var options = Object.extend({
				parent:null
		}, arguments[1] || {});
		this.options=options;
		
		this.container=$(containerId);
		
		this.opened=null;
		this.moving=false;
	},
	show:function() {
		if (!this.moving) {
			this.moving=true;
			var url="cisjax.php";
			new Ajax.Updater(
				{success:'dettaglio_contents'},
				url,
				{
					method: 'post',
					parameters: {
						a:"dettaglio",
						nocache:new Date().getTime()
					},
					onFailure:function(request) {
						alert("Error!!!!!");
					},
					onComplete: function(originalRequest) {
						var img=$("dettaglio_contents").down("img");
						this.container.setStyle({
								backgroundImage:'url('+img.src+')'
						});
						if (img.complete) {
							new Effect.Fade('dettaglio_overlay',{
								duration:0.5,
								afterFinish:function(){
									this.moving=false;
								}.bind(this)
							});
						} else {
							img.onload = function() {
								new Effect.Fade('dettaglio_overlay',{
									duration:0.5,
									afterFinish:function(){
										this.moving=false;
									}.bind(this)
								});
							}.bind(this);
						}
					}.bind(this)
				}
			);
		}
	}
}
var cisReteVendita=Class.create();
cisReteVendita.prototype= {
	initialize:function(containerId) {
		var options = Object.extend({
				parent:null
		}, arguments[1] || {});
		this.options=options;
		
		this.container=$(containerId);
		
		this.opened=null;
		this.moving=false;
	},
	show:function() {
		if (!this.moving) {
			this.moving=true;
			var url="cisjax.php";
			new Ajax.Updater(
				{success:'rete_vendita_contents'},
				url,
				{
					method: 'post',
					parameters: {
						a:"rete_vendita",
						nocache:new Date().getTime()
					},
					insertion:Insertion.Bottom,
					onFailure:function(request) {
						alert("Error!!!!!");
					},
					onComplete: function(originalRequest) {
						var img=$("rete_vendita_contents").down("img");
						this.container.setStyle({
								backgroundImage:'url('+img.src+')'
						});
						if (img.complete) {
							new Effect.Fade('rete_vendita_overlay',{
								duration:0.5,
								afterFinish:function(){
									this.moving=false;
								}.bind(this)
							});
						} else {
							img.onload = function() {
								new Effect.Fade('rete_vendita_overlay',{
									duration:0.5,
									afterFinish:function(){
										this.moving=false;
									}.bind(this)
								});
							}.bind(this);
						}
					}.bind(this)
				}
			);
		}
	},
	search:function(option) {
		var url="cisjax.php";
		$('punti').update("caricamento...");
		new Ajax.Updater(
			{success:"punti"},
			url,
			{
				method: 'post',
				parameters: {
					a:"resellers",
					p:option.value,
					nocache:new Date().getTime()
				},
				onFailure:function(request) {
					alert("Error!!!!!");
				},
				onComplete: function(originalRequest) {
					new Effect.Appear('punti');
					
				}.bind(this)
			}
		);
	}
}
var cisAzienda=Class.create();
cisAzienda.prototype= {
	initialize:function(containerId) {
		var options = Object.extend({
				parent:null
		}, arguments[1] || {});
		this.options=options;
		
		this.container=$(containerId);
		
		this.opened=null;
		this.moving=false;
		this.descr_moving=false;
		this.desc_open=false;
		
		
	},
	openDescription:function() {
		if (!this.descr_moving) {
			this.descr_moving=true;
			new Effect.Move('azienda_footer',{
								x:0,
								y:-90,
								mode: 'relative',
								afterFinish:function() {
									this.descr_moving=false;
									this.desc_open=true;
									$('descr_open').hide();
									$('descr_close').show();
								}.bind(this)
			});
		}
	},
	closeDescription:function() {
		if (!this.descr_moving) {
			this.descr_moving=true;
			new Effect.Move('azienda_footer',{
								x:0,
								y:90,
								afterFinish:function() {
									this.descr_moving=false;
									this.desc_open=false;
									$('descr_close').hide();
									$('descr_open').show();
								}.bind(this)
			});
		}
	},
	toggleDescription:function() {
		if (this.desc_open) {
			this.closeDescription();
		} else {
			this.openDescription();
		}
	},
	changePicture:function(id) {
		if (!this.moving) {
			this.moving=true;
			var url="cisjax.php";
			new Effect.Appear('az_overlay',{
				duration:0.5,
				afterFinish:function() {
					new Ajax.Updater(
						{success:'azienda_pics'},
						url,
						{
							method: 'post',
							parameters: {
								a:"az_pic",
								p:id,
								nocache:new Date().getTime()
							},
							onFailure:function(request) {
								alert("Error!!!!!");
							},
							onComplete: function(originalRequest) {
								var img=$("azienda_pics").down("img");
								if (img.complete) {
									new Effect.Fade('az_overlay',{
										duration:0.5,
										afterFinish:function(){
											this.moving=false;
										}.bind(this)
									});
								} else {
									img.onload = function() {
										new Effect.Fade('az_overlay',{
											duration:0.5,
											afterFinish:function(){
												this.moving=false;
											}.bind(this)
										});
									}.bind(this);
								}
							}.bind(this)
						}
					);
				}.bind(this)
			});
		}
	}
};
var cisMenu = Class.create();
cisMenu.prototype= {
	initialize:function(containerId) {
		var options = Object.extend({
				parent:null
		}, arguments[1] || {});
		this.options=options;
		
		this.container=$(containerId);
		
		this.opened=null;
		this.moving=false;
		
	},
	goForward:function(idMenu,func) {
		if (!this.moving) {
			this.moving=true;
			$("m_"+idMenu).show();
			new Effect.Move(this.container,{
					x: -195,
					mode:'relative',
					afterFinish:function() {
						func();
					}.bind(this)
			});
		}
	},
	goBack:function(idMenu) {
		if (!this.moving) {
			this.moving=true;
			new Effect.Move(this.container,{
					x: 195,
					mode:'relative',
					afterFinish:function() {
						this.moving=false;
						$("m_"+idMenu).hide();
					}.bind(this)
			});
		}
	}
}
var cisProducts = Class.create();
cisProducts.prototype= {
	initialize:function() {
		var options = Object.extend({
				parent:null
		}, arguments[0] || {});
		this.options=options;
		
		this.opened=null;
		this.moving=false;
		this.descriptions=[];
	},
	show:function(product) {
		if (!this.moving && !this.options.parent.menu.moving) {
			if (this.opened)
				this.hide(this.opened);
			this.moving=true;
			var url="cisjax.php";
			new Effect.Appear('prod_overlay',{
				duration:0.5,
				afterFinish:function() {
					new Ajax.Updater(
						{success:$(product).down("div.picture")},
						url,
						{
							method: 'post',
							parameters: {
								a:"prod_pic",
								p:product,
								nocache:new Date().getTime()
							},
							onFailure:function(request) {
								alert("Error!!!!!");
							},
							onComplete: function(originalRequest) {
								$(product).show();
								var img=$(product).down("div.picture").down("img");
								if (img.complete) {
									new Effect.Fade('prod_overlay',{
										duration:0.5,
										afterFinish:function(){
											this.opened=product;
											this.options.parent.section=product;
											this.moving=false;
										}.bind(this)
									});
								} else {
									img.onload = function() {
										new Effect.Fade('prod_overlay',{
											duration:0.5,
											afterFinish:function(){
												this.opened=product;
												this.options.parent.section=product;
												this.moving=false;
											}.bind(this)
										});
									}.bind(this);
								}
							}.bind(this)
						}
					);
				}.bind(this)
			});
		}
	},
	hide:function(product) {
		//console.log(product)
		$(product).hide();
		if (recipe=$(product).down("div.ricetta")) {
			if (recipe.visible()) {
				recipe.hide();
			}
		}
		//if (product!='insaccati_freschi' && product!='insaccati_stagionati')
		if ($(product).down("div.picture").down("img"))
			$(product).down("div.picture").down("img").remove();
	},
	showRecipe:function(recipe) {
		if (!this.recipe_moving) {
			this.recipe_moving=true;
			new Effect.BlindDown(recipe,{afterFinish:function() {
					this.recipe_moving=false;
				}.bind(this)
			});
		}
	},
	hideRecipe:function(recipe) {
		if (!this.recipe_moving) {
			this.recipe_moving=true;
			new Effect.BlindUp(recipe,{afterFinish:function() {
					this.recipe_moving=false;
				}.bind(this)
			});
		}
	},
	toggleRecipe:function(recipe) {
		if ($(recipe).visible()) {
			this.hideRecipe(recipe);
		} else {
			this.showRecipe(recipe);
		}
	},
	openDescription:function(product) {
		if (!this.descr_moving) {
			this.descr_moving=true;
			var el=$(product).down('div.p_footer');
			new Effect.Move(el,{
								x:0,
								y:500-el.getHeight(),
								mode: 'absolute',
								afterFinish:function() {
									this.descr_moving=false;
									this.desc_open=true;
									el.down('img.descr_close').show();
									el.down('img.descr_open').hide();
									this.descriptions[product]=0;
								}.bind(this)
			});
		}
	},
	closeDescription:function(product) {
		if (!this.descr_moving) {
			this.descr_moving=true;
			var el=$(product).down('div.p_footer');
			//alert(el.positionedOffset().top);
			el.setStyle({
				top:el.positionedOffset().top+"px"
			});
			new Effect.Move(el,{
								x:0,
								y:479,
								mode:'absolute',
								afterFinish:function() {
									this.descr_moving=false;
									this.desc_open=false;
									el.down('img.descr_open').show();
									el.down('img.descr_close').hide();
									this.descriptions[product]=1;
								}.bind(this)
			});
		}
	},
	toggleDescription:function(product) {
		if (!this.descriptions[product]) {
			this.closeDescription(product);
		} else {
			this.openDescription(product);
		}
	}
}
var cisContatti = Class.create();
cisContatti.prototype= {
	initialize:function(containerId) {
		var options = Object.extend({
				form:'contacts_f',
				name_mandatory:0,
				email_mandatory:0,
				parent:null
		}, arguments[1] || {});
		this.options=options;
		this.container=$(containerId);
		
		this.initForm();
		
	},
	initForm:function() {
		$(this.options.form).observe('submit' , function(e) {
				Event.stop(e);
				if (this.checkForm()) {
					var url="cisjax.php";
					/*new Effect.Fade($(this.options.form).up());
					new Effect.Appear('c_loader',{afterFinish:function() {*/
							new Ajax.Request(
								url,
								{
									method: 'post',
									parameters: "a=send_msg&"+$(this.options.form).serialize()+"&nocache="+new Date().getTime(),
									onFailure:function(request) {
										alert("Error!!!!!");
									},
									onComplete: function(originalRequest) {
										$('grazie').show();
										new Effect.Fade('contatti_div');
									}.bind(this)
								}
							);
						/*}.bind(this)
					});*/
				}
		}.bind(this));
		
	},
	checkForm:function(form) {
		var form=(form?$(form):$(this.options.form));
		var return_value=true;
		
		if ($('nome').getValue()=='') {
			return_value=false;
			$('n_mand').show();
		} else {
			$('n_mand').hide();
		}
		if ($('email').getValue()=='') {
			return_value=false;
			$('e_mand').show();
		} else {
			$('e_mand').hide();
		}
		return return_value;
	},
	setForm:function() {
		var url="df2008.php";
		new Effect.Fade('thanks');
		
		new Effect.Appear('c_loader',{afterFinish:function() {
				new Ajax.Updater(
					{success:'c_r'},
					url,
					{
						method: 'post',
						parameters: {
							a:"set_form",
							nocache:new Date().getTime()
						},
						onFailure:function(request) {
							alert("Error!!!!!");
						},
						onComplete: function(originalRequest) {
							this.initForm();
						}.bind(this)
					}
				);
			}.bind(this)
		});
		
	}
}


var onloads = new Array();

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

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