var list;

/* List
----------------------------------------------- */
var List = function() {
	list = this;
	list.intervalid;
	list.items = new Array();
}

List.prototype = {
	ListInitialize: function() {
		$(window).unbind("resize", list.ListRePosition);
		$(window).unbind("mousemove", list.ListMouseMove);
		$("#items").show();
		$("#items .item").remove();
		$("#items .item").unbind("click", list.ListClick);
		$("#items .item").unbind("mouseenter").unbind("mouseleave");
	},
	ListOpen: function() {
		var count = 0;
		$("#items .item").each(function() {
			list.items[count] = $(this);
			count++;
		});
		
		list.ListSet();
		
		$("#header h1 a").bind("click", list.ListSet);
	},
	ListSet: function() {

		// stage grid
		StageOb.StageListGrid();

		// initialize
		list.ListInitialize();
		FlipOb.FlipInitialize();
		DetailOb.DetailInitialize();

		// position
		var count = 0;
		var cols = 0;
		var rows = 0;
		for (var i in list.items) {

			var item = list.items[i];

			if (rows < stage.max_rows) {

				var left_pos = ((itemsize + stage.over_cols) * cols) + (itemsize / 2);
				var top_pos = ((itemsize + stage.over_rows) * rows) + (itemsize / 2);

				$("#items").append(item);
				item.css({ opacity:0, top:top_pos-15, left:left_pos, zIndex:1 });
				item.delay(50*count).animate({ opacity:1 }, 250, function() {
					$(this).hover(list.ListRollOver, list.ListRollOut);
					$(this).bind("click", list.ListClick);
					$(this).animate({ top:$(this).offset().top+15 }, 500, "easeOutBounce");
				});				
			}
			else {
				item.css({ opacity:0 });
			}

			cols++;
			if (cols == stage.max_cols) {
				cols = 0;
				rows++;
			}
			count++;
		}

		// window resize
		$(window).bind("resize", list.ListRePosition);
		
		// list control
		KeysOb.KeysListControl();
		
		return false;
	},
	ListRePosition: function() {
		clearInterval(list.intervalid);
		list.intervalid = setInterval(list.ListReset, 500);
	},
	ListReset: function() {

		// stage grid
		StageOb.StageListGrid();

		// initialize
		clearInterval(list.intervalid);
		$("#items .item").remove();

		// position
		var count = 0;
		var cols = 0;
		var rows = 0;
		for (var i in list.items) {

			var item = list.items[i];

			if (rows < stage.max_rows) {

				var left_pos = ((itemsize + stage.over_cols) * cols) + (itemsize / 2);
				var top_pos = ((itemsize + stage.over_rows) * rows) + (itemsize / 2);

				$("#items").append(item);
				item.css({ zIndex:1 });
				item.delay(25*count).animate({ top:top_pos-15, left:left_pos }, 500, "easeInOutCirc", function() {
					$(this).animate({ opacity:1 }, 250, function() {
						$(this).hover(list.ListRollOver, list.ListRollOut);
						$(this).bind("click", list.ListClick);
						$(this).animate({ top:$(this).offset().top+15 }, 500, "easeOutBounce");					
					});
				});
				
			}
			else {
				item.css({ opacity:0 });
			}

			cols++;
			if (cols == stage.max_cols) {
				cols = 0;
				rows++;
			}
			count++;
		}

		return false;
	},
	ListRollOver: function() {
		var title = $("h2", this).text();
		$("#guide").stop();
		$("#guide").show();
		$("#guide").html('<p>' + title + '</p>');
		$("#guide").css({ opacity:0 });
		$("#guide").delay(100).animate({ opacity:0.8 }, 250);
		$("img", this).stop();
		$("img", this).css({ opacity:0.25 });
		$("img", this).animate({ opacity:1 }, 1000, "easeOutCirc");
		$(window).bind("mousemove", list.ListMouseMove);
	},
	ListRollOut: function() {
		$("#guide").hide();
		$(window).unbind("mousemove", list.ListMouseMove);
	},
	ListMouseMove: function(event) {
		var left_pos = event.pageX - ($("#guide").width() / 2);
		var top_pos = event.pageY + 30;
		$("#guide").css({ top:top_pos, left:left_pos });
	},
	ListClick: function() {
	
		list.ListClose($(this));

		return false;
	},
	ListClose: function(item) {

		// detail open
		DetailOb.DetailOpen(item);

		// initialize
		list.ListInitialize();
		KeysOb.KeysInitialize();
		
		$("#guide").animate({ opacity:0 }, 250, function() {
			$(this).hide();
		});
		
		$("#items .item").animate({ opacity:0 }, 250, function() {
			$(this).remove();
		});
	}
}
