// configuration
var minfade = 0.4;
var maxfade = 1;
var c_vids = [];
$(document).ready(function(){
	// initialise
	$(".series_img").stop(true, true).fadeTo("slow", maxfade).fadeTo("slow", minfade);
	$("#videos").fadeOut(1);
	$("#video").fadeOut(1);
	// item hovers
	$(".series_img").hover(
		function () {
			$(this).stop(true, true).fadeTo("fast", maxfade);
		}, 
		function () {
			$(this).stop(true, true).fadeTo("fast", minfade);
		}
	);
	// item actions
	$(".series_img").click(function(){
		var series = $(this).attr('id');
		if(series=="nh2"){
			setup_videos(NH2);
		}else if(series=="nh3"){
			setup_videos(NH3);
		}else if(series=="nh4"){
			setup_videos(NH4);
		}else if(series=="nho"){
			setup_videos(NHO);
		}else if(series=="nh5"){
			setup_videos(NH5);
		}else if(series=="nh6"){
			setup_videos(NH6);
		}else{
			setup_videos(NH2);
		}
		$("#video").html("<h2>Click on a thumbnail to view an episode</h2>");
		$(".nav").stop(true, true).slideUp(500, function(){
			$("#videos").stop(true, true).fadeIn(500);
			$("#video").stop(true, true).fadeIn(200);
		});
		$("#back").stop(true, true).animate({
			height:"60px"
		}, 500);
	});
	$("#back").click(function(){
		$("#video").stop(true, true).fadeOut(500);
		$("#videos").stop(true, true).fadeOut(500, function(){
			$(".nav").slideDown(500, function(){
				$(".series_img").stop(true, true).fadeTo("fast", maxfade).fadeTo("slow", minfade);
			});
		});
		$("#back").stop(true, true).animate({
			height:"0px"
		}, 1000);
	});
});

function setup_videos(videos){
	$("#videos").html("");
	c_vids = videos;
	for(var i=0; i<videos.length; i++){
		$("#videos").append(
			$("<li><img src='http://img.youtube.com/vi/"+videos[i].youtube+"/default.jpg' /><p>"+videos[i].name+"</p></li>").data("vid", i).click(function(){
				show_vid($(this).data("vid")); return false;
			})
		);
	}
}

function show_vid(i){
	var c = "<img src='http://img.youtube.com/vi/"+c_vids[i].youtube+"/0.jpg' height='160px' class='video_img' /><h2>"+c_vids[i].name+"</h2>";
	c += "<a href='http://www.youtube.com/embed/"+c_vids[i].youtube+"?origin=http://owl.cubeshack.co.uk' rel='shadowbox;width=640;height=480;'><img src='img/film.png' /> Watch Here</a><br />";
	c += "<a href='http://www.youtube.com/watch?v="+c_vids[i].youtube+"' target='_blank'><img src='img/film_link.png' /> Watch on YouTube</a><br />";
	c += "<script type='text/javascript'>Shadowbox.clearCache();Shadowbox.setup();</script>";
	if (isset(c_vids[i-1])){
		c += "<a href='#' onclick='show_vid("+(i-1)+"); return false;'>&lt;&lt; Previous Episode</a>";
		if (isset(c_vids[i+1])) c += " || ";
	}
	if (isset(c_vids[i+1])){
		c += "<a href='#' onclick='show_vid("+(i+1)+"); return false;'>Next Episode &gt;&gt;</a>";
	}
	$("#video").html(c);
	Shadowbox.clearCache();
	Shadowbox.setup();
}

function isset(foo){
	return (typeof foo != 'undefined')
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

