$(document).ready(function() {
	$.evalJSON = function(src) {
		if(window.JSON && JSON.parse) return JSON.parse(src);
		return eval("(" + src + ")");
	};

	function _loadContent(evt, e) {
		if(e.attr("tagName") == "A" && /(iwishes\/|video\/).+/.test(e.attr("href")) && !/^more/i.test(e.html())) {
			var params = { w: 352, h: 290, format: "html" };
			if(RegExp.$1 == "iwishes/") {
				params.w = 240;
				params.h = 170;
				isVideo = false;
			} else isVideo = true;

			$.post(e.attr("href"), params, function(data, textStatus) {
				var d = $.evalJSON(data);
				$("#currently-viewing").html(d.title);
				if(isVideo) {
					/*if(d.content.indexOf("http://") == 0) $f().play(d.content);
					else */$("#content-viewer").html(d.content);
				} else $("#content-viewer").html(d.content + d.description);
			});
			evt.preventDefault();
		}
	}

	$("DIV.content-selector").click(function(evt) {
		var e = $(evt.target);
		if(e.attr("tagName") != "A") {
			e = e.parents("LI.video").find("A:first");
		}

		_loadContent(evt, e);
	});

	$("DIV.sidebar .video-pager").click(function(evt) {
		var e = evt.target;
		if(e.tagName == "A") {
			var pageUrl = location.href.replace(/\/(page|sort)\/.*$/, "");
			if(e.href.indexOf(pageUrl) == 0 && /(.+)\/page\/(\d+)/.test(e.href)) {
				var url = e.href;
				var parent = $(e).parents(".video-pager");
				$.get(url, { }, function(data, textStatus) {
					parent
						.removeClass("loading")
						.html(data);
				});

				parent.addClass("loading");

				evt.preventDefault();
			}
		}
	});
});

