/*
 * jQuery dnbslideshow
 *
 * @author Christopher Einarsrud (christopher@einarsrud.no)
 */
$().ready(function() {
	var slideshowPagingStorage = [];
	var slideshowTotalPagesStorage = [];
	var slideshowAnimInProgress = [];
	var slideshowTimers = [];
	var slideshowActiveId;
	$('.slideshow').each(
		function(index){
			
			// unique id for the slideshow
			var slideshowDomId = "slideshow_"+index;
			// get the total number of slides
			var slideshowChildren = $(this).children().length;
			
			// set a unique id on the slideshow
			$(this).attr("id", slideshowDomId);
			
			// store current slide and total number of slides in an array for later use
			slideshowPagingStorage[index] = 0;
			slideshowTotalPagesStorage[index] = slideshowChildren;
			
			// add a slide class to all the children
			$(this).children().addClass('slideshowSlideWrapper');
			
			var originalContent = $(this).html();
			
			var originalDivs = $(this).children();
			originalContent = '<div class="slideshowSlideWrapper">'+$(originalDivs[slideshowChildren-1]).html()+'</div>' + originalContent +'<div class="slideshowSlideWrapper">'+ $(originalDivs[0]).html()+'</div>';
			$(this).html('<a id="bookingBadge" class="slideshowSlideWrapper" href="?page_id=7">Klikk her for booking</a><div class="contentContainer" style="height:'+((slideshowChildren+2)*400)+'px;">'+originalContent+'</div>');
			
			//addpaging
	
			/*
			$('#'+slideshowDomId).mouseover(
				function(e){
					var slideshowDomId = $(e.target).parents('.slideshow').attr("id");
					if (slideshowDomId) {
						var arr = slideshowDomId.split("_");
						clearInterval(slideshowTimers[arr[1]]);
					}
				}
			);
			
			$('#'+slideshowDomId).mouseout(
				function(e){
					var slideshowDomId = $(e.target).parents('.slideshow').attr("id");
					if (slideshowDomId) {
						var arr = slideshowDomId.split("_");
						slideshowTimers[arr[1]] = setInterval(function() {autoSlide(arr[1])},8000);
					}
					//slideshowTimers[arr[1]] = setInterval("autoSlide('0')",5000);
				}
			);
			*/
			slideshowTimers[index] = setInterval(function() {autoSlide(index)},5000);
			//update paging and arrows
			changeSlide(index,0);
		}
	);
	
	// the active slideshow id is set when looping thru all of the 
	slideshowActiveId = undefined;
	$(document).click(
		function(e){
			// check if the mouse click was made within a slideshow container
			var slideshowDomId = $(e.target).parents('.slideshow').attr("id");
			if (slideshowDomId != undefined) {
				var arr = slideshowDomId.split("_");
				slideshowActiveId = arr[1];
				
				// add keypress function to enable the user to use the arrow keys to navigate the slideshow
				$(document).unbind('keypress');
				$(document).keypress(
					function(e){
						switch(e.keyCode) {
							case 37:
								changeSlide(slideshowActiveId, -1);
								break;
							case 39:
								changeSlide(slideshowActiveId, 1);
								break;
						}
					}
				);
			}
			else {
				slideshowActiveId = undefined;
				$(document).unbind('keypress');
			}
		}
	);
	
	
	function changeSlide(slideshowId, pageInc) {
		if (!slideshowAnimInProgress[slideshowId]) {
			// variable used to keep track of the active slide.
			slideshowActiveId = slideshowId;
			var slideshowDomId = 'slideshow_'+slideshowId;
			var currentPage = slideshowPagingStorage[slideshowId];
			var totalPages = slideshowTotalPagesStorage[slideshowId];
			currentPage += pageInc;
			
			// go to first page once you've reached the last page
			var currentPageId = (currentPage < 0) ? totalPages-1:((currentPage >= totalPages) ? 0:currentPage);
			
			
			// don't allow user to go past the last slide
			//currentPage = (currentPage < 0) ? 0:((currentPage >= totalPages) ? totalPages-1:currentPage);
			slideshowPagingStorage[slideshowId] = currentPage;
			
			// reset the pagination buttons and set the active one
			$('#'+slideshowDomId +' .paginationList a').removeClass('activeSlide');
			$('#pagingBtn_'+slideshowId+'_'+currentPageId).addClass('activeSlide');
						
			// hide the arrows if the slideshow has reached it's first of last slide
			//$('#'+slideshowDomId +' .leftArrow').css("display", (currentPage === 0) ? "none":"block");
			//$('#'+slideshowDomId +' .rightArrow').css("display", (currentPage === totalPages-1) ? "none":"block");
			
			// stop the current animation before starting a new one
			//$('#'+slideshowDomId +' .contentContainer').stop(true, false);
			
			// animate the slides into place
			slideshowAnimInProgress[slideshowId] = true;
			$('#'+slideshowDomId +' .contentContainer').animate({'top':-((currentPage+1)*400)},1000,function(){
				var slideshowDomId = $(this).parents('.slideshow').attr("id");
				var arr = slideshowDomId.split("_");
				checkLooping(arr[1]);
			});
		}
		//$('#'+slideshowDomId +' .contentContainer').animate({'left':-(currentPage*700)},1000,function(){});
	}
	function checkLooping(slideshowId) {
		slideshowAnimInProgress[slideshowId] = false;
		var slideshowDomId = 'slideshow_'+slideshowId;
		var currentPage = slideshowPagingStorage[slideshowId];
		var totalPages = slideshowTotalPagesStorage[slideshowId];
		
		currentPage = (currentPage < 0) ? totalPages-1:((currentPage >= totalPages) ? 0:currentPage);
		
		slideshowPagingStorage[slideshowId] = currentPage;
		
		$('#'+slideshowDomId +' .contentContainer').css("top", -((currentPage+1)*400));
	}
	function autoSlide(slideshowId) {
		changeSlide(slideshowId, 1);
	}
});
