

$(document).ready(function(){

	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});

	// webkit browsers load CSS and JS at the same time so make them wait a bit more
	// if (jQuery.browser.safari && document.readyState != 'complete') { setTimeout( arguments.callee, 100 ); return; }
	
	$('form *[rel="submit"]').click(function(){
		// we submit the form with JS because the validation requires JS.
		// this is to prevent a user from disabling JS in their browser and bypassing validation
		$(this).parents('form').submit();
		return false;
	});
	
	
	
	

	if ($('a.colorbox').length>0) {
	$('a.colorbox').colorbox({'width':800,'height':500});
	}
	if ($('select.stylish').length>0) {
	$('select.stylish').selectbox();
	}


	// SERMON DROP-DOWN BOES
	// ****************************************************************************
	
	function hideDropbox (elm) {
		$(elm).removeClass('active').next().stop().removeAttr('style').hide();
	}

	var bRemoveActive = false;
	var nHasFocus = false;
	var oTimeout = null;
	// I'm not sure why the plugin does not do this already but .. 
	// 1. disable/remove the selectbox
	// 2. take the selectbox name and give it to the input field
	// 3. add focus and blur functions
	$('input.selectbox').each(function(){
		var strSelectID = String(this.id).replace('_input','');
		var oSelect = $('select#'+strSelectID);
		var strSelectName = oSelect.attr('name');
		//oSelect.attr('disabled','disabled');
		oSelect.remove();
		this.name = strSelectName;
	}).bind({'focus':function(){
		$(this).addClass('active');
		//$(this).next().hide().slideDown(500,'easeOutQuint');
		$(this).next().show();
		clearTimeout(oTimeout);
	},'blur':function(){
		if (nHasFocus===true) { // if the mouse is over the entire dropdown box including the scroll bar
			if (bRemoveActive===true) { // this is true during a click release 'keyup'
				hideDropbox(this);
			} else {
				// this is for broken browsers that remove focus on scroll bar use
				var o = this;
				oTimeout = setTimeout(function(){
					o.focus();
				},1);
			}
		} else { // blur for any other reason ie: clicked outside of dropdown box
			hideDropbox(this);
		}
	}});
	
	/*
	$('div.selectbox-wrapper').each(function(){	
		var oInput = $(this).prev();
		$(this).offset({'top':oInput.height()+23,'left':15});
	});
	*/

	$('div.selectbox-wrapper').bind({
		'mouseenter':function(){
			nHasFocus = true;
		},
		'mouseleave':function(){
			nHasFocus = false;
		}
	});
	
	$('div.selectbox-wrapper ul li').bind({
		
		'mouseleave':function(){
			bRemoveActive = false; // this is for broken browsers that remove focus on scroll bar use
		},
		'mousedown':function(){
			bRemoveActive = false;
		},
		'mouseup':function(){
			bRemoveActive = true;
		},
		'click':function(e){
			var oParent = $(e.currentTarget).parents('div.selectbox-wrapper');
			if (oParent.length>0) {
				oParent.prev().blur();
			}
			return false;
		}
	});
	
	//$('input.selectbox').disableTextSelect();
	
	
	
	

	// TAB SLIDER PLUGIN
	// ****************************************************************************

	var autobar = $('span.JQTS_bar');
	$('div#JQTS_wrap').tabslider({

		slideTime: 800,
		autoscrollLength: 6000,
		easeType: 'easeOutQuad',
		nMargin: 10,
		nSelected: 0,
		bPrepare: true,
		oSlideHolder: 'div#JQTS_slides',
		oSlide: 'div.JQTS_slide',
		oSlideButton: 'a.JQTS_button',
		oCanvas: 'div#JQTS_canvas',
		oImageHolder: 'div.JQTS_images',
		oImageHolderMask: 'div.JQTS_mask',
		classActive: 'JQTS_active',
		listener: function(e,o){
			switch(e){
				case 'BUBBLE_CLICK': /* autobar.stop().fadeOut(o.slideTime); $(this).disableAutoscroll(); */ break;
				case 'AUTOSCROLL_START': autobar.stop().animate({'width':'100%'},(o.autoscrollLength-10),'linear'); break; // remove 10 miliseconds from the scroll length so that our bar finishes the animation a tab bit earlier
				//case 'AUTOSCROLL_END':break;
				case 'AUTOSCROLL_ENABLED': autobar.stop().width(0).show().css({'opacity':1}); break;
				case 'AUTOSCROLL_DISABLED': autobar.stop().fadeOut(o.slideTime); break;
				case 'SLIDE_START': autobar.stop().fadeOut(o.slideTime); break;
				case 'SLIDE_END': autobar.stop().width(0).show().css({'opacity':1}); break;
			}
		}


	});
	
	// CELL EVENT STUFF
	// ****************************************************************************
	
	var vCell = {
		strEaseType: 'easeOutQuint',
		nSlideLength: 800,
		nCurrItem: 0,
		nChildren: null,
		nWidth: null,
		nLeft: null,
		oParent: $('div.cell.M'),
		oParentBody: $('div.cell.M div.body'),
		oParentSlabs: $('div.cell.M div.slabs'),
		oArrowPrev:	$('div.sm_arrows a.prev:not(.disabled)'),
		oArrowNext:	$('div.sm_arrows a.next:not(.disabled)')
	};
	
	vCell.nChildren = vCell.oParentSlabs.children().length;
	vCell.oParentSlabs.width(vCell.oParentSlabs.width()*vCell.nChildren);
	vCell.nWidth = vCell.oParentBody.width();

	vCell.oArrowPrev.click(function(){
		vCell.nCurrItem = (vCell.nCurrItem===0)? vCell.nChildren-1:vCell.nCurrItem-1;
		vCell.nLeft = -(vCell.nWidth*vCell.nCurrItem);
		vCell.oParentSlabs.animate({'left':vCell.nLeft},vCell.nSlideLength,vCell.strEaseType);
		return false;
	});

	vCell.oArrowNext.click(function(){
		vCell.nCurrItem = ((vCell.nCurrItem+1)==vCell.nChildren)? 0:vCell.nCurrItem+1;
		vCell.nLeft = -(vCell.nWidth*vCell.nCurrItem);
		vCell.oParentSlabs.animate({'left':vCell.nLeft},vCell.nSlideLength,vCell.strEaseType);
		return false;
	});
	
	// NEWSLETTER DROPDOWN
	// ****************************************************************************

	var vSignup = {
		nTopMargin: null,
		arrInitialVals: [],
		oBody: $('div#body'),
		oSignup: $('div#newsletter_signup'),
		oSignupForm: $('div#newsletter_signup form#signup'),
		oSignupInputs: $('div#newsletter_signup form#signup input[type="text"]'),
		oSignupSubmit: $('div#newsletter_signup a[rel="submit"]'),
		oSignupBtn: $('div#newsletter_signup a[rel="toggle"]')
	};

	$.fn.extend({
		reset: function() {
			return this.each(function() {
				if ($(this).is('form')) {
					this.reset();
				}
			});
		}
	});
	
	vSignup.nTopMargin = Math.abs(vSignup.oSignup.offset().top); // get top offset of container
	vSignup.oSignupForm.reset(); // reset the form on initial load
	vSignup.oSignupInputs.each(function(){ vSignup.arrInitialVals[this.name] = this.value; }); // dump form values into an array
	vSignup.oSignupInputs.focus(function(){ if (this.value==vSignup.arrInitialVals[this.name]) { $(this).val(''); } }); // on focus, if our current value is the same as our initial value, then empty the field
	vSignup.oSignupInputs.blur(function(){ if (!this.value) { $(this).val(vSignup.arrInitialVals[this.name]); } }); // on blur, if our field is empty then fill it with our initial value
	vSignup.oSignupForm.submit(function(){ vSignup.oSignupInputs.each(function(){ if (this.value==vSignup.arrInitialVals[this.name]) { $(this).val(''); } }); }); // on submit, if a field value is the same as its initial value, then empty that field because it is not a user provided value
	vSignup.oSignupSubmit.click(function(){ vSignup.oSignupForm.submit(); return false; }); // on submit button click, submit the form
	
	vSignup.oSignupBtn.toggle(function(){
		vSignup.oSignup.css({'top':0});
		vSignup.oBody.css({'top':vSignup.nTopMargin});
	},function(){
		vSignup.oSignup.css({'top':-(vSignup.nTopMargin)});
		vSignup.oBody.css({'top':0});
	});
	
	
	// INFOBAR FADER
	// ****************************************************************************
	
	var vInfobar = {
		strEaseType: 'easeOutQuint',
		nSlideLength: 600,
		nCurrItem: 0,
		nHeight: null,
		nTop: null,
		nChildren: null,
		oParent: $('div#infobar'),
		oSlideHolder: $('div#infobar div.slides'),
		oBtnPrev: $('div#infobar div.controls a.prev'),
		oBtnNext: $('div#infobar div.controls a.next')
	};

	vInfobar.oBtnPrev.click(function(){
		vInfobar.nChildren = vInfobar.oSlideHolder.children().length;
		vInfobar.nHeight = vInfobar.oParent.height();
		vInfobar.nCurrItem = (vInfobar.nCurrItem===0)? vInfobar.nChildren-1:vInfobar.nCurrItem-1;
		vInfobar.nTop = -(vInfobar.nHeight*vInfobar.nCurrItem);
		vInfobar.oSlideHolder.animate({'top':vInfobar.nTop},vInfobar.nSlideLength,vInfobar.strEaseType);
		return false;
	});

	vInfobar.oBtnNext.click(function(){
		vInfobar.nChildren = vInfobar.oSlideHolder.children().length;
		vInfobar.nHeight = vInfobar.oParent.height();
		vInfobar.nCurrItem = ((vInfobar.nCurrItem+1)==vInfobar.nChildren)? 0:vInfobar.nCurrItem+1;
		vInfobar.nTop = -(vInfobar.nHeight*vInfobar.nCurrItem);
		vInfobar.oSlideHolder.animate({'top':vInfobar.nTop},vInfobar.nSlideLength,vInfobar.strEaseType);
		return false;
	});


	// SERMON ARCHIVES
	// ****************************************************************************
	
	var vArchive = {
		strEaseType: 'easeOutQuint',
		nSlideLength: 500,
		nCurrItem: 0,
		nChildren: null,
		nWidth: null,
		nHeight: null,
		oParent: $('div#archive'),
		oParentSlabs: $('div#archive div.archives'),
		oParentBody: $('div#archive div.archives div.group'),
		oArrowPrev:	$('div#archive div.controls a.prev'),
		oArrowNext:	$('div#archive div.controls a.next'),
		oDateMask: $('div#archive div.dates'),
		oDateHolder: $('div#archive div.inner'),
		oDate: $('div#archive div.dates h2'),
		classDisabled: 'disabled'
	};

	function setGroupHeight(nHeight) {
		vArchive.oParentSlabs.height(nHeight);
		//vArchive.oParentSlabs.animate({'height':nHeight},vArchive.nSlideLength);
	}

	function getGroupHeight(offset) {

		var nHeight = 0;
		
		vArchive.oParentBody.hide();

		if (offset!==null) {
			vArchive.oParentBody.eq(offset).show();
		}
		
		vArchive.oParentBody.eq(vArchive.nCurrItem).show().children().each(function(){
			nHeight += $(this).height();
			nHeight += parseInt($(this).css('paddingTop'), 10) + parseInt($(this).css('paddingBottom'), 10); //Total Padding Height
			nHeight += parseInt($(this).css('marginTop'), 10) + parseInt($(this).css('marginBottom'), 10); //Total Margin Height
			nHeight += parseInt($(this).css('borderTopWidth'), 10) + parseInt($(this).css('borderBottomWidth'), 10); //Total Border Height
		});

		return nHeight;

	}

	function getQueueLen(e) {
		
		return e.queue("fx").length;

	}

	function slideArchive(i) {
		
		var curHeight = vArchive.oParentSlabs.height();
		var newHeight = getGroupHeight(i);
		
		if (curHeight<newHeight) {
			setGroupHeight(newHeight);
		}

		var nLeft = -(vArchive.nWidth*vArchive.nCurrItem);
		vArchive.oParentSlabs.animate({'left':nLeft},vArchive.nSlideLength,vArchive.strEaseType,function(){
				
			if (curHeight>newHeight) {
				setGroupHeight(newHeight);
			}

		});

		var nTop = -(vArchive.nHeight*vArchive.nCurrItem);
		vArchive.oDateHolder.animate({'top':nTop},vArchive.nSlideLength,vArchive.strEaseType);

	}
	
	vArchive.nChildren = vArchive.oParentBody.length;
	vArchive.nCurrItem = vArchive.nChildren-1;
	
	// if we have children then set parent width/height to fit them
	if (vArchive.nChildren>0) {
		vArchive.oParentSlabs.width(vArchive.oParent.width()*vArchive.nChildren);
		vArchive.oDateHolder.height(vArchive.oDateMask.height()*vArchive.nChildren);
	}
	
	vArchive.nWidth = vArchive.oParentBody.width();
	vArchive.nHeight = vArchive.oDateMask.height();
	
	// set each group left value
	vArchive.oParentBody.each(function(i,e){
		$(e).css({'left':i*vArchive.nWidth});
	});

	setGroupHeight(getGroupHeight(null)); // SET INIT HEIGHT
	vArchive.oParent.show(); // WE HAVE SITE THE HEIGHT SO NOW SHOW BODY // AVOIDS FLICKER
	slideArchive(null); // INIT SLIDE TO CURRENT ITEM = LAST ITEM
	vArchive.oArrowNext.addClass(vArchive.classDisabled); // WE ARE ON LAST ITEM SO DISABLE NEXT BTN


	vArchive.oArrowPrev.click(function(){
		
		//console.log(getQueueLen(vArchive.oParentSlabs));
		// if not disabled and not animating
		if ($(this).hasClass(vArchive.classDisabled)===false&&getQueueLen(vArchive.oParentSlabs)===0) {
		
			vArchive.nCurrItem = (vArchive.nCurrItem===0)? vArchive.nChildren-1:vArchive.nCurrItem-1;
			
			//console.log(vArchive.nCurrItem,vArchive.nChildren);
			vArchive.oArrowNext.removeClass(vArchive.classDisabled);
			if (vArchive.nCurrItem===0) {
				$(this).addClass(vArchive.classDisabled);
			}
			
			slideArchive(vArchive.nCurrItem+1);
			
		}

		return false;
	});

	vArchive.oArrowNext.click(function(){
		
		//console.log(getQueueLen(vArchive.oParentSlabs));
		// if not disabled and not animating
		if ($(this).hasClass(vArchive.classDisabled)===false&&getQueueLen(vArchive.oParentSlabs)===0) {
			
			vArchive.nCurrItem = ((vArchive.nCurrItem+1)==vArchive.nChildren)? 0:vArchive.nCurrItem+1;
			
			//console.log(vArchive.nCurrItem,vArchive.nChildren);
			vArchive.oArrowPrev.removeClass(vArchive.classDisabled);
			if (vArchive.nCurrItem==vArchive.nChildren-1) {
				$(this).addClass(vArchive.classDisabled);
			}
			
			slideArchive(vArchive.nCurrItem-1);

		}

		return false;
	});




});




