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

	http://headjs.com
*/(function(a){var b=a.documentElement,c,d,e=[],f=[],g={},h={},i=a.createElement("script").async===true||"MozAppearance"in a.documentElement.style||window.opera;var j=window.head_conf&&head_conf.head||"head",k=window[j]=window[j]||function(){k.ready.apply(null,arguments)};var l=0,m=1,n=2,o=3;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.push(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(a,b){if(a=="dom"){d?p(b):e.push(b);return k}t(a)&&(b=a,a="ALL");var c=h[a];if(c&&c.state==o||a=="ALL"&&u()&&d){p(b);return k}var f=g[a];f?f.push(b):f=g[a]=[b];return k},k.ready("dom",function(){c&&u()&&s(g.ALL,function(a){p(a)}),k.feature&&k.feature("domloaded",true)});function p(a){a._done||(a(),a._done=1)}function q(a){var b=a.split("/"),c=b[b.length-1],d=c.indexOf("?");return d!=-1?c.substring(0,d):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 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 t(a){return Object.prototype.toString.call(a)=="[object Function]"}function u(a){a=a||h;var b=false,c=0;for(var d in a){if(a[d].state!=o)return false;b=true,c++}return b||c===0}function v(a){a.state=l,s(a.onpreload,function(a){a.call()})}function w(a,b){a.state||(a.state=m,a.onpreload=[],y({src:a.url,type:"cache"},function(){v(a)}))}function x(a,b){if(a.state==o&&b)return 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)}),d&&u()&&s(g.ALL,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=false,e.onreadystatechange=e.onload=function(){var a=e.readyState;!d.done&&(!a||/loaded|complete/.test(a))&&(d(),d.done=true)},b.appendChild(e)}setTimeout(function(){c=true,s(f,function(a){a()})},0);function z(){d||(d=true,s(e,function(a){p(a)}))}window.addEventListener?(a.addEventListener("DOMContentLoaded",z,false),window.addEventListener("onload",z,false)):window.attachEvent&&(a.attachEvent("onreadystatechange",function(){a.readyState==="complete"&&z()}),window.frameElement==null&&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,false),a.readyState="complete"},false))})(document)





/**
	Load All Scripts
*/
head.js(

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

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

	// jQuery Colorbox Plugin from Site
	"/_js/jquery.colorbox-min.js",

	// Nivo Slider Plugin from Site
	"/_js/jquery.nivo.slider.pack.js",

	// Ekklesia360 Scripts
	"http://api.monkcms.com/Clients/monkcms-dev.js"
);








