//**********
// Extensions
//**********

// Check whether links are external:
// (Only works with elements that have href):
jQuery.extend(jQuery.expr[':'],{
    external: function(a,i,m) {
        if(!a.href) {return false;}
        return a.hostname && a.hostname !== window.location.hostname;
    }
});

//**********
// jQuery
//**********

jQuery(document).ready(function(){

// Accordion with all closed initially
	jQuery(".accordion-first-closed .hide").hide();

	jQuery(".accordion-first-closed .heading").click(function(){
		jQuery(this).next(".hide").slideToggle("fast")
		.siblings(".hide:visible").slideUp("fast");
		jQuery(this).parent().next().slideDown("fast");
		return false;
	});


// Acordion with first open initially
	jQuery(".accordion-first-open .hide:not(:first)").hide();

	jQuery(".accordion-first-open .heading").click(function(){
		jQuery(this).next(".hide").slideToggle("fast")
		.siblings(".hide:visible").slideUp("fast");
		jQuery(this).parent().next().slideDown("fast");
		return false;
	});

// Adds class to sermons in service Sunday Morning
	jQuery("a:contains('Sunday Morning')").parent().parent().addClass("service-sunday-morning");

// Adds rel="external" to external links
	jQuery("a:external").attr("rel", "external");
	jQuery("a:external img").parent().removeAttr("rel", "external");

// Add classes to certain file type links
	jQuery("a[href$='pdf']").addClass("file-pdf");
	jQuery("a[href$='mp3']").addClass("file-mp3");
	jQuery("a[href$='mov']").addClass("file-mov");
	jQuery("a[href$='avi']").addClass("file-avi");
	jQuery("a[href$='wmv']").addClass("file-wmv");
	jQuery("a[href$='png'],[href$='gif'],[href$='jpg'],[href$='bmp']").addClass("file-image");
	// unless the link contains an image
	jQuery("a[href$='pdf'] img").parent().removeClass("file-pdf");
	jQuery("a[href$='mp3'] img").parent().removeClass("file-mp3");
	jQuery("a[href$='mov'] img").parent().removeClass("file-mov");
	jQuery("a[href$='avi'] img").parent().removeClass("file-avi");
	jQuery("a[href$='wmv'] img").parent().removeClass("file-wmv");
	jQuery("a[href$='png'] img, [href$='gif'] img, [href$='jpg'] img, [href$='bmp'] img").parent().removeClass("file-image");

// Show wp-ecommerce shopping cart widget if full, hide if empty
	jQuery("#primary table.shoppingcart").parent().parent().parent().show();
	jQuery("input.wpsc_buy_button").click(function(){
		jQuery("div#shopping-cart").slideDown("fast");
	});


// Tabs
	// setting the tabs to hide, setting the first tab to current and to show
	jQuery('div.tab-content').hide();
	jQuery('div.t1').show();
	jQuery('div.tabbed ul.tabs li.t1').addClass('tab-current');

	// Setting the tabs to switch on click
	jQuery('div.tabbed ul.tabs li').click(function(){
		var thisClass = this.className.slice(0,2);
		jQuery(' div.tab-content').hide();
		jQuery('div.' + thisClass).show();
		jQuery('div.tabbed ul.tabs li').removeClass('tab-current');
		jQuery(this).addClass('tab-current');
	});

//**********
// External links open in new window
//**********
	jQuery('a[rel="external"]').attr('target', '_blank');

//**********
// Hide read more on all paragraphs except for the last one in an excerpt
//**********
	jQuery('div.entry-summary p').prev().addClass("hide-read-more");

});