var Y = YAHOO.util;

Y.Event.onContentReady("current", newSongLoaded);

function timer() {
	var startMillis = 0;
	var endMillis = 0;
	this.start = function() {
		var curDateTime = new Date();
		startMillis = curDateTime.getTime();
	}
	this.start();
	this.stop = function() {
		var curDateTime = new Date();
		endMillis = curDateTime.getTime();
	}
	this.getMillis = function() {
		if (endMillis == 0) {
			alert("You forgot to STOP the timer!");
			return 0;
		}
		return (endMillis - startMillis);
	}
}

if (typeof PROGULUS == "undefined" || !PROGULUS) {
	var PROGULUS = {};
}

PROGULUS.loadContent = function(url, el, loadingValue, onSuccessAdditional, onFailureAdditional) {
	var callback = {
		success : Success,
		failure : Failure,
		argument : null,
		timeout : 30000
	}
	
	function Success(o) {
		if (o.responseText !== undefined) {
			if (o.argument && o.argument.el) {
				o.argument.el.innerHTML = o.responseText;
				if (o.argument.success) {
					o.argument.success();
				}
			}
		}
	}
	
	function Failure(o) {
		if (o.argument && o.argument.el) {
			o.argument.el.innerHTML = "PROGULUS.loadContent FAILED!<br>"
					+ "<a onclick=PROGULUS.loadContent(";
			if (o.argument.failure) {
				o.argument.failure();
			}
		}
	}
	
	el = YAHOO.util.Dom.get(el);
	if (el && !('length' in el)) {
		callback.argument = {
			url : url,
			el : el,
			loadingValue : loadingValue,
			success : (typeof onSuccessAdditional == "function" ? onSuccessAdditional
					: null),
			failure : (typeof onFailureAdditional == "function" ? onFailureAdditional
					: null)
		}
		el.innerHTML = (typeof loadingValue != null
				&& typeof loadingValue != "undefined" ? loadingValue
				: "loading...");
		var get = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	}
}

PROGULUS.Gutentag = {
	chatFormID : "gutenform",
	chatForm : null,
	submitOnEnterStatusID : "soeStatus",
	submitOnEnterStatusEl : null
};

PROGULUS.Gutentag.init = function() {
	PROGULUS.Gutentag.chatForm = document.getElementById(PROGULUS.Gutentag.chatFormID);
	PROGULUS.Gutentag.submitOnEnterStatusEl = document.getElementById(PROGULUS.Gutentag.submitOnEnterStatusID);
	var d = new Date();
	PROGULUS.Gutentag.chatForm.timezone.value = d.getTimezoneOffset();
	PROGULUS.Gutentag.chatForm.submitOnEnter.checked = true;
	YAHOO.util.Event.addListener(PROGULUS.Gutentag.chatForm.message, "keydown",	PROGULUS.Gutentag.messageKeyDownHandler);
	YAHOO.util.Event.addListener(PROGULUS.Gutentag.chatForm.chatSubmit,	"click", PROGULUS.Gutentag.messageSubmit);
	YAHOO.util.Event.addListener(PROGULUS.Gutentag.chatForm.submitOnEnter, "click", PROGULUS.Gutentag.submitOnEnterStatus);
}
YAHOO.util.Event.onContentReady("gutenform", PROGULUS.Gutentag.init);
PROGULUS.Gutentag.messageKeyDownHandler = function(e) {
	if (e.keyCode == 13	&& PROGULUS.Gutentag.chatForm.submitOnEnter.checked == true) {
		PROGULUS.Gutentag.messageSubmit();
		YAHOO.util.Event.preventDefault(e)
	}
}
PROGULUS.Gutentag.messageSubmit = function(e) {
	PROGULUS.Gutentag.chatForm.submit();
	PROGULUS.Gutentag.chatForm.message.value = "";
	YAHOO.util.Event.preventDefault(e);
}
PROGULUS.Gutentag.submitOnEnterStatus = function() {
	PROGULUS.Gutentag.submitOnEnterStatusEl.innerHTML = (PROGULUS.Gutentag.chatForm.submitOnEnter.checked == true ? 'On' : 'Off');
}
var expandomatic = null;
PROGULUS.Expandomatic = {
	timer : false,
	wait : 1000,
	obj : false,
	savedmouseout : false,
	popupEl : false,
	popupImage : false,
	onCover : false
}
PROGULUS.Expandomatic.init = function() {
	PROGULUS.Expandomatic.popupEl = document.getElementById("albumcoverpopup");
	PROGULUS.Expandomatic.popupImage = document.getElementById("albumcoverpopup_img");
	YAHOO.util.Event.addListener(PROGULUS.Expandomatic.popupEl, "mouseout", PROGULUS.Expandomatic.shrink);
	expandomatic = PROGULUS.Expandomatic;
	expandomatic.delay = PROGULUS.Expandomatic.delay;
	expandomatic.abort = PROGULUS.Expandomatic.abort;
	expandomatic.expand = PROGULUS.Expandomatic.expand;
}
YAHOO.util.Event.onContentReady("albumcoverpopup", PROGULUS.Expandomatic.init);
PROGULUS.Expandomatic.delay = function(obj) {
	PROGULUS.Expandomatic.obj = obj;
	PROGULUS.Expandomatic.savedmouseout = obj.onmouseout;
	PROGULUS.Expandomatic.obj.onmouseout = PROGULUS.Expandomatic.abort;
	PROGULUS.Expandomatic.timer = setTimeout("PROGULUS.Expandomatic.expand(true)", PROGULUS.Expandomatic.wait);
	PROGULUS.Expandomatic.onCover = true;
}
PROGULUS.Expandomatic.abort = function() {
	clearTimeout(PROGULUS.Expandomatic.timer);
	PROGULUS.Expandomatic.obj.onmouseout = PROGULUS.Expandomatic.savedmouseout;
	PROGULUS.Expandomatic.onCover = false;
}
PROGULUS.Expandomatic.expand = function(state) {
	if (PROGULUS.Expandomatic.onCover == false)
		return;
	PROGULUS.Expandomatic.obj.onmouseout = null;
	clearTimeout(PROGULUS.Expandomatic.timer);
	if (state == true && PROGULUS.Expandomatic.obj != false) {
		var p = PROGULUS.Expandomatic.obj;
		var coverx, covery;
		coverx = covery = 0;
		while (p != null) {
			coverx += p.offsetLeft;
			covery += p.offsetTop;
			p = p.offsetParent;
		}
		PROGULUS.Expandomatic.popupImage.src = PROGULUS.Expandomatic.obj.src;
		var vpHeight = YAHOO.util.Dom.getViewportHeight() + YAHOO.util.Dom.getDocumentScrollTop();
		if (covery + PROGULUS.Expandomatic.popupImage.height > vpHeight)
			covery = (vpHeight - PROGULUS.Expandomatic.popupImage.height);
		YAHOO.util.Dom.addClass(PROGULUS.Expandomatic.popupEl, "MessageBox");
		YAHOO.util.Dom.removeClass(PROGULUS.Expandomatic.popupEl, "hide");
		PROGULUS.Expandomatic.popupEl.style.top = covery + 0 + "px";
		PROGULUS.Expandomatic.popupEl.style.left = coverx + 0 + "px";
	} else {
		YAHOO.util.Dom.addClass(PROGULUS.Expandomatic.popupEl, "hide");
		YAHOO.util.Dom.removeClass(PROGULUS.Expandomatic.popupEl, "MessageBox");
		PROGULUS.Expandomatic.popupImage.src = "";
		PROGULUS.Expandomatic.obj.onmouseout = PROGULUS.Expandomatic.savedmouseout;
	}
}
PROGULUS.Expandomatic.shrink = function() {
	PROGULUS.Expandomatic.expand(false);
}
PROGULUS.Playing = {
	currentSongElement : false,
	currentSongURL : "currentsong.php",
	currentSongJSON: 'currentsong.json.php',
	retryCurrentSongTimer : false,
	retryTimer : 60000,
	loadingEl : null,
	progressEl : null,
	progress : {
		start : 0,
		current : 0,
		length : 0
	}
}

