/*
 *	Gartenheim JS
 *	(c) 2010 CORE4
 *
 *	description: display only 2 modules and make them slideable	
 *	author: dominik jansen <d.jansen@core4.de>
 *
 */
 
 
 $.fn.moduleSlider = function(options) {
		
		var defaults = {
			containerMargin: 10,
			noImage: '../img/bull_grey.png',
			actImage: '../img/bull_white.png',
			showBoxes: 2,
			
			boxes: $(this),
			scroller: $('#slidebox .scroll'),
			slidebox: $('#slidebox'),
			sliderConsole: $('#moduleSliderConsole'),
				
			sliderType: 'bullets',
			arrowType: 'small',
			
			speed: 700
		};
		
		var $options = $.extend(defaults, options);
		
		// disable if not needed
		if ($options.boxes.length > $options.showBoxes) {
			
			// Some configuration:
			var containerMargin = $options.containerMargin,
				activeIndex = 0,
			
				noImage = $options.noImage,
				actImage = $options.actImage,
			
			// save dom node references in an array
			// var modules = new Array;
				$boxes = $options.boxes,
				$scroller = $options.scroller,
				$slidebox = $options.slidebox,
				arrowClass = ($options.arrowType === 'big') ? "bigArrows" : "smallArrows";
			
			$boxes.css({ 'position': 'relative', 'float': 'left' });
			
			
			
			/* insert module */
			$options.slideModule = $(
				'<div class="slideModule ' + arrowClass + '">'+
					'<span class="left">'+
						'<a href="#" title="Nach links bl&auml;ttern"></a>'+
					'</span>'+
					'<div class="sliderNavigation"></div>'+
					'<span class="right">'+
						'<a href="#" title="Nach rechts bl&auml;ttern"></a>'+
					'</span>'+
				'</div>'
			).appendTo($options.sliderConsole);
			
			/* Add some CSS */
			$slidebox.css({
							   'height': ($boxes[0].offsetHeight>300) ? 300 : 'auto' ,
							   'width': ($boxes[0].offsetWidth * $options.showBoxes + containerMargin),
							   'position': 'relative'
			});
			$scroller.css({
						  'width': ($boxes[0].offsetWidth + containerMargin) * $boxes.length,
						  'position': 'relative',
						  'left': $boxes[0].offsetWidth + containerMargin
			});
			$scroller.data('initiate', true);			
			
			
			/*
			 * Slides to Positon 'index'
			 * if index is boolean value, it'll slide 
			 * for 'false' to left, for 'true' to right
			 *
			 */
			function slideTo(index) {
				/* First check if already scrolling */
				if (!$slidebox.data("currentlyMoving")) {
					$slidebox.data("currentlyMoving", true);
					/* Index Setup */
					if (typeof(index) == 'boolean') {
						activeIndex = (index) ? activeIndex + 1 : activeIndex - 1;
					} else {
						activeIndex = index;
						if ((activeIndex+'').lastIndexOf('#') > 0) activeIndex = activeIndex.substr(activeIndex.lastIndexOf('#')+1);
					}
					if (activeIndex >= $boxes.length -( $options.showBoxes -1 )) activeIndex = 0;
					if (activeIndex < 0) activeIndex = ($boxes.length - $options.showBoxes);
					activeIndex = parseInt(activeIndex, 10);
					
					/* Sliding */
					var tmpPos = 0;
					for (i = 0; i < activeIndex; i++) {
						tmpPos = tmpPos + $boxes[i].offsetWidth + containerMargin;
					}
					tmpPos = tmpPos * -1;
					// $scroller.animate({'opacity':0.5 },100);
					$scroller.animate({
						"left": tmpPos
					}, $options.speed, function() {
						$slidebox.data("currentlyMoving", false);
						/* Regenerating imageMenu */
						imageMenu();
					});
					// $scroller.animate({'opacity':1});
				}
			}
			
			
			/* Set ImageMenu to position 'index' */
			function imageMenu() {
				var sliderNav = $options.slideModule.find(".sliderNavigation");
				sliderNav.html('');
				if ($options.sliderType == 'text') {
					var imgText = 'Bild <b>'+(activeIndex+1)+'</b> von <b>'+$boxes.length+'</b>';
					sliderNav.addClass('slideNavigationText').html(imgText);
				} else {
					for (var i= 0; i < $boxes.length-($options.showBoxes-1); i++) {
						var cClass = (!$scroller.data('initiate') && i == activeIndex) ? 'active ' : '';
						var specialClass = (i==0) ? 'first' : (i==$boxes.length-$options.showBoxes) ? 'last' : '';
						sliderNav.append('<a href="'+window.location+'#'+i+'" title="Seite '+(i+1)+'" class="'+cClass+specialClass+'"></a>');
					}
					
					sliderNav.find("a").unbind();
					sliderNav.find("a").click( function (e) {
						slideToEvent(e, $(this).attr('href'));
					});
				}
			}
			
			/* Just a small Function for Event Handling */
			function slideToEvent(e, direction) {
				e.preventDefault();
				slideTo(direction);
			}
			
			/* Run Initial call */
			imageMenu(); // Only grey bullets
			$scroller.delay(500).data('initiate', false); // Wait 500ms to scroll in
			slideTo(0); // Show first itemset
			
			
			$options.slideModule.find('.left a').click( function (e) {
				slideToEvent(e, false);
			});
			
			$options.slideModule.find('.right a').click( function (e) {
				slideToEvent(e, true);
			});
			
			$(window).keydown( function (e) {
				switch (e.keyCode) {
					/*
					case 13: //enter
						slideToEvent(e, true);
						break;
					case 32: //space
						slideToEvent(e, true);
						break;
					*/
					case 37: //left arrow
						//slideToEvent(e, false);
						break;
					case 39: //right arrow
						//slideToEvent(e, true);
						break;
					default: // Default
						//if (e.keyCode >= 48 && e.keyCode <= 57) slideToEvent(e, e.keyCode - 48);
						break;
				}
			});

			

		};
	};
