var fadeIndex = new Array();

var fadeImages = {
	"fade-header":[
		{"src":"/media/images/imagefade/img-home1.jpg","desc":'"Glory to God in the Highest"'},
		{"src":"/media/images/imagefade/img-home2.jpg","desc":'"Let your light so shine before men"'},
		{"src":"/media/images/imagefade/img-home3.jpg","desc":'"Your words are Spirit, Lord, and they are life"'},
		{"src":"/media/images/imagefade/img-home4.jpg","desc":'"Loud organs His Glory forth tell"'},
		{"src":"/media/images/imagefade/img-home5.jpg","desc":'"Lord, teach us to pray"'},
		{"src":"/media/images/imagefade/img-home6.jpg","desc":'"The Light shone in the darkness, and the darkness did not overcome it"'}
	]
};

jQuery(function(){
	// External Links
	$('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

	// Setup image faders
	$(".imagefade").each(function(){
		var id = $(this).attr("id");
		fadeIndex[id] = parseInt(0);

		if(typeof(fadeImages[id])!="object")
			return;
		
		var imgs = fadeImages[id];
		var img = $('<img src="" alt="" />');
		$(img).attr("src",imgs[0].src);
		$(this).append(img);

		if(typeof(imgs[0].desc)=="string")
			$(this).find("p").html(imgs[0].desc);
		
		setInterval(function(){
			var elm = document.getElementById(id);
			var total = imgs.length;
			fadeIndex[id]+=1;

			if(fadeIndex[id]==total)
				fadeIndex[id] = parseInt(0);
				
			$(elm).children("img:first").attr("src",imgs[fadeIndex[id]].src);
			$(elm).children("img:last").fadeOut("slow",function(){
				$(elm).children("img:last").attr("src",imgs[fadeIndex[id]].src);
				$(elm).children("img:last").show();
			});
			
			if(typeof(imgs[fadeIndex[id]].desc)=="string") {
				$(elm).children("p").html(imgs[fadeIndex[id]].desc);
			}
		},5000);
	});

});
