(function($){

	
	var images = new Array();
	
	var fadeDelay = 500;
	
	var currentIndex = 0;
	var current = null;
	var currentIn = null;	
	
	var type = "refit";
	
	function setupSlider(obj) {
				
		$(obj).find('img').each(function(index, item) {
			if (! current) {
				current = item;
				currentIndex = index;	
			} else {
				$(item).css({
					display: 'none'
				});
								
			}
				
			images.push(item);
		});
		
		if (images.length > 1) {		
			var arrowLeft = $('<div class="arrow-left">PREVIOUS</div>');
			var arrowRight = $('<div class="arrow-right">NEXT</div>');
			
			$(obj).append(arrowLeft);
			$(obj).append(arrowRight);		
			
			arrowRight.click(function() {
				nextImage();
			});
			
			arrowLeft.click(function() {
				prevImage();
			});	
		}

	}
	
	function nextImage() {
		if (currentIn) current = currentIn;
		$(current).fadeOut(fadeDelay);
		currentIndex++;
		
		if (currentIndex == images.length) currentIndex = 0;
		
		currentIn = images[currentIndex];
		
		$(currentIn).fadeIn(fadeDelay);
		
		if (type == "refit") {
			$('#description').text($(currentIn).attr('title'));		
		} else {
			var picDesc = '#' + $(currentIn).attr('rel');
		
			$('#description').html($(picDesc).html());
		}				
	}

	function prevImage() {
		if (currentIn) current = currentIn;
		$(current).fadeOut(fadeDelay);
		currentIndex--;
		
		if (currentIndex == -1) currentIndex = images.length-1;
		
		currentIn = images[currentIndex];
		
		$(currentIn).fadeIn(fadeDelay);
		
		if (type == "refit") {
			$('#description').text($(currentIn).attr('title'));		
		} else {
			var picDesc = '#' + $(currentIn).attr('rel');
		
			$('#description').html($(picDesc).html());
		}

	}

	$.fn.slider = function() {
		return this.each(function(index, item) {
		
			if ($(item).attr('rel') == "awards")
				type = "awards";
		
			setupSlider(item);
		});
	}

})(this.jQuery);

$('#slider').slider();
