$(document).ready(function() {
	y.init();
});

y	=	new function yearbook() {
			this.ready		= false;
			this.images		= [
				"/images/top-bkd-new.png",
				"/images/main-bkd-new.jpg",
				"/images/situation.png",
				"/images/main-prompt_plaque.png",
				"/images/main-frame-top-bkd.png",
				"/images/main-frame-middle-bkd.png",
				"/images/main-frame-bottom-bkd.png",
				"/images/footer-msg.gif"
			];
			this.loaded			= [];
			this.num_loaded	= 0;
			//this.timer			= null;
			this.tag_freq 	= new Array();
			this.init	= function() {
				$("#top, #footer, #main .logo, #main .frame").css({ display: "none" });
				//this.preload_notice();
				//this.start_timer();
				for(i=0; i<this.images.length; i++) {
					this.loaded[i] 				=	new Image();
					this.loaded[i].src		=	this.images[i];
					this.loaded[i].onload	= y.status;
				}
				y.start();
			};
			this.preload_notice	= function() {
				if($("#top").css("display") != "none") { 
					$("#loading").fadeOut(100, function() {
						$("#loading").remove();
					});
					return;
				}
				if($("#loading").length == 0) {
					$loading_container	= $("<div>").attr("id", "loading");
					$load_msg						= $("<div>").addClass("load-msg").html("<img src=\"/images/loading-msg.png\" alt=\"Loading...\" />").appendTo($loading_container);
					$loading_container.appendTo("body");
				}
				$msg	= $("#loading .load-msg");				
				$msg.fadeTo(300, 1.0, function() {
					$(this).fadeTo(300, 0.4, function() {
						y.preload_notice();
					});
				});
			};
			this.status		= function() {
				//y.timer	= setTimeout("y.start_timer()", 3000);
				y.num_loaded++;
				if(y.num_loaded == y.loaded.length) {
					//clearTimeout(y.timer);
					//y.start();
				}
			};
			this.start_timer	= function(directive) {
				y.timer	= true;
				y.start();
			};
			this.start	= function() {
				$("#top").fadeIn(300, function() {
					$("#main .logo").fadeIn(300, function() {
						y.display_tags();
						$("#main .frame").fadeIn(300, function() {
							y.ready	= true;
						});
						$("#top").css({ height: $("#main .frame").height() + 300 });
					});
					$("#footer").fadeIn(300);
				});
			};
			this.display_tags	= function() {
				for(var i=0; i<y.tags.length; i++) {
					$tag	= $("<a class=\"tag\" style=\"font-size:" + y.tag_freq[y.tags[i]] + "px;\">" + y.tags[i] + "</a>")
					$("#main .frame .middle").append($tag).append(" \n");
					this.bind_event($tag);
				}
			};
			this.focus_tag	= function() {
			
			};
			this.random_tag	= function() {
			
			};
			this.tag_html	= null;
			this.bind_event	= function(el) {
				el.click(function() {
					y.tag_html	= $("#main .frame .middle").html();
					$("#main .frame").fadeTo(300, 0.0, function() {
						$.ajax({
							url: "tag.php?" + el.text(),
							success: function(response) {
								$("#main .frame .middle").html(response);
								$("#main .frame").css({ marginTop: -30 });
								$("#main .frame").fadeTo(300, 1.0);
								$("#main .frame .back").click(function() { y.revert_frame(); return false; });
							}
						});
					});
					return false;
				});
			}
			this.revert_frame	= function() {
				$("#main .frame").fadeTo(300, 0.0, function() {
					$("#main .frame .middle").html(y.tag_html);
					$("#main .frame .middle .tag").each(function() {
						y.bind_event($(this));
					});
					$("#main .frame").css({ marginTop: -30 }).fadeTo(300, 1.0);
				});
			}
	 	};