function CheckOnEnter (obj, string)
{
	if (obj.value == string)
	{
		obj.value = "";
	}

	return;
}

function CheckOnLeave (obj, string)
{
	if (obj.value == "")
	{
		obj.value = string
	}

	return;
}

function addSlashes(string)
{
	string = string.split("'").join("\\'");
	string = string.split("\"").join("\\\"");
	return string;
}

function getLinkText(thisLink)
{
	var linkText = $.trim($(thisLink).text());
//	alert(linkText);

	if (linkText == "")
	{
		linkText = $.trim($(thisLink).children("img").attr("alt"));
	}

	return addSlashes(linkText);
}

$.getScript = function(url, callback, cache) {
	$.ajax({
		type: "GET",
		url: url,
		success: callback,
		dataType: "script",
		cache: cache
	});
};

jQuery.getScripts = function(scripts, onComplete)
{
	var i = 1;
	var ii = scripts.length;
	var onScriptLoaded = function(data, response) { if (i++ == ii) onComplete(); } ;
	for (var s in scripts) { $.getScript(scripts[s], onScriptLoaded, true); } ;
};

jQuery.fn.comments = function(blnDeep)
{
	var blnDeep = (blnDeep || false);
	var jComments = $([]);
 
	// Loop over each node to search its children for
	// comment nodes and element nodes (if deep search).
	this.each(
		function(intI, objNode) {
			var objChildNode = objNode.firstChild;
			var strParentID = $(this).attr("id");
 
			// Keep looping over the top-level children
			// while we have a node to examine.
			while (objChildNode) {
 
				// Check to see if this node is a comment.
				if (objChildNode.nodeType === 8)
				{
					// We found a comment node. Add it to
					// the nodes collection wrapped in a
					// DIV (as we may have HTML).
					jComments = jComments.add("<div rel='" + strParentID + "'>" + objChildNode.nodeValue + "</div>");
				}
				else if (blnDeep && (objChildNode.nodeType === 1))
				{
					// Traverse this node deeply.
					jComments = jComments.add(
						$( objChildNode ).comments( true )
					);
 				}
 
				// Move to the next sibling.
				objChildNode = objChildNode.nextSibling;
			}
		}
	);
 
	// Return the jQuery comments collection.
	return(jComments);
}

if (typeof(featureInterval) === "number") {
	if (!isNaN(featureInterval) && featureInterval > 0) {
		featureInterval = featureInterval * 1000;
	} else {
		featureInterval = 12000;
	}
} else {
	var featureInterval = 12000;
}

