/*

--------------------------------------------------------->8---

AUTHOR		g madison / ohsoso.com
CLIENT		agent evers
VERSION		1.7 more stuff and edits

--------------------------------------------------------------

*/

document.documentElement.className += " js";

$(document).ready(function(){

	// HIDE INITIALLY
	
//	$('.cat').hide();
	$('#thumbs img, #first img, #cols img, .cat').hide();
	$('#thumbs img, #first img, #cols img').one('load', function(){
			$(this).fadeTo(200, 1);
	})
	.each(function(){
			if(this.complete) $(this).trigger('load');
	});
		
	// LOADING MESSAGE
	
	$(window).load(function(){ 
			$("#load").fadeOut(200);
	});
	
	// SHOW PHOTOGRAPHERS IMAGE ON FIRST PAGE
	
	var cat = [3,4,19,19,20,22,29,36,37,28,30,31,32,33];

	$.each(cat, function(index, item) {
			$('#p' + item).hover(function() {
					$('#first li').stop(true, false).fadeTo(200, 0.1);
					$('li.' + item).stop(true, false).fadeTo(0, 1);
//					$('.cat').hide();
//					$('#cat' + item).show();
					$('.cat').stop(true, true).fadeOut(200);
					$('#cat' + item).stop(true, true).fadeIn(200);
			}, function() {
					$('#first li').fadeTo(200, 1);
					$('.cat').stop(true, true).fadeOut(200);
			});
	});

	// SHOW CONTACT
	
	$('#logo').hover(function() {
			$('#contact dt').css('color', '#000');
			$('#contact a').addClass('activehover');
    }, function() {
			$('#contact dt').css('color', '#999');
			$('#contact a').removeClass('activehover');
    });
	
	// SHOW NAME ON HOVER
	
	$('#first li').hover(function() {
			var showname = $(this).attr('class');
			$('#p' + showname + ' a').addClass('activehover');
    }, function() {
			var showname2 = $(this).attr('class');
			$('#p' + showname2 + ' a').removeClass('activehover');
    });
	
	// SHOW CATEGORY ON HOVER

	$('#thumbs li').hover(function() {
			var showcat = $(this).attr('class').split(" ")[0];
			$('#' + showcat + ' a').addClass('activehover');
	}, function() {
			var showcat2 = $(this).attr('class').split(" ")[0];
			$('#' + showcat2 + ' a').not('.active').removeClass('activehover');
	});
	
	// IMAGE GALLERY
	
	$('#thumbs img').click(function() {
				$('html,body').stop(true, false).animate({ scrollTop: 0 }, 400);
	});

	var thumbs = $('#thumbs li'),
		 next = $('#next'),
		 prev = $('#prev'),
		 imageIndex = 0;

	thumbs.click(function() {
	
			var showcat = $(this).attr('class').split(" ")[0];
			var imgpath = $('a', this).attr('href');
//			var imgpathnext = $('a', this).parent().next().children().attr('href');
			var imgname = $('a', this).attr('href').replace('/images/', '').replace('.jpg', '');
			
			$('#next, #prev').show();
			$('#load').show();
			
			// CATEGORY
	
			$('#' + showcat + ' a').addClass('active').parent().siblings().children().removeClass('active').removeClass('activehover');
			$(thumbs).fadeTo(200, 1);
			$(this).fadeTo(50, 0.1);
			$('#imglrg').css('padding', '0').fadeTo(0, 0.1).attr({ src: imgpath }).load(function() { 
					$(this).fadeIn(10);
					var imgheight = $(this).height();
					$('#img').stop(true, false).animate({ height: imgheight }, 200);
					$('#prev, #next').height(imgheight);
					
//			$('#imglrgnext').fadeTo(50, 0.2).attr({ src: imgpathnext });

			$('#load').fadeOut(200);
			});

			window.location.hash = imgname;
			imageIndex = thumbs.index(this);
			return false;
	});
			
	next.click(function() {
			if (imageIndex !== thumbs.length) { 
					imageIndex++;
					thumbs.eq(imageIndex).click();
					$('#imglrg').css('padding', '0');
			} else {
					next.hide();
			}
			return false;
	});
			
	prev.click(function() {
			if (imageIndex !== 0) {
					imageIndex--;
					thumbs.eq(imageIndex).click();
					$('#imglrg').css('padding', '0');
			} else {
					prev.hide();
					$('#imglrg').css('padding', '0 0 0 28px');
			}
			return false;
	});
	
	// KEYBOARD NAVIGATION
	
	$(document.documentElement).keyup(function (event) {
			var direction = null;
			if (event.keyCode == 37) {
					prev.click();
			} else if (event.keyCode == 39) {
					next.click();
			}
	});
	
	// IF DEEP LINKED
	
	if(window.location.hash.length){
			imgpath2 = '/images/' + window.location.hash.replace('#','') + '.jpg';
			imageIndex = $(thumbs).index($('li:has(a[href="' + imgpath2 + '"])'));
			$('#next, #prev').show();
			$('#imglrg').attr({ src: imgpath2 }).load(function() { 
					$(this).fadeTo(400, 1);
					var imgheight = $(this).height();
					$('#img').stop(true, false).animate({ height: imgheight }, 200);
					$('#prev, #next').height(imgheight);
			});
			$('li:has(a[href="' + imgpath2 + '"])').delay(400).fadeTo(50, 0.1);
	} else {
			$('#imglrg').load(function() { 
					$(this).fadeTo(400, 1);
			});
	};

	return false;

});
