/* jQUERY
--------------------------------------------------*/
$(document).ready(function() {
	
	// Preload images
	$.preloadImages('/framework/images/caption.png', '/framework/images/UILoader.gif', '/framework/images/UILoader.png');
	
	// NAVIGATION SETUP
	$('div#site-navigation ul ul').each(function() {
		$(this).prepend('<li class="top">');
		$('li:last',this).addClass('last');
	});
	
	// NAVIGATION HOVER/DROP-DOWN
	$('div#site-navigation ul > li').hover(
		function() {
			$this = $(this);
			
			$('a',this).addClass('hover');
			$('ul',this).css({
				top: 26,
				left: ($this.width() + 1 - 252)/2
			});
		},
		function() {
			$('a',this).removeClass('hover');
			$('ul',this).css({
				top: -9999,
				left: -9999
			});
		}
	);
	
	// IMAGE GALLERY
	if ($('body').is('#portfolio')) {
		$('ul#image-thumbs li:first').addClass('selected');
		
		$('body#portfolio div#image-container').append('<div class="UILoader"><img src="/framework/images/home-UILoader.gif" height="66" width="66" /></div>');
		
		$('<img class="preload" />').css('opacity',0).attr('src',$('ul#image-thumbs li:first a').attr('href')).load(function() {
			$img = $(this);
			
			$('div#image-container').append($img);
			$('div#image-container').append('<img src="/framework/images/please-dont-steal-my-images.gif" id="overlay" />');
			$('div.UILoader').remove();
			$img.removeClass('preload').animate({
				opacity: 1
			});
			
			$.imageCaption();
		});
	}
	
	$('ul#image-thumbs a').click(function() {
		$(this).parent('li').addClass('preselected');
		$.loadImage();
		
		return false;
	});
	
	// IMAGE NAVIGATION
	$('a#image-nav-prev').click(function() {
		$current = $('ul#image-thumbs li.selected');
		$previous = $('ul#image-thumbs li.selected').prev('li');
		
		$current.removeClass('selected');
		if (!$previous.html()) {
			$('ul#image-thumbs li:last').addClass('preselected');
		} else {
			$previous.addClass('preselected');
		}
		$.loadImage();
		
		return false;
	});
	$('a#image-nav-next').click(function() {
		$current = $('ul#image-thumbs li.selected');
		$next = $('ul#image-thumbs li.selected').next('li');
		
		$current.removeClass('selected');
		if (!$next.html()) {
			$('ul#image-thumbs li:first').addClass('preselected');
		} else {
			$next.addClass('preselected');
		}
		$.loadImage();
		
		return false;
	});
	
	$(document).keyup(function(event) {
		if (event.keyCode == 39 || event.keyCode == 40) {
			$('a#image-nav-next').trigger('click');
		} else if (event.keyCode == 37 || event.keyCode == 38) {
			$('a#image-nav-prev').trigger('click');
		}
	});
	
	// Disable right-click context menu on community images
	$('div#image-container img').live('contextmenu',function(e) {
		return false;
	});
	
	// Home page image
	$('body#home div#image-container').append('<div class="UILoader"><img src="/framework/images/home-UILoader.gif" height="66" width="66" /></div>');
	
	$('<img class="preload" />').css('opacity',0).attr('src',$('body#home span#image-src').text()).load(function() {
		
		$img = $(this);
		
		$('div#image-container').append($img);
		$('div#image-container').append('<img src="/framework/images/please-dont-steal-my-images.gif" id="overlay" />');
		$('div.UILoader').remove();
		
		$img.removeClass('preload').animate({
			opacity: 1
		});
		
		// Caption
		if ($('body#home span#image-caption').html() != '') {
			$('div#site-content').append('<a href="#" id="caption-link">' + $('body#home span#image-caption').html() + '</a>');
			
			$('a#caption-link').css({
				right: (parseInt($('div#image-container').width()) - parseInt($('div#image-container img').outerWidth()))/2 + 'px'
			});
			
			$('body#home a#caption-link').toggle(
				function() {
					$('div#image-container').append('<div id="caption">' + $(this).html() + '</div>');
					$('div#caption').css({
						left: parseInt($('div#image-container img').position().left) + 1,
						width: $('div#image-container img').width() - 22
					});
					
					return false;
				},
				function() {
					$('div#caption').remove();
					
					return false;
				}
			);
		}
		
	});
	
	// SUBMIT BUTTON
	$('form').submit(function() {
		$this = $(this);
		
		if ($('input:submit',$this).metadata().disabledText) {
			
			$('input:submit',$this).attr('disabled','disabled').val($('input:submit',$this).metadata().disabledText).addClass('disabled');
		}
	});
		
});

/* FUNCTION: load image */
$.loadImage = function() {
	$thisLI = $('li.preselected');
	$thisLink = $('li.preselected a').attr('href');
	
	$('a#caption-link, div#UILoader').remove();
	
	$thisLI.append('<div class="UILoader"><img src="/framework/images/UILoader.gif" height="16" width="16" /></div>');
	
	$('<img class="preload" />').css('opacity',0).attr('src',$thisLink).load(function() {
		$img = $(this);
		
		$('div#image-container').empty().append($img);
		$('div#image-container').append('<img src="/framework/images/please-dont-steal-my-images.gif" id="overlay" />');
		$('div.UILoader').remove();
		
		$('ul#image-thumbs li').removeClass('selected');
		$thisLI.removeClass('preselected');
		$thisLI.addClass('selected');
		
		$img.removeClass('preload').animate({
			opacity: 1
		});
		
		$.imageCaption();
	});
};

/* FUNCTION: Display image caption */
$.imageCaption = function() {
	if ( $('ul#image-thumbs li.selected img').attr('alt') != '' ) {
		$('div#site-content').append('<a href="#" id="caption-link">' + $('ul#image-thumbs li.selected img').attr('alt') + '</a>');
		
		$('a#caption-link').css({
			right: (parseInt($('div#image-container').width()) - parseInt($('div#image-container img').outerWidth()))/2 + 'px'
		});
		
		// CAPTION LINK
		$('a#caption-link').toggle(
			function() {
				$('div#image-container').append('<div id="caption">' + $(this).html() + '</div>');
				$('div#caption').css({
					left: parseInt($('div#image-container img').position().left) + 1,
					width: $('div#image-container img').width() - 20
				});
				
				return false;
			},
			function() {
				$('div#caption').remove();
				
				return false;
			}
		);
	}
};

/* FUNCTION: Preload images */
$.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		$('<img>').attr('src', arguments[i]);
	}
};