function initVideo() {
	initVideos();
	initVideoPlayer();
	initVideoSearch();
	
	var close_window = dojo.byId("VideoClose");
	if(close_window) {
		dojo.connect(close_window, "onclick", function (e) {
			window.close();
			e.preventDefault();
		});
	}
	
	var browse = dojo.byId("VideoBrowse");
	if(browse) {
		dojo.connect(browse, "onclick", function (e) {
			dojo.addClass(dojo.byId("VideoBrowse"), "Active");
			dojo.addClass(dojo.byId("VideoCategoryContent"), "Active");
			dojo.removeClass(dojo.byId("VideoSearch"), "Active");
			dojo.removeClass(dojo.byId("VideoSearchContent"), "Active");
			e.preventDefault();
		});
	}
	
	//Category and subcategory behavior
	var categories = dojo.query(".VideoCategory");
	if(categories) {
		dojo.forEach(categories, function (category) {
			dojo.connect(category, "onclick", function (e) {
				//Make all categories inactive and remove highlight
				var categories = dojo.query(".VideoCategory");
				dojo.forEach(categories, function (category) {
					dojo.removeClass(category, "Active");
					dojo.removeClass(category, "MainHighlight");
				});
				
				//Make selected category active and highlight
				dojo.addClass(e.currentTarget, "Active");
				dojo.addClass(e.currentTarget, "MainHighlight");
				
				//Hide all subcategories
				var subcategory_list = dojo.query(".VideoSubcategoryList");
				dojo.forEach(subcategory_list, function (list) {
					dojo.removeClass(list, "Active");
				});
				
				//Make all subcategories inactive
				var subcategories = dojo.query(".VideoSubcategory");
				if(subcategories) {
					dojo.forEach(subcategories, function (subcategory) {
						dojo.removeClass(subcategory, "Active");
					});
				}
				
				var id = e.currentTarget.id.split("VideoCategory")[1];
				var subcategory = dojo.byId("VideoSubcategoryList" + id);
				if(subcategory) {
					dojo.addClass(subcategory, "Active");
				}
				
				dojo.xhrGet({
					url: e.currentTarget.href,
					load: videoCategoryCallback,
					error: dataError
				});
				
				e.preventDefault();
			});
			
			//Scroll to active category
			var category_list = dojo.byId("VideoCategoryList");
			if(dojo.hasClass(category, "Active")) {
				category_list.scrollTop = category.offsetTop - category_list.offsetTop;
			}
		});
	}
	
	var subcategories = dojo.query(".VideoSubcategory");
	if(subcategories) {
		dojo.forEach(subcategories, function (subcategory) {
			dojo.connect(subcategory, "onclick", function (e) {
				//Remove main category highlight
				var categories = dojo.query(".VideoCategory");
				dojo.forEach(categories, function (category) {
					dojo.removeClass(category, "MainHighlight");
				});
				
				//Remove subcategory highlight
				var subcategories = dojo.query(".VideoSubcategory");
				if(subcategories) {
					dojo.forEach(subcategories, function (subcategory) {
						dojo.removeClass(subcategory, "Active");
					});
				}
				
				//Highlight selected subcategory
				dojo.addClass(e.currentTarget, "Active");
				
				dojo.xhrGet({
					url: e.currentTarget.href,
					load: videoCategoryCallback,
					error: dataError
				});
				e.preventDefault();
			});
			
			//Scroll to active subcategory
			var category_list = dojo.byId("VideoCategoryList");
			if(dojo.hasClass(subcategory, "Active")) {
				category_list.scrollTop = subcategory.offsetTop - category_list.offsetTop;
			}
		});
	}
	
	truncateVideoTitles();
}

function videoCategoryCallback(response, ioArgs) {
	dojo.byId("VideoList").innerHTML = response;
	initVideos();
	truncateVideoTitles();
}

//Video list
function initVideos() {
	var videos = dojo.query(".Video");
	var video_list = dojo.byId("VideoList");
	
	if(videos) {
		dojo.forEach(videos,
			function (video) {
				dojo.connect(video, "onclick", function (e) {
					var target = e.currentTarget;
					dojo.xhrGet({
						url: e.currentTarget.href,
						load: function (response, ioArgs) {
							dojo.byId("VideoPlayer").sendEvent("STOP");
							videoDetailsCallback(response, ioArgs, target);
						},
						error: dataError
					});
					e.preventDefault();
				});
				
				//Scroll active video
				if(dojo.hasClass(video.parentNode, "Active")) {
					dojo.byId("VideoList").scrollTop = video.parentNode.offsetTop - video_list.offsetTop;
				}
			}
		);
	}
}