/**
	When Scripts are Loaded and DOM is ready
	Do the this stuff...
*/
head.ready(function(){


	/**
	|	Calendar
	|
	|	When a cal link is clicked, empty the calendar <div>
	|	Then load the contents of the link into the <div> via ajax
	|
	|	Uses jQuery's built in event delegation to keep click events on ajax loaded calendar controls
	|
	*/

		// small utility to get arguments from the url
		function url_to_array(string)
		{
			var url = string;
			var url_array = url.split('/');
			return url_array;
		}

		var cb_cal_settings = {
			transition	:	'none',
			title		:	'',
			width		:	550,
			height		:	380,
			iframe		:	false,
			photo		:	false,
			inline		:	true,
			opacity		:	0,
			scrolling	:	true
		}


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

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

			if($tgt.is('a'))
			{
				get_array = url_to_array($tgt.attr('rel'));
			}
			else if($tgt.is('select'))
			{
				get_array = url_to_array($tgt.val());
			}

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

			// get the content via ajax and add them to the page
			$.ajax(
				{
					url:		'/mcms_page_large_calendar_ajax.php',
					data:		{
									view:get_array[2],
									year:get_array[3],
									month:get_array[4],
									category:get_array[5]
								},
					success:	function(returned_data)
								{
									$('#large-calendar').html(returned_data);
									// we must add these event handlers again for the calendar pop ups
									// colorbox does not use event delegation
									$('a.trigger-modal', '#large-calendar').off().colorbox(cb_cal_settings);
									$('a.thickbox', '#large-calendar').off().colorbox(cb_regis_settings);
								},
					/*
					error:		function(jqXHR, textStatus, errorThrown)
								{
									alert(textStatus + ' ' + errorThrown);
								},
					*/
					dataType:	'html'
				}
			);
		}


	// Delegate Events for all the calendar controls
	$('#large-calendar').on(
		'click',
		'#cal-nav-previous, #cal-nav-next, #cal-nav-today, #cal-view-toggle, #cal-category-clear',
		update_cal
	);
	$('#large-calendar').on(
		'change',
		'#cal-category-select',
		update_cal
	);
	$('#large-calendar').on(
		'click',
		'#cal-print',
		function(evt){evt.preventDefault(); window.print();}
	);
	// colorbox does not use delegation
	// we have to call this again in the ajax callback to get pop-ups working on loaded content
	$('a.trigger-modal').colorbox(cb_cal_settings);






	/**
	|	Other Scripts
	*/



	// Primary Nav Drop Down Menus
	$('ul#nav li').hover(function() {
		$(this).children('ul').addClass("show");
		$(this).addClass("stick");
	}, function() {
		$(this).children('ul').removeClass("show");
		$(this).removeClass("stick");
	} );

	// Home Page Sliders
	$('#nivo-home-large-slider').nivoSlider({
		effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
		slices: 15, // For slice animations
		boxCols: 8, // For box animations
		boxRows: 4, // For box animations
		animSpeed: 1200, // Slide transition speed
		pauseTime: 12000, // How long each slide will show
		startSlide: 0, // Set starting Slide (0 index)
		directionNav: true, // Next & Prev navigation
		directionNavHide: true, // Only show on hover
		controlNav: true, // 1,2,3... navigation
		controlNavThumbs: false, // Use thumbnails for Control Nav
		controlNavThumbsFromRel: false, // Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', // Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
		keyboardNav: true, // Use left & right arrows
		pauseOnHover: true, // Stop animation while hovering
		manualAdvance: false, // Force manual transitions
		captionOpacity: 0.8, // Universal caption opacity
		prevText: 'Prev', // Prev directionNav text
		nextText: 'Next', // Next directionNav text
		randomStart: false, // Start on a random slide
		beforeChange: function(){}, // Triggers before a slide transition
		afterChange: function(){}, // Triggers after a slide transition
		slideshowEnd: function(){}, // Triggers after all slides have been shown
		lastSlide: function(){}, // Triggers when last slide is shown
		afterLoad: function(){} // Triggers when slider has loaded
	});
	$('#home-nivo-small-ad').nivoSlider({
		effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
		slices: 15, // For slice animations
		boxCols: 8, // For box animations
		boxRows: 4, // For box animations
		animSpeed: 1200, // Slide transition speed
		pauseTime: 7000, // How long each slide will show
		startSlide: 0, // Set starting Slide (0 index)
		directionNav: true, // Next & Prev navigation
		directionNavHide: true, // Only show on hover
		controlNav: true, // 1,2,3... navigation
		controlNavThumbs: false, // Use thumbnails for Control Nav
		controlNavThumbsFromRel: false, // Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', // Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
		keyboardNav: true, // Use left & right arrows
		pauseOnHover: true, // Stop animation while hovering
		manualAdvance: false, // Force manual transitions
		captionOpacity: 0.8, // Universal caption opacity
		prevText: 'Prev', // Prev directionNav text
		nextText: 'Next', // Next directionNav text
		randomStart: false, // Start on a random slide
		beforeChange: function(){}, // Triggers before a slide transition
		afterChange: function(){}, // Triggers after a slide transition
		slideshowEnd: function(){}, // Triggers after all slides have been shown
		lastSlide: function(){}, // Triggers when last slide is shown
		afterLoad: function(){} // Triggers when slider has loaded
	});



	// Tabs
	$("ul.tabs").tabs("div.panes > div.pane");

	// Modal Registration Forms
	var cb_regis_settings = {
		transition	:	'none',
		width		:	780,
		height		:	545,
		iframe		:	true,
		fastIframe	:	false,
		title		:	'Event Registration',
		opacity		:	.75
	}
	$('a.thickbox').colorbox(cb_regis_settings);

	// Photo Gallery pop ups
	$('a.gallery-modal').colorbox(
		{
			transition	:	'none',
			maxWidth	:	780,
			maxHeight	:	545,
			photo		:	true,
			opacity		:	.75
		}
	);


});
