/* ---------------------------------------
// 
// Ultratune SA Custom OO Content Slider
// CDAA Pty Ltd 2011-07
// 
--------------------------------------- */

$(function() {
	
	/* ---------------------------------------
	// SET UP
	--------------------------------------- */
	// Settings
	var maxSlides = 4;
	
	// Creation Navigation block
	$("#dnn_contentSlider").append("<div id='slider_navigation'></div>");
	
	$(".slider_container").each(function(i) {
		// Give class to target
		$(this).addClass("slide_"+i).attr("id","slide_"+i);
		// Duplicate h2s to navigation for navigation purposes
		$(this).find("h2").clone().appendTo("#slider_navigation").attr("id","ms"+i);
	});	
	
	// Hide all slides, show first
	slideTo(0);
	
	/* ---------------------------------------
	// ACCESSABILITY
	--------------------------------------- */
	// Keyboard Navigation
	// Check key function
	function checkKey(e){
		 switch (e.keyCode) {
			case 37:
				e.preventDefault();
				prevSlide();
				break;
			case 39:
				e.preventDefault();
				nextSlide();
				break;
			default:
				}
	}
	// Listen for key presses
	if ($.browser.mozilla) {
		$(document).keypress(checkKey);
	} else {
		$(document).keydown(checkKey);
	}
	
	// Swipe Gestures
	// Assign handlers to the simple direction handlers.
	// This requires the touchSwipe plugin
	$("#dnn_contentSlider").swipe({
		swipeLeft:swipeLeft,
		swipeRight:swipeRight,
		threshold:0
	});
	function swipeLeft(event) {nextSlide();}
	function swipeRight(event) {prevSlide();}
	
	/* ---------------------------------------
	// FUNCTION ALL THE THINGS
	--------------------------------------- */
	// Hide All Slides
	function hideAllSlides() {
		$(".slider_container").hide().removeClass("current_slide");
		$("#slider_navigation h2").removeClass("current");
	}
	
	// Start Slide - Show slide and start animation
	function startSlide(slideNum) {
		
		// Gather the troops
		var thisSlide = $(".slide_"+slideNum);
		var thisTitle = thisSlide.find(".slider_heading").find("h2");
		var thisText = thisSlide.find(".slider_content").find("em");
		var thisImage = thisSlide.find(".slider_content").find("img");
		
		// Making up for DNN's ineptness
		if(thisText == '') {
			var thisText = thisSlide.find(".slider_content").find("i");
		}
		
		// Reset positioning
		if(jQuery.browser.version.substring(0, 2) == "8.") {
			// No Opacities in IE8
			thisTitle.css({"top": "60px"});
			thisImage.css({"left": "0px"});
			thisText.css({"top":"173px"});
		} else {
			thisTitle.css({"top": "60px", "opacity": 0});
			thisImage.css({"left": "0px", "opacity": 0});
			thisText.css({"top":"173px", "opacity": 0});
		}
		
		// Show slide
		thisSlide.show().addClass("current_slide");
		
		// Start Animation
		if(jQuery.browser.version.substring(0, 2) == "8.") {
			// No Opacities in IE8
			thisTitle.stop().animate({"top": "80px"});
			thisImage.stop().animate({"left": "20px"});
			thisText.stop().animate({"top": "169px"});
		} else {
			thisTitle.stop().animate({"top": "80px","opacity": 1});
			thisImage.stop().animate({"left": "20px", "opacity": 1});
			thisText.stop().animate({"top": "169px","opacity": 1});
		}
		
	}
	
	// Highlight Slide Title - Highlights the slider link in the navbar
	function highlightSlideTitle(slideNum) {
		$("#slider_navigation h2").removeClass("current");
		$("#ms"+slideNum).addClass("current");
	}
	
	// Next Slide - Goes to the next slide.
	function nextSlide() {
		current = getCurrentSlide();
		current = parseInt(current)+1;
		slideTo(current);
	}
	
	// Previous Slide - Goes to Perth
	function prevSlide() {
		current = getCurrentSlide();
		current = parseInt(current)-1;
		slideTo(current);
	}
	
	// Get Current Slide - Gets the current slide number
	function getCurrentSlide() {
		current = $(".current_slide").attr("id").substr(6);
		return current;
	}
	
	// Slide to a slide with a number to slide to.
	function slideTo(slideNum) {
		
		// Check SlideNum against limits
		// There are only ever going to be 4 slides. 
		// If 4, goes to 0. If -1, goes to 3.
		slideNum = parseInt(slideNum);
		if(slideNum == maxSlides) {slideNum = 0;} else
		if(slideNum == -1) {slideNum = parseInt(maxSlides)-1;}
		
		//console.log("The time is: "+slideNum);
		
		// I like to think these function names are pretty self explanitory
		hideAllSlides();
		highlightSlideTitle(slideNum);
		startSlide(slideNum);
		
		// Automatically slide to next slide on the ride.
		clearTimeout(slideTimeOut);
		//console.log("I've fought Mudcrabs more fearsome than you!");
		autoSlide();
		
	}
	
	
	/* ---------------------------------------
	// MINI SLIDE PRESSES
	--------------------------------------- */
	// Button press!
	$("#slider_navigation h2").live("click",function() {
		
		// Do nothing if already active, silly.
		if($(this).hasClass("current")) { return false; }
		
		// Get the number
		slideNum = $(this).attr("id").substring(2);
		
		// Slide to slide with the same number
		slideTo(slideNum);
		
		// Reset Timeout 
		/* Depreciated - Restarts autoslide while your over the slider, hence breaking the hover stopping
		clearTimeout(slideTimeOut);
		autoSlide();
		*/
		
	});
	
		
	/* ---------------------------------------
	// AUTOMATIC PROGRESSION
	--------------------------------------- */
	// AutoSlide - Slides automatically. 
	var slideTimeOut;
	function autoSlide(){
		slideTimeOut = setTimeout(nextSlide,5000);
		//console.log("I've fought Mudcrabs more fearsome than you!");
	}
	
	// Stopping the auto slide on hover.
	$("#dnn_contentSlider").hover(function() {
		// Hover In
		//console.log("Stop right there, criminal scum!");
		clearTimeout(slideTimeOut);
	}, function() {
		// Hover Out
		autoSlide();
	});
	
	
	
});