var xmlhttp = false;
try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		xmlhttp = false;
	}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
}
function fetch(url, destID, callback, cbParam) {
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			var dest = document.getElementById(destID);
			if (dest != null) {
				dest.innerHTML = xmlhttp.responseText;
				if (callback) {
					callback(cbParam);
				}
			} else {
				window.status = "Unable to locate '" + destID + "'"
			}
		}
	}
	xmlhttp.send(null)
}
function request(songID) {
	reqwin = window.open(
		"req.php?songID=" + songID,
		"_AR_request",
		"location=no,status=no,menubar=no,scrollbars=no,resizeable=yes,height=420,width=668"
	);
}
var oProgress = {
	start : 0,
	current : 0,
	length : 0
}
function initProgress() {
	var now = new Date();
	var progressBar = document.getElementById("progress");
	if (progressBar == null) {
		return;
	}
	oProgress.current = progressBar.getAttribute("curr");
	if (oProgress.current == null) {
		oProgress.current = 0;
	} else {
		oProgress.current = parseInt(oProgress.current) * 1000;
	}
	oProgress.start = now.getTime() - parseInt(oProgress.current);
	oProgress.length = parseInt(progressBar.getAttribute("len") * 1000);
	progressBar.setAttribute("start",
			(now.getTime() - parseInt(oProgress.current)));
	updateProgress();
}
var songCount = 0;

function updateProgress() {
	var progressBar = document.getElementById("progress");
	var progressImg = document.getElementById("progressimg");
	if (progressBar == null || progressImg == null) {
		return;
	}
	oProgress.current = parseInt(new Date().getTime());
	if (oProgress.current - oProgress.start > oProgress.length) {
		progressImg.style.width = "100%";
		progressBar.parentNode.removeChild(progressBar);
		var loading = document.getElementById("loading");
		if (loading != null) {
			loading.style.display = "block";
		}
//		copyCurrentToRecent();
		getCurrentSong('current');
		return;
	}
	var newWidth = (100 * (oProgress.current - oProgress.start)) / oProgress.length;
	progressImg.style.width = newWidth + "%";
	setTimeout(updateProgress, 1000);
}

var lastSongID = 0;

