// String Replace
function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

$(document).ready(function() {
	$('#search-form label, #email-signup label').hide();
	$('#search-input').val( $('#search-form label').html() ).addClass('default');
	$('#email-input').val( $('#email-signup label').html() ).addClass('default');

	$('#search-input').focus(function() {
		if($(this).hasClass('default')) { $(this).val('').removeClass('default'); }
	}).blur(function() {
		if($(this).val() == $('#search-form label').html() || $(this).val() == '') { $(this).val( $('#search-form label').html() ).addClass('default'); }
	});
	$('#email-input').focus(function() {
		if($(this).hasClass('default')) { $(this).val('').removeClass('default'); }
	}).blur(function() {
		if($(this).val() == $('#email-signup label').html() || $(this).val() == '') { $(this).val( $('#email-signup label').html() ).addClass('default'); }
	});

	/* News Scrolling */
	if($('#featured-news li').length > 2) {
		$('#featured-news').wrap('<div id="featured" />'); $('#featured-news').append('<li class="overlay" />');
		$('#featured-news').after('<ul class="content-list" id="featured-links" />');
		var scr = setInterval(featuredScroll, 5000);
		$('#news').bind('mouseenter', function() {
			clearInterval(scr);
		}).bind('mouseleave', function() {
			scr = setInterval(featuredScroll, 5000);
		});
		var featured_height = 0;
		$('#featured-news li').each(function() {
			if (!$(this).hasClass('overlay')) {
				$(this).attr('id', 'news-summary_' + ($(this).index() + 1));
				if($(this).height() > featured_height) { featured_height = $(this).height(); }
                                if (!$(this).hasClass('overlay') && !$('#news-link_' + ($(this).index() + 1)).length) {
                                    $('#featured-links').append('<li id="news-link_' + ($(this).index() + 1) + '"><p class="date more">' + $(this).children('.date').html() + '</p><p class="title">' + $(this).children('.title').html() + '</p></li>');
                                }
			}
		});
		$('#featured-news li').css('height', featured_height);
	}
	$('#featured-news li:first, #featured-links li:first').addClass('selected');

	$('#featured-links li').hover(function() {
		var id = str_replace('news-link_', '', $(this).attr('id'));
		if (!$(this).hasClass('selected')) {
			featuredNav(id);
		}
	});
	function featuredNav(which, speed) {
		if (speed) {
			$('#featured-news .overlay').fadeIn(speed, 'easeInOutQuad', function() {
				$('#news .selected').removeClass('selected');
				$('#news-summary_' + which + ', #news-link_' + which).addClass('selected');
				$('#featured-news .overlay').fadeOut(speed, 'easeInOutQuad');
			});
		} else {
			$('#news .selected').removeClass('selected');
			$('#news-summary_' + which + ', #news-link_' + which).addClass('selected');
		}
	}
	function featuredScroll() {
		if($('#featured-news .selected').next().length && !$('#featured-news .selected').next().hasClass('overlay')) {
			var which = str_replace('news-summary_', '', $('#featured-news .selected').next().attr('id'));
		} else {
			var which = str_replace('news-summary_', '', $('#featured-news li:first').attr('id'));
		}
		featuredNav(which, 600);
	}

});


function sloganScroll() {
	$('#slogans .selected').fadeOut(300, function() {
		if($(this).next().length) {
			var i = $(this).index() + 1;
		} else {
			var i = 0;
		}
		$('#slogans li:eq(' + i + ')').fadeIn(300, function() {
			$('#slogans .selected').children().hide();
			$('#slogans .selected').removeClass('selected');
			$(this).children('h3').fadeIn(700, function() {
				$(this).next().animate({'left':'-1px'}, 200, function() {
					$(this).animate({'left':0}, 200, function() {
						$(this).fadeIn(700, function() {
							$(this).parent().addClass('selected');
						});
					});
				});
			});
		});
	});
}

function bannerScroll() {
	var selected = $('.banners img:visible');
	if(selected.next('img').length) {
		var next_img = selected.next();
	} else {
		var next_img = $('.banners img:first');
	}
	next_img.fadeIn(300);
	selected.fadeOut(300);
}
