var sort = "sermon_date";
var sermon_asc = false;
var title_asc = false;
var method = "DESC";
var showAll = "";
var limit = 0;

function increment() {
	limit+= 15;
	getMediaList();
}

function decrement() {
	limit -=15;
	getMediaList();
}

function setPage(pageNum) {
	limit = (pageNum - 1) * 15;
	getMediaList();
}

function reset() {
	sort = "sermon_date";
	sermon_asc = false;
	title_asc = false;
	method = "DESC";
	limit = 0;
	getMediaList();
}

function sort_list(type) {
	sort = type;
	if (type == "sermon_date") {
		if (sermon_asc) {
			method= "DESC";
			sermon_asc = false;
		}
		else {
			method = "ASC";
			sermon_asc = true;
		}
	}
	else {
		if (title_asc) {
			method="DESC";
			title_asc = false;
		}
		else {
			method = "ASC";
			title_asc = true;
		}
	}
	limit = 0;
	getMediaList();
}

function show_all() {
	showAll = "All";
	getMediaList();
	showAll = "";
}

function getMediaList() {
	showMessage();
	var title = $("#search_title").val();
	var month = $("#search_month").val();
	var data = new Array();
	var new_data = {"name" : "sort", "value" : sort};
	data.push(new_data);
	new_data = {"name" : "method", "value" : method};
	data.push(new_data);
	new_data = {"name" : "limit", "value" : limit};
	data.push(new_data);
	new_data = {"name" : "title", "value": title};
	data.push(new_data);
	new_data = {"name" : "month", "value" : month};
	data.push(new_data);
	new_data = {"name" : "showAll", "value" : showAll};
	data.push(new_data);
	
	$("#files").load("get_media.php", data, hideMessage);
}

function hideMessage() {
	$("#waitmessage").html("");
}

function showMessage() {
	$("#waitmessage").html("<font color='blue' size='3'>Please Wait...</font><br />");
}

function playMediaFile(file, title) {
	var so = new SWFObject('mediaplayer.swf','mpl','470','20','8');
	so.addParam('allowscriptaccess','always');
	so.addVariable('width','470');
	so.addVariable('height','20');
	so.addVariable('type', 'mp3');
	so.addVariable('file', escape('http://www.wefcvalpo.com/Sermons/play_media.php?file='+file));
	so.addVariable('autostart','true');
	so.addVariable('showstop','true');
	so.write('player');
	$("#player").append("<br /><b><u>&quot;"+title+"&quot;</u></b>");
}

function handleEvent(e) {
	if (e.which == 12 || e.which == 13) 
		reset();
}

$(document).ready(function() {
	getMediaList(); 
	$("#search_title").keypress(function(e) {handleEvent(e);});
});	
