
function isIOS() {
	return (
		//Detect iPhone
		(navigator.platform.indexOf("iPhone") != -1) ||
		//Detect iPod
		(navigator.platform.indexOf("iPod") != -1) ||
		//Detect iPad
		(navigator.platform.indexOf("iPad") != -1)
		);
}

var slideIdent = "img, video";

if(!$("html").hasClass("video")) {
	slideIdent = "img";
	$("#slideshow .container video").remove();
} else {
	if(isIOS()) {
		$("#slideshow .container video").parent().find("img");
	} else {
		$("body").addClass("no-touch");
		$("#slideshow .container video").parent().find("img").remove();
	}
}

$("#slideshow .container .desc").hide();


$("#slideshow").epslider({
	slideContainer: ".container",
	slide: slideIdent,
	duration: 10000,
	pagination: true,
	autoplay: false,
	randomAnimation: true,
	animations: [
	{
		from: [0, 0, 90],
		to: [50, 100, 100]
	},
	{
		from: [0, 0, 90],
		to: [50, 0, 100]
	},
	{
		from: [0, 0, 90],
		to: [0, 50, 100]
	}],
	onDraw: function(base, skipCheck) {
		var fadeDur = 2;
		var start = (base.animationStart + (base.options.duration/1000)) - fadeDur;
		var cur = base.time();
		
		var curCont = base.currentContainer;
		
		if(cur == start || undefined != skipCheck) {
			base.cont.fadeOut(fadeDur*1000, function() {
				base.stopAnimationDelay[curCont] = true;
			});
			base.next(true);
		}
		
	},
	before: function() {
		$("#slideshow").data("epslider").cont.find(".desc").hide();
		window.setTimeout(function() {
			var $$ = $("#slideshow").data("epslider").cont.find(".desc");
			$$.slideDown("slow");
			$$ = null;
		}, 1500)
	},
	after: function() {
		var $$ = $(this).data("epslider").cont.find(".desc");
		$$.slideUp("slow");
		$$ = null;
	}
})

var lastFocusCheck = time();
function checkFocus() {
	
	var check = time() - lastFocusCheck;
	if(check >= 2 && $("#slideshow").data("epslider")) {
		$("#slideshow").data("epslider").next();
	}
	
	lastFocusCheck = time()
	requestAnimationFrame(checkFocus);
}

checkFocus();


if ( !window.requestAnimationFrame ) {
	window.requestAnimationFrame = ( function() {
		return window.webkitRequestAnimationFrame ||
		window.mozRequestAnimationFrame ||
		window.oRequestAnimationFrame ||
		window.msRequestAnimationFrame ||
		function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element ) {
			window.setTimeout( callback, 1000 / 500 );
		};
	} )();
}

function time() {
	return Math.floor(new Date().getTime() / 1000);
}
