// JavaScript Document

var totWidth=0;
var num_slides=0;
var curr_slide=0;
var offset=0;
var positions = new Array();
	
$(document).ready(function(){ 
	$("#white_out").fadeOut();	
	setTimeout(function() {
		$('#splash').fadeIn();
	}, 100);
	setTimeout(function() {
		$('#splash').fadeOut();
	}, 6000);
	
	$('#splash').live("click", function() {
		$('#splash').fadeOut();
	});
	
	$('a').live("click", function(e) {
		if($(this).attr("href")=="#") {
			e.preventDefault();
			$('#blackout, #lightbox').fadeIn();
		}
	});
	
	$('#blackout, #close').live("click", function(e) {
		e.preventDefault();
		$('#blackout, #lightbox').fadeOut();
	});
	
	$('.bwWrapper').BlackAndWhite(); 

	$('#slides .slide').each(function(i){
		/* Loop through all the slides and store their accumulative widths in totWidth */
		positions[i]= totWidth;
		totWidth += $(this).width();
		num_slides++;

		/* The positions array contains each slide's commulutative offset from the left part of the container */

		if(!$(this).width())
		{
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});
	
	var menu_width=950;
	
	$('#main .menuItem').css("width", (menu_width/num_slides) - (1/num_slides) - 1 + "px");

	$('#slides').width(totWidth);
	
	//var time_test=setInterval(timed_slide, 1000);
	
	var timer=setInterval(timed_slide,7000);

	/* Change the cotnainer div's width to the exact width of all the slides combined */

	$('#menu ul li a').click(function(e){
		clearInterval(timer);
		timer=setInterval(timed_slide,7000);
		/* On a thumbnail click */
		$('.menuItem').removeClass('act').addClass('inact');
		$(this).parent().addClass('act');
		curr_slide=$(this).parent().attr("id").substring(5, 6);

		var pos = $(this).parent().prevAll('.menuItem').length;

		$('#slides').stop().animate({marginLeft:-positions[pos]+'px'},450, 'linear');
		offset=positions[pos];
		/* Start the sliding animation */

		e.preventDefault();
		/* Prevent the default action of the link */
	});

	$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	/* On page load, mark the first thumbnail as active */
	
	$('.secondary').hide();
	
	$('#site_nav').live("click", function(e) {
		$('.secondary').show();
		$('.secondary').animate({height:"335px"}, 50, 'linear');
		$('#site_nav').addClass('active');
		$('#site_nav').html("CLOSE MENU");
	});
	
	
	$('#site_nav').mouseover(function() {
		$('.secondary').show(0, function() {
			$('#bottom_container').animate({height:"335px"}, 100, 'linear', function() {
				$('#site_nav').addClass('active');
				$('#site_nav').html("CLOSE MENU");
			});
		});
	});
	
	
	$('#site_nav.active').live("click", function(e) {
		$('.secondary').animate({height:"0"}, 100, 'linear', function() { $('.secondary').hide();} );
		$(this).removeClass('active');
		$(this).html("SITE NAVIGATION");
	});
	

	$('#wrapper').mouseover(function() {
		$('.secondary').animate({height:"0"}, 100, 'linear', function() { $('.secondary').hide();} );
		$('#site_nav').removeClass('active');
		$('#site_nav').html("SITE NAVIGATION");
	});
	
	$('#contact_form').live("submit", function(e) {
		var has_error=false;
		$('.required').each(function() {	
			
			$(this).removeClass("error");
			
			if($(this).val()=="") {
				$(this).parent().find("label").addClass("error");
				has_error=true;
			}
		});
		
		if(has_error) {
			e.preventDefault();
			$('#thank_you').html("Please fill in all required fields.");
			return false;
		} else {
			return true;
		}
	});
	
	
	
	$(".series").live("click", function(e) {
		$(".sermons").smoothDivScroll("destroy");
		$(".sermons").smoothDivScroll({});
		
		$('.sermon_picker').slideDown();
		$(".sermons").smoothDivScroll("replaceContent", "/wp-content/themes/mission_church/get_sermons.php?id="+$(this).attr("id"));
		$(".sermons").smoothDivScroll("option", "scrollStep", "10");
	});
	
	$(".ind_sermon").live("click", function(e) {
		$("#jPlayer").jPlayer("destroy");
		$.ajax({
		   type: "POST",
		   url: "/wp-content/themes/mission_church/play_sermon.php",
		   dataType: 'json',
		   data: "id="+$(this).attr("id"),
		   success: function(json) {
				$('#img').html(json.img);
				$('#name').html(json.name);
				$('#descript').html(json.descript);
				$('#title').html(json.title);
				$('#download').attr("href", json.audio);
				$("#jPlayer").jPlayer( {
				  	ready: function () {
					    $(this).jPlayer("setMedia", {
					    	m4a: json.audio
					    }).jPlayer("play");
				    },
					cssSelectorAncestor: '#jp_container_1',
					cssSelector: {
						videoPlay: '.jp-video-play',
						play: '.jp-play',
						pause: '.jp-pause',
						stop: '.jp-stop',
						seekBar: '.jp-seek-bar',
						playBar: '.jp-play-bar',
						mute: '.jp-mute',
						unmute: '.jp-unmute',
						volumeBar: '.jp-volume-bar',
						volumeBarValue: '.jp-volume-bar-value',
						volumeMax: '.jp-volume-max',
						currentTime: '.jp-current-time',
						duration: '.jp-duration',
						fullScreen: '.jp-full-screen',
						restoreScreen: '.jp-restore-screen',
						repeat: '.jp-repeat',
						repeatOff: '.jp-repeat-off',
						gui: '.jp-gui',
						noSolution: '.jp-no-solution'
					},
					wmode: "window",
				    solution: "flash, html",
				    supplied: "m4a",
				    swfPath: "/wp-content/themes/mission_church/scripts/jPlayer"
				});
		   }
		});
	});
	
	$("#download").live("click", function(e) {
		e.preventDefault();
		window.location.href="/wp-content/themes/mission_church/download.php?file="+$(this).attr("href");
	});
	
	$("#page_1").addClass("active");
	$(".nav_page").live("click", function(e) {
		$(".nav_page").removeClass("active");
		$(this).addClass("active");
		$('.paged').hide();
		$('.'+$(this).attr("id")).show();
	});
	
	function timed_slide() {
		offset+=menu_width;
		curr_slide++;
		if(offset>=totWidth) {
			offset=0;
			curr_slide=0;
		}
		$('#slides').stop().animate({marginLeft:-offset+'px'},450, 'linear', function() {
			$('li.menuItem').removeClass('act').addClass('inact');
			$('#menu_'+curr_slide).addClass('act');
		});
	}	
	
	$("#sidebar h2").live("click", function() {
		location.href="http://www.flickr.com/photos/67487815@N03/"
	});	
});

function load_sermon(id) {
	$("#jPlayer").jPlayer("destroy");
	$.ajax({
	   type: "POST",
	   url: "/wp-content/themes/mission_church/play_sermon.php",
	   dataType: 'json',
	   data: "id="+id,
	   success: function(json) {
			$('#img').html(json.img);
			$('#name').html(json.name);
			$('#descript').html(json.descript);
			$('#download').attr("href", json.audio);
			$('#title').html(json.title);
			$("#jPlayer").jPlayer( {
			  	ready: function () {
				    $(this).jPlayer("setMedia", {
				    	m4a: json.audio
				    });
			    },
				cssSelectorAncestor: '#jp_container_1',
				cssSelector: {
					videoPlay: '.jp-video-play',
					play: '.jp-play',
					pause: '.jp-pause',
					stop: '.jp-stop',
					seekBar: '.jp-seek-bar',
					playBar: '.jp-play-bar',
					mute: '.jp-mute',
					unmute: '.jp-unmute',
					volumeBar: '.jp-volume-bar',
					volumeBarValue: '.jp-volume-bar-value',
					volumeMax: '.jp-volume-max',
					currentTime: '.jp-current-time',
					duration: '.jp-duration',
					fullScreen: '.jp-full-screen',
					restoreScreen: '.jp-restore-screen',
					repeat: '.jp-repeat',
					repeatOff: '.jp-repeat-off',
					gui: '.jp-gui',
					noSolution: '.jp-no-solution'
				},
				wmode: "window",
			    solution: "flash, html",
			    supplied: "m4a",
			    swfPath: "/wp-content/themes/mission_church/scripts/jPlayer"
			});
	   }
	});
}

function search_sermon(id, container) {
	$.ajax({
	   type: "POST",
	   url: "/wp-content/themes/mission_church/play_sermon.php",
	   dataType: 'json',
	   data: "id="+id,
	   success: function(json) {
			$("#"+container+"_img").html(json.img);
			$("#"+container).jPlayer( {
			  	ready: function () {
				    $(this).jPlayer("setMedia", {
				    	m4a: json.audio
				    });
			    },
				cssSelectorAncestor: '#jp_container_'+container,
				cssSelector: {
					videoPlay: '.jp-video-play',
					play: '.jp-play',
					pause: '.jp-pause',
					stop: '.jp-stop',
					seekBar: '.jp-seek-bar',
					playBar: '.jp-play-bar',
					mute: '.jp-mute',
					unmute: '.jp-unmute',
					volumeBar: '.jp-volume-bar',
					volumeBarValue: '.jp-volume-bar-value',
					volumeMax: '.jp-volume-max',
					currentTime: '.jp-current-time',
					duration: '.jp-duration',
					fullScreen: '.jp-full-screen',
					restoreScreen: '.jp-restore-screen',
					repeat: '.jp-repeat',
					repeatOff: '.jp-repeat-off',
					gui: '.jp-gui',
					noSolution: '.jp-no-solution'
				},
				wmode: "window",
			    solution: "flash, html",
			    supplied: "m4a",
			    swfPath: "/wp-content/themes/mission_church/scripts/jPlayer"
			});
	   }
	});
}


