// jKickPortfolio v0.1 - a full featured, light-weight, customizable portfolio rotator based on jQuery 1.4+
// Copyright (c) 2011 Universal Web Services, LLC - development@universalwebservices.net
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

(function( $ ){

	$.fn.jKickPortfolio = function(options){

		var settings = {
			'delaySpeed'			: 5000,
			'fadeSpeed'				: 50,
			'backgroundColor'		: '',
			'color'					: '#fed',
			'imagePadding'			: 0,
			'sliderImage'			: 'img/slider-image.png',
			'showSlider'			: true
		};

		if ( options ) { 
			$.extend( settings, options );
		}
	
		return this.each( function() {

			$this = $(this);
			$this.addClass('jKickPortfolio');
			$this.css('background-color', settings.backgroundColor);
			$this.children().not(':first').hide();
			$this.children().css('position','absolute').css('color', settings.color);
			var slideCount = $this.children().length;

			// get the width of the widest image
			maxW = 0
		    $this.children().each(function() {
				if ($(this).children('img:last').length)
					maxW = Math.max( maxW, $(this).children('img:last').attr('width'));
		    });
			
			// move the image to the top of the slide so it can be floated right						
			$this.children().each(function() {
				$(this).prepend($(this).children('img:last'));
				$(this).children('img:first').wrap('<div class="jKickPortfolioImageContainer" />');
				$(this).children('.jKickPortfolioImageContainer').css('width', maxW + 'px').css('marginLeft', settings.imagePadding + 'px').css('marginRight', settings.imagePadding + 'px');
			});


			var sliderHeight = 0;

			if (settings.showSlider) {
				$this.append('<div class="jKickPortfolioSlider" />');
				for (i = 0; i < slideCount; i++) {
					$this.children('.jKickPortfolioSlider').append('<span class="slide-index-' + (i + 1) + '"><img src="' + settings.sliderImage + '" /></span>');
				}
				
				var sliderWidth = parseInt($this.width() - maxW - (settings.imagePadding * 2)) - 200;
				$this.children('.jKickPortfolioSlider').css('width', sliderWidth).css('marginLeft', settings.imagePadding);
				if ($.browser.msie)
					$this.children('.jKickPortfolioSlider').children('span').css('background', 'url(' + settings.sliderImage + ') no-repeat').css('filter', 'alpha(opacity = .5)'); // for IE bug not displaying images
					
				sliderHeight = $this.children('.jKickPortfolioSlider').height();
				sliderHeight += 10;  // for 5px padding
			}



			// set the height of the element to the height of the tallest child
		    var maxH = 0;
		    $this.children().each(function() {
				maxH = Math.max( maxH, $(this).height() + parseInt($(this).css('paddingTop')) + parseInt($(this).css('paddingBottom')) );
			});
			$this.css('height', maxH + sliderHeight);


			// adjust the width/height of the wrapper and image container
		    $this.children().not('.jKickPortfolioSlider').each(function() {
				$(this).children('.jKickPortfolioImageContainer').css('height', maxH + sliderHeight + 'px');
				$(this).css('width', '90%').css('paddingLeft', settings.imagePadding);
		    });
			
			
			// center the portfolio images
			$('.jKickPortfolioImageContainer img').each(function() {
				$(this).css('paddingTop', (maxH - $(this).attr('height')) / 2);
			});


			// start the transitions
			var currentPosition = 1;
			var t = setTimeout('transition()', settings.delaySpeed);

			transition = function() {
				currentPosition++;
				lastPosition = currentPosition - 1;
				if (currentPosition == slideCount + 1)
					currentPosition = 1;
				
				if (lastPosition < 1)
					lastPosition = slideCount - 1;
					
				$this.children(':eq(' + lastPosition + ')').fadeOut(settings.fadeSpeed);
				$this.children(':eq(' + currentPosition + ')').fadeIn(settings.fadeSpeed, function() {
					$this.children('.jKickPortfolioSlider').children('span').removeClass('current');
					$this.children('.jKickPortfolioSlider').children('span:eq(' + (currentPosition - 1) + ')').addClass('current');
					
					t = setTimeout('transition()', settings.delaySpeed);	
				});
				
			};

			// a fixed version of an image container
			var childType = $this.children().first().attr('tagName');
			$this.prepend('<' + childType + ' class="fixedSlide" />');
			$this.children().first().append('<div class="jKickPortfolioImageContainer" />');
			$this.children().first().css('position','absolute').css('width', '90%').css('paddingLeft', settings.imagePadding);;
			$this.children().first().children().css('width', maxW + 'px').css('marginLeft', settings.imagePadding + 'px').css('marginRight', settings.imagePadding + 'px').css('height', maxH + sliderHeight + 'px');;
		
			$this.children('.jKickPortfolioSlider').children('span').first().addClass('current');
			
			//alert($('.jKickPortfolioSlider span img').first().height());
			
			$this.children('.jKickPortfolioSlider').children('span').children('img').click(function() {
				position = $(this).parent().attr('class').split('-').pop();
				currentPosition = position;
				clearTimeout(t);
		
				$this.children().not('.jKickPortfolioSlider,.jKickPortfolioImageContainer').hide();
				$this.children(':eq(' + currentPosition + ')').show();
			
				$this.children('.jKickPortfolioSlider').children('span').removeClass('current');
				$this.children('.jKickPortfolioSlider').children('span:eq(' + (currentPosition - 1) + ')').addClass('current');		
			});
			
		});
	};
	
})(jQuery);
