$(function(e) {
	// disabled download buttons
	$("a.disabled").click(function(e){
		e.preventDefault();
	});
	
	// ajax download
	$("a.file-download").click(function(e) {
		var reSid = new RegExp("sid=[0-9]+");
		var reMtype = new RegExp("mtype=[^&]");
		var downLink = $(this).attr("href");
		var sid = downLink.substring(reSid.exec(downLink).index);
		var mtype = downLink.substring(reMtype.exec(downLink).index);

		// post to user-download page with song id and media type
		$.ajax({
			url: "user-download.asp?download=verify",
			async: false,
			type: "POST",
			data: {
				sid: sid.substring(4), 
				mtype: mtype.substring(6,7)
			},
			// process postback results
			success: function(data) {
				var result = eval('(' + data + ')');
				// user is not authorized for download
				if (result.Ok != 1) {
					e.preventDefault();
					$.openDOMWindow({
						windowSource: "iframe",
						windowSourceURL: result.Link,
						width: 650,
						height: 480,
						borderSize:0,
						windowPadding:0
					});
				}
				// else, we continue the event chain and let the user load the user-download page
				// which, since we just authenticated above, should redirect to the actual file
			}
		});
	});
	
	// ajax tooltip for viewing cd/dvd listing
	// $(".download-widget > tbody > tr > td > a.show-compilation").bind("mouseover", onListingHover);
	$(".download-widget > tbody > tr > td > a.show-compilation").bind("click", onListingClick);
	
	$("a.videosample").bind("click", onSampleClick);
});

// open DOM Window for video preview
function onSampleClick(e) {
	e.preventDefault();
	url = $(this).attr("href");
	$.openDOMWindow({
		windowSource: "iframe",
		windowSourceURL: url,
		width: 373,
		height: 241,
		borderSize:5,
		windowPadding:0
	});
}

function onListingClick(e) {
	var url = $(this).attr("link");
	if (url == null || url == "") {
		url = $(this).attr("href");
	}
	e.preventDefault();
	$.openDOMWindow({
		windowSource: "iframe",
		windowSourceURL: url,
		width: 360,
		height: 300,
		borderSize:0,
		windowPadding:0
	});
}

// ajax tooltip for viewing cd/dvd listing
// @deprecated
function onListingHover(e) {
	e.preventDefault();
	var sid, ttip, me;
	// only need to make ajax request once, so unbind once handled
	$(this).unbind("mouseover");
	// assert that it hasn't already been requested (by checking href attribute)
	//if ($($(this).attr("href")).length == 0) {
		// clear default title as we will be replacing it with the tooltip
		$(this).attr("title", "");
		// find artist and title to search for in the table row it's in
		//artist = $(this).parents("table#body_tracks > tbody > tr").find(".artist > a").eq(0).attr("title").replace("Find more songs by ", "");
		//title = $(this).parents("table#body_tracks > tbody > tr").find(".title > a").eq(0).text();
		sid = $(this).attr("href").substring(15);
		
		// create the tooltip contents
		ttip = document.createElement("div");
		$(ttip).html("<p>Loading...</p>");
		// give it a random id
		$(ttip).attr("id", Math.random().toString().substring(2));
		$(this).attr("id", Math.random().toString().substring(2));
		// add it to the list of tooltips
		$("#tooltips").append(ttip);
		// save href as custom "link" attribute
		$(this).attr("link", $(this).attr("href"));
		// set the href attribute to the newly created tooltip's id
		$(this).attr("href", "#" + $(ttip).attr("id"));

		// bind the link to the newly created tooltip
		$(this).tooltip({
			tip: "#" + $(ttip).attr("id"),
			effect: 'slide',
			position: 'top left',
			offset: [10, $(this).text() == "CD" ? 75 : 45],
			delay: 750,
			predelay: 100
		});
		// show the tooltip (which contains a loading message);
		$(this).mouseover();
		me = this;
		
		// make ajax query to populate the tooltip
		$.ajax({
			url: "ajax/search.asp", 
			data : {
				sid: sid,
				c: $(this).text() == "CD" ? "C" : "D"
			},
			success: function(data) {
				var output = eval('(' + data + ')');
				var results = "";
				if (output.Ok == 1) {
					for (var i = 0; i < output.Results.length; i++) {
						results += "<li><a href=\"compilation.asp?series=" + output.Results[i].Series + "&amp;cd=" + output.Results[i].CD + "\">" + 
							output.Results[i].SeriesName + " vol. " + output.Results[i].CD + "</a></li>";
					}
					if (output.Results.length > 0) {
						results = "This song can be found on:<br /><br /><ol>" + results + "</ol>";
						// hide loading message
						$(ttip).hide();
						// generate contents
						$(ttip).html(results);
						// show the tooltip again so that it repositions itself
						setTimeout('$("#' + $(me).attr("id") + '").mouseover()', 250);
					} else {
						$(ttip).html("<p>Error!</p>");
					}
				} else {
					$(ttip).html("<p>Error!</p>");
				}
			}
		});
	//}
}