// Drag-slide settings
var slideAmount = 5; // Based on drag speed
var slideTime = 20; // Based on drag speed
var edgeBounceTime = 350;

// Swap settings
var fadeOut = 500;
var fadeIn = 200;
var loadingIn = 2000;
var loadingOut = 200;

// Hover-fade settings
var hoverFadeOut = 200;
var hoverFadeIn = 150;

// Lightbox hack
var lightboxEnabled = true;
var lightboxShrink = 95;  // Percent

jQuery.extend(true, {
	easing: 
	{
		edgeease:function(p, n, firstNum, diff) {
			var c=firstNum+diff;
			var s = 1.70158; // default overshoot value, can be adjusted to suit
			return c*((p=p/1-1)*p*((s+1)*p + s) + 1) + firstNum;
		},

		myease:function(p, n, firstNum, diff) {
			var c=firstNum+diff;
			return c*((p=p/1-1)*p*p*p*p + 1) + firstNum;
		},
		
		elastic:function (x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		},
		
		bounce: function (x, t, b, c, d) {
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		}
	}
});

$(document).ready(function() {
	if ($('#header').size() > 0)
		initHoverFade(false, '#header');
	else
		initHoverFade(false, '#navigation');
});

function initHoverFade(overlay, selector) {
	if (overlay == null) { overlay = false; }
	if (selector == null) { selector = ''; }
	
	$(selector + ' .hover-fade .over').not('.active').parent().mouseover(function() {
		$(this).children('.over').fadeOut(hoverFadeOut);
	}).mouseleave(function() {
		$(this).children('.over').stop(true, true).fadeIn(hoverFadeIn);
	});
	
	if (overlay) {
		$(selector + ' .hover-fade').parent().append('<div class="overlay hover-remove"></div>');
		$('.overlay').css('width', $('.window').css('width'));
		$(selector + ' .hover-remove').mouseover(function() { $(this).remove(); } );
	}
}

function initHoverThumbs() {
	$('.thumbnails img').imgpreload(function() {
		$('.thumbnails li').each(function() {
			var img = $(this).find('img');
			height = img.outerHeight();
			width = img.outerWidth();
			
			$(this).width(width).height(height);
			
			$(this).hover(function() { 
				img.width(width * 0.9).height(height * 0.9);
				img.css('margin-top', 4).css('margin-left', 3);
			}, function() {
				img.css('margin-top', 0).css('margin-left', 0); 
				img.width(width).height(height);
			});
		});
	});
}

function initDraggable() {
	speed = null;
	lastx = null;
	startx = null;
	
	$(".fade-left, .fade-right").height($('.draggable > div').height());
	$('.window').width($(window).width() - parseInt($('body').css('margin-left')) -  parseInt($('body').css('margin-right')) - $('.left-links').outerWidth() - 20);

	$('.draggable').draggable({
		axis: 'x',
		start: function(event, ui) {
			startx = ui.position.left;
			lightboxEnabled = false; 
		},
		drag: function(event, ui) {
			if (lastx) { speed = lastx - ui.position.left; }
			lastx = ui.position.left;
		},
		stop: function(event, ui) {
			easeEffect = "myease";
			if (speed > 30) { speed = 30; }
			if (speed < -30) { speed = -30; }
			
			easeSpeed = (Math.abs(speed) * slideTime);

			endpos = lastx - (speed * slideAmount);
			if (endpos < - $(this).children().width() + $(this).parent().width()) { 
				endpos = - $(this).children().width() + $(this).parent().width() + 20; easeEffect = "edgeease"; easeSpeed = edgeBounceTime; 
			}
			
			if (endpos > 0) { 
				endpos = 0; 
				easeEffect = "edgeease"; 
				easeSpeed = edgeBounceTime; 
			}
			
			if (lastx != endpos) {
				$(this).animate({ "left" : endpos },{queue:false, duration:easeSpeed, easing:easeEffect, complete: function() { lightboxEnabled = true; }});
			}
			
			if (startx - lastx == 0) {
				lightboxEnabled = true;
			}
		}
	});
	
	$('.content img').imgpreload(function() {
		$('.draggable').append('<div class="overlay click-fade"></div>');
		$('.overlay').css('width', $('.window').css('width'));
		$('.click-fade').mousedown(function() { $(this).remove(); } );
	});
}

var hiding;
var swapNum = 0;

