/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *
 *  modified for BPW Microsite 'Mehrwertoffensive'
 *  by David Schneider - david.schneider@xyqom.net
 *
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	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
 *
 */
 
(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			nextId: 		'nextBtn',	
			prevUrl: 		'',
			nextUrl: 		'',
			ulId:			'slide',
			liClass:		'slide',
			orientation:	'', //  'vertical' is optional;
			speed: 			800			
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 			
			var s = $("li."+options.liClass, obj).length;
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$("ul."+options.ulId, obj).css('width',s*w);			
			if(!vertical) $("li."+options.liClass, obj).css('float','left');
//			$(".arrows").append('<li><a id="'+options.prevId+'" href="'+options.prevUrl+'" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'arrow_l\',\'\',\'images/ui/navi/button_arrow_left_hover.jpg\',1)"><img src="images/ui/navi/button_arrow_left.jpg" width="48" height="24" name="arrow_l" id="arror_l" alt="zurück" /></a></li>');
			$(".arrows").append('<li><a id="'+options.prevId+'" href="'+options.prevUrl+'" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'arrow_l\',\'\',\'images/ui/navi/button_arrow_left_hover.jpg\',1)"><img src="images/ui/navi/button_arrow_left.jpg" width="48" height="24" name="arrow_l" id="arror_l" alt="zurueck" /></a></li><li><a id="'+options.nextId+'" href="'+options.nextUrl+'" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'arrow_r\',\'\',\'images/ui/navi/button_arrow_right_hover.jpg\',1)"><img src="images/ui/navi/button_arrow_right.jpg" width="48" height="24" name="arrow_r" id="arror_r" alt="weiter" /></a></li>');

			$(".more #"+options.nextId).click(function(event){
				if(!t) {
					animate("next");
					event.preventDefault();
				}
			});
				
			$(".arrows #"+options.nextId).click(function(event){
				if(!t) {
					animate("next");
					event.preventDefault();
				}
			});

			$("a.more").click(function(event){
				if(!t) {
					animate("next");
					event.preventDefault();
				}
			});
			
			$("#"+options.prevId).click(function(event){		
				if(t) {
					animate("prev");
					event.preventDefault();
				}
			});	
			
			function animate(dir){
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;	
					if(options.nextUrl == 'end' && t) $(".arrows #"+options.nextId).fadeOut();
					$("a.more").hide();
				} else {
					t = (t<=0) ? 0 : t-1;
					if(options.nextUrl == 'end' && !t) $(".arrows #"+options.nextId).fadeIn();
					$("a.more").show();
				};								
				if(!vertical) {
					p = (t*w*-1);
					$("ul."+options.ulId,obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul."+options.ulId,obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
		});
	  
	};

})(jQuery);
