var totalPages;

function galleryLoaded(responseText, textStatus, XMLHttpRequest) {
	// load content from server via JSON
	$.post(api_paths.gallery, {sort:lastSort,page:lastPage,per_page:6}, function(data) {
		if (data.success == "true") {
			for (var i = 0; i < data.records.length; i++) {
				var video_id = data.records[i].video_id;
				var rating = data.records[i].rating;
				var thumb_url = data.records[i].thumb_url;
				totalPages = parseInt(data.total_pages);
				var videoDiv = "<div class=\"video\"><a href=\"javascript:setLocation('/gallery/" + video_id + "')\"><img src=\"" + thumb_url + "\" width=\"126\" height=\"94\" alt=\"\" /></a></div>";
				videoDiv += "<ul class=\"rating\">";
				var floatRating = parseFloat(rating);
				for (var j = 0; j < 5; j++) {
					if (floatRating - 1 >= 0) {
						videoDiv += "<li class=\"full\"><!-- filled star --></li>";
						floatRating -= 1;
					} else if (floatRating - 0.5 >= 0) {
						videoDiv += "<li class=\"half\"><!-- half-filled star --></li>";						
						floatRating -= 0.5;
					} else {
						videoDiv += "<li><!-- empty star --></li>";						
					}
				}
				videoDiv += "</ul>";
				$("#gallery .gallery-page li.video")[i].innerHTML = videoDiv;
			}
			if (lastPage > 1) {
				// show prev link
				$("#html-content #gallery a.prev").css("display","inline");
			} else {
				$("#html-content #gallery a.prev").css("display","none");				
			}
			if (lastPage < totalPages) {
				// show next link
				$("#html-content #gallery a.next").css("display","inline");
			} else {
				$("#html-content #gallery a.next").css("display","none");
			}
			$("#html-content #gallery #current-page").html(lastPage);
			$("#html-content #gallery #total-pages").html(totalPages);
			$("#html-content #gallery li.button p").show();
			omniture("?event=event19");
		}
	}, "json");
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function galleryVideoLoaded(responseText, textStatus, XMLHttpRequest) {
	// glean id from the location
	var hashSplit = document.location.hash.split("/");
	currentVideo = hashSplit[hashSplit.length - 1];
	
	$.post(api_paths.gallery_video, {video_id:currentVideo}, function(data) {
		if (data.success == "true") {
			var rating = data.rating;
			var video_url = data.video_url;
			var ratingList = "";
			var floatRating = parseFloat(rating);
			for (var j = 0; j < 5; j++) {
				if (floatRating - 1 >= 0) {
					ratingList += "<li class=\"full\"><a href=\"javascript:sendRating(currentVideo," + (j + 1) + ")\"><!-- full --></a></li>";
					floatRating -= 1;
				} else if (floatRating - 0.5 >= 0) {
					ratingList += "<li class=\"half\"><a href=\"javascript:sendRating(currentVideo," + (j + 1) + ")\"><!-- half --></a></li>";						
					floatRating -= 0.5;
				} else {
					ratingList += "<li><a href=\"javascript:sendRating(currentVideo," + (j + 1) + ")\"><!-- empty --></a></li>";						
				}
			}
			$("#html-content #gallery .gallery-video .share ul.rating").html(ratingList);
			// create rating rollovers
			$("#html-content #gallery .gallery-video .share ul.rating li").bind("mouseenter", function() {
				$(this).find("a").addClass("full");
				$(this).prevAll().find("a").addClass("full");
				$(this).nextAll().find("a").addClass("empty");
			}).bind("mouseleave", function() {
				$("#html-content #gallery .gallery-video .share ul.rating li a").removeClass();
			});
			
			$("#html-content #gallery textarea").text(root_url + "#/gallery/" + currentVideo);
			
			var flashvars = {};
			flashvars.w = "400";
			flashvars.h = "300";
			flashvars.fn = video_url;
			flashvars.auto = "true";
			var params = {};
			params.menu = "false";
			params.bgcolor = "#000000";
			params.wmode = "opaque";
			var attributes = {};
			swfobject.embedSWF("swf/MediaPlayer.swf", "video", "400", "300", "9.0.115", false, flashvars, params, attributes);
			
			omniture("?event=event20");
		}
	}, "json");
	
	htmlContentFullLoaded(responseText, textStatus, XMLHttpRequest);
}

function showGallery(page, sort) {
	lastPage = page;
	lastSort = sort;
	var galleryURL = html_paths.gallery + "?p=" + page + "&s=" + sort;
	showPage(galleryURL, "full", galleryLoaded);
}

function sendRating(video, rating) {
	$.post(api_paths.gallery_video_rating, {video_id:currentVideo,rating:rating}, function(data) {
		if (data.success == "true") {
			var ratingList = "";
			var floatRating = parseFloat(data.rating);
			for (var j = 0; j < 5; j++) {
				if (floatRating - 1 >= 0) {
					ratingList += "<li class=\"full\"><!-- filled star --></li>";
					floatRating -= 1;
				} else if (floatRating - 0.5 >= 0) {
					ratingList += "<li class=\"half\"><!-- half-filled star --></li>";						
					floatRating -= 0.5;
				} else {
					ratingList += "<li><!-- empty star --></li>";						
				}
			}
			$("#gallery ul.rating").html(ratingList);
		}
	}, "json");
}