$(document).ready(function(){ 
	$('select').selectmenu({
        style: 'dropdown',
		transferClasses: true
    });
	
    $('#sort_select').change(function(){
		var theTab = $("#tabs2 a.active").attr("rel");
		//if(theTab == "" || theTab == "undefined") theTab = getctg;
		var theSort = $(this).val();
		var method = $("#method").val();
		sermonAjax(theTab, getseries, gettopic, theSort, method);
		//sermonAjax("", theTab, theSort);
    });
	
	$('#topic_select').change(function(){
		var topic = $(this).val();
		if (topic != "") {
			$('#topics_ajax_div').load("/revivalcms-includes/view/sermon-sidebar.php", {
				'top': topic
			});
		}
		//show sermons based on topic
    });
    
    $(".right_box").each(function() {
		$(this).hide();
	});
    
    $("#tabs a").each(function() {
		if($(this).attr("class") == "active") {
			var theTab = $(this).attr("rel");
			$("#"+theTab).fadeIn("slow", function() {
			});
		}
	});
	
	$("#tabs a").click(function() {
		$("#tabs a.active").each(function() {
			$(this).removeClass("active");
			var theTab = $(this).attr("rel");
			//$("#right_everything .right_box").fadeOut("slow", function(){
			//});
		});
		//$("#loading_anim").fadeIn("slow");
		//$(this).animate({borderWidth: "0px"}, 1000);
		$(this).addClass("active");
		var theTab = $(this).attr("rel");
		//$("#loading_anim").fadeOut("slow", function() {
			$("#right_everything .right_box").hide();
			$("#"+theTab).fadeIn("slow");
		//});
		return false;
	});
	
	$("#tabs2 a").each(function() {
		if($(this).attr("class") == "active") {
			var theTab = $(this).attr("rel");
			
			$("#"+theTab).fadeIn("slow", function() {
				
			});
		}
	});
	
	$("#tabs2 a").click(function() {
		$("#tabs2 a.active").each(function() {
			$(this).removeClass("active");
			var theTab = $(this).attr("rel");
			//$("#tabs2 .right_box").fadeOut("slow", function(){
			//});
		});
		//$("#ajaxsermons").fadeOut("slow");
		//$("#loading_anim_sermons").fadeIn("slow");
		$(this).animate({borderWidth: "0px"}, 1000);
		$(this).addClass("active");
		var theTab = $(this).attr("rel");
		var theSort = $("#sort_select").val();
		//sermonAjax("", theTab, theSort);
		sermonAjax(theTab, '', gettopic, theSort, "gallery");
		/*$("#ajaxsermons").load("includes/ajax/sermons.php", {'ctg':theTab, 'sort':theSort}, function(){
			$("#loading_anim_sermons").fadeOut("slow", function() {
				$("#ajaxsermons").fadeIn("slow");
			});
		});*/
		return false;
	});
	
	var theSort = $("#sort_select").val();
	sermonAjax(getctg, getseries, gettopic, theSort, "list");
	
});

function sermonAjax($ctg, $series, $topic, $sort, $method){
	$("#ajaxsermons").fadeOut("slow");
	var current_sermon = $("#current_sermon_id").val();
	$("#loading_anim_sermons").fadeIn("slow");
	$("#ajaxsermons").load("/revivalcms-includes/ajax/sermons.php", {'series':$series, 'topic':$topic, 'ctg':$ctg, 'sort':$sort, 'method':$method, 'current':current_sermon}, function(){
		if($("#ajaxsermons").html() == ""){
			$("#tab2_default").click();
		}else{
			fadeInContent();
		}
	});
}

function fadeInContent(){
	$("#loading_anim_sermons").fadeOut("slow", function() {
			$("#ajaxsermons").fadeIn("slow");
		});
}