var main = function() {
	var speed="slow";
	var self
	return {
		init: function() {
				self = this;
				$(window).load(function() {
					$("#loader").remove();
					$("#preloader").remove();
					var so = new SWFObject("/images/collage.swf", "flash", "425", "575", "7", "#000000");
					so.addParam("menu", "false");
					so.addParam("quality", "best");
					so.write("flash");
					//self.backstage();
				})
			},
		backstage: function() {
				if (this.mode == "locked" || this.mode == "backstage") return true;
				this.mode = "locked";
			
				var lock=false;
				function load(id) {
					function display() {
						if (lock) return false
						lock=true;
						var old = $("#bsPhoto img").get(0)
						if (old) { 
							$(old).hide(speed,function() { $(old).remove(); lock=false});
							$("#bsPhoto").append(img)
						} else {
							$("#bsPhoto").append(img)
							$("#bsPhoto").fadeIn(speed, function() { lock=false});
						}				
					}
					var img = jQuery("<img>")
							.attr({src:"/images/BS/"+id+".jpg",width:512})
							.get(0);
					if (img.complete) {
						display()
					} else {
						$(img).load(function() { display() })
					}
				}
				function show(template) {
					$("#content").fadeOut(speed,function() {
						$("#content").html(template);
						$("#content").fadeIn(speed,function() {
							function addImg(i) {
								var img = jQuery("<img>")
										.attr({src:"/images/BS/bs"+i+"Off.jpg",width:153,id:"bs"+i})
										.mouseover(function() {
											this.src="/images/BS/"+this.id+"On.jpg";
										})
										.mouseout(function() {
											this.src="/images/BS/"+this.id+"Off.jpg";
										})
										.click(function() {
											load(this.id);
										})
										.get(0);
								$("#content .backstage").append(img);
								$(img).show(speed,function() {
									if (i<6) { 
										addImg(++i);
									} else {
										$("#content .backstage").append('<br style="clear: both">');
										if (!self.footer) {
											$("#footer").fadeIn("slow");
											self.footer = true;
										}
					
										self.mode="backstage"										
									}
								});
							}
							addImg(1);
						});
					});
				}
				// carico il template
				$.get("/backstage.html",function(data) {
					show(data);
				});
			},
		gallery: function(id,cat) {
			if (this.mode == "locked") return true;
			if (this.mode != "gallery") {
				this.mode = "locked";
				$("#content").fadeOut(speed,function() {
					$("#content").empty().append('<div id="category"></div><div id="photo"></div>').show();
					self.mode = "gallery"
					self.gallery(id,cat);
				})
				return true;
			}
			function display() {
				old = $("#photo img").get(0);
				function show() {
					$("#photo").empty().append(img)
					$(img).fadeIn(speed);
				} 
				if (old) {
					$(old).fadeOut(speed,function() {
						show()
					})
				} else {
					show();
				}
			}
			function displayCat() {
				$("#category").prepend(imgCat)
				$(imgCat).show(speed,function() {
					$(imgCat).next("img").remove();
				});
			}
			var img = jQuery("<img>")
							.attr({src:"/images/Big/"+id+".jpg",width:512})
							.css({display:"none"})
							.get(0);
			var imgCat = jQuery("<img>")
							.attr({src:"/images/Big/"+cat,width:61})
							.css({display:"none"})
							.get(0);
			
			if (img.complete) {
				display()
			} else {
				$(img).load(function() { display() })
			} 
			if (imgCat.complete) {
				displayCat()
			} else {
				$(imgCat).load(function() { displayCat() })
			}
			
			
		},
		biografia: function() {
			if (this.mode == "locked" || this.mode == "biografia") return true;
			this.mode = "locked";
			function show(template,img) {
			
				$("#content").fadeOut(speed,function() {
					$("#content").html(template).fadeIn(speed,function() {
						$("#biografia").prepend(img);
						$(img).show(speed);
						self.mode = "biografia";
					});
				});
			}
			// carico il template
			$.get("/biografia.html",function(data) {
				var img = jQuery("<img>")
						.attr({src:"/images/biografia.jpg",width:374})
						.css({display:"none"})
						.get(0);
				if (img.complete) {
					show(data,img)
				} else {
					$(img).load(function() { show(data,img) })
				} 
			});
		}
	}
}()


$(document).ready(function(){
	main.init()
});

function show(id,cat) { 
	switch (id) {
		case "backstage": main.backstage(); break;
		case "biografia": main.biografia(); break;
		default: main.gallery(id,cat) 
	} 
}

function start() {
	main.backstage();
}
