(function($) {
	jQuery.fn.kSlide = function(options) {
		var defaults = {
			width: 500,
			height: 'auto',
			units: 'px',
			shadow: 3,
			easing: '',
			speed: 500,
			drop: 200,
			preloader: 'please wait...',
			phpthumb: '',
			phpthumbsize: '100'
		};
		var options = $.extend(defaults, options);
		return $(this).each(function() {
			function swapTheImage(ul, direction) {
				if(ul.children('li').length == 1) {
					if(direction == 'prev') {
						ul.children('li:last').animate({left: '0'+options.units, top: '0'+options.units, opacity: 0}, options.speed, options.easing, function() { $(this).remove(); });
						singleImage(ul, current_image, 'prev');
						ul.children('li:first').css({opacity: 0}).animate({left: '0'+options.units, top: '0px', opacity: 1}, options.speed, options.easing);
						imageDescription(ul, current_image);
						ul.parent().next().children('li[rel='+current_image+']').addClass('selected').siblings().removeClass('selected');
					} else {
						ul.children('li:first').animate({left: '0'+options.units, top: '0'+options.units, opacity: 0}, options.speed, options.easing, function() { $(this).remove(); });
						singleImage(ul, current_image, 'next');
						ul.children('li:last').css({opacity: 0}).animate({left: '0'+options.units, top: '0px', opacity: 1}, options.speed, options.easing);
						imageDescription(ul, current_image);
						ul.parent().next().children('li[rel='+current_image+']').addClass('selected').siblings().removeClass('selected');
					}
				}
			}
			function clickTheImage(direction, ul) {
				if(direction == 'prev') {
					var rel = ul.children('li:last').attr('rel')-1;
					if(rel < 0) {
						current_image = arr.length-1;
					} else {
						current_image = rel;
					}
					swapTheImage(ul, 'prev');
				} else if(direction == 'next') {
					var rel = eval(ul.children('li:first').attr('rel'))+1;
					if(rel > arr.length-1) {
						current_image = 0;
					} else {
						current_image = rel;
					}
					swapTheImage(ul, 'next');
				} else {
					current_image = direction;
					if(ul.children('li:first').attr('rel') > current_image) {
						swapTheImage(ul, 'prev');
					} else if(ul.children('li:first').attr('rel') < current_image) {
						swapTheImage(ul, 'next');
					}
				}
			}
			function imageDescription(ul, current_image) {
				if(ul.next('.slideText').length > 0) {
					ul.next('.slideText').html('');
				} else {
					ul.after('<div class="slideText" style="display:none;"><span class="zoom"></span></div>');
				}
				if(arr[current_image][3] != '') {
					ul.next('.slideText').html(arr[current_image][3]+'<span class="zoom"></span>').fadeIn();
				} else {
					ul.next('.slideText').hide();
				}
				// lightbox stuff
				ul.parent().attr({href: arr[current_image][2], title: arr[current_image][3], rel: 'up'}).addClass('lightbox');
				//ul.next('.slideText').attr({href: arr[current_image][2], title: arr[current_image][3], rel: 'down'}).addClass('lightbox');
			}
			function biggestDimension(place) {
				var size = 0;
				for (i=0; i<arr.length; i++) {
					if(arr[i][place] > size) {
						size = arr[i][place];
					}
				}
				return size;
			}
			function singleImage(ul, current_image, direction) {
				var marginLeft = arr[current_image][0]/2;
				if(direction == 'prev') {
					return ul.prepend('<li rel="'+current_image+'" style="top:0'+options.units+';left:0'+options.units+';width:'+options.width+options.units+';height:'+(biggestDimension(1)+options.shadow)+options.units+';"><div class="shadow" style="position:absolute;bottom:0px;left:50%;margin-left:-'+marginLeft+options.units+';width:'+(arr[current_image][0]+options.shadow)+options.units+';height:'+(arr[current_image][1]+options.shadow)+options.units+'"><div class="kLoader"><img src="'+arr[current_image][2]+'" width="'+arr[current_image][0]+'" height="'+arr[current_image][1]+'" /></div></div></li>');
				} else {
					var left = options.width;
					if(!direction) {
						left = 0;
					}
					return ul.append('<li rel="'+current_image+'" style="top:0'+options.units+';left:0'+options.units+';width:'+options.width+options.units+';height:'+(biggestDimension(1)+options.shadow)+options.units+';"><div class="shadow" style="position:absolute;bottom:0px;left:50%;margin-left:-'+marginLeft+options.units+';width:'+(arr[current_image][0]+options.shadow)+options.units+';height:'+(arr[current_image][1]+options.shadow)+options.units+'"><div class="kLoader"><img src="'+arr[current_image][2]+'" width="'+arr[current_image][0]+'" height="'+arr[current_image][1]+'" /></div></div></li>');
				}
			}
			var arr = new Array();
			function createArray(ul, i, delay) {
				var img = ul.children('li').eq(i).children('img');
				if(img[0].complete) {
					arr.push([img.width(), img.height(), img.attr('src'), img.attr('title')]);
					// WAIT UNTILL LAST IMAGE BEEN LOADED THEN START HERE
					// CHECK FOR LAST IMG IN THE LOOP
					if(i == (ul.children('li').length-1)) {
						// remove loader
						ul.prev().remove();
						// create first image container
						var current_image = 0;
						ul.html('');
						// adding first image
						singleImage(ul, current_image);
						ul.children('li').css({top: '0px', opacity: 1});
						//
						ul.wrapAll('<div style="position:relative;margin:auto;width:'+options.width+options.units+';height:'+(options.height+options.shadow)+options.units+'"></div>');
						ul.after('<div class="slidePrev"></div><div class="slideNext"></div>');
						imageDescription(ul, current_image);
						ul.css({height: (biggestDimension(1)+options.shadow)+options.units});
						ul.parent().find('.slidePrev').bind('click', function() {
							clickTheImage('prev', $(this).parent().children('ul'));
							return false;
						});
						ul.parent().find('.slideNext').bind('click', function() {
							clickTheImage('next', $(this).parent().children('ul'));
							return false;
						});
						// navigation
						ul.parent().after('<ul class="slideNav" style="width:'+options.width+options.units+'"></ul>');
						for (i=0; i<arr.length; i++) {
							var path = arr[i][2];
							if(options.phpthumb) {
								// filtering phpthumb and taking image path out from string
								var string = new RegExp("src=([^&]*)");
								if(string.exec(arr[i][2])) {
									path = string.exec(arr[i][2])[1];
								}
								path = options.phpthumb + '/phpThumb.php?src='+path+'&w='+options.phpthumbsize+'&h='+options.phpthumbsize;
							}
							ul.parent().next('.slideNav').append('<li rel="'+i+'"><div style="background-image:url('+path+')"></div></li>');
						}
						ul.parent().next('.slideNav').children('li').each(function() {
							$(this).children('div').css({width: $(this).width(), height: $(this).width(), backgroundPosition: '50% 50%', backgroundRepeat: 'no-repeat'});
						});
						ul.parent().next('.slideNav').children('li:first').addClass('selected');
						ul.parent().next('.slideNav').children('li').bind('click', function() {
							var rel = $(this).attr('rel');
							clickTheImage($(this).attr('rel'), $(this).parent().prev().children('ul'));
						});
					} else {
						createArray(ul, i+1, delay);
					}
				} else {
					var t = setTimeout(function(){ createArray(ul, i, delay); }, delay);
				}
			}
			$(this).before('<div><span class="kSlideLoader"></span>'+options.preloader+'</div>');
			createArray($(this), 0, 1);
			$(this).css({width: options.width+options.units});
		});
	};
})(jQuery);