$(document).ready(function() {


	// ** HIDE THE STAFF LISTS AND SLIDE THEM DOWN WHEN THE STAFF BUTTON IS CLICKED
	$("#staff-list-overlay").hide();
	$("a.staff-list").click(function() {
		$("#staff-list-overlay").slideToggle();		// TOGGLE THE COMMENT FORM FIELDSET OPEN AND CLOSED AND HIDE SUBMIT BUTTON
		return false;								// DEACTIVATE THE SUBMIT FUNCTION OF THE HIDE/SHOW BUTTON
	});
	
	
	
	// ** FAQ SHOW/HIDE
	$("dl#faq_list dd").hide();
	$("dl#faq_list dt").click(function() {
		$(this).next("dd").slideToggle("slow").siblings("dd").slideUp("slow");
		return false;
	});
	
	
	// ** GALLERY -- Add "no-margin" to last Image on Recent and Random Galleries
	$(".gal-row img:last").addClass("no-margin");
	
	// ** GALLERY -- Show and Hide on Galleries
	$(".gal-list li.sub").hide();
	
	//$("ul.gal-list li.cattitle").toggle(function(){
	//	$(this).addClass("open");
	//	}, function () {
	//		$(this).removeClass("open");
	//});
	
	
	$('ul.gal-list > li.catheading a').click(function(e) {
	    e.preventDefault();
	    var r = $(this).parent();
	    while (r.length > 0) {
	        r = r.next(".sub");
	        r.slideToggle("slow");
	    }
	});
	
	
	$("#select").change(function() {
		var option = $(this).val();
		if ( $(this).val() == 'all-events' ) {
				$(".calendarBG li").show();
        	}
    		else {
        		$(".calendarBG li." + option).show();
				$(".calendarBG li").not("." + option).hide();
        	}
	});
	
	
	// This launches all rel links in a new window
    $('a[rel="sermon_audio"]').click(function(){
        window.open(this.href,'mywindow','height=55,width=265,scrollTo,resizable=no,scrollbars=no,location=0,status=no','false');
		return false;
    });

	
	if($('.accordion').length > 0) {
		$('div.accordion div.reveal').wrapInner('<div></div>');
		$(".accordion h3").live('click', function (event) {
				if ($(this).hasClass('accordion-active')) {
					$(this).removeClass('accordion-active');
					$(this).next('div').slideUp();
				}
				else {
					$(this).addClass('accordion-active');
					$(this).next('div').slideDown();
				}
				return false;
			});
		}

		if($('#gallery-wrapper').length > 0) {
			// We only want these styles applied when javascript is enabled
							$('div.navigation').css({'width' : '200px', 'float' : 'left'});
							$('div.content').css('display', 'block');

							// Initially set opacity on thumbs and add
							// additional styling for hover effect on thumbs
							var onMouseOutOpacity = 0.67;
							$('#thumbs ul.thumbs li').opacityrollover({
								mouseOutOpacity:   onMouseOutOpacity,
								mouseOverOpacity:  1.0,
								fadeSpeed:         'fast',
								exemptionSelector: '.selected'
							});

							// Initialize Advanced Galleriffic Gallery
							var gallery = $('#thumbs').galleriffic({
								delay:                     2500,
								numThumbs:                 10,
								preloadAhead:              10,
								enableTopPager:            true,
								enableBottomPager:         true,
								maxPagesToShow:            7,
								imageContainerSel:         '#slideshow',
								controlsContainerSel:      '#controls',
								captionContainerSel:       '#caption',
								loadingContainerSel:       '#loading',
								renderSSControls:          true,
								renderNavControls:         true,
								playLinkText:              'Play Slideshow',
								pauseLinkText:             'Pause Slideshow',
								prevLinkText:              '&lsaquo; Previous Photo',
								nextLinkText:              'Next Photo &rsaquo;',
								nextPageLinkText:          'Next &rsaquo;',
								prevPageLinkText:          '&lsaquo; Prev',
								enableHistory:             false,
								autoStart:                 false,
								syncTransitions:           true,
								defaultTransitionDuration: 900,
								onSlideChange:             function(prevIndex, nextIndex) {
									// 'this' refers to the gallery, which is an extension of $('#thumbs')
									this.find('ul.thumbs').children()
										.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
										.eq(nextIndex).fadeTo('fast', 1.0);
										
								},
								onTransitionIn: function() {
								    $('#slideshow').fadeTo('fast', 1.0);
								    $('#slideshow span.image-wrapper').fadeTo('fast', 1.0);
								    $('#caption').fadeTo('fast', 1.0);
								    $('#caption span.image-caption').fadeTo('fast', 1.0);
								    $('#caption').fadeIn('fast', function() {
								        //$('#caption a.lightbox').lightBox();
								    });
								
									$next = $('#thumbs li.selected').find('a').attr('original');
								
								if($next != undefined) {
									$href = '<a href="'+$next+'" class="colorbox">Click to view larger image</a>';
									
									$('.colorbox').remove();
									if($href != '') {
										$('#main-content').append($href);
									}
									
									$('.colorbox').colorbox();
								}
								},
								onPageTransitionOut:       function(callback) {
									this.fadeTo('fast', 0.0, callback);
								},
								onPageTransitionIn:        function() {
									this.fadeTo('fast', 1.0);
									
								}
							});
		}
		
		
		// var a = $('.advance-link');	
		// a.attr('href', a.find('img').attr('src'));

		
		$(document).click(function() {
			$next = $('#thumbs li.selected').find('a').attr('original');
		if($next != undefined) {
			$href = '<a href="'+$next+'" class="colorbox">Click to view larger image</a>';
			
			$('.colorbox').remove();
			if($href != '') {
				$('#main-content').append($href);
			}
			
			$('.colorbox').colorbox();
		}
		});
		
		$(document).keyup(function() {
				$next = $('#thumbs li.selected').find('a').attr('original');
			if($next != undefined) {
				$href = '<a href="'+$next+'" class="colorbox">Click to view larger image</a>';

				$('.colorbox').remove();
				if($href != '') {
					$('#main-content').append($href);
				}

				$('.colorbox').colorbox();
			}
		});
		
		
		if($('#servant-profile').length > 0) {
			var $tabs = $('#servant-profile').tabs({
				event: 'change',
				select: function(event, ui) {
					if(ui.index == 2) {
						$('#step-3b').hide();
					}
				}
			});
			
			$('#step-3a .button').click(function(e) {
				$('#step-3a').hide();
				$('#step-3b').show();
				e.preventDefault();
			});
			
			$('#step-1 .button').click(function(e) {
				if(validateTab(1))
				{
					$tabs.tabs('select', 1);
				}
				
				e.preventDefault();
			});
			
			$('#step-2 .button').click(function(e) {
				$tabs.tabs('select', 2);
				e.preventDefault();
			});
		
			$('#servant-profile h3').live('click', function (event) {
				if ($(this).hasClass('off')) {
					$(this).removeClass('off');
					$(this).addClass('on');
					$(this).next('.show-hide').slideDown();
				}
				else {
					$(this).removeClass('on');
					$(this).addClass('off');
					$(this).next('.show-hide').slideUp();
				}
				return false;
			});
			$('.show-hide ul li').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});
			$('.form-check label').wrapInner('<a href="#" class="checkbox"></a>');
		
			
			$('#servant-profile form a.checkbox').live('click', function (event) {
				
				if($(this).parent().parent().parent().parent().attr('id') == 'personal_style') {
					$('#personal_style li').each(function(index) {
				  		$(this).parent().find('.active').removeClass('active');
					});
				}
				if($(this).parent().parent().parent().parent().attr('id') == 'availability') {
					$('#availability li').each(function(index) {
				  		$(this).parent().find('.active').removeClass('active');
					});
				}
				
				if ($(this).parent().parent().hasClass('active')) {
					$(this).parent().parent().removeClass('active');
					$(this).parent().parent().addClass('off');
					$('#servant-profile form .off input').attr('checked', false);
				}
				else {
					$(this).parent().parent().removeClass('off');
					$(this).parent().parent().addClass('active');
					$('#servant-profile form .active input').attr('checked', true);
				}
				return false;
			});
			
			
		
		}
		
				

		var pathname = location.pathname.split('/');
		
		for(var i=0; i< pathname.length; i++) {
			if(pathname[3] == 'category') {
				$('#sidebar ul li.here').removeClass('here');
				
				$("#sidebar ul li a[src*=" +  + "]").addClass('here');
			}
		}
		
		//disable enter keypress
		$("#servant-profile form").bind("keypress", function(e) {
             if (e.keyCode == 13) {
                 return false;
            }
         });


		$location = window.location.pathname;
		$location = $location.substr(1);
		$location = $location.split('/');
		
		if($location[1] == 'articles' || $location[1] == 'blog') {
			$('#sidebar').find('a').filter(':contains("Blog")').parent('li').addClass('here');
			$('#sidebar').find('a').filter(':contains("Articles")').parent('li').addClass('here');
		}
		
		if($location[1] == 'servant-profile') {
			$('#sidebar').find('a').filter(':contains("Servant Profile")').parent('li').addClass('here');
		}
		
		$('#volunteer-ops-page .checkbox').each(function() {
			$(this).removeClass('checkbox');
		});
		
});
