var currSet = 'set2';
var loadedImgs = new Array();
var startCheck = new Date().getTime();
var pageWidth;
var mainOffset;
$(function() {
	var subNavs = $('div.subNav');
	var navOffset = 32;
	if ($.browser.msie && $.browser.version <= 7) navOffset = 0;
	subNavs.each(function() {
		var $this = $(this);
		var link = $this.prev('a');
		var center = link.width() / 2 + navOffset;
		$this.css('left', center - ($this.width() / 2));
	}); 
	subNavs.hide();
	if ($(window).width() < 982) $(window).width(982);
	if ($(window).height() < 650) $(window).height(650);
	pageWidth = $(window).width();
	mainOffset = (pageWidth - $('#main').width()) / 2;
	$('#images').css('left', -mainOffset);
	$('#navSections').find('a.groupLink').mouseover(function() {
		$(this).attr('_count', parseInt($(this).attr('_count')) + 1);
		clearTimeout($(this).attr('_timer'));
		if ( ! $(this).hasClass('hover')) {
			$('li.section > a.hover').each(function() {
				hideMenu('#' + this.id);
				clearTimeout($(this).attr('_timer'));
			});
			$(this).addClass('hover');
			if ($.support.opacity) {
				$('#' + this.id + 'Sub').fadeIn(300);
			} else {
				$('#' + this.id + 'Sub').show(0);
			}
		}
	}).mouseout(function() {
		var newCount = parseInt($(this).attr('_count')) - 1;
		$(this).attr('_count', newCount);
		if (newCount <= 0 && $(this).hasClass('hover')) {
			$(this).attr('_count', 0);
			var id = '#' + this.id;
			$(this).attr('_timer', setTimeout(function() {
				hideMenu(id);
			}, 300));
		}
	}).attr('_count', 0);
	$('div.subNav a').mouseover(function() {
		$(this).parent().children('span').text($(this).children('img').attr('alt'));
	}).mouseout(function() {
		$(this).parent().children('span').html('&nbsp;');
	}).click(function() {
		$(this).parent().hide(0);
	});
	$('div.subNav').mouseover(function() {
		$('#' + this.id.substr(0, this.id.length - 3)).trigger('mouseover');
	}).mouseout(function() {
		$('#' + this.id.substr(0, this.id.length - 3)).trigger('mouseout');
	});

	if ($('#main').hasClass('home')) {
		$('div.homeBgImg').hide();
		$('div.current').show();
		setInterval(function() {
			var currId = $('#content div.current').attr('id');
			var nextId = parseInt(currId.substr(2)) + 1;
			if ($('#bg' + nextId).size() == 0) {
				nextId = 1;
			}
			nextId = 'bg' + nextId;
			$('#' + currId).fadeOut(1500).removeClass('current');
			$('#' + nextId).fadeIn(1500).addClass('current');
		}, 6500);
	}
	$('#images img').bind('load', fixImagesWidth);
	setTimeout(timedFixImagesWidth, 500);

	if (isiPad) {
		$('#images img').bind('touchstart', function(e) {
			if (e.originalEvent.targetTouches.length != 1) return false;

			var $this = $(this);
			var $imgs = $('#images');
			var startX = e.originalEvent.targetTouches[0].clientX;
			var left = $imgs.position()['left'];
			var lastLeft = 0;
			var lastTime = new Date();
			var lastDelta = e.originalEvent.targetTouches[0].clientX;

			var touchMove = function(e) {
				if (e.originalEvent.targetTouches.length != 1) return false;
				e.preventDefault();
				lastDelta = lastLeft - e.originalEvent.targetTouches[0].clientX;
				lastTime = new Date();
				lastLeft = e.originalEvent.targetTouches[0].clientX;

				$imgs.css('left', left - (startX - e.originalEvent.targetTouches[0].clientX));
				return false;
			};
			var touchEnd = function(e) {
				$this.unbind('touchmove', touchMove);
				$this.unbind('touchend', touchEnd);

				var endLeft = $imgs.position()['left'];
				console.log(endLeft + ' ? ' + ($imgs.width() - pageWidth) + ' ? ' + (-pageWidth));
				if (endLeft > 0) {
					// need to move it back to the correct position
					$imgs.animate({'left': 0}, 500);
				} else if (endLeft < -($imgs.width() - pageWidth)) {
					// need to move it back to the correct position
					$imgs.animate({'left': -($imgs.width() - pageWidth)}, 500);
				} else {

				}
			};

			$this.bind('touchmove', touchMove).bind('touchend', touchEnd);
			return false;
		});
	} else {
		$('#scrollHandle').draggable({axis: 'x', containment: '#scrollbar', drag: function(event, ui) {
				var pct = ($(this).position()['left'] - 23) / 924;
				if (pct > 1) {
					pct = 1;
				}

				$('#images').css('left', -(($('#images').width() - pageWidth) * pct + mainOffset));
		}});
		$('#images').mousewheel(function(evt, delta) {
			delta = delta * 10;
			var list = $(this);
			var left = parseInt(list.css('left'));
			var width = $('#container').width() - (list.width() + mainOffset);
			if (delta < 0) {
				if (width < (left + mainOffset)) {
					if (delta + left < width) delta = width - left;
					list.css('left', left + delta);
				}
			} else {
				if (0 > left + mainOffset) {
					if (delta + left > 0) delta = -left;
					list.css('left', left + delta);
				}
			}
			// readjust scroll handle
			pct = -(left + mainOffset) / (list.width() - pageWidth);
			if (pct > 1) {
				pct = 1;
			} else if (pct < 0) {
				pct = 0;
			}
			$('#scrollHandle').css('left', (($('#scrollbar').width() - 10) * pct)); // 10 for the width of the handle
			return false;
		});
	}

	$('#imageInfo').mouseover(function() {
		$('div.imageInfo').each(function() {
			$(this).animate({'top': 517 - $(this).height()}, 200);
		});
	}).mouseout(function() {
		$('div.imageInfo').each(function() {
			$(this).animate({'top': 533}, 200);
		});
	}).click(function() { return false; });
	$('#portfolioInfo').hide();

	$('#portfolioCredits').mouseover(function() {
		$('#portfolioInfo').fadeIn(200);
	}).mouseout(function() {
		$('#portfolioInfo').fadeOut(200);
	}).click(function() { return false; });


	$('img').bind('contextmenu', function(e){
		alert('All images are copyrighted by Courtney Weston.  Duplication is prohibited.');
		return false;
	});
	$(window).resize(function() {
		pageWidth = $(window).width();
		mainOffset = (pageWidth - $('#main').width()) / 2;
	});

	// Email obfuscator script 2.1 by Tim Williams, University of Arizona
	// Random encryption key feature by Andrew Moulden, Site Engineering Ltd
	// This code is freeware provided these four comment lines remain intact
	// A wizard to generate this code is at http://www.jottings.com/obfuscator/
	coded = "0nvE@0nvEWRvdXR4b4SJvXRs.e4z";
	key = "7EoA9rnmUYg6iCBse1Hb8MRvXWlNK54FQJdzIxSVjuGD03q2ZycfPtLOhapkwT";
	shift=coded.length;
	link="";
	for (i=0; i<coded.length; i++) {
		if (key.indexOf(coded.charAt(i))==-1) {
			ltr = coded.charAt(i);
			link += (ltr);
		} else {
			ltr = (key.indexOf(coded.charAt(i))-shift+key.length) % key.length;
			link += (key.charAt(ltr));
		}
	}
	$('#contactMail').attr('href', 'mailto:' + link);


});
function fixImagesWidth() {
	var width = 0;
	$('#images img, #images object, #images iframe').each(function() {
		width += $(this).width();
	});
	$('#images').width(width);
}
function timedFixImagesWidth() {
	fixImagesWidth();
	if (new Date().getTime() - startCheck < 60000) {
		setTimeout(timedFixImagesWidth, 200);
	}
}
function hideMenu(id) {
	if (parseInt($(id).attr('_count')) == 0) {
		if ($.support.opacity) {
			$(id + 'Sub').fadeOut(300);
		} else {
			$(id + 'Sub').hide(0);
		}
		$(id).removeClass('hover');
	}
}
