//<![CDATA[
var AOLVS = null;			
var VIDEO_PANEL = null;
var TITLE_BAR = null;
var CATEGORY_MENU = null;
var CHANNEL_MENU = null;
var TAG_MENU = null;
var USER_MENU = null;
var LOGIN_BUTTON = null;
function getVideos(query, start) { 
	VIDEO_PANEL.displayMessage('loading...');
	if (arguments.length < 1) { AOLVS.getVideos(); }
	else if (arguments.length < 2) { AOLVS.getVideos(query); }
	else { AOLVS.getVideos(query, start); }
}
function getVideosByCategory(category) {
	if (category.toLowerCase() == 'all') { 
		var query = AOLVS.query;
		query = query.replace(/\s?category:"[^"]*"/gi, "");
		query = query.replace(/\s?category:\S*/gi, "");
		getVideos(query); 
	}
	else { 
		var query = AOLVS.query;
		var theRegExp = new RegExp('\\s?category:"\\s*'+VSUTIL.escapeRegExp(category)+'\\s*"', "gi");
		query = query.replace(theRegExp, "");
		theRegExp = new RegExp('\\s?category:'+VSUTIL.escapeRegExp(category), "gi");
		query = query.replace(theRegExp, "");
		getVideos(query + ' category:"' + category + '"'); 
	}
}
function getVideosByChannel(channel) {
	if (channel.toLowerCase() == 'all') { 
		var query = AOLVS.query;
		query = query.replace(/\s?channel:"[^"]*"/gi, "");
		query = query.replace(/\s?channel:\S*/gi, "");
		getVideos(query); 
	}
	else { 
		var query = AOLVS.query;
		var theRegExp = new RegExp('\\s?channel:"\\s*'+VSUTIL.escapeRegExp(channel)+'\\s*"', "gi");
		query = query.replace(theRegExp, "");
		theRegExp = new RegExp('\\s?channel:'+VSUTIL.escapeRegExp(channel), "gi");
		query = query.replace(theRegExp, "");
		getVideos(query + ' channel:"' + channel + '"'); 
	}
}
function getVideosByTag(tag) {
	if (tag.toLowerCase() == 'all') { 
		var query = AOLVS.query;
		query = query.replace(/\s?tag:"[^"]*"/gi, "");
		query = query.replace(/\s?tag:\S*/gi, "");
		getVideos(query); 
	}
	else { 
		var query = AOLVS.query;
		var theRegExp = new RegExp('\\s?tag:"\\s*'+VSUTIL.escapeRegExp(tag)+'\\s*"', "gi");
		query = query.replace(theRegExp, "");
		theRegExp = new RegExp('\\s?tag:'+VSUTIL.escapeRegExp(tag), "gi");
		query = query.replace(theRegExp, "");
		getVideos(query + ' tag:"' + tag + '"'); 
	}
}
function getVideosByUser(user) {
	if (user.toLowerCase() == 'all') { 
		var query = AOLVS.query;
		query = query.replace(/\s?user:"[^"]*"/gi, "");
		query = query.replace(/\s?user:\S*/gi, "");
		getVideos(query); 
	}
	else { 
		var query = AOLVS.query;
		var theRegExp = new RegExp('\\s?user:"\\s*'+VSUTIL.escapeRegExp(user)+'\\s*"', "gi");
		query = query.replace(theRegExp, "");
		theRegExp = new RegExp('\\s?user:'+VSUTIL.escapeRegExp(user), "gi");
		query = query.replace(theRegExp, "");
		getVideos(query + ' user:"' + user + '"'); 
	}
}
function nextPage() {
	VIDEO_PANEL.displayMessage('loading...');
	AOLVS.nextPage()
}
function previousPage() {
	VIDEO_PANEL.displayMessage('loading...');
	AOLVS.previousPage()
}
function goToPage(pageNum) {
	VIDEO_PANEL.displayMessage('loading...');
	AOLVS.goToPage(pageNum)
}
function hasNextPage() { return(AOLVS.hasNextPage()); }
function hasPreviousPage() { return(AOLVS.hasPreviousPage()); }
function sortBy(sortOption) { getVideos(AOLVS.query.replace(/\s?sort:\S*/gi, "") + " sort:" + sortOption); }
function filterType(type) { 
	var query = AOLVS.query.replace(/\s?type:\S*/gi, "");
	if (type == 'all') { getVideos(query); }
	else { getVideos(query + " type:" + type); }
}
function filterRuntime(runtime) { 
	var query = AOLVS.query.replace(/\s?runtime:\S*/gi, "");
	switch(runtime) {
		case 'short': getVideos(query + " runtime:<2"); break;
		case 'medium': getVideos(query + " runtime:>2 AND runtime:<10"); break;
		case 'long': getVideos(query + " runtime:>10"); break;
		default: getVideos(query); break;
	}
}
function filterQuality(quality) { 
	var query = AOLVS.query.replace(/\s?quality:\S*/gi, "");
	if (quality == 'all') { getVideos(query); }
	else { getVideos(query + " quality:" + quality); }
}
function filterFormat(format) { 
	var query = AOLVS.query.replace(/\s?format:\S*/gi, "");
	if (format == 'all') { getVideos(query); }
	else { getVideos(query + " format:" + format); }
}
function setStyle(styleName) {
	document.getElementById('bodyDiv').className = styleName; 
	setStyleBtn();
}
function setView(value) {
	VIDEO_PANEL.setView(value); 
	setViewBtn();
	VIDEO_PANEL.refresh(AOLVS);
}
function setVideosPerPage(numVideos) {
	AOLVS.results = numVideos;
	setVideosPerPageBtn();
	getVideos(AOLVS.query, AOLVS.start);
}
function setShowAdult(value) { 
	AOLVS.showAdult = value;
	setShowAdultBtn();
	getVideos(AOLVS.query);
}
function submitRating(videoId, rating) { AOLVS.submitRating(videoId, rating); }
function login() {
	saveState();
	AOLVS.login(window.location.href);
}
function logout() { 
	saveState();
	AOLVS.logout(window.location.href);
}
function getFavoriteVideos() { AOLVS.getFavoriteVideos(); }
function addFavorite(id) { AOLVS.addFavorite(id); }
function removeFavorite(id) { AOLVS.removeFavorite(id); }
function getRecentVideos() { AOLVS.getRecentVideos(); }
function addRecentVideo(id) { AOLVS.addRecentVideo(id); }
function clearRecentVideos() {
	if (confirm("Click 'OK' to confirm that you would like to delete all of your recent videos.")) {
		AOLVS.clearRecentVideos();
	}
}
function getWatchlists() { AOLVS.getWatchlists(); }
function addWatchlist(query) { AOLVS.addWatchlist(query); }
function removeWatchlist(query) { AOLVS.removeWatchlist(query); }
function playVideo(videoUrl, id) {
	addRecentVideo(id);
	window.open(videoUrl, '', 'width=800,height=800,location=no,menubar=no,resizable=yes,scrollbars=yes');
}
function handleUpdate(methodName) {
	if ((methodName == 'getVideos') || (methodName == 'getFavoriteVideos') || (methodName == 'getRecentVideos')) {
		TITLE_BAR.refresh(AOLVS);
		document.getElementById('searchBox').value = AOLVS.query;
		if ((AOLVS.VideoSet) && (AOLVS.VideoSet.totalResultsReturned == 0) || !(AOLVS.VideoSet.Video) || (AOLVS.VideoSet.Video.length == 0)) {
			VIDEO_PANEL.displayMessage('There are no videos available.');
			CATEGORY_MENU.clear();
			CHANNEL_MENU.clear();
			TAG_MENU.clear();
			USER_MENU.clear();
		}
		else if ((AOLVS.VideoSet) && (AOLVS.VideoSet.totalResultsAvailable == 1) && (AOLVS.query) && (AOLVS.query.indexOf("id:") >= 0)) {
			VIDEO_PANEL.displayFullView(AOLVS, AOLVS.previousQuery);
			CATEGORY_MENU.clear();
			CHANNEL_MENU.clear();
			TAG_MENU.clear();
			USER_MENU.clear();
		}
		else {
			VIDEO_PANEL.refresh(AOLVS);
			if ((methodName == 'getFavoriteVideos') || (methodName == 'getRecentVideos')) {
				CATEGORY_MENU.clear();
				CHANNEL_MENU.clear();
				TAG_MENU.clear();
				USER_MENU.clear();
			}
			else {
				CATEGORY_MENU.refresh(AOLVS.CategorySet);
				CHANNEL_MENU.refresh(AOLVS.ChannelSet);
				TAG_MENU.refresh(AOLVS.TagSet);
				USER_MENU.refresh(AOLVS.UserSet);
			}
		}
	}
	else if (methodName == 'submitRating') {
		alert(AOLVS.Result.message + " (User rating: " + AOLVS.Result.userRating + ", User rating count: " + AOLVS.Result.userRatingCount +")");
	}
	else if (methodName == 'addFavorite') {
		alert(AOLVS.Result.message);
	}
	else if (methodName == 'removeFavorite') {
		getFavoriteVideos();
	}
	else if (methodName == 'clearRecentVideos') {
		getRecentVideos();
	}
	else if (methodName == 'getWatchlists') {
		TITLE_BAR.displayTitle('My Watchlists');
		VIDEO_PANEL.displayWatchlists(AOLVS);
		CATEGORY_MENU.clear();
		CHANNEL_MENU.clear();
		TAG_MENU.clear();
		USER_MENU.clear();
	}
	else if (methodName == 'addWatchlist') {
		alert(AOLVS.Result.message);
	}
	else if (methodName == 'removeWatchlist') {
		getWatchlists();
	}
	LOGIN_BUTTON.refresh(AOLVS.isLoggedIn());
}
function handleLoad(reloadStateFlag) {
	if (reloadStateFlag) { 
		loadState();
		getVideos(AOLVS.query, AOLVS.start);
	}
	else { getVideos(); }
}
function handleError(errorCode, errorMessage) {
	alert("ERROR: Code " + errorCode + "; " + errorMessage);
}
function VSLoad() {
	VIDEO_PANEL = new VideoPanel1("VideoPanelViewDiv");
	TITLE_BAR = new TitleBar1("TitleBarDiv");
	CATEGORY_MENU = new CategoryMenu1("CategoryMenuDiv");
	CHANNEL_MENU = new ChannelMenu1("ChannelMenuDiv");
	TAG_MENU = new TagMenu1("TagMenuDiv");
	USER_MENU = new UserMenu1("UserMenuDiv");
	LOGIN_BUTTON = new LoginButton1("LoginButton");
	AOLVS = new AOLVideoSearch('1x1jhj64466mi12ia');
	AOLVS.showRelatedItems = 1;
	AOLVS.attachEvent('onerror', 'handleError(errorCode, errorMessage);');
	AOLVS.attachEvent('onupdate', 'handleUpdate(methodName);');
	AOLVS.attachEvent('onload', 'handleLoad(reloadStateFlag);');
	AOLVS.initialize();
}
function setViewBtn() {
	switch(parseInt(VIDEO_PANEL.getView())) {
		case 0: 
			document.getElementById('gridBtn').style.textDecoration = "underline"; 
			document.getElementById('listBtn').style.textDecoration = "none"; 
			document.getElementById('detailBtn').style.textDecoration = "none"; 
			break;
		case 1: 
			document.getElementById('gridBtn').style.textDecoration = "none"; 
			document.getElementById('listBtn').style.textDecoration = "underline"; 
			document.getElementById('detailBtn').style.textDecoration = "none"; 
			break;
		case 2: 
			document.getElementById('gridBtn').style.textDecoration = "none"; 
			document.getElementById('listBtn').style.textDecoration = "none"; 
			document.getElementById('detailBtn').style.textDecoration = "underline"; 
			break;
	}
}
function setVideosPerPageBtn() {
	switch(parseInt(AOLVS.results)) {
		case 10: 
			document.getElementById('10Btn').style.textDecoration = "underline"; 
			document.getElementById('20Btn').style.textDecoration = "none"; 
			document.getElementById('50Btn').style.textDecoration = "none"; 
			break;
		case 20: 
			document.getElementById('10Btn').style.textDecoration = "none"; 
			document.getElementById('20Btn').style.textDecoration = "underline"; 
			document.getElementById('50Btn').style.textDecoration = "none"; 
			break;
		case 50: 
			document.getElementById('10Btn').style.textDecoration = "none"; 
			document.getElementById('20Btn').style.textDecoration = "none"; 
			document.getElementById('50Btn').style.textDecoration = "underline"; 
			break;
	}
}
function setShowAdultBtn() {
	if (AOLVS.showAdult == 1) {
		document.getElementById('familyFilterOffBtn').style.textDecoration = "underline";
		document.getElementById('familyFilterOnBtn').style.textDecoration = "none";
	}
	else {
		document.getElementById('familyFilterOffBtn').style.textDecoration = "none";
		document.getElementById('familyFilterOnBtn').style.textDecoration = "underline";
	}
}
function saveState() {
	var stateCookie = new Cookie(document, "app1_state");
	stateCookie.style = document.getElementById('bodyDiv').className;
	stateCookie.view = VIDEO_PANEL.getView();
	stateCookie.store();
}
function loadState() {
	var stateCookie = new Cookie(document, "app1_state");
	if (stateCookie.load()) {
		document.getElementById('bodyDiv').className = stateCookie.style; 
		VIDEO_PANEL.setView(stateCookie.view); 
		stateCookie.remove();
	}
	setStyleBtn();
	setViewBtn();
	setVideosPerPageBtn();
	setShowAdultBtn();
}
//]]>
