var tickerspeed = 5000; //speed for Latest ticker

jQuery(document).ready(function() {

	//banners
	initHomepageBanner();
	
	//ticker
	if(jQuery('#latest_ticker ul li').length > 1){
		jQuery('#latest_ticker ul li:not(:first)').hide();
		animateTickerLoop(0, jQuery('#latest_ticker ul li'), tickerspeed);
		
		//add hover to pause animation
		jQuery('#latest_ticker ul li').hover(
			function () {
				jQuery(this).stop();
			}, 
			function () {
				var elems = jQuery('#latest_ticker ul li');
				var thisindex = elems.index(this);
				animateTickerLoop(thisindex, elems, tickerspeed);
			});
	 };
	
	//Scrolling feature (using carousellite)
	jQuery('.scrolling_buttons').show();
	jQuery(function() {
		jQuery(".scrolling_feature").jCarouselLite({
			btnNext: ".scrolling_feature_next",
			btnPrev: ".scrolling_feature_prev",
			visible: 1
		});
	});

	//podcast MP3 player
	jQuery('.podcast_teaser a.playaudio').jmp3({
		backcolor: "c0c0c0",
		forecolor: "7a2991",
		width: 86,
		height: 15,
		showdownload: "false"
	});
	
	
	jQuery('.donateform').submit(function(){
		if(jQuery('#method_monthly:checked').length){
			jQuery('.donateform').submit(function(){
				pageTracker._trackPageview('/donation/external_direct_debit_donation_form.rm');
				return true;
			});
		}
	});
	
	
	//donation form in multivariant 4
	jQuery('.donateform .methods input').bind("change click",function(){
		jQuery('.donateform').attr("action",jQuery(this).val());
	});
	
});

function animateTickerLoop(current_index, elems, selected_speed){
	var next_index = current_index + 1;
	if(next_index >= elems.length){
		next_index = 0;
	};
	
	jQuery(elems[current_index]).animate({width:jQuery(this).width()}, selected_speed, '', function(){
		jQuery(elems[current_index]).fadeOut('slow', function(){
			jQuery(elems[next_index]).fadeIn('fast', function(){
				jQuery(elems[current_index]).stop();
				animateTickerLoop(next_index, elems, selected_speed);
			});
		});
	});
};

function initHomepageBanner(){
	jQuery('#banners li.banner_item:not(:first)').hide();
	jQuery('#banners .thumbs').show();
	jQuery('#banners .thumbs li a').click(function(eventObj){
		var selected = jQuery(this).attr('href');
		var bg = jQuery(this).attr('class');
		jQuery('#banners li.banner_item').hide();
		jQuery(selected).show();
		//update maincontent bg color to this theme
		jQuery('#maincontent').attr('class', 'maincontent_' + bg);
		eventObj.preventDefault();
	});
}
