$(window).ready(function() {
	if($("#home-slideshow")) prepareHomeSlideshow();
	if($("#landing-slideshow")) prepareLandingSlideshow();
	if($("#landing-singlephoto")) prepareSinglephoto();
	if($("#landing-process")) prepareProcess();
	if($("#landing-videoplayer")) prepareVideoPlayer();
});

$(window).load(function() {
	if($("#page-content div.main-wrapper")) prepareGeneric();
});

var prepareGeneric = function () {
$("#page-content").same_height();
mainWrapperHeight = $("#page-content .main-wrapper").height();
$("#page-content .main-wrapper .main").css("min-height" , mainWrapperHeight - $(".main-wrapper .breadcrumb").outerHeight() - 17)
}

/** Slideshow functions on the home page. */
var prepareHomeSlideshow = function() {
	$("#home-slideshow ul.slides").innerFade({
		timeout:5000,
		prevLink:"ul.slide-nav .previous",
		nextLink:"ul.slide-nav .next",
		callback_index_update:homeSlideChanged
	});
}

var homeSlideChanged = function(index) {
	$thisSlide = $("#home-slideshow ul.slides li:eq("+index+") a");
	$("#home-slideshow ul.slide-nav a.gallery").attr("href",$thisSlide.attr("href")).text($thisSlide.find("img").attr("alt"));
}

/** Slideshow functions on the landing page. */
var prepareLandingSlideshow = function() {
	var horizontal = $("#page-landing .sidebar h2").outerHeight();
	var containerHeight = $("#page-landing").height();
	$("#landing-slideshow h3").css("bottom",containerHeight - horizontal);
	$("#landing-slideshow ul.content").css("top",horizontal);
	$("#landing-slideshow ul.slides").innerFade({
		animationType:"fade",
		timeout:5000,
		callback_index_update:landingSlideChanged
	});
	$("#landing-slideshow ul.thumbs a").click(landingSlideNavigation);
}

var landingSlideNavigation = function(e) {
	e.preventDefault();	
	var thisIndex = $(this).parent("li").index();
	$("#landing-slideshow ul.slides").innerFadeTo(thisIndex);
	if($("#landing-slideshow ul.content li").length > 1) {
		$("#landing-slideshow ul.content li").removeClass("active").eq(thisIndex).addClass("active");
	}
}

var landingSlideChanged = function(index) {
	$("#landing-slideshow ul.thumbs li").removeClass("active").eq(index).addClass("active");
	if($("#landing-slideshow ul.content li").length > 1) {
		$("#landing-slideshow ul.content li").removeClass("active").eq(index).addClass("active");
	}
}

/** Layout adjustments for the single photo news page. */

var prepareSinglephoto = function() {
	var horizontal = $("#page-landing .sidebar h2").outerHeight();
	var containerHeight = $("#page-landing").height();
	$("#landing-singlephoto h3").css("bottom",containerHeight - horizontal);
	$("#landing-singlephoto div.content").css("top",horizontal);
	
	var imageHeight = $("#landing-singlephoto img").outerHeight();
	var imageWidth = $("#landing-singlephoto img").outerWidth();
	$("#landing-singlephoto div.photoregion").height(imageHeight);
	$("#landing-singlephoto div.photoregion").width(imageWidth);

	// position the content in the middle of the frame.
	var newtop = Math.floor(($("#landing-singlephoto").height() - $("#landing-singlephoto img").height()) / 2);
	$("#landing-singlephoto div.photoregion").css("top",newtop);
}

/** Layout adjustments for the process page */

var prepareProcess = function() {
	var horizontal = $("#page-landing .sidebar h2").outerHeight();
	$("#landing-process .top").css("top",horizontal - $("#landing-process .top").outerHeight());
	$("#landing-process .bottom").css("top",horizontal);
	$("#landing-process ul").same_height();
	var ulHeight = $("#landing-process ul").outerHeight();
	var bottomHeight = $("#page-landing").height() - horizontal;
	$("#landing-process ul").css("top",Math.floor(((bottomHeight - ulHeight) / 2)));
}

/** Prepare the video player */
var fp;

var prepareVideoPlayer = function() {
	$("#landing-videoplayer ul.slides").innerFade({
		animationType:"fade",
		timeout:5000,
		callback_index_update:landingVideoChanged
	});
	$("#landing-videoplayer ul.thumbs a").click(landingVideoNavigation);
	$("#landing-videoplayer a.playbutton").click(landingVideoPlayVideo);
}

var landingVideoChanged = function(index)  {
	$("#landing-videoplayer ul.thumbs li").removeClass("active").eq(index).addClass("active");
	if($("#landing-videoplayer ul.content li").length > 1) {
		$("#landing-videoplayer ul.content li").removeClass("active").eq(index).addClass("active");
	}
}

var landingVideoPlayVideo = function(e) {
	e.preventDefault();
	var thisIndex = $(this).parents("li").index();
	var clipPath = $(this).attr("href");
	$("#landing-videoplayer ul.slides").innerFadeTo(thisIndex); // this stops the player
	$("#landing-videoplayer .video-player-target").fadeIn(1000,function(){loadVideo(clipPath)});
}

var loadVideo = function(clipurl) {
	fp = flowplayer(
		"flowplayer", 
		"/media/yorkwebsite/styleassets/javascript/flowplayer-3.2.7.swf", 
		{
			clip: {
				url:clipurl,
				// these two configuration variables does the trick
				autoPlay: false, 
				autoBuffering: true
			}
		}
	);
	fp.play();
}

var landingVideoNavigation = function(e) {
	e.preventDefault();	
	var thisIndex = $(this).parent("li").index();
	if(fp) {
		$("#landing-videoplayer .video-player-target").fadeOut(1000,function(){fp.unload()});
	}
	$("#landing-videoplayer ul.slides").innerFadeTo(thisIndex);
	if($("#landing-videoplayer ul.thumbs li").length > 1) {
		$("#landing-videoplayer ul.thumbs li").removeClass("active").eq(thisIndex).addClass("active");
	}
}