$(document).ready(function()
{
	$(".tab ul li span.currentbranch0").parent("li").addClass("currentbranch0");
//	$(".tab ul li span.currentbranch0 a").parent("span").parent("li").removeClass("currentbranch0");

	var bodyClass = $("body").prop("class");
	var pageType;

	if (bodyClass.indexOf("leader") > -1)
	{
		var currentPath = location.pathname;
		var leaderPathPieces = currentPath.split("/");
		var leaderPath = leaderPathPieces[1];

		$("#middle").addClass(leaderPath);
	}

	if ($("#nav-secondary").length > 0)
	{
		var sectionHeader;
/*
		if ($(".nav-secondary-sectionmenu li").length == 0 || $(".nav-secondary-sectionmenu li").length != $(".nav-secondary-sectionmenu li a").length)
		{
			sectionHeader = $("li.nav-secondary-breadcrumb").last().prev().html();
			$("li.nav-secondary-breadcrumb").last().remove();
		}
		else
		{
			sectionHeader = $("li.nav-secondary-breadcrumb").last().html();
		}
*/
		if ($("ul#children-menu li").length > 0)
		{
			sectionHeader = $("li.nav-secondary-breadcrumb").last().html();
		}
		else
		{
			sectionHeader = $("li.nav-secondary-breadcrumb").last().prev().html();
			$("li.nav-secondary-breadcrumb").last().remove();
			$("#siblings-menu").show();
		}

		$("li.nav-secondary-breadcrumb").last().remove();
		$("#sectionHeaderPlaceholder").remove();
		$(".nav-secondary-sectionheader").prepend(sectionHeader);
	}

	$.getScripts(
		[
			"http://www.manchester.ac.uk/medialibrary/degreeprogrammes/project/javascript/jquery.easing.1.3.js",
			"http://www.manchester.ac.uk/medialibrary/degreeprogrammes/project/javascript/jquery.coda-slider-2.0.js"
		],
		function()
		{
			var numberSlides = $("div#coda-nav-1 ul li a").length;
			var slideCounter = 1;
			var featureHeight = $("#leaderfeature").height();

			if (numberSlides > 1) {
				$(".coda-slider-wrapper").css("height", (featureHeight - 40) + "px");
//				$(".panel.hidden").removeClass("hidden");
				$("#coda-nav-1").css("display", "block");
				$(".coda-nav ul li:last").css("margin-right", "0");

				var listWidth = 0;

				$(".coda-nav ul li").each(function() {
					listWidth += $(this).width();
					listWidth += 5;
				});

//				listWidth -= 5;
//				alert($(".coda-nav").width() + " - " + listWidth);
				$(".coda-nav ul").css("width", listWidth + "px").css("margin-left", (($(".coda-nav").width() - listWidth) / 2) + "px").css("padding-left", "0");
//				alert($(".coda-nav ul").css("padding-left"));
			}

			$("#coda-slider-1").codaSlider({
				autoSlide: true,
				autoSlideInterval: featureInterval,
				autoHeight: false,
				dynamicArrows: false,
				dynamicTabs: false,
				crossLinking: false
//				autoSlideStopWhenClicked: false
			});
		}
	);

	$(".leaderbox.onhover .strapline, .leaderbox.videobox .strapline").hide();
	$(".leaderbox.onhover .hint-off, .leaderbox.videobox .play-off").show();

	$(".leaderbox.onhover, .leaderbox.onebiglink").hover(
		function() {
			$(this).find(".strapline").stop(true, true).fadeIn();
			$(this).find(".hint-over").show();
			$(this).find(".hint-off").hide();

			if ($(this).find("a").length > 0) {
				$(this).css("cursor", "pointer");
				$(this).find("a").css("text-decoration", "underline");
			}
		},
		function() {
			$(this).find(".strapline").stop(true, true).fadeOut();
			$(this).find(".hint-off").show();
			$(this).find(".hint-over").hide();

			if ($(this).find("a").length > 0) {
				$(this).css("cursor", "auto");
				$(this).find("a").css("text-decoration", "none");
			}
		}
	).click(function() {
		var currentPath = location.pathname;
		var bodyClass = $("body").prop("class");

		bodyClass = bodyClass.charAt(0).toUpperCase() + bodyClass.slice(1);
/*
		if (bodyClass != "Home" && bodyClass != "Leader")
		{
			bodyClass = "Content";
		}
*/
		if (typeof(_gaq) != "undefined")
		{
			_gaq.push(['b._trackEvent', bodyClass + ' - promobox hover - whole box', currentPath, getLinkText($("h2 a", this)) + " - " + $("h2 a", this).attr("href")]);
		}

		window.location.href = $(this).find("a").attr("href");
	});

	if ($("a.playvideo").length + $("div.replacewithvideo").length > 0)
	{

		$("<link/>", {
			rel: "stylesheet",
			type: "text/css",
			media: "screen",
			href: "http://www.manchester.ac.uk/medialibrary/javascript/jquery/plugins/fancybox-1.3.4/jquery.fancybox-1.3.4.css"
		}).appendTo("head");

		// We're going to hide video play buttons until we've loaded the necessary JavaScript files.
		$(".leaderbox.videobox .play-off").hide();

		$.getScripts(
			[
				"http://www.manchester.ac.uk/medialibrary/javascript/jquery/plugins/fancybox-1.3.4/jquery.fancybox-1.3.4.pack.js",
				"http://www.manchester.ac.uk/medialibrary/javascript/jwplayer/jwplayer.js"
			],
			function()
			{
				$("div.replacewithvideo").each(function() {
					var divId = $(this).attr("id");
					var videoId = divId.substr(6);
					var imageUrl = $(this).find("img").attr("src");
//					alert(imageUrl);

					jwplayer(divId).setup({
						id: "videoplayer-" + videoId,
						width: "528",
						height: "321",
						provider: "rtmp",
						streamer: "rtmp://helix.stream.manchester.ac.uk/flash",
						file: videoId + ".mp4",
						image: imageUrl,
						controlbar: "bottom",
						bufferlength: "2",
						backcolor: "ffffff",
						frontcolor: "660099",
						lightcolor: "9966cc",
						abouttext: "Adobe Flash Player",
						modes: [
							{
								type: "flash",
								src: "http://www.manchester.ac.uk/medialibrary/video/player/player_5.8-licensed.swf"
							},
							{
								type: "html5",
								config: {
									"file": "http://helix.stream.manchester.ac.uk/flash/" + videoId + ".mp4",
									"provider": "video"
								}
							}
						],
						plugins: {
							"gapro-2": {}
						}/*,
						// We can add this event listener in if we want to manually fire a video play event to Google Analytics.
						events: {
							onPlay: function(event) {
								if (event.oldstate == "BUFFERING" && jwplayer().getPosition() == 0)
								{
									if (typeof(_gaq) != "undefined")
									{
										_gaq.push(["._trackEvent", "Video Plays", "rtmp://helix.stream.manchester.ac.uk/flash/" + videoId + ".mp4", document.URL]);
									}
								}
							}
						}*/
					});
				});

				if ($("a.playvideo").length > 0)
				{
					$("body").append("<div id=\"videoContainer\" style=\"display: none;\"></div>");

					$("a.playvideo").click(function() {
						$(this).parent().mouseout();

						var linkId = $(this).attr("id");
						var videoId = linkId.substr(6);

						// At this point we can work out what aspect ratio the video is, but we'll need this to be passed in via the link's id attribute. We will default to 16:9 but will allow this to be overriden to 4:3 if necessary.

//						alert("http://helix.stream.manchester.ac.uk/flash/" + videoId + ".mp4");

						$("div#videoContainer").append("<div id=\"videoContainer" + videoId + "\" style=\"margin: 0; padding: 0; width: 640px; height: 384px; overflow: hidden;\"><div id=\"videoReplace" + videoId + "\"></div></div>");
//						$("div#videoContainer").append("<div id=\"videoContainer" + videoId + "\" style=\"margin: 0; padding: 0; width: 480px; height: 384px; overflow: hidden;\"><div id=\"videoReplace" + videoId + "\"></div></div>");

						jwplayer("videoReplace" + videoId).setup({
							id: "videoplayer-" + videoId,
							width: 640,
//							width: 480,
							height: 384,
							stretching: "exactfit", // for 16:9 videos
//							stretching: "fill", // for smaller 16:9 videos
//							stretching: "exactfit", // for 4:3 videos
							provider: "rtmp",
							streamer: "rtmp://helix.stream.manchester.ac.uk/flash",
							file: videoId + ".mp4",
							controlbar: "bottom",
							bufferlength: "2",
							backcolor: "ffffff",
							frontcolor: "660099",
							lightcolor: "9966cc",
							abouttext: "Adobe Flash Player",
							autoplay: true,
							modes: [
								{
									type: "flash",
									src: "http://www.manchester.ac.uk/medialibrary/video/player/player_5.8-licensed.swf"
								},
								{
									type: "html5",
									config: {
										"file": "http://helix.stream.manchester.ac.uk/flash/" + videoId + ".mp4",
										"provider": "video"
									}
								}
							],
							plugins: {
								"gapro-2": {}
							},
							events: {
								onComplete: function() {
									$.fancybox.close();
								}
							}
						});

						$.fancybox({
							"transitionIn" : "fade",
							"transitionOut" : "fade",
							"padding" : 0,
							"margin" : 0,
							"width" : 640,
//							"width" : 480,
							"height" : 384, // 640 x 360 (16:9) + 24 pixels vertically for control bar
							"type" : "inline",
							"content" : "#videoContainer" + videoId,
							"showCloseButton" : true,
							"scrolling" : "no",
							"onStart" : function() {
								if ($(".coda-slider").length > 0)
								{
									$(".coda-slider").mouseover();
								}
							},
							"onCleanup" : function() {
								if (jwplayer("videoReplace" + videoId).getState() != "IDLE")
								{
									jwplayer("videoReplace" + videoId).stop();
								}
							},
							"onClosed" : function() {
								if ($(".coda-slider").length > 0)
								{
									$(".coda-slider").mouseout();
								}
							}
						});

						return false;
					});
				}

				$(".leaderbox.videobox .play-off").show();

				$(".leaderbox.videobox").hover(
					function() {
//						$(this).css("cursor", "pointer");
						$(this).find(".strapline").stop(true, true).fadeIn();
						$(this).find(".play-over").show();
						$(this).find(".play-off").hide();
						$(this).find(".title").addClass("hover");
					},
					function() {
//						$(this).css("cursor", "auto");
						$(this).find(".strapline").stop(true, true).fadeOut();
						$(this).find(".play-off").show();
						$(this).find(".play-over").hide();
						$(this).find(".title").removeClass("hover");
					}
				);
			}
		);
	}

	$("div.addthis_toolbox").html("<a class=\"addthis_button_facebook_like\" fb:like:layout=\"button_count\"></a>\n<a class=\"addthis_button_tweet\"></a>\n<a class=\"addthis_button_google_plusone\" g:plusone:size=\"medium\"></a>\n<a class=\"addthis_counter addthis_pill_style\"></a>\n<script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4ee5e9ee7e1d7000\"></script>");

	$.getScript("http://www.manchester.ac.uk/medialibrary/degreeprogrammes/project/javascript/eventtracking.js", function() {}, true);

	$("#footer .navigation").hover(
		function() {
			$("#editthispage").show();
		},
		function() {
			$("#editthispage").hide();
		}
	);

	$("a.newWindow").attr("target", "_blank");
});

$(window).load(function() {
	$("div.panel.hidden", document).each(function() {
		var containerId = $(".panel-wrapper", this).prop("id");
		var backgroundImageComment = $(".panel-wrapper", this).comments();
		var backgroundImage = backgroundImageComment.html();
		$("#" + containerId).css("background-image", "url('" + backgroundImage + "')");
//		$("#" + containerId.replace("container", "nav")).hide().css("visibility", "visible").fadeIn(500);
		$(this).removeClass("hidden");
	});
});
