$(document).ready(function(){
	
	$('#skip-overlay-right').hide();
	$('#skip-overlay-left').hide();
	
	$("a[rel=#overlay]").click(function(){

		$.ajax({
			url: $(this).attr('href'),
			success: function(data){
				var ani = $(".animation", data).html();
				$('#overlay').fadeIn().children('.content').html(ani).animate({
					width: 640,
					height: 480
				});
				
				$('#overlay a.close').click(function(){
					$("#overlay").fadeOut().html();
					return false;
				});
				
				$('#close-overlay').css({
					left: (640 / 2) + 5
				});
			}
		});
		
		return false;
	});
	
	$("a[rel=function-overlay]").click(function(e){
		
		var top = 0;
		
		if ($.browser.msie && $.browser.version < 7){
			top = e.pageY - 200;
		}

		var img = '<img src="'+$(this).attr('href')+'" alt="'+$(this).attr('href')+'" id="image" />';
		var text = $(this).parent().next("p").html();
		var html = img+"<p class='hidden'>"+text+"</p>";

		$('#overlay').css('top',top).fadeIn('slow').children('.content').html(html);
		$('#skip-overlay-right').hide();
		$('#skip-overlay-left').hide();
		
		setTimeout(function(){
			$('#overlay .content').animate({
				width: $('#image').width(),
				height: $('#image').height() + 110
			}).children('p').fadeIn('slow');
			
			$('#close-overlay').animate({
				left: ($('#image').width() / 2) + 5
			});
			
		},980);
		
	
		$('#overlay a').click(function(){
			$("#overlay").css('top','0').fadeOut().html();
			$(this).blur();
			return false;
		});
		
		return false;
	});
	
	var activeElement = null;
	$("a[rel=gallery]").click(function(e){
		
		activeElement = $(this);
		var top = 0;
		
		if ($.browser.msie && $.browser.version < 7){
			top = e.pageY - 200;
		}

		var img = '<img src="'+$(this).attr('href')+'" alt="'+$(this).attr('href')+'" id="image" />';
		var html = img;

		$('#overlay').css('top',top).fadeIn('slow').children('.content').html(html);
		$('#skip-overlay-right').css('top',320);
		$('#skip-overlay-left').css('top',320);
		
		setTimeout(function(){
			$('#overlay .content').animate({
				width: $('#image').width(),
				height: $('#image').height()
			}).children('p').fadeIn('slow');
			
			$('#close-overlay').animate({
				left: ($('#image').width() / 2) + 5
			}).fadeIn();
			
			$('#skip-overlay-right').animate({
				left: ($('#image').width() / 2) - 25
			}).fadeIn();
			$('#skip-overlay-left').animate({
				left: -($('#image').width() / 2) - 5
			}).fadeIn();
			
		},980);
		
	
		$('#overlay a.close').click(function(){
			$("#overlay").css('top','0').fadeOut().html();
			activeElement = null;
			$(this).blur();
			return false;
		});
		
		return false;
	});
	
	var galleryImages = new Array();
	var next = 0;
	var prev = 0;
	
	$("a[rel=gallery]").each(function(){
		galleryImages.push($(this));
	});
	
	$(window).keydown(function(e){
		
		if (activeElement != null) {
			
			switch (e.keyCode) {
				
				case 39:
					$.each(galleryImages, function(i){
						
						if (this.attr('href') == activeElement.attr('href')) {
							next = i + 1; 
							return 0;
						}
					});
					if (next < galleryImages.length) {
						
						activeElement = galleryImages[next];
						
						var img = '<img src="'+activeElement.attr('href')+'" alt="'+activeElement.attr('href')+'" id="image" />';
						var html = img;
				
						$('#overlay').children('.content').html(html);
						
						setTimeout(function(){
							$('#overlay .content').animate({
								width: $('#image').width(),
								height: $('#image').height()
							}).children('p').fadeIn('slow');
							
							$('#close-overlay').animate({
								left: ($('#image').width() / 2) + 5
							});
							$('#skip-overlay-right').animate({
								left: ($('#image').width() / 2) - 20
							});
							$('#skip-overlay-left').animate({
								left: -($('#image').width() / 2) - 10
							});
							
						},980);
						
					}
					else 
						activeElement = activeElement;
						
				break;
				
				case 37:
					$.each(galleryImages, function(i){
						
						if (this.attr('href') == activeElement.attr('href')) {
							prev = i - 1; 
							return 0;
						}
					});
					if (prev >= 0) {
						
						activeElement = galleryImages[prev];
						
						var img = '<img src="'+activeElement.attr('href')+'" alt="'+activeElement.attr('href')+'" id="image" />';
						var html = img;
				
						$('#overlay').children('.content').html(html);
						
						setTimeout(function(){
							$('#overlay .content').animate({
								width: $('#image').width(),
								height: $('#image').height()
							}).children('p').fadeIn('slow');
							
							$('#close-overlay').animate({
								left: ($('#image').width() / 2) + 5
							});
							$('#skip-overlay-right').animate({
								left: ($('#image').width() / 2) - 20
							});
							$('#skip-overlay-left').animate({
								left: -($('#image').width() / 2) - 10
							});
							
						},980);
						
					}
					else 
						activeElement = activeElement;
						
				break;
				
			}
			
		}
	});
	
	$('a.skip-right').click(function(){
		$(this).blur();
		
		$.each(galleryImages, function(i){
			//alert("!");		
			if (this.attr('href') == activeElement.attr('href')) {
				next = i + 1; 
				return this;
			}
		});
		if (next < galleryImages.length) {
			
			activeElement = galleryImages[next];
			
			var img = '<img src="' + activeElement.attr('href') + '" alt="' + activeElement.attr('href') + '" id="image" />';
			var html = img;
			
			$('#overlay').children('.content').html(html);

			setTimeout(function(){
				$('#overlay .content').animate({
					width: $('#image').width(),
					height: $('#image').height()
				});
				
				$('#close-overlay').animate({
					left: ($('#image').width() / 2) + 5
				});
				$('#skip-overlay-right').animate({
					left: ($('#image').width() / 2) - 20
				});
				$('#skip-overlay-left').animate({
					left: -($('#image').width() / 2) - 10
				});
				
			}, 980);
	
		}
		else {
			activeElement = activeElement;
		}

		return false;
	});
	
	$('a.skip-left').click(function(){
		$(this).blur();
		$.each(galleryImages, function(i){
						
			if (this.attr('href') == activeElement.attr('href')) {
				prev = i - 1; 
				return 0;
			}
		});
		if (prev >= 0) {
			
			activeElement = galleryImages[prev];
			
			var img = '<img src="'+activeElement.attr('href')+'" alt="'+activeElement.attr('href')+'" id="image" />';
			var html = img;
	
			$('#overlay').children('.content').html(html);
			
			setTimeout(function(){
				$('#overlay .content').animate({
					width: $('#image').width(),
					height: $('#image').height()
				}).children('p').fadeIn('slow');
				
				$('#close-overlay').animate({
					left: ($('#image').width() / 2) + 5
				});
				$('#skip-overlay-right').animate({
					left: ($('#image').width() / 2) - 20
				});
				$('#skip-overlay-left').animate({
					left: -($('#image').width() / 2) - 10
				});
				
			},980);
			
		}
		else 
			activeElement = activeElement;
			
		return false;
	});
	
});