/**
    Head JS     The only script in your <HEAD>
    Copyright   Tero Piirainen (tipiirai)
    License     MIT / http://bit.ly/mit-license
    Version     0.96

    http://headjs.com
*/(function(a){function z(){d||(d=!0,s(e,function(a){p(a)}))}function y(c,d){var e=a.createElement("script");e.type="text/"+(c.type||"javascript"),e.src=c.src||c,e.async=!1,e.onreadystatechange=e.onload=function(){var a=e.readyState;!d.done&&(!a||/loaded|complete/.test(a))&&(d.done=!0,d())},(a.body||b).appendChild(e)}function x(a,b){if(a.state==o)return b&&b();if(a.state==n)return k.ready(a.name,b);if(a.state==m)return a.onpreload.push(function(){x(a,b)});a.state=n,y(a.url,function(){a.state=o,b&&b(),s(g[a.name],function(a){p(a)}),u()&&d&&s(g.ALL,function(a){p(a)})})}function w(a,b){a.state===undefined&&(a.state=m,a.onpreload=[],y({src:a.url,type:"cache"},function(){v(a)}))}function v(a){a.state=l,s(a.onpreload,function(a){a.call()})}function u(a){a=a||h;var b;for(var c in a){if(a.hasOwnProperty(c)&&a[c].state!=o)return!1;b=!0}return b}function t(a){return Object.prototype.toString.call(a)=="[object Function]"}function s(a,b){if(!!a){typeof a=="object"&&(a=[].slice.call(a));for(var c=0;c<a.length;c++)b.call(a,a[c],c)}}function r(a){var b;if(typeof a=="object")for(var c in a)a[c]&&(b={name:c,url:a[c]});else b={name:q(a),url:a};var d=h[b.name];if(d&&d.url===b.url)return d;h[b.name]=b;return b}function q(a){var b=a.split("/"),c=b[b.length-1],d=c.indexOf("?");return d!=-1?c.substring(0,d):c}function p(a){a._done||(a(),a._done=1)}var b=a.documentElement,c,d,e=[],f=[],g={},h={},i=a.createElement("script").async===!0||"MozAppearance"in a.documentElement.style||window.opera,j=window.head_conf&&head_conf.head||"head",k=window[j]=window[j]||function(){k.ready.apply(null,arguments)},l=1,m=2,n=3,o=4;i?k.js=function(){var a=arguments,b=a[a.length-1],c={};t(b)||(b=null),s(a,function(d,e){d!=b&&(d=r(d),c[d.name]=d,x(d,b&&e==a.length-2?function(){u(c)&&p(b)}:null))});return k}:k.js=function(){var a=arguments,b=[].slice.call(a,1),d=b[0];if(!c){f.push(function(){k.js.apply(null,a)});return k}d?(s(b,function(a){t(a)||w(r(a))}),x(r(a[0]),t(d)?d:function(){k.js.apply(null,b)})):x(r(a[0]));return k},k.ready=function(b,c){if(b==a){d?p(c):e.push(c);return k}t(b)&&(c=b,b="ALL");if(typeof b!="string"||!t(c))return k;var f=h[b];if(f&&f.state==o||b=="ALL"&&u()&&d){p(c);return k}var i=g[b];i?i.push(c):i=g[b]=[c];return k},k.ready(a,function(){u()&&s(g.ALL,function(a){p(a)}),k.feature&&k.feature("domloaded",!0)});if(window.addEventListener)a.addEventListener("DOMContentLoaded",z,!1),window.addEventListener("load",z,!1);else if(window.attachEvent){a.attachEvent("onreadystatechange",function(){a.readyState==="complete"&&z()});var A=1;try{A=window.frameElement}catch(B){}!A&&b.doScroll&&function(){try{b.doScroll("left"),z()}catch(a){setTimeout(arguments.callee,1);return}}(),window.attachEvent("onload",z)}!a.readyState&&a.addEventListener&&(a.readyState="loading",a.addEventListener("DOMContentLoaded",handler=function(){a.removeEventListener("DOMContentLoaded",handler,!1),a.readyState="complete"},!1)),setTimeout(function(){c=!0,s(f,function(a){a()})},300)})(document)


/**
	Load All Scripts

	use the full url because this file is shared across church and school sites
*/
head.js(

	// jQuery from CDN
	"https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js",

	// jQuery Tools from CDN
	"http://cdn.jquerytools.org/1.2.5/all/jquery.tools.min.js",

	// Flowplayer from site
	"http://tbcnh.org/_js/flowplayer-3.2.6.min.js",

	// Colorbox from site
	"http://tbcnh.org/_js/jquery.colorbox-min.js"
);



