/********************************
Background Rotator by Eric Draken, 2009
2010.03.06 - Added staggered loading
********************************/

$(document).ready(function() {

	// Set which random background is loaded
	$journal_backgrounds = new Array(
					  "journal-garden2",
					  "journal-gi2",
					  "journal-bonsai",
					  "journal-jodan2",
					  "journal-buki",
					  "journal-morotedori",
					  //"journal-calgaryaikikai",					  
					  "journal-zori",
  					  "journal-osensei-statue",
					  "journal-bb2",
					  "journal-tenchinage",					  
					  "journal-shiro",
					  "journal-kotegaeshi2",
					  "journal-ki-shodo2");

	//$bg_directory = 'http://wazajournal.com/wp-content/themes/journal/images/back/';

	// OFFSITE
	$bg_directory = 'http://www.masakokoro.com/journalbackgrounds/';

	var delay = 20000;
	$bg_paused = false;
	$bg_index = 0;

	//////////////

	// http://sedition.com/perl/javascript-fy.html
	function fisherYates ( myArray ) {
	  var i = myArray.length;
	  if ( i == 0 ) return false;
	  while ( --i ) {
		 var j = Math.floor( Math.random() * ( i + 1 ) );
		 var tempi = myArray[i];
		 var tempj = myArray[j];
		 myArray[i] = tempj;
		 myArray[j] = tempi;
	   }
	}

	function resync(){
		if( $bg_paused ){ return; }
		$('#container_bg').cycle('resume',true);
		$('#entry_top_first_bg').cycle('resume',true);
		$('#entry_content_first_bg').cycle('resume',true);
	}

	function bgpause(){
		$bg_paused = true;
		$('#container_bg').cycle('pause');
		$('#entry_top_first_bg').cycle('pause');
		$('#entry_content_first_bg').cycle('pause');
	}

	function bgresume(){
		$bg_paused = false;
		$('#container_bg').cycle('resume');
		$('#entry_top_first_bg').cycle('resume');
		$('#entry_content_first_bg').cycle('resume');
	}

	function bgslideup(){
		$("div#header:visible").slideUp("slow");
		$("div#sidebar_piece:visible").fadeOut("fast");
		$("div#menupadding:hidden").slideDown("slow");
	}

	function bgslidedown(){
		$("div#header:hidden").slideDown("slow");
		$("div#sidebar_piece:hidden").fadeIn("slow");
		$("div#menupadding:visible").slideUp("slow");
	}

	function bgshow() {
		bgslidedown();
		bgresume();
	}

	function bghide() {
		bgslideup();
		bgpause();
	}

	// Preload picture holders
	function setStage() {
		for(var i = 0; i < $journal_backgrounds.length; i++) {
			$("#container_bg").append( '<im'+'g src="" width="952" height="350"/'+'>' );
			$("#entry_top_first_bg").append( '<im'+'g src="" width="629" height="62"/'+'>' );
			$("#entry_content_first_bg").append( '<im'+'g src="" width="628" height="88"/'+'>' );
		}
	}

	function loadNextBG() {
		if($bg_index >= $journal_backgrounds.length) { return; }

		$("#container_bg img").eq($bg_index).attr("src", $bg_directory+$journal_backgrounds[$bg_index]+'/bg_aikido.jpg');
		$("#entry_top_first_bg img").eq($bg_index).attr("src", $bg_directory+$journal_backgrounds[$bg_index]+'/bg_post_top_first.jpg');
		$("#entry_content_first_bg img").eq($bg_index).attr("src", $bg_directory+$journal_backgrounds[$bg_index]+'/bg_post_first.jpg');
		$bg_index++;
	}

	function staggerdLoading() {
		loadNextBG();
		if($bg_index < $journal_backgrounds.length) {
			setTimeout ( staggerdLoading, delay );
		}
	}

	/////////////////////

	function bgrinit()
	{
		// Randomize the bg array
		fisherYates( $journal_backgrounds );

		// Attach the toggle button
		$("#pages_nav_right").after('<div id="pages_nav_right_toggle"><a href="#" id="hidebg" title="You can hide these cool zen background images to save space if you have a small screen.">Hide</a><a href="#" id="showbg" title="See the zen background images and watch them change every so often.">Unhide</a></div>');

		$('a#showbg').click(function() {
			$('a#hidebg').toggle(400);
			$('a#showbg').toggle(400);
			bgshow();
			$.cookie("shouldHideHeader", "false", { path: '/'});
			return false;
		});

		$('a#hidebg').click(function() {
			$('a#hidebg').toggle(400);
			$('a#showbg').toggle(400);
			bghide();
			$.cookie("shouldHideHeader", "true", { path: '/'});
			return false;
		});

		// Add special divs
		$("#container").prepend( '<div id="container_bg"></div>' );
		$(".entry").eq(0).before( '<div id="entry_top_first_bg"></div>' );
		$(".faux_entry").eq(0).before( '<div id="entry_content_first_bg"></div>' );

		// Remove the backgrounds
		$(".entry_top_first").eq(0).css('background', 'transparent');
		$(".entry_content_first").eq(0).css('background', 'transparent');
		$(".faux_entry").eq(0).css('background', 'transparent');
		$(".entry").eq(0).css('background', 'transparent');

		// Load the first bg
		setStage();
		loadNextBG();

		$('#container_bg').cycle({
			fx: 'fade',
			timeout: delay,
			autostop: 0,
			speed: 1500
		});

		$("#entry_top_first_bg").cycle({
			fx: 'fade',
			timeout: delay,
			autostop: 0,
			speed: 1500
		});

		$('#entry_content_first_bg').cycle({
			fx: 'fade',
			timeout: delay,
			autostop: 0,
			speed: 1500
		});

		// Memory of show or hide
		if( eval($.cookie("shouldHideHeader")) == true ) {
			$('a#hidebg').hide();
			$("div#header").hide();
			$("div#sidebar_piece").hide();
			$("div#menupadding").show();
			bgpause();
		} else {
			$('a#showbg').hide();
		}

		setInterval (resync, ($journal_backgrounds.length * delay * 2) + 1000 );
		setTimeout(staggerdLoading, 2000);	// Small initial delay
	}

	bgrinit();

});
