var Mzz = (function(article) {
var the_window, direction, press, menu, articles;
			
			//------ Utilities.
			
			var nope = function nope(event) { event.preventDefault(); };


			//------ Content manipulation, deferred where appropriate until a navigation event (see address). 
			
			var patches = { 
				
				positionarticles: function() {				
					//------ Set the wrap width to be as many screens wide as the number of products.
					$('#wrap').css('width', (articles.length*100) + "%");
					//------ Position the articles.				
					articles.each(function(eq) {
						var article = $(this);
						var w = 100/articles.length;
						//------ The target position for animation is stored on the element as data. Tidy.
						article.data('index', eq)
							.css({ 'left': (eq * w) + "%",
								   'width': w + "%",
								   'visibility': 'visible'})
							.data('translate', "translate(-" + (eq * w) + "%),0");
						//------ Also, build out a product menu. Presumes menu variable is in initial state.			
						$('<li\/>', { 
							text: article.find('h4:eq(0)').text(),
							data: { 'address' : article.attr('id') }
						}).prependTo(menu);
					}).data('current', 0);
				},
				
				shuttles: function(sel) {
					//------ Build shuttle controls before any gallery.
					$(sel).each(function(eq) {
						var target = $(this);
						var children = target.children();
						$('<ul class="shuttle"><li class="next"><a class="next">Next<\/a><\/li><li class="previous"><a class="previous">Previous<\/a><\/li><li class="status">1/' + children.length + '<\/li><\/ul>')
							.insertBefore(target)
							.data('target', target)
							.delegate("a.next, a.previous", "click", navigation.shuttle);
						children.not(':first').hide();
					});				
				},
				
				buildControls: function() {
					//------ Build out direction shuttle navigation. Must first position articles before this will be operable.
					$('<li class="next"><a class="next">Next<\/a><\/li>')
						.appendTo('#direction>ul')
						.delegate("a.next, a.previous", "click", navigation.shuttleProduct);
					
					//------ Must have previously run positionarticles to populate the menu.
					menu = $('#releases .bezel').append(menu).parent().parent();
					menu.parent().hover(function(e) {
						menu.addClass('shown');
					}, function(e) {
						menu.removeClass('shown');					
					});
				}
			};
			
			
			
			//------ Navigation and event bindings.
			
			var navigation = {				
				scrollElement: $("html"),
				direction: function(event) {
					$.address.value($(event.target).attr('href').replace(/#/,''));
					event.preventDefault();
					return false;
				},

				menuClick: function(event) {
					var target = $.data(this, 'address');
					if(target && target.length) {
						//------ Hide menu.
						menu.removeClass('shown');
						//------ Go to article.
						$.address.value(target);
						event.preventDefault();
					}
				},

				smoothScroll: function(event) {
					event.preventDefault();
					event.stopPropagation();
					$.address.value($(this).attr('href').replace(/#/,''));
					return false;
				},

				tabClick: function(event) {
					var a = $(this);
					
					if(a.length && a.attr('href')) {
						var content = $(a.attr('href'));
						if(content.length) {
							content.siblings('div').hide();				
							var h = content.show().css('opacity', 0).stop().animate({ opacity: 1.0 }, 500).closest('.chapter').outerHeight();
							event.preventDefault();
							$('.toggle a[href=' + a.attr('href') + ']').parent().siblings().removeClass('active').end().addClass('active');				
						}
					}
					return false;
				},
				
				shuttleProduct: function(event) {
					var button = $(event.target); 
					var current = articles.data('current');

					if(button.hasClass('previous')) {
						current = (current - 1 < 0) ? articles.length - 1 : current - 1; 
					} else {
						current = (current + 1 >= articles.length) ? 0 : current + 1;
					}
					//------ Store value.
					articles.data('current', current);
					//------ Navigate to item.
					$.address.value(articles.eq(current).attr('id'));
					event.preventDefault();
				},
				
				shuttle: function(event) {
					var button = $(this);
					var shuttle = button.closest('.shuttle');
					var gallery = $.data(shuttle[0], 'target');
					var children = gallery.children();
	
					if(button.hasClass('previous')) {
						//------ Try to shuttle back one; may come up empty.
						var match = children.filter(':visible:last').prev();
		
						//------ Won't somebody please think of the children!
						children.hide();
						
						//------ Show this one or the first one.
						if(match.length) match.stop().fadeIn("slow");
						else match = children.last().stop().fadeIn("slow");						
					} else if(button.hasClass('next')) {
						var match = children.filter(':visible:last').next();
						children.hide();
						if(match.length) match.stop().fadeIn("slow");
						else match = children.first().stop().fadeIn("slow");
					}
					event.preventDefault();
		
					shuttle.find('li:last').text( (1 + children.index(match)) + '/' + children.length);
				},				
				
				showChapter: function(target) {
					var chapter = target.data("chapter");
					if(!chapter) {
				
						feedback = setTimeout(function(){
							//display loading status.
						}, 100);
						
						chapter = $("<div class='chapter'/>").appendTo(press);

	
						target.data('chapter', chapter);
						
						chapter.load(target.attr('href'), function() {
							clearTimeout(feedback);
						});
					}

					
				}
			};
			
		

			$.address.change(function(event) {
				var target = $("#" + event.pathNames[0]);
					
				if(!target.length) {
	
					target = direction.find('[href=' + event.path.replace(/^\//,'') + ']');
					if(target.length) {
						navigation.showChapter(target);
					}
				} else if(target.hasClass('textblock')) {
					navigation.scrollElement.stop().animate({ scrollTop: 0 }, 600);
					var pan = $('#wrap');								
					var i = target.data('index');								
					if($.support.transition) {
						pan.css( "-webkit-transform", "translate(-" + (25 * i) + "%,0)");
					} else {
						pan.stop().animate({ left: (-100 * i) + '%'}, 400);
					}
	
									
				} else {				
					if(event.pathNames[0] == "updates") {

						navigation.scrollElement.stop().animate({ scrollTop: press.offset().top }, 600);
						$('.chapter').hide();
						target.show();
						return;
					}

					var y = target.offset().top;
					navigation.scrollElement.stop().animate({ scrollTop: y - 200 }, 600);
				}
			});
			

			
			$(document).ready(function() {
			

				the_window = $(window);
				articles = $('#articles').find('.textblock');
				direction = $('#direction');
				press = $('#press');
				menu = $('#releases ul').delegate("li", "click", navigation.menuClick).detach();

				patches.positionarticles();
				patches.buildControls();
				patches.shuttles('#updates .gallery');

				navigation.scrollElement = (function(){
					var detected = "body";
					$('html, body').each(function(eq, elem) {
						var initScrollTop = elem.scrollTop;
						elem.scrollTop = initScrollTop + 1;
						if(elem.scrollTop == initScrollTop + 1) {
							detected = elem.nodeName.toLowerCase();
							elem.scrollTop = initScrollTop;
							return false;
						}
					});			
					return $(detected);
				})();

				$(".toggle").next().nextAll().hide();

				direction.delegate("a[rel=chapter], a[href$=updates]", "click", navigation.direction)
					   .bind('onselectstart', nope);
					   
				$('a[href^=#]').live('click', navigation.smoothScroll);				   

				press.delegate('.continued, .toggle a', 'click', navigation.tabClick);

				if($.support.rgba) {
					the_window.scroll(function() {
						var alpha = Math.min(94, the_window.scrollTop())/100;
						direction.css({backgroundColor: "rgba(255, 255, 255, " + alpha + ")" });
					});
				} else {
					direction.css({ background: "url('images/direction.png') repeat-x left bottom" });
				}		

				if($.support.transition) $('body').addClass('unboring');
			});			


			return { navigation: navigation, patches: patches };
			
		})("#post1");