function swap(images, text, outTime, inEase, inTime) {
	//if (!this.JSON) { JSON = $.getJSON(); }
	if (text) { text = eval('(' + text + ')'); }
	if (outTime == null) { outTime = fadeOut; }
	if (inTime == null) { inTime = fadeIn; }
	
	var mySwap = ++swapNum;
	var content = $('.content');
	
	if (hiding != null) {
		hiding.stop(true).remove();
	}
	
	var imgs = [];
	
	// Add each hover-fade pair or image to the preload array
	$.each(images, function() {
		if (this.hoverFade) {
			imgs.push(this.hoverFade[0]);
			imgs.push(this.hoverFade[1]);
		} else if (this.lightbox) {
			imgs.push(this.lightbox[0]);
		} else if(this.clickShow || this.media) {
			// do nothing
		} else {
			imgs.push(this);
		}
	});
	
	var removal = content.children();
	var container = ($('.swap-container').length > 0) ? $('.swap-container') : $('.content');
	hiding = container.clone().css('position', 'absolute').css('left', container.position().left).insertAfter(container).fadeOut(outTime, function() { $(this).remove(); });
	removal.remove();

	// Load the images
	$.imgpreload(imgs, function() {
		if (mySwap != swapNum) { return; };
		
		loaded = this;
		myContent = $("<div></div>");

		srcs = [];
		for (var i = 0 ; i < loaded.length ; i++) {
			srcs.push(loaded[i].src);
		}
		
		// Build the hover-fade or the regular image
		$.each(images, function() {
			hf = $('<div class="hover-fade"></div>');
			if(this.hoverFade) {
				myContent.append(hf
					.append($('<div class="over"></div>').append(loaded[$.inArray(location.protocol + '//' + location.host + this.hoverFade[0], srcs)]))
					.append($('<div class="under"></div>').append(loaded[$.inArray(location.protocol + '//' + location.host + this.hoverFade[1], srcs)])));
			} else if (this.media) {
				youtube = "";
				if (this.media.indexOf("youtube") >= 0 || this.media.indexOf("vimeo") >= 0) { youtube = ", type:'swf'"; }
				myContent.append($('<a class="media {width:' + this.width + ', height:' + this.height + youtube + '}" href="' + this.media + '"></a>'));
			} else if (this.lightbox) {
				myContent.append($('<a class="lightbox" href="' + this.lightbox[1] + '"></a>').append(loaded[$.inArray(location.protocol + '//' + location.host + this.lightbox[0], srcs)]));
			}else {
				myContent.append(loaded[$.inArray(location.protocol + '//' + location.host + this, srcs)]);
			}
		});
		
		if (text) { myContent.append('<div class="clear"></div>').append(formatText(text)); }
		
		content.append(myContent);
		initHoverFade(true, '.content');
		
		if (inEase == null) {
			content.css('left', 0).hide().fadeIn(inTime);
		} else {
			content.css("left", -250).animate({ left: 0 }, inTime, inEase);
		}
		
		$('#loading').stop(false,true).fadeOut(loadingOut);
		$('a.media').media();
		$('a.lightbox').each(function() { initZoom(this); });
		container.children(".fade-left, .fade-right").height(content.children().height());
	});
	
	$('#loading').fadeIn(loadingIn);
}

function initZoom(element) {
	var img = $(element).find('img');
	var height = img.outerHeight();
	var width = img.outerWidth();
	
	$(element).width(width).height(height);
	
	$(element).hover(function() { 
		img.width(width * lightboxShrink / 100).height(height * lightboxShrink / 100);
		img.css('margin-top', height * (100 - lightboxShrink) / 200).css('margin-left', width * (100 - lightboxShrink) / 200);
	}, function() {
		img.css('margin-top', 0).css('margin-left', 0); 
		img.width(width).height(height);
	});
	
	$(element).lightBox().hover();
}

function formatText(text, theClass) {
	if (theClass == null) { theClass = "credits"; }
	
	var c = $('<ul class="' + theClass + '"></ul>');

	if (text.title) {
		c.append('<li class="title">' + text.title + '</li>');
	}
	
	if (text.quote) {
		c.append('<li class="quote">' + text.quote + '</li>');
	}
	
	if (text.items) {
		for (var i = 0 ; i < text.items.length ; i++) {
			c.append('<li>' + text.items[i] + '</li>');
		}
	}
	
	if (text.sections) {
		for (var i = 0 ; i < text.sections.length ; i++) {
			c.append(formatText(text.sections[i], 'section'));
		}
	}
	
	return c;
}