function newSongLoaded() {
	var ratebar = document.getElementById("ratebar");
	if (ratebar != null) {
		if (document.addEventListener) {
			ratebar.addEventListener("mouseover", showRating, true);
			ratebar.addEventListener("mouseup", setRating, true);
		} else {
			ratebar.onmouseover = showRating;
			ratebar.onmouseup = setRating;
		}
		ratebar.id += ratebar.getAttribute("songID");
	}
	var lsrc = document.getElementById("lsrc");
	var ldst = document.getElementById("ldst");
	if (lsrc != null && ldst != null) {
		ldst.innerHTML = lsrc.innerHTML;
	}
	initProgress();
}
function saveRating(songID, rating) {
//	var theURL = "do_rating.php?songID=" + songID + "&rating=" + rating;
//	fetch(theURL, 'ratingbox_' + songID, adjustAvgRating, songID);
}
function adjustAvgRating(songID) {
//	var avgRating = document.getElementById("avgrating_" + songID);
//	if (avgRating != null) {
//		var avgRatingSrc = document.getElementById("avgratingsrc_" + songID);
//		if (avgRatingSrc != null) {
//			avgRating.innerHTML = avgRatingSrc.innerHTML;
//			avgRatingSrc.parentNode.removeChild(avgRatingSrc);
//		}
//		var votes = document.getElementById("votes_" + songID);
//		if (votes != null) {
//			var voteSrc = document.getElementById("votesrc_" + songID);
//			if (voteSrc != null) {
//				votes.innerHTML = voteSrc.innerHTML;
//				voteSrc.parentNode.removeChild(voteSrc);
//			}
//		}
//	}
}

$('.ratingbox a').live('click', function(ev) {
	var songID, rating;
	songID = $(this).parent().attr('data-song-id');
	rating = $(this).attr('data-rated');
	$.ajax('do_rating.json.php', {
		type: 'get',
		dataType: 'json',
		data: 'songID=' + songID + '&rating=' + rating,
		cache: false,
		timeout: 30000,
		success: function (o) {
			var avgRating, showHalf, stars, div;
			
			$('#avgrating_' + o.SongID).parent().html(o.RatingBar);
			$('#rater_' + o.SongID).replaceWith(o.UserRatingBar);
			/*
			if (typeof o.AvgRating !== 'undefined') {
				showHalf = (o.AvgRating % 1) >= 0.25 && (o.AvgRating % 1) < 0.75;
				stars = o.AvgRating - o.AvgRating % 1;
				if (o.AvgRating % 1 >= 0.25) {
					stars += 1;
				}
				
				$('div#avgrating_' + o.SongID).html(
					$('<div></div>')
						.attr('title', 'Average Rating: ' + o.AvgRating.toFixed(4))
						.addClass('displayrating')
						.addClass('disp' + stars + 'star ' + (showHalf ? 'half' : ''))
				);
			}*/
		},
		error: function (jqXHR, textStatus, errorThrown) {
			window.alert(textStatus + '<br>' + errorThrown);
		}
	});
});

var prevTab = null;
function selectTab(theTab) {
	if (theTab != null && typeof theTab == "string") {
		theTab = document.getElementById(theTab);
	}
	unselectTab();
	setTabView(theTab, "selected");
	prevTab = theTab;
}
function unselectTab(theTab) {
	if (theTab == null) {
		theTab = prevTab;
		if (theTab == null) {
			return;
		}
	}
	setTabView(theTab, "");
}
function setTabView(theTab, view) {
	if (theTab == null) {
		return;
	}
	var tabViewBoxID = theTab.id.replace(/-tab/, "-box");
	var tabViewBox = document.getElementById(tabViewBoxID);
	if (tabViewBox != null) {
		tabViewBox.style.display = (view == "selected" ? "block" : "");
	}
	theTab.className = view;
}

var currentSongElement = false;
var retryCurrentSongTimer = false;
var currentSongURL = "currentsong.php";
var currentSongJSON = 'currentsong.json.php';
var currentSong = {
	ID: '0'
};

