/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Modified 2009/08/10 by Henri Schomäcker hs AT byteconcepts.com
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

/*
 *	markup example for $("#images").easySlider();
 *
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {
	$.fn.easySlider = function(options){
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			remember:		0,
			path:			'/', // Set the page path for which a cookie should be valid
			cookie_name:	'easySlider.bc-mod',
			items:			1,
			speed: 			800,
			hideButtons:	1,
			switchButtonClass:	0
		};

		var options = $.extend(defaults, options);

		return this.each(function() {
			obj = $(this);
			var s = $("li", obj).length;
			var w = obj.width();
			var h = obj.height();
			var ts = s-1;
			var t = 0;
			var item_height = h / (ts - options.items);
			var vertical = (options.orientation == 'vertical');
			if (!vertical) {
				$("ul", obj).css('width',s*w);
				$("li", obj).css('float','left');
			}

			$(obj).before('<span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>');
			$(obj).after('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>');

			if (options.switchButtonClass == 1) {
				$("#albumNavButtonUp").removeClass("albumNavButtonUpActive");
				$("#albumNavButtonUp").addClass("albumNavButtonUpInactive");

				$("#albumNavButtonDown").removeClass("albumNavButtonDownActive");
				$("#albumNavButtonDown").addClass("albumNavButtonDownInactive");
			}
			if (options.hideButtons == 1) {
				$("a","#"+options.prevId).hide();
				$("a","#"+options.nextId).hide();
			}

			$("a","#"+options.nextId).click(function(){
				animate("next");

				if (t >= ts ) {
					if (options.switchButtonClass == 1) {
						$("#albumNavButtonUp").removeClass("albumNavButtonUpActive");
						$("#albumNavButtonUp").addClass("albumNavButtonUpInactive");
					}
					if (options.hideButtons == 1) {
						$(this).fadeOut();
					}
				}

				if (options.switchButtonClass == 1) {
					$("#albumNavButtonDown").removeClass("albumNavButtonDownInactive");
					$("#albumNavButtonDown").addClass("albumNavButtonDownActive");
				}
				$("a","#"+options.prevId).fadeIn();
			});

			$("a","#"+options.prevId).click(function(){
				animate("prev");

				if (t <= 0 ) {
					if (options.switchButtonClass == 1) {
						$("#albumNavButtonDown").removeClass("albumNavButtonDownActive");
						$("#albumNavButtonDown").addClass("albumNavButtonDownInactive");
					}
					if (options.hideButtons == 1) {
						$(this).fadeOut();
					}
				}

				if (options.switchButtonClass == 1) {
					$("#albumNavButtonUp").removeClass("albumNavButtonUpInactive");
					$("#albumNavButtonUp").addClass("albumNavButtonUpActive");
				}
				$("a","#"+options.nextId).fadeIn();
			});

			if (options.remember == 1) {
				// try to read a cookie and then call animate to set
				// the slider to the position left when leaving the page
				var goto = $.cookie(options.cookie_name);

				if (goto != null) {
					var newt = parseInt(goto) - 1;

					if (newt >= 0 && options.switchButtonClass == 1) {
						$("#albumNavButtonDown").removeClass("albumNavButtonDownInactive");
						$("#albumNavButtonDown").addClass("albumNavButtonDownActive");
					}
					t = newt;
					animate("next");
				}
			}

			function animate(dir, goto) {
				if(dir == "next"){
					if (t + options.items >= ts && options.switchButtonClass == 1) {
						$("#albumNavButtonUp").removeClass("albumNavButtonUpActive");
						$("#albumNavButtonUp").addClass("albumNavButtonUpInactive");
					}
					if (t + options.items >= ts + 1) {
						return;
					}

					t = (t>=ts) ? ts : t+1;
				}
				else {
					t = (t<=0) ? 0 : t-1;
				};

				if (!vertical) {
					p = (t * w / options.items * -1);
					$.cookie(options.cookie_name, t, { path: options.path });
					$("ul",obj).animate(
						{ marginLeft: p },
						options.speed
					);
				}
				else {
					if (options.remember == 1 && goto) {
						p = goto;
					}
					else {
						p = (t * h / options.items * -1);
					}
					$.cookie(options.cookie_name, t, { path: options.path });
					$("ul",obj).animate(
						{ marginTop: p },
						options.speed
					);
				}
			};

			if (s > 1) {
				if (options.switchButtonClass == 1) {
					$("#albumNavButtonUp").removeClass("albumNavButtonUpInactive");
					$("#albumNavButtonUp").addClass("albumNavButtonUpActive");
				}

				$("a","#"+options.nextId).fadeIn();
			}
		});

	};

})(jQuery);