//Video Details
function videoDetailsCallback(response, ioArgs, target) {
	if(response != "") {
		dojo.byId("VideoInformation").innerHTML = response;
		var video_active = dojo.query("#VideoList .Active");
		dojo.forEach(video_active, function (video) {
			dojo.removeClass(video, "Active");
		});
		dojo.addClass(target.parentNode, "Active");
		document.title = dojo.byId("VideoInformationTitle").value;
		initVideoPlayer(true);
	}
}

//Video search
function initVideoSearch() {
	var search_form = dojo.byId("VideoSearch");
	var search_query = dojo.byId("VideoSearchQuery");
	var search_submit = dojo.byId("VideoSearchSubmit");
	var helper_text = "Search all video";
	
	if(search_form) {
		if(search_submit) {
			dojo.connect(search_form, "onsubmit", function (e) {
				if(search_query.value != helper_text) {
					dojo.xhrPost({
						url: search_form.action,
						form: search_form.id,
						load: videoSearchCallback,
						error: dataError
					});
				}
				e.preventDefault();
			});
		}
	}
	
	var search_query = dojo.byId("VideoSearchQuery");
	if(search_query) {
		if(search_query.value != helper_text) {
			dojo.addClass(search_query, "Active");
		}
		dojo.connect(search_query, "onclick", function (e) {
			dojo.addClass(e.currentTarget, "Active");
			if(e.currentTarget.value == helper_text) {
				e.currentTarget.value = "";
			}
		});
	}
}

function videoSearchCallback(response, ioArgs) {
	dojo.byId("VideoSearchContent").innerHTML = response;
	
	dojo.addClass(dojo.byId("VideoSearch"), "Active");
	dojo.addClass(dojo.byId("VideoSearchContent"), "Active");
	dojo.removeClass(dojo.byId("VideoBrowse"), "Active");
	dojo.removeClass(dojo.byId("VideoCategoryContent"), "Active");
	
	//No search results link back to browse
	var browse = dojo.byId("VideoSearchBrowse");
	if(browse) {
		dojo.connect(dojo.byId("VideoSearchBrowse"), "onclick", function (e) {
			dojo.addClass(dojo.byId("VideoBrowse"), "Active");
			dojo.addClass(dojo.byId("VideoCategoryContent"), "Active");
			dojo.removeClass(dojo.byId("VideoSearch"), "Active");
			dojo.removeClass(dojo.byId("VideoSearchContent"), "Active");
			e.preventDefault();
		});
	}
	
	initVideos();
	truncateVideoTitles();
}

function truncateVideoTitles() {
	var video_title = dojo.query(".VideoTitle");
	dojo.forEach(video_title, function (title) {
		var max_height = 30;
		if(title.offsetHeight > max_height) {
			dojo.style(title, "height", max_height + "px");
			var max_length = 30;
			var text = title.innerHTML;
			if(text.length > max_length) {
				text = text.substring(0, max_length);
				text = text.replace(/\w+$/, "");
				text += "...";
				title.innerHTML = text;
			}
		}
	});
}

function initVideoPlayer(autoplay) {
	if(autoplay == null) {
		var autoplay = false;
	}
	var flash_req = dojo.byId("VideoFlashRequired");
	if(flash_req) {
		dojo.connect(flash_req, "onclick", function (e) {
			window.open(e.currentTarget.href);
			e.preventDefault();
		});
	}
	
	var id = "VideoPlayer";
	var path = "";
	var info = dojo.byId("VideoInformationPath");
	if(info) {
		path = info.value;
	} else {
		info = dojo.byId("VideoBroadcastInfo");
		if(info) {
			autoplay = true;
			path = info.value;
		}
	}
	
	if(document.location.href.indexOf("autostart") != -1 || autoplay == true) {
		var flashvars = {
			id: id,
			autostart: "true"
		};
	} else {
		var flashvars = {id: id};
	}
	var params = {
		allowscriptaccess: "always",
		allowfullscreen: "true",
		wmode: "opaque",
		flashvars: path,
		id: id
	};
	var attributes = {
		styleclass: "VideoEmbed",
		id: id
	};
	swfobject.embedSWF("swf/player.swf", id, "100", "100", "10", false, flashvars, params, attributes);
}

function playerReady(obj) {
	dojo.byId(obj.id).addModelListener("STATE", "function (obj) {if(obj.newstate == \"COMPLETED\") {dojo.byId(\"" + obj.id + "\").sendEvent(\"STOP\");}}");
}

//Request error handler.
function dataError(response, ioArgs) {
	console.error("HTTP status code: ", ioArgs.xhr.status);
	return response;
}

dojo.addOnLoad(initVideo);