/**
 * Execute Scripts when all are loaded
 */

head.ready(function(){



//
// Calendar Functions
//

	function update_cal(evt)
	{
		// don't follow the link
		evt.preventDefault();

		// get the calendar data from the link's href of select option value
		// and turn it into an array
		var $tgt = $(evt.target);
		var url_to_get;

		if($tgt.is('a'))
		{
			url_to_get = $tgt.attr('href');
		}
		else if($tgt.is('select'))
		{
			url_to_get = $tgt.val();
		}

		// empty the calendar and show a loading graphic
		$('#cal-content').css({
			background:'url(http://media.tbcnh.org/interface_img/ajax-load.gif) no-repeat center center',
			height:'500px'
		}).html('');

		// get the content via ajax and add them to the page
		$.get(url_to_get, {},
			function(returned_data)
			{
				$('#large-calendar').html(returned_data);
				// must call this to add events to the newly added ajax elements
				add_cal_events();

				// update select menu
				// var slug- + get_array[5]
				// check if cat filter exists
				// if it does, find out what it says
				// select option for that category
				// return false so don't trigger change event
			}
		);
	}


	function add_cal_events()
	{
		$('#cal-nav-previous').unbind().bind('click.prev', update_cal);
		$('#cal-nav-next').unbind().bind('click.next', update_cal);
		$('#cal-nav-today').unbind().bind('click.today', update_cal);
		$('#cal-view-toggle').unbind().bind('click.view', update_cal);
		$('#cal-print').unbind().bind('click.print', function(evt){
			window.print();
			evt.preventDefault();
		});
		$('#cal-category-select').unbind().bind('change.filter', update_cal);
		$('#cal-category-clear').unbind().bind('click.clear', update_cal);
		$('a.trigger-modal').unbind().colorbox(
			{
				transition	:	'none',
				title		:	false,
				width		:	500,
				height		:	380,
				iframe		:	false,
				photo		:	false,
				opacity		:	0,
				scrolling	:	true
			}
		);
	}






	// Home Page

		// select #slides and make it scrollable, use circular and navigator plugins
		$('#slides')
			.scrollable({circular:true, speed:800})
			.navigator({navi:'#slides-nav', activeClass:'current'})
			.autoscroll({interval:6000, autoplay:true, autopause:false});

			// load api to for pause play and stop actions
			var scroll_api = $('#slides').data('scrollable');

			// pause and play buttons
			var $slides_pause = $('#slides-pause');
			var $slides_play = $('#slides-play');

			$slides_play.hide(0);

			$slides_pause.click(function(evt)
				{
					evt.preventDefault();
					scroll_api.stop();
					$(this).hide(0);
					$slides_play.show(0);
				}
			);

			$slides_play.click(function(evt)
				{
					evt.preventDefault();
					scroll_api.play();
					$(this).hide(0);
					$slides_pause.show(0);
				}
			);


		// video popup
		var $welcome_vid_a = $('#trigger-welcome-video');

			// pause slideshow on click
			$welcome_vid_a.colorbox(
				{
					//inline		:	true,
					transition		:	'none',
					title			:	false,
					innerWidth		:	600,
					innerHeight		:	450,
					iframe			:	true,
					photo			:	false,
					opacity			:	0.5,
					scrolling		:	false,

					// pause the scrollable slides when video is open, and start them again when video is closed
					onOpen			:	function(){scroll_api.stop();},
					onClosed		:	function(){scroll_api.play();}
				}
			);

			// video link fade effect on hover
			$welcome_vid_a_img = $welcome_vid_a.children('img:first');

			$welcome_vid_a.mouseenter(
				function(evt)
				{
					$welcome_vid_a_img.animate({opacity: '0'}, 800);
				}
			);
			$welcome_vid_a.mouseleave(
				function(evt)
				{
					$welcome_vid_a_img.animate({opacity: '1'}, 800);
				}
			);




	// Load event handlers for calendar controls
	add_cal_events();

	// tabs - via jquery tools
	$('ul.tabs').tabs('div.panes > div.pane');

	// flowplayer
	if(document.getElementById('flowplayer')){
		flowplayer("flowplayer", "/media/swf/flowplayer-3.2.7.swf");
	}




});