function getCurrentSong(element) {
	$('#loading').fadeIn('slow');
	$.ajax(currentSongJSON, {
		type: 'get',
		dataType: 'json',
		timeout: 30000,
		cache: false,
		success: function (o) {
			if (typeof o.currentSong !== 'undefined') {
				if (currentSong.ID === o.currentSong.ID) {
					window.setTimeout(getCurrentSong, 60 * 1000);
				} else {
					$('#loading').fadeOut('slow');
					currentSong = o.currentSong;
					if (typeof o.html !== 'undefined') {
						$('#loading').remove();	
						$('#current div.songentry').clone().prependTo('#recent');
						while ($('#recent div.songentry').length > 9) {
							$('#recent div.songentry').last().fadeOut().remove();
						}
						$('#current').html(o.html);
						newSongLoaded();
					}
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			$('#current').html('<div class="ui-state-error">' + textStatus + '</div>' + errorThrown);
			$('#loading').fadeOut('slow');
			window.setTimeout(getCurrentSong, 60 * 1000);
		}
	});
}


if (typeof PROGULUS == "undefined" || !PROGULUS) {
	var PROGULUS = {};
}
PROGULUS.stats = PROGULUS.stats || {
	statsURL : "/rprweb/stats.ajax.php",
	submitID : "get_stats",
	buildURL : "/rprweb/stats.build.ajax.php",
	buildID : "build_stats",
	historyURL : "/rprweb/stats.history.ajax.php",
	historyID : "get_history",
	contentID : "stats_content",
	contentEl : false,
	titleID : "stats_title",
	titleEl : false,
	formID : "stats_form",
	formEl : false,
	imagesVisible : false,
	requestsVisible : false,
	ratingsVisible : false,
	listenersVisible : false,
	optionID : "stats_"
}
PROGULUS.stats.init = function() {
	PROGULUS.stats.contentEl = document
			.getElementById(PROGULUS.stats.contentID);
	PROGULUS.stats.titleEl = document.getElementById(PROGULUS.stats.titleID);
	PROGULUS.stats.formEl = document.getElementById(PROGULUS.stats.formID);
	YAHOO.util.Event.addListener(PROGULUS.stats.submitID, "click",
			PROGULUS.stats.getStats);
	var btnBuild = document.getElementById(PROGULUS.stats.buildID);
	if (btnBuild)
		YAHOO.util.Event.addListener(PROGULUS.stats.buildID, "click",
				PROGULUS.stats.buildStats);
	var btnHistory = document.getElementById(PROGULUS.stats.historyID);
	if (btnHistory)
		YAHOO.util.Event.addListener(PROGULUS.stats.historyID, "click",
				PROGULUS.stats.getHistory);
	var options = PROGULUS.stats.formEl.month.options;
}
PROGULUS.stats.getStats = function(ym) {
	var callback = {
		success : Success,
		failure : Failure,
		timeout : 20000,
		cache : false
	}
	function Success(o) {
		if (o.responseText) {
			PROGULUS.stats.titleEl.innerHTML = document
					.getElementById(PROGULUS.stats.optionID
							+ PROGULUS.stats.formEl.month.value).innerHTML;
			PROGULUS.stats.contentEl.innerHTML = o.responseText;
			document.getElementById('stats_images_toggle').innerHTML = "Show Images";
			PROGULUS.stats.imagesVisible = false;
		} else {
			PROGULUS.stats.contentEl.innerHTML = "Loading the stats: unknown failure. Please retry";
		}
	}
	function Failure(o) {
		PROGULUS.stats.contentEl.innerHTML = "Loading the stats: failed to connect. Please retry";
	}
	var parms = "?ym=" + PROGULUS.stats.formEl.month.value;
	var getCS = YAHOO.util.Connect.asyncRequest('GET', PROGULUS.stats.statsURL
			+ parms, callback);
	PROGULUS.stats.contentEl.innerHTML = "Loading the stats:";
	YAHOO.util.Dom.addClass("stats_content", "hideimg");
}
PROGULUS.stats.buildStats = function(ym) {
	var callback = {
		success : Success,
		failure : Failure,
		timeout : 20000,
		cache : false
	}
	function Success(o) {
		if (o.responseText) {
			PROGULUS.stats.titleEl.innerHTML = document
					.getElementById(PROGULUS.stats.optionID
							+ PROGULUS.stats.formEl.month.value).innerHTML;
			PROGULUS.stats.contentEl.innerHTML = o.responseText;
		} else {
			PROGULUS.stats.contentEl.innerHTML = "Building the stats: unknown failure. Please retry";
		}
	}
	function Failure(o) {
		PROGULUS.stats.contentEl.innerHTML = "Building the stats: failed to connect. Please retry";
	}
	var parms = "?ym=" + PROGULUS.stats.formEl.month.value;
	var getCS = YAHOO.util.Connect.asyncRequest('GET', PROGULUS.stats.buildURL
			+ parms, callback);
	PROGULUS.stats.contentEl.innerHTML = "Building the stats:";
	YAHOO.util.Dom.addClass("stats_content", "hideimg");
}
PROGULUS.stats.getHistory = function(start) {
	var callback = {
		success : Success,
		failure : Failure,
		timeout : 20000,
		cache : false
	}
	function Success(o) {
		if (o.responseText) {
			PROGULUS.stats.titleEl.innerHTML = document
					.getElementById(PROGULUS.stats.optionID
							+ PROGULUS.stats.formEl.month.value).innerHTML;
			PROGULUS.stats.contentEl.innerHTML = o.responseText;
		} else {
			PROGULUS.stats.contentEl.innerHTML = "Loading current song history: unknown failure. Please retry";
		}
	}
	function Failure(o) {
		PROGULUS.stats.contentEl.innerHTML = "Loading current song history: failed to connect. Please retry";
	}
	if (!start)
		start = 0;
	var parms = "?start=" + start;
	var getCS = YAHOO.util.Connect.asyncRequest('GET',
			PROGULUS.stats.historyURL + parms, callback);
	PROGULUS.stats.contentEl.innerHTML = "Loading current song history:";
	YAHOO.util.Dom.addClass("stats_content", "hideimg");
}
PROGULUS.stats.showRequestDetail = function() {
	if (PROGULUS.stats.requestsVisible == false) {
		YAHOO.util.Dom.removeClass("stats_requests_h", "hidden");
		YAHOO.util.Dom.removeClass("stats_requests_d", "hidden");
		document.getElementById('stats_requests_toggle').innerHTML = "Hide Detail";
		PROGULUS.stats.requestsVisible = true;
	} else {
		YAHOO.util.Dom.addClass("stats_requests_h", "hidden");
		YAHOO.util.Dom.addClass("stats_requests_d", "hidden");
		document.getElementById('stats_requests_toggle').innerHTML = "Show Detail";
		PROGULUS.stats.requestsVisible = false;
	}
}
PROGULUS.stats.showListenersDetail = function() {
	if (PROGULUS.stats.listenersVisible == false) {
		YAHOO.util.Dom.removeClass("stats_listeners_h", "hidden");
		YAHOO.util.Dom.removeClass("stats_listeners_d", "hidden");
		document.getElementById('stats_listeners_toggle').innerHTML = "Hide Detail";
		PROGULUS.stats.listenersVisible = true;
	} else {
		YAHOO.util.Dom.addClass("stats_listeners_h", "hidden");
		YAHOO.util.Dom.addClass("stats_listeners_d", "hidden");
		document.getElementById('stats_listeners_toggle').innerHTML = "Show Detail";
		PROGULUS.stats.listenersVisible = false;
	}
}
PROGULUS.stats.showRatingsDetail = function() {
	if (PROGULUS.stats.ratingsVisible == false) {
		YAHOO.util.Dom.removeClass("stats_ratings_h", "hidden");
		YAHOO.util.Dom.removeClass("stats_ratings_d", "hidden");
		document.getElementById('stats_ratings_toggle').innerHTML = "Hide Detail";
		PROGULUS.stats.ratingsVisible = true;
	} else {
		YAHOO.util.Dom.addClass("stats_ratings_h", "hidden");
		YAHOO.util.Dom.addClass("stats_ratings_d", "hidden");
		document.getElementById('stats_ratings_toggle').innerHTML = "Show Detail";
		PROGULUS.stats.ratingsVisible = false;
	}
}
PROGULUS.stats.showCovers = function() {
	if (PROGULUS.stats.imagesVisible == false) {
		YAHOO.util.Dom.removeClass("stats_content", "hideimg");
		document.getElementById('stats_images_toggle').innerHTML = "Hide Images";
		PROGULUS.stats.imagesVisible = true;
	} else {
		YAHOO.util.Dom.addClass("stats_content", "hideimg");
		document.getElementById('stats_images_toggle').innerHTML = "Show Images";
		PROGULUS.stats.imagesVisible = false;
	}
}

if (typeof PROGULUS == "undefined" || !PROGULUS) {
	var PROGULUS = {};
}
PROGULUS.request = {
	requestURL : "/rprweb/request.json.php",
	dedicateURL : "/rprweb/dedicate.json.php",
	artistlistURL : "/rprweb/search.artistlist.php",
	albumlistURL : "/rprweb/search.albumlist.php",
	songlistURL : "/rprweb/search.songlist.php",
	doneURL : "/rprweb/playing.php",
	songID : 0,
	requestID : 0,
	signature : "",
	filterYear : "",
	prevArtistNode : null,
	prevAlbumNode : null,
	messageEl : null,
	reqEl : null,
	reqmsgEl : null,
	errorFormEl : null,
	errorForm : null,
	dedicateFormEl : null,
	dedicateForm : null,
	searchForm : null,
	aristsEl : null,
	albumsEl : null,
	songsEl : null,
	info : null,
	infoEl : null,
	confirmEl : null,
	confirmForm : null
}
PROGULUS.request.init = function() {
	PROGULUS.request.messageEl = document.getElementById('procmsg');
	PROGULUS.request.reqEl = document.getElementById('requestform');
	PROGULUS.request.reqmsgEl = document.getElementById('reqmsg');
	PROGULUS.request.searchForm = document.getElementById("searchCriteria");
	PROGULUS.request.artistsEl = document.getElementById('artists');
	PROGULUS.request.albumsEl = document.getElementById('albums');
	PROGULUS.request.songsEl = document.getElementById('songs');
	PROGULUS.request.infoEl = document.getElementById('mouseOverInfo');
	YAHOO.util.Event.addListener('btnSearch', "click", PROGULUS.request.Search);
	PROGULUS.request.confirmEl = document.getElementById('confirm');
	PROGULUS.request.confirmForm = document.getElementById('formConfirm');
	YAHOO.util.Event
			.addListener('c_confirm', "click", PROGULUS.request.Request);
	YAHOO.util.Event.addListener('c_cancel', "click",
			PROGULUS.request.CancelRequest);
	PROGULUS.request.dedicateForm = document.getElementById('formDedicate');
	PROGULUS.request.dedicateFormEl = document.getElementById('dedicate');
	YAHOO.util.Event.addListener('d_dedicate', "click",
			PROGULUS.request.Dedicate);
	YAHOO.util.Event.addListener('d_return', "click", PROGULUS.request.Return);
	PROGULUS.request.errorForm = document.getElementById('formError');
	PROGULUS.request.errorFormEl = document.getElementById('reqerror');
	YAHOO.util.Event.addListener('e_again', "click",
			PROGULUS.request.CancelRequest);
	YAHOO.util.Event.addListener('e_return', "click", PROGULUS.request.Return);
	YAHOO.util.Event.addListener(PROGULUS.request.infoEl, 'mouseout',
			PROGULUS.request.info.Hide);
	YAHOO.util.Event.addListener(PROGULUS.request.searchForm, 'submit',
			PROGULUS.request.Search);
}
PROGULUS.request.FailureHandler = function(o) {
	var message = (o.argument.message ? o.argument.message
			: "An error occurred. Please try again.");
	var messageEl = (o.argument.messageEl ? document
			.getElementById(o.argument.messageEl) : PROGULUS.request.messageEl);
	YAHOO.util.Dom.removeClass(o.argument.busyEl, "searchloading");
	if (messageEl)
		messageEl.innerHTML = message;
}
PROGULUS.request.searchParms = function() {
	var parms = "?"
			+ "ss="
			+ encodeURIComponent(PROGULUS.request.searchForm.search_str.value)
			+ "&year="
			+ encodeURIComponent(PROGULUS.request.searchForm.year.value)
			+ "&country="
			+ encodeURIComponent(PROGULUS.request.searchForm.country.value)
			+ "&artistid="
			+ PROGULUS.request.searchForm.artistid.value
			+ "&cbSearchArtist="
			+ (PROGULUS.request.searchForm.cbSearchArtist.checked == true ? 1
					: 0)
			+ "&cbSearchAlbum="
			+ (PROGULUS.request.searchForm.cbSearchAlbum.checked == true ? 1
					: 0)
			+ "&cbSearchTitle="
			+ (PROGULUS.request.searchForm.cbSearchTitle.checked == true ? 1
					: 0) + "&rating="
			+ encodeURIComponent(PROGULUS.request.searchForm.rating.value)
			+ "&rated="
			+ encodeURIComponent(PROGULUS.request.searchForm.rated.value);
	return parms;
}
PROGULUS.request.LoadArtists = function(letter) {
	var callback = {
		success : Success,
		failure : PROGULUS.request.FailureHandler,
		argument : {
			busyEl : "artists_box",
			messageEl : "artists_box",
			message : "Your artist search was unsuccesful, please retry in a moment."
		},
		timeout : 30000,
		cache : false
	}
	function Success(o) {
		PROGULUS.request.messageEl.innerHTML = "";
		if (o.responseText !== undefined) {
			YAHOO.util.Dom.removeClass("artists_box", "searchloading");
			document.getElementById("artists").innerHTML = o.responseText;
		}
		if (PROGULUS.request.prevArtistNode) {
			YAHOO.util.Dom
					.addClass(PROGULUS.request.prevArtistNode, "selected");
			PROGULUS.request.prevArtistNode = null;
		}
	}
	var parms = "?letter="
			+ (typeof letter == "string" ? encodeURIComponent(letter + "%")
					: encodeURIComponent(PROGULUS.request.searchForm.search_str.value));
	YAHOO.util.Dom.addClass("artists_box", "searchloading");
	YAHOO.util.Connect.setForm(PROGULUS.request.searchForm, false);
	var search = YAHOO.util.Connect.asyncRequest('GET',
			PROGULUS.request.artistlistURL + parms, callback);
}
PROGULUS.request.LoadAlbums = function(obj) {
	var callback = {
		success : Success,
		failure : PROGULUS.request.FailureHandler,
		argument : {
			busyEl : "albums_box",
			messageEl : "albums_box",
			message : "Your album search was unsuccesful, please retry in a moment."
		},
		timeout : 30000,
		cache : false
	}
	function Success(o) {
		YAHOO.util.Dom.removeClass("albums_box", "searchloading");
		if (o.responseText !== undefined && PROGULUS.request.albumsEl.innerHTML) {
			PROGULUS.request.albumsEl.innerHTML = o.responseText;
		}
	}
	var parms = "";
	if (obj) {
		if (PROGULUS.request.prevArtistNode) {
			YAHOO.util.Dom.removeClass(PROGULUS.request.prevArtistNode,
					"selected");
		}
		YAHOO.util.Dom.addClass(obj.n, "selected");
		PROGULUS.request.prevArtistNode = obj.n;
		PROGULUS.request.searchForm.search_artistid.value = obj.a;
	} else {
		PROGULUS.request.searchForm.search_artistid.value = "";
	}
	YAHOO.util.Dom.addClass("albums_box", "searchloading");
	var search = YAHOO.util.Connect.asyncRequest('GET',
			PROGULUS.request.albumlistURL + PROGULUS.request.searchParms(),
			callback);
}
PROGULUS.request.LoadSongs = function(obj) {
	var callback = {
		success : Success,
		failure : PROGULUS.request.FailureHandler,
		argument : {
			busyEl : "songs_box",
			messageEl : "songs_box",
			message : "Your song search was unsuccesful, please retry in a moment."
		},
		timeout : 30000,
		cache : false
	}
	function Success(o) {
		YAHOO.util.Dom.removeClass("songs_box", "searchloading");
		if (o.responseText !== undefined && PROGULUS.request.albumsEl.innerHTML) {
			PROGULUS.request.songsEl.innerHTML = o.responseText;
		}
	}
	var parms = "";
	if (obj) {
		if (PROGULUS.request.prevAlbumNode) {
			YAHOO.util.Dom.removeClass(PROGULUS.request.prevAlbumNode,
					"selected");
		}
		YAHOO.util.Dom.addClass(obj.node, "selected");
		PROGULUS.request.prevAlbumNode = obj.node;
		parms = "?artistid=" + obj.artist + "&albumid=" + obj.album + "&year="
				+ PROGULUS.request.searchForm.search_year.value + "&country="
				+ PROGULUS.request.searchForm.search_country.value;
	} else if (PROGULUS.request.searchForm.search_str.value != "") {
		parms += "?ss="
				+ encodeURIComponent(PROGULUS.request.searchForm.search_str.value)
				+ "&year=" + PROGULUS.request.searchForm.search_year.value
				+ "&country="
				+ PROGULUS.request.searchForm.search_country.value;
	}
	YAHOO.util.Dom.addClass("songs_box", "searchloading");
	var search = YAHOO.util.Connect.asyncRequest('GET',
			PROGULUS.request.songlistURL + parms, callback);
}
PROGULUS.request.Confirm = function(o) {
	if (o && PROGULUS.request.confirmForm) {
		PROGULUS.request.confirmForm.c_artist.value = o.artist;
		PROGULUS.request.confirmForm.c_title.value = o.title
		PROGULUS.request.confirmForm.c_id.value = o.id;
		YAHOO.util.Dom.addClass(PROGULUS.request.dedicateFormEl, "hidden");
		YAHOO.util.Dom.removeClass(PROGULUS.request.reqEl, "hidden");
		YAHOO.util.Dom.removeClass(PROGULUS.request.confirmEl, "hidden");
	}
}
PROGULUS.request.CancelRequest = function() {
	YAHOO.util.Dom.addClass(PROGULUS.request.reqEl, "hidden");
	YAHOO.util.Dom.addClass(PROGULUS.request.errorFormEl, "hidden");
	YAHOO.util.Dom.addClass(PROGULUS.request.confirmEl, "hidden");
}
PROGULUS.request.Request = function() {
	var callback = {
		success : Success,
		failure : PROGULUS.request.FailureHandler,
		argument : {
			func : "PROGULUS.request.Request"
		},
		timeout : 30000,
		cache : false
	}
	function Success(o) {
		PROGULUS.request.reqmsgEl.innerHTML = "";
		if (o.responseText !== undefined) {
			var result = false;
			try {
				result = YAHOO.lang.JSON.parse(o.responseText);
			} catch (e) {
				PROGULUS.request.reqmsgEl.innerHTML = "Error: Unable to parse response from server.";
				YAHOO.util.Dom.addClass(PROGULUS.request.reqmsgEl, "error");
			}
			if (result) {
				PROGULUS.request.songID = result.SongID;
				PROGULUS.request.requestID = result.RequestID;
				PROGULUS.request.signature = result.Signature;
				if (result.Result != "200") {
					PROGULUS.request.reqmsgEl.innerHTML += "<br>Result: "
							+ result.Result + "<br>" + result.Text + "<br>";
					YAHOO.util.Dom.addClass(PROGULUS.request.reqmsgEl, "error");
					YAHOO.util.Dom.removeClass(PROGULUS.request.errorFormEl,
							"hidden");
				} else {
					YAHOO.util.Dom.removeClass(PROGULUS.request.dedicateFormEl,
							'hidden');
				}
			}
		}
	}
	var requestSongID = PROGULUS.request.confirmForm.c_id.value;
	YAHOO.util.Dom.addClass(PROGULUS.request.confirmEl, "hidden");
	YAHOO.util.Dom.removeClass(PROGULUS.request.reqEl, "hidden");
	PROGULUS.request.reqmsgEl.innerHTML = "Sending request";
	var parms = "?songid=" + requestSongID;
	var request = YAHOO.util.Connect.asyncRequest('GET',
			PROGULUS.request.requestURL + parms, callback);
}
PROGULUS.request.Search = function(e) {
	YAHOO.util.Event.preventDefault(e);
	var searching = false;
	if (PROGULUS.request.searchForm.cbSearchArtist.checked == true
			&& PROGULUS.request.searchForm.search_str.value != "") {
		searching = true;
		PROGULUS.request.LoadArtists();
	}
	if (PROGULUS.request.searchForm.cbSearchAlbum.checked == true
			&& PROGULUS.request.searchForm.search_str.value != "") {
		searching = true;
		PROGULUS.request.LoadAlbums();
	}
	if (PROGULUS.request.searchForm.cbSearchTitle.checked == true
			&& PROGULUS.request.searchForm.search_str.value != "") {
		searching = true;
		PROGULUS.request.LoadSongs();
	}
	if (searching == false) {
		PROGULUS.request.LoadArtists();
	}
}
PROGULUS.request.Dedicate = function() {
	var callback = {
		success : Success,
		failure : PROGULUS.request.FailureHandler,
		argument : {
			func : "PROGULUS.request.Request"
		},
		timeout : 30000,
		cache : false
	}
	function Success(o) {
		PROGULUS.request.reqmsgEl.innerHTML = "";
		if (o.responseText !== undefined) {
			var result = false;
			try {
				result = YAHOO.lang.JSON.parse(o.responseText);
			} catch (e) {
				PROGULUS.request.reqmsgEl.innerHTML = "Error: Unable to parse response from server.";
				YAHOO.util.Dom.addClass(PROGULUS.request.reqmsgEl, "error");
			}
			if (result) {
				if (result.Result != "200") {
					PROGULUS.request.reqmsgEl.innerHTML += "<br>Result: "
							+ result.Result + "<br>" + result.Text;
					YAHOO.util.Dom.addClass(PROGULUS.request.reqmsgEl, "error");
					YAHOO.util.Dom.removeClass(PROGULUS.request.dedicateFormEl,
							"hidden");
				} else {
					if (PROGULUS.request.dedicateForm.request_again.checked == true) {
						YAHOO.util.Dom.addClass(
								PROGULUS.request.dedicateFormEl, "hidden");
						YAHOO.util.Dom.addClass(PROGULUS.request.reqEl,
								"hidden");
					} else {
						PROGULUS.request.Return();
					}
				}
			}
		}
	}
	if (PROGULUS.request.songID == 0 || PROGULUS.request.requestID == 0) {
		alert("Invalid Dedicaton");
		return false;
	}
	PROGULUS.request.reqmsgEl.innerHTML = "Processing dedication....";
	var postData = "songid=" + PROGULUS.request.songID + "&requestid="
			+ PROGULUS.request.requestID + "&name="
			+ encodeURIComponent(PROGULUS.request.dedicateForm.d_name.value)
			+ "&message="
			+ encodeURIComponent(PROGULUS.request.dedicateForm.d_message.value);
	var request = YAHOO.util.Connect.asyncRequest('POST',
			PROGULUS.request.dedicateURL, callback, postData);
	YAHOO.util.Dom.addClass(PROGULUS.request.dedicateFormEl, "hidden");
}
PROGULUS.request.Return = function(timelimit) {
	if (typeof timelimit == 'undefined') {
		timelimit = 500;
	}
	YAHOO.util.Dom.addClass(PROGULUS.request.reqEl, "hidden");
	setTimeout("location.href='" + PROGULUS.request.doneURL + "'", timelimit);
}
PROGULUS.request.info = {
	anchor : null,
	title : "",
	requested : 0,
	played : 0,
	lastplayed : "",
	avgrating : 0,
	avgvotes : 0,
	myrating : 0,
	myvotes : 0,
	timer : null
}
PROGULUS.request.info.Delay = function(o) {
	PROGULUS.request.info.anchor = o.node;
	PROGULUS.request.info.title = o.title;
	PROGULUS.request.info.requested = o.requested;
	PROGULUS.request.info.played = o.played;
	PROGULUS.request.info.lastplayed = o.lastplayed;
	PROGULUS.request.info.avgrating = o.avgrating;
	PROGULUS.request.info.avgvotes = o.avgvotes;
	PROGULUS.request.info.myrating = o.myrating;
	PROGULUS.request.info.myvotes = o.myvotes;
	PROGULUS.request.info.timer = window.setTimeout(PROGULUS.request.info.Show,
			500);
}
PROGULUS.request.info.Show = function() {
	if (PROGULUS.request.info.anchor) {
		YAHOO.util.Dom.addClass(PROGULUS.request.infoEl, "hidden");
		YAHOO.util.Dom.removeClass(PROGULUS.request.infoEl, "hidden");
		document.getElementById('moTitle').innerHTML = PROGULUS.request.info.title;
		document.getElementById('moRequested').innerHTML = PROGULUS.request.info.requested;
		document.getElementById('moPlayed').innerHTML = PROGULUS.request.info.played
				+ " play" + (PROGULUS.request.info.played != 1 ? "s" : "");
		document.getElementById('moLastPlayed').innerHTML = PROGULUS.request.info.lastplayed;
		document.getElementById('moAvgRating').innerHTML = PROGULUS.request.info.avgrating;
		document.getElementById('moAvgVotes').innerHTML = PROGULUS.request.info.avgvotes
				+ " vote" + (PROGULUS.request.info.avgvotes != 1 ? "s" : "");
		document.getElementById('moMyRating').innerHTML = PROGULUS.request.info.myrating;
		document.getElementById('moMyVotes').innerHTML = PROGULUS.request.info.myvotes
				+ " vote" + (PROGULUS.request.info.myvotes != 1 ? "s" : "");
		var x = YAHOO.util.Dom.getX(PROGULUS.request.info.anchor);
		var y = YAHOO.util.Dom.getY(PROGULUS.request.info.anchor)
				+ PROGULUS.request.info.anchor.offsetHeight;
		var y2 = PROGULUS.request.infoEl.clientHeight
				+ PROGULUS.request.info.anchor.clientHeight + 3;
		YAHOO.util.Dom.setX(PROGULUS.request.infoEl, x);
		YAHOO.util.Dom.setY(PROGULUS.request.infoEl, y - y2);
		YAHOO.util.Dom.removeClass(PROGULUS.request.infoEl, "hidden");
	}
}
PROGULUS.request.info.Hide = function() {
	PROGULUS.request.info.timer = null;
	if (PROGULUS.request.info.anchor != null) {
		PROGULUS.request.info.anchor = null;
	}
	YAHOO.util.Dom.addClass(PROGULUS.request.infoEl, "hidden");
}
PROGULUS.request.info.ShowSongInfo = function(o) {
	if (o.node) {
		o.title = "\"" + o.title + "\"<br>" + o.artist;
		PROGULUS.request.info.Delay(o);
	}
}
PROGULUS.request.info.ShowAlbumInfo = function(o) {
	if (o.node) {
		o.title = "<span class=\"album\">" + o.album + "</span><br>" + o.artist;
		PROGULUS.request.info.Delay(o);
	}
}
PROGULUS.request.info.ShowArtistInfo = function(o) {
	if (o.node) {
		o.title = o.artist;
		PROGULUS.request.info.Delay(o);
	}
}
PROGULUS.PopupWindow = function(url, title, attribs) {
	if (!title)
		title = "";
	if (!attribs)
		attribs = "";
	if (typeof url != "undefined" && url != null)
		window.open(url, title, attribs);
}
p_rla = PROGULUS.request.LoadArtists;
p_rll = PROGULUS.request.LoadAlbums;
PROGULUS.Login = (function() {
	var IDs = {
		DIALOG_LOGIN : 'dlgLogin',
		LOGIN_WAITING : 'login_waiting',
		MENU_LOGIN : 'menuLogin'
	}, elements = {
		login : null
	}, dialogs = {
		login : null
	}, init;

	init = function() {
		elements.login = Y.Dom.get(IDs.MENU_LOGIN);
		dialogs.login = new YAHOO.widget.Dialog(IDs.DIALOG_LOGIN, {
			width : '20em',
			fixedcenter : true,
			visible : false,
			constraintoviewport : true,
			draggable : true,
			zIndex : 2000,
			buttons : [ {
				text : "Login",
				handler : function() {
					Y.Dom.removeClass(IDs.LOGIN_WAITING, "hidden");
					this.submit();
				},
				isDefault : true
			}, {
				text : "Cancel",
				handler : function() {
					this.cancel()
				}
			} ],
			hideaftersubmit : false
		});
		dialogs.login.render();
		dialogs.login.callback.success = function() {
			location.reload();
		};
		Y.Dom.removeClass(IDs.DIALOG_LOGIN, 'hidden');

		Y.Event.addListener(elements.login, 'click', function(ev) {
			dialogs.login.show();
		});
	};

	Y.Event.onDOMReady(init);
}());

