(function($) {
	$.fn.anoSlider = function (oList) {
		var oElement = $(this);
		var _oList = oList;
		var _oCurrentList = new Array ();
		var bMoving = false, iCurrentItem = -1, iItems, iWidth, iHeight;

		$("#previous").click (function() {
			animate ("prev");
		});

		$("#next").click (function () {
			animate ("next");
		});

		$('#real_print').click (function () {
			create (_oList, 'print');
			animate ('next', true);
		});

		$('#real_web').click (function () {
			create (_oList, 'web');
			animate ('next', true);
		});

		$('#real_all').click (function () {
			create (_oList);
			animate ('next', true);
		});

		function init () {
			create (_oList);
			animate ('next', true);
		}

		function create (oList, sFilter) {
			_oCurrentList = new Array ();
			oElement.empty ();
			$('#related_images').empty ();

			if (oList.length > 0) {
				var oUl = $('<ul />');
				var oRelatedUl = $('<ul />');

				for (item in oList) {
					if (typeof (sFilter) == 'undefined' || (sFilter != '' && oList[item].type == sFilter)) {
						var oLi = $('<li />');
						var oImg = $('<img src="' + oList[item].url + '" alt="' + oList[item].title + '" />');
						oLi.append (oImg);
						oUl.append (oLi);
						_oCurrentList.push (oList[item]);

						if (typeof oList[item].images != 'undefined' && oList[item].images.length > 0) {
							oLi.css ('cursor', 'pointer');
							oImg.click (function () {
								if ($('#related_images a.lightbox').length > 0)
									$($('#related_images a.lightbox')[0]).click ();
							});
						}
					}
				}

				oElement.append (oUl);
			}

			iCurrentItem = -1;

			iItems = $("li", oElement).length;
			iWidth = $("li", oElement).width ();
			iHeight = $("li", oElement).height ();

			$("#image").width (iWidth);
			$('ul', oElement).height (iHeight);

			$("li", oElement).css ('float', 'left');
			oElement.css ('overflow', 'hidden');
			$('ul', oElement).css ('width', (iWidth * iItems));
		}

		function animate (sMovement, bFirst) {
			if (bMoving == false) {
				bMoving = true;
				switch (sMovement) {
					case 'prev':
						iCurrentItem = (iCurrentItem <= 0) ? iItems - 1 : iCurrentItem - 1;
						break;
					case 'next':
						iCurrentItem = (iCurrentItem + 1 >= iItems) ? 0 : iCurrentItem + 1;
						break;
				}

				var iSpeed = (bFirst == true) ? 1 : 800;

				$('ul', oElement).animate ({'marginLeft': iCurrentItem * iWidth * -1}, iSpeed, slideHandler);
			}
		}

		function slideHandler () {
			$('#client>span').text (_oCurrentList[iCurrentItem].client);
			$('#description>p').text (_oCurrentList[iCurrentItem].description);

			switch (_oCurrentList[iCurrentItem].type) {
				case 'print':
					$('#real_web').css ('color', '#c8c7c4');
					$('#real_all').css ('color', '#c8c7c4');
					$('#real_print').css ('color', '#fff');
					break;
				case 'web':
					$('#real_web').css ('color', '#fff');
					$('#real_print').css ('color', '#c8c7c4');
					$('#real_all').css ('color', '#c8c7c4');
					break;
			}

			var oRelatedUl = $('<ul />');
			$('#related_images').empty ();

			if (typeof _oCurrentList[iCurrentItem].images != 'undefined' && _oCurrentList[iCurrentItem].images.length > 0) {

				for (image in _oCurrentList[iCurrentItem].images)
					oRelatedUl.append ('<li><a href="' + _oCurrentList[iCurrentItem].images[image].url + '" title="' + _oCurrentList[iCurrentItem].images[image].description + '" class="lightbox"><img src="' + _oCurrentList[iCurrentItem].images[image].url + '" alt="' + _oCurrentList[iCurrentItem].images[image].description + '" /></a></li>');

				$('#related_images').append (oRelatedUl);
				$('#related_images .lightbox').lightBox ();
			}

			bMoving = false;
		}

		init ();
	};
})(jQuery);