/*
  Matt Flynn
  http://mattflynn.net
  http://fertileground.ac

  Email:  matt@fertileground.ac

*/



var dragging = false;
var moving = false;
var zIndex = 1;
var ajax = true;
var dragBorder = 50;
var minWidth = 100;

function adjustDraggable(img) {
	var maxX = $("#images").width()-dragBorder;
	var maxY = $("#images").height()-dragBorder;
	if (img.position().left>maxX) {
		img.css("left", maxX+"px");
	}
	if (img.position().top>maxY) {
		img.css("top", maxY+"px");
	}
	img.draggable("option", "containment", [dragBorder-img.width(), dragBorder-img.height(), maxX, maxY])
}

function adjustResizable(img) {
	var x = img.position().left;
	var y = img.position().top;
	var minW = minWidth;
	var minH = minWidth/img.data("aspectRatio");
	if (x<dragBorder) {
		minW = Math.max(minW, dragBorder-x);
		minH = minW/img.data("aspectRatio");
	}
	if (y<dragBorder) {
		minH = Math.max(minH, dragBorder-y);
		minW = minH/img.data("aspectRatio");
	}
	img.resizable("option", "minWidth", minW);
	img.resizable("option", "minHeight", minH);
}

function init() {
	function navigateTo(url) {
		if (!ajax) {
			location.href = url;
			return true;
		}

		$("#content").load(url+" #content", function() {
			init();
		});
	}

	var cookieOptions = {
		path: "/",
		expires: 30
	};
	if ($.cookie("z")) {
		zIndex = $.cookie("z");
	}
	var imgCount = $(".image").length;
	var i = 0;
	function toFront(img) {
		img.css("z-index", (img.hasClass("level2") ? 5000 : 0)+(++zIndex));
		$.cookie($(img).attr("id")+"_z", zIndex, cookieOptions);
		$.cookie("z", zIndex, cookieOptions);
	}
	$(".image")
		.wrapAll('<div id="images"></div>')
		.each(function() {
			i++;
			var img = $(this);
			var imgImg = img.find("img");
			var maxWidth = imgImg.attr("width");
			var maxHeight = imgImg.attr("height");
			var aspectRatio = maxWidth/maxHeight;
			img.data("aspectRatio", aspectRatio);
	
			function dragStart() {
				moving = true;
				toFront(img);
			}
			function dragStop() {
				moving = false;
				adjustResizable(img);
				$.cookie(img.attr("id")+"_p", img.position().left+","+img.position().top, cookieOptions);
			}
			function resize() {
				adjustDraggable(img);
				img
					.children(".frame").css({
						height: img.height()-2, 
						width: img.width()-2
					})
					.children(".frame2").css({
						height: img.height()-4, 
						width: img.width()-4
					});
				imgImg.css({
					height: img.height(), 
					width: img.width()
				});
			};
			function resizeStart() {
				moving = true;
			}
			function resizeStop() {
				moving = false;
				$.cookie(img.attr("id")+"_s", img.width(), cookieOptions);
			}
	
			var p = $.cookie(img.attr("id")+"_p");
			p = p ? p.split(",") : [];
			var s = $.cookie(img.attr("id")+"_s");
			var z = $.cookie(img.attr("id")+"_z");
			var angle = i/imgCount*2*Math.PI;
			var radius = 200*Math.random();
			var w = s==null ? maxWidth*(0.2+0.3*Math.random()) : s;
			var h = w/aspectRatio;
			img
				.css({
					left: (p.length==2 ? p[0] : $(window).width()/2+radius*Math.cos(angle)-w/2)+"px",
					top: (p.length==2 ? p[1] : $(window).height()/2+radius*Math.sin(angle)-h/2)+"px",
					height: h+"px",
					width: w+"px",
					zIndex: (img.hasClass("level2") ? 5000 : 0)+parseInt(z)
				})
				.append(
					$('<div class="frame"><div class="frame2"></div></div>')
						.click(function() {
							if (dragging) {
								dragging = false;
								return false;
							}
							navigateTo(img.find("a").attr("href"));
						})
				)
				.draggable({
					start: function() {
						dragging = true;
						dragStart();
					},
					stop: dragStop
				})
				.resizable({
					aspectRatio: true,
					maxWidth: maxWidth,
					maxHeight: maxHeight,
					minWidth: minWidth,
					minHeight: minWidth/aspectRatio,
					resize: resize,
					start: resizeStart,
					stop: resizeStop
				});
			img[0].addEventListener("touchstart", function(e) {
				if (e.touches.length!=1) {
					return;
				}
				e.preventDefault();
				var touch = e.touches[0];
				$(this).data({
					deltaX: touch.clientX-$(this).position().left,
					deltaY: touch.clientY-$(this).position().top
				});
				dragStart();
			}, true);
			img[0].addEventListener("touchmove", function(e) {
				if (e.touches.length!=1) {
					return;
				}
				e.preventDefault();
				var touch = e.touches[0];
				$(this).css({
					left: (touch.clientX-$(this).data("deltaX"))+"px",
					top: (touch.clientY-$(this).data("deltaY"))+"px"
				});
				dragStop();
			}, true);
			img[0].addEventListener("touchend", dragStop, true);
			img[0].addEventListener("gesturestart", function(e) {
				e.preventDefault();
				$(this).data({
					x: $(this).position().left,
					y: $(this).position().top,
					width: $(this).width(),
					height: $(this).height()
				});
				resizeStart();
			}, true);
			img[0].addEventListener("gesturechange", function(e) {
				e.preventDefault();
				var newWidth = Math.max(minWidth, Math.min($(this).data("width")*e.scale, maxWidth));
				var newHeight = newWidth/aspectRatio;
				$(this).css({
					left: ($(this).data("x")-(newWidth-$(this).data("width"))/2)+"px",
					top: ($(this).data("y")-(newHeight-$(this).data("height"))/2)+"px",
					width: newWidth+"px",
					height: newHeight+"px"
				});
				resize();
			}, true);
			img[0].addEventListener("gestureend", resizeStop, true);
			img.hover(
				function() {
					if (moving) {
						return;
					}
					$("#series-title span")
						.text(img.find("img").attr("alt"))
						.show();
					toFront(img);
				},
				function() {
					$("#series-title span").hide();
				}
			);
			resize();
			resizeStop();
			dragStop();
		});
	$("#logo a,#menu1 a,#menu2 a,#prevnext a,#text a.internal").click(function() {
		this.blur();
		navigateTo(this.href);
		return false;
	});
	$("#text a:not(.internal)").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});
	$("#text hr").each(function() {
		var prev = $(this).prev("p");
		if (prev.length==1) {
			prev.css("margin-bottom", 0);
		}
		$(this).wrap('<div class="rule"></div>');
	});
	$("#text > *:last-child").css("margin-bottom", 0);
	$("#image").each(function() {
		$('<div class="frame"></div>')
			.css({
				height: $(this).height()-2,
				width: $(this).width()-2
			})
			.append(
				$('<div class="frame2"></div>')
				.css({
					height: $(this).height()-4,
					width: $(this).width()-4
				})
				.click(function() {
					navigateTo($(this).closest("#image").find("a").attr("href"));
				})
			)
			.appendTo(this);
	});
	$("#overlay").click(function() {
		navigateTo("/");
	});
	$(window).resize();
}

$(function() {
	$(window).resize(function() {
		$("#images").css({
			height: Math.max($(window).height(), $("#series").height()),
			width: $(window).width()
		});
		$(".image").each(function() {
			adjustDraggable($(this));
		});
	});
	init();
});

$(document).ready(function(){
	
	$('.gallery-auto').bxSlider({
		auto: true,
		controls: false,
		mode: 'fade',
		pause: '6000'
	});	

});





