(function ($) {
	$.fn.elSlider = function (options) {
		options = $.extend({
				visibleitems: 3,
				vertical: false,
				continuous: false,
				numbers: true,
				thumbs: true,
				animationspeed: 300,
				fadingspeed: 200,
				itemclass: ".sl_item",
				visiblecontainer: ".sl_visible",
				movingcontainer: ".sl_movingcontainer",
				leftarrow: ".sl_leftarrow",
				rightarrow: ".sl_rightarrow",
				numberscontainer: ".numbers",
				thumbscontainer: ".hiddenthumbs",
				bar: ".navbar"
			}, options);
		return this.each(function () {

			// MAIN CONTAINERS

			var sl_item = $(options.itemclass, this);
			var sl_movingcontainer = $(options.movingcontainer, this);
			var sl_visiblecontainer = $(options.visiblecontainer, this);
			var sl_leftarrow = $(options.leftarrow, this);
			var sl_rightarrow = $(options.rightarrow, this);
			var sl_numberscontainer = $(options.numberscontainer, this);
			var sl_thumbscontainer = $(options.thumbscontainer, this);
			var sl_bar = $(options.bar, this);

			// ITEM ARRAY

			var sl_itemarray = new Array();
			var sl_itemcount = $(sl_movingcontainer).children().length;
			
			for (j = 0; j < sl_itemcount; j++) {
				var currentitem = $(sl_item[j]);
				sl_itemarray.push(currentitem);
			}

			// FURTHER VARIABLES

			var sl_itemwidth = $(sl_item).outerWidth();
			var sl_itemheight = $(sl_item).outerHeight();
			var sl_visibleitems = options.visibleitems;
			var sl_visiblewidth = sl_visibleitems * sl_itemwidth;

			var sl_currentpos = 0;
			var sl_arrowwidth = $(options.leftarrow, this).width();
			var sl_totalwidth = sl_visiblewidth;
			var sl_vertical = options.vertical;
			var sl_continuous = options.continuous;
			var sl_numbers = options.numbers;
			var sl_thumbs = options.thumbs;
			var sl_animationspeed = options.animationspeed;
			var sl_fadingspeed = options.fadingspeed;

			var arguments = new Object;
			var sl_cont_dir = "";
			var sl_aniarg_right = {};
			var sl_aniarg_left = {};

			// ************* DEFINE ARGUMENTS FOR ANIMATION *************** //

			function defineArgs(times) {
				if (sl_vertical == true) {
					$(sl_item).css({"float":"none"});
					$(sl_visiblecontainer).css({"height":sl_itemheight * sl_visibleitems});
					sl_aniarg_right["top"] = "-=" + sl_itemheight * times;
					sl_aniarg_right["queue"] = false;
					sl_aniarg_left["top"] = "+=" + sl_itemheight * times;
					sl_aniarg_left["queue"] = false;
					sl_cont_dir = "top";
				}
				else {
					sl_aniarg_right["left"] = "-=" + sl_itemwidth * times;
					sl_aniarg_right["queue"] = false;
					sl_aniarg_left["left"] = "+=" + sl_itemwidth * times;
					sl_aniarg_left["queue"] = false;
					sl_cont_dir = "left";
				}
			}

			defineArgs(1);
			
			// APPLY TOTAL WIDTH ON SLIDER AND VISIBLE WIDTH ON VISIBLECONTAINER,
			// HIDE ARROW IF CONTINUOUS IS FALSE

			$(this).width(sl_totalwidth);
			$(sl_visiblecontainer).width(sl_visiblewidth);
			if (sl_continuous == false) {
				$(sl_leftarrow).addClass("ishidden");
			}

			// IF CONTINUOUS IS TRUE, APPEND LAST ITEM BEFORE FIRST ONE

			//if ( sl_continuous == true ) {
				var sl_itemtomove = sl_itemarray.pop();
				sl_itemarray.unshift(sl_itemtomove);
				if ( sl_vertical == false ) {
					if (sl_itemcount > 1 ) {
						$(sl_movingcontainer).css(sl_cont_dir, -sl_itemwidth);
					}
				}
				else {
					$(sl_movingcontainer).css(sl_cont_dir, -sl_itemheight);
				}
				$(sl_movingcontainer).prepend(sl_itemtomove);
			//}

			// ***************** FADE FUNCTION ****************** //

			function Fade(direction, speed, times) {
				$(sl_movingcontainer).fadeOut(speed, function() {
					if ( direction == "right" ) {
						FadeToRight(times);
					}
					else {
						FadeToLeft(times);
					}
				});
				$(sl_movingcontainer).fadeIn(speed);
				$(".text",sl_movingcontainer).fadeIn("slow");
			}

			// *************** SLIDE FUNCTION ****************** //

			function Slide(direction, speed, times) {
				defineArgs(times);
				if ( direction == "right") {
					arguments = sl_aniarg_right;
					ToRight(times);
				}
				else {
					arguments = sl_aniarg_left;
					ToLeft(times);
				}
				$(sl_movingcontainer).stop().animate(
					arguments, speed, function() {
						$(".text",sl_movingcontainer).fadeIn("slow");
					}
				);
			}

			// sommercard switch text visibility & lightbox problem solution

			if ( $(".text .hidden",sl_item).length > 0 ) {
				$(sl_item).hover(function() {
					var short = $(".short",this);
					var long = $(".hidden",this);
					$(long).hide();
					$(short).hide();
					$(long).stop(true,true).fadeIn("fast");
				}, function() {
					var short = $(".short",this);
					var long = $(".hidden",this);
					$(short).hide();
					$(long).hide();
					$(short).stop(false,true).fadeIn("fast").show();
				});
				
			}

			// *************** ARRAY FUNCTIONS (IF CONTINUOUS) ***************** //

			function ToRight(times) {
				for ( i=1; i<=times; i++ ) {
					var sl_itemtomove = sl_itemarray.shift();
					if ( sl_vertical == false ) {
						$(sl_movingcontainer).css(sl_cont_dir, 0);
					}
					else {
						$(sl_movingcontainer).css(sl_cont_dir, 0);
					}
					sl_itemarray.push(sl_itemtomove);
					$(sl_movingcontainer).append(sl_itemtomove);
				}
			}

			function ToLeft(times) {
				for ( i=1; i<=times; i++ ) {
					var sl_itemtomove = sl_itemarray.pop();
					sl_itemarray.unshift(sl_itemtomove);
					if ( sl_vertical == false ) {
						$(sl_movingcontainer).css(sl_cont_dir, -(2 * sl_itemwidth));
					}
					else {
						$(sl_movingcontainer).css(sl_cont_dir, -(2 * sl_itemheight));
					}
					$(sl_movingcontainer).prepend(sl_itemtomove);
				}
			}

			function FadeToRight(times) {
				for ( i=1; i<=times; i++ ) {
					var sl_itemtomove = sl_itemarray.shift();
					if ( sl_vertical == false ) {
						$(sl_movingcontainer).css(sl_cont_dir, -sl_itemwidth);
					}
					else {
						$(sl_movingcontainer).css(sl_cont_dir, -sl_itemheight);
					}
					sl_itemarray.push(sl_itemtomove);
					$(sl_movingcontainer).append(sl_itemtomove);
				}
			}

			function FadeToLeft(times) {
				for ( i=1; i<=times; i++ ) {
					var sl_itemtomove = sl_itemarray.pop();
					sl_itemarray.unshift(sl_itemtomove);
					if ( sl_vertical == false ) {
						$(sl_movingcontainer).css(sl_cont_dir, -sl_itemwidth);
					}
					else {
						$(sl_movingcontainer).css(sl_cont_dir, -sl_itemheight);
					}
					$(sl_movingcontainer).prepend(sl_itemtomove);
				}
			}
			

			// ************** ANIMATE FUNCTION ************** // differs between slide and fade

				function Animation(direction, fade, times) {


					// sommercard special hack

					$(".text",sl_movingcontainer).hide();
					$(".hidden").hide();
					

					if ( sl_continuous == false ) {
						if ( direction == "right" ) {
							if ($(sl_leftarrow).hasClass("ishidden")) {
								$(sl_leftarrow).removeClass("ishidden");
							}
						}
						else {
							if ($(sl_rightarrow).hasClass("ishidden")) {
								$(sl_rightarrow).removeClass("ishidden");
							}
						}
					}

					if ( fade == true ) {
						Fade(direction, sl_fadingspeed, times);
					}
					else {
						Slide(direction, sl_animationspeed, times);
					}

					// Position Adjustments

					if ( direction == "right" ) {
						if (sl_continuous == false) {
							sl_currentpos = sl_currentpos + (1 * times);
							if (sl_currentpos > 0) {
								$(sl_leftarrow).removeClass("ishidden");
							}
							if (sl_currentpos == (sl_itemcount - sl_visibleitems)) {
								$(sl_rightarrow).addClass("ishidden");
							}
						}
						else {
							sl_currentpos++;
							if ( sl_currentpos == sl_itemcount ) {
								sl_currentpos = 0;
							}
						}
					}
					else {
						if (sl_continuous == false) {
							sl_currentpos = sl_currentpos - (1 * times);
							if (sl_currentpos == 0) {
								$(sl_leftarrow).addClass("ishidden");
							}
						}
						else {
							sl_currentpos--;
							if (sl_currentpos == -1) {
								sl_currentpos = (sl_itemcount - 1);
							}
						}
					}

					// Thumb Adjustments
					if ( sl_thumbs == true ) {
						$(".thumb",sl_thumbscontainer).removeClass("current");
						$(".thumb:eq("+ (sl_currentpos) +")").addClass("current");
					}

					//$(this).blur();
					return false;

				};

			// CLICK RIGHT ARROW

			$(sl_rightarrow).click(
				function() {
					Animation("right",false,1);
					if ( sl_numbers == true ) {
						$(".nr a.current",sl_numberscontainer).removeClass("current");
						$(".nr a:eq("+(sl_currentpos)+")",sl_numberscontainer).addClass("current");
					}
				}
			)

			// CLICK LEFT ARROW

			$(sl_leftarrow).live("click",
				function() {
					Animation("left",false,1);
					if ( sl_numbers == true ) {
						$(".nr a.current",sl_numberscontainer).removeClass("current");
						$(".nr a:eq("+(sl_currentpos)+")",sl_numberscontainer).addClass("current");
					}
				}
			)

			// APPEND NUMBERS

			if ( sl_numbers == true ) {
				for ( i=1; i<=sl_itemcount; i++ ) {
					$(sl_numberscontainer).append("<div class='nr'><a href='#'>"+i+"</a></div>");
				}
				$(".nr a:eq(0)",sl_numberscontainer).addClass("current");
			}

			// NUMBERS CLICK FUNCTIONS

			$("a", sl_numberscontainer).live("click",function() {

				var thisnr = $(this).text();
				$(".nr a",sl_numberscontainer).removeClass("current");
				$(".nr a:eq("+(thisnr - 1)+")",sl_numberscontainer).addClass("current");
				var currentpos = sl_currentpos + 1;
				var difference = thisnr - currentpos;
				difference = Math.abs(difference);
				if ( difference == 0 ) {
					// do nothing
				}
				else if ( difference == 1) {
					if ( thisnr < currentpos ) {
						Animation("left",false, 1);
					}
					else {
						Animation("right",false, 1);
					}

				}
				else {
					if ( thisnr < currentpos ) {
						Animation("left",true, difference);
					}
					else {
						Animation("right",true, difference);
					}					
				}

				sl_currentpos = thisnr - 1;

				if ( sl_thumbs == true ) {
					$(".thumb",sl_thumbscontainer).removeClass("current");
					$(".thumb:eq("+ (sl_currentpos) +")").addClass("current");
				}

				//$(this).blur();
				return false;
			});

			// SHOW THUMBNAILS
			

			if ( sl_thumbs == true ) {
				$(sl_bar).hover(function() {
					if (sl_itemcount > 1 ) {
						$(sl_thumbscontainer).toggleClass("isnone");
					}
				});
			}

			// DISABLE NAVBAR WHEN THERE IS ONLY 1 ITEM
			
			if ( sl_itemcount == 1 ) {
				$(sl_bar).hide();
			}


			// THUMBNAIL CLICK FUNCTIONS

			$(".thumb", sl_thumbscontainer).live("click",function() {

				var currentthumb = 0;
				for ( i=0; i<sl_itemcount; i++ ) {
					var thisclass = $(".thumb:eq("+i+")",sl_thumbscontainer).attr("class");
					if ( thisclass == "thumb current" ) {
						var currentthumb = i;
						break;
					}
				}

				$(".thumb",sl_thumbscontainer).removeClass("current");
				$(this).addClass("current");

				var clickpos = 0;

				for ( i=0; i<sl_itemcount; i++ ) {
					var thisclass = $(".thumb:eq("+i+")",sl_thumbscontainer).attr("class");
					if ( thisclass == "thumb current" ) {
						var clickpos = i;
						break;
					}
				}

				var difference = currentthumb - clickpos;
				difference = Math.abs(difference);

				if ( difference == 0 ) {
					// do nothing
				}
				else if ( difference == 1) {
					if ( currentthumb < clickpos ) {
						Animation("right",false, 1);
					}
					else {
						Animation("left",false, 1);
					}
				}

				// sl_currentpos = clickpos;
				
				else {
					if ( currentthumb < clickpos ) {
						Animation("right",true, difference);
					}
					else {
						Animation("left",true, difference);
					}
				}

				if ( sl_numbers == true ) {
					$(".nr a.current",sl_numberscontainer).removeClass("current");
					$(".nr a:eq("+clickpos+")",sl_numberscontainer).addClass("current");
				}

				sl_currentpos = clickpos;

				if ( sl_thumbs == true ) {
					$(".thumb",sl_thumbscontainer).removeClass("current");
					$(".thumb:eq("+ (sl_currentpos) +")").addClass("current");
				}
				
				//$(this).blur();
				return false;
			});

		});
	};
})(jQuery);


