/*
 * jQuery hashchange event - v1.2 - 2/11/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,i,b){var j,k=$.event.special,c="location",d="hashchange",l="href",f=$.browser,g=document.documentMode,h=f.msie&&(g===b||g<8),e="on"+d in i&&!h;function a(m){m=m||i[c][l];return m.replace(/^[^#]*#?(.*)$/,"$1")}$[d+"Delay"]=100;k[d]=$.extend(k[d],{setup:function(){if(e){return false}$(j.start)},teardown:function(){if(e){return false}$(j.stop)}});j=(function(){var m={},r,n,o,q;function p(){o=q=function(s){return s};if(h){n=$('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow;q=function(){return a(n.document[c][l])};o=function(u,s){if(u!==s){var t=n.document;t.open().close();t[c].hash="#"+u}};o(a())}}m.start=function(){if(r){return}var t=a();o||p();(function s(){var v=a(),u=q(t);if(v!==t){o(t=v,u);$(i).trigger(d)}else{if(u!==t){i[c][l]=i[c][l].replace(/#.*/,"")+"#"+u}}r=setTimeout(s,$[d+"Delay"])})()};m.stop=function(){if(!n){r&&clearTimeout(r);r=0}};return m})()})(jQuery,this);


$(function() {
		// if($(document.body).is(".home")) $("ul.images").hide();
		   
		   
		var stage = $("div.stage");
		if(stage.length == 0) return false;
	
		var resizeTimeout;
		function setViewport(changed) {
			var newHeight = $(window).height();
			if(!$(document.body).is(".home")) newHeight = newHeight - $("div.head").outerHeight("true") - 36;
			stage.height(newHeight);
			
			var vpHeight = stage.height();
			var vpWidth = stage.width();
			var vpRatio = vpWidth / vpHeight;
		
			var img = stage.find("img");
			var imgRel = img.attr("rel");
			
			var imgSize = imgRel ? imgRel.split("x") : vpRatio;
			var imgRatio = imgSize[0] / imgSize[1];
		
			if(vpRatio <= imgRatio) {
				img.height("auto");
				img.width(vpWidth);
			} else {
				img.height(vpHeight);
				img.width("auto");
			}
		
			document.cookie = 'viewport=' + vpWidth + "x" + vpHeight + "; path=/";

			clearTimeout(resizeTimeout);
			resizeTimeout = setTimeout(function() {resizeDone(changed)}, 500);
		};
		$(window).resize(function() {setViewport(false);});
		setViewport(true);
	
		function resizeDone(changed) {
			updateImage(changed);
		};
	
		
		function updateImage(changed) {
			var hash = location.hash;
				hash = hash.replace( /^#/, '' );
			if(hash.length == 0) {
				var imgRel = $("ul.images li:first a").attr('rel');
				if(!imgRel) return false;
				if($(document.body).is(".home")) {
					$("ul.images").fadeOut("fast");
					stage.find("div.inner").load("/" + URLSegment + '/showImage/' + imgRel);
				} else {
					window.location.href = "/" + URLSegment + "/#" + imgRel;
				}
			} else if(hash == 'thumbs') {
				stage.fadeOut("fast", function() { $('ul.images').fadeIn('fast'); });
			} else {
				if(changed) {
				$("ul.images").fadeOut("fast");
				stage.fadeOut("fast", function() { $(this).find('div.inner').load('/' + URLSegment + '/showImage/' + hash, function() {
					addCaption();
					stage.fadeIn("fast");
				})});
				} else {
					stage.find("div.inner").load('/' + URLSegment + '/showImage/' + hash, addCaption);
				}
			}
		};
	
		function addCaption() {
				var hash = location.hash;
				hash = hash.replace( /^#/, '' );
				
				var imgHolder = $("ul.images a[rel=" + hash + "]");
				
				var img = imgHolder.find("img");
				var link = imgHolder.next("a");
								
				var alt = img.attr("alt");
				if(link.length > 0) {
					alt = alt + ' <a class="details" href="' + link.attr("href") + '">' + link.html() + '</a> ';
				}
				if(alt.length > 0) alt = alt + " - ";
				
				
				var total = $("ul.images li").length;
				var index = img.closest("li").index() + 1;
				
				$("<span>" + alt + index + " &middot; " + total + "</span>").appendTo(stage.find("div.inner"));
				
				$("div.preload").load("/" + URLSegment + '/showImage/' + getImage("next"));
		};
	
		$(window).bind('hashchange', function() {
			updateImage(true);
		});
	
		$("ul.images li").find("a:first").click(function(e) {
			e.preventDefault();
			window.location.href = "/" + URLSegment + "/#" + this.rel;
		});
		
		$("div.holder a").css("opacity", 0).click(function () {
			showImage(this.className);
		});
		
		function getImage(dir) {
			var hash = location.hash;
				hash = hash.replace( /^#/, '' );
				var curItem = $("ul.images li:has(a[rel=" + hash + "])");
			
				if(dir == "prev") { 
					curItem = curItem.prev();
					if(curItem.length == 0) curItem = $("ul.images li:last");
				} else if(dir == "next") {
					curItem = curItem.next();
					if(curItem.length == 0) curItem = $("ul.images li:first");
				}
				return curItem.find("a").attr('rel');
		};
		
		function showImage(dir) {
				window.location.href = "/" + URLSegment + "/#" + getImage(dir);			
		};
		
		$(document).bind('keydown', function(event) {
			var dir = 0;
			if(event.which == '37') {
				dir = "prev";
			} else if(event.which == '39') {
				dir = "next";
			}
			if(dir != 0) {
				event.preventDefault();
				showImage(dir);
			}
		});

	});
