var scroller_options = {
	title: 'Latest News',
	rss_url: 'rss.xml',	// Needs to have 'text/xml' mimetype for IE?
	container: '#rss_scroller',
	speed: 3,	//higher number = faster speed
	to_display: 10
}

function move(dir){
	the_position = $(scroller_options.container+' ul').position().top;
	if(the_position >= -($(scroller_options.container+' ul').height()-$(scroller_options.container).height()+22) && dir == '-' || the_position <= 20 && dir == '+'){
		$(scroller_options.container+' ul').animate({top:dir+'='+scroller_options.speed+'px'},1,'linear');
	}
};

function stop_animations(){
	$(scroller_options.container+' ul').stop(true);
	clearInterval(interval_id);
};

$(function(){
	var month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	interval_id = 0;
	
	/* generate the base HTML in the container */
	$(scroller_options.container).append('\
		<h1>{title}</h1>\
		<img class="spinner" src="/shared/js/rss-scroller/images/spinner.gif" alt="Loading...">\
		<ul></ul>\
		<div class="scroller_nav">\
			<a class="scroll up" href="#up"><img src="/shared/js/rss-scroller/images/up.png" alt="Scroll up" title="Scroll up" /></a>\
			<a class="scroll down" href="#down"><img src="/shared/js/rss-scroller/images/down.png" alt="Scroll down" title="Scroll down" /></a>\
			<a class="rss" href="{rss_url}"><img src="/shared/js/rss-scroller/images/rss.png" alt="RSS feed" title="RSS feed" /></a>\
		</div>\
	'.supplant(scroller_options));
	/* end the base HTML generation */
	/* GET AJAX Call to the RSS feed  */
	$.get(scroller_options.rss_url,{},function(xml){
		$('item',xml).each(function(i){
			if(i+1 <= scroller_options.to_display){
				the_date = new Date($(this).find('pubDate').text());
				the_hour = the_date.getHours();
				ap = "AM";
				if (the_hour > 12) {
					the_hour -= 12;
					ap = "PM";
				}
				the_minutes = the_date.getMinutes();
				if (the_minutes < 10)
					the_minutes = "0" + the_minutes;
				$(scroller_options.container+' ul').append('\
					<li>\
						<p><a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a></p>\
						<span>' + the_date.getDate() + ' ' + month[the_date.getMonth()] + ', ' + the_date.getFullYear() + ' ' + the_hour + ':' + the_minutes + ' ' + ap + '</span>\
					</li>\
				');
			}
		});
		// Hide the AJAX spinner
		$(scroller_options.container+' img.spinner').hide();
	},'xml');

	/* Scrolling Code Start */
	$('.scroller_nav a.scroll').mousedown(function(){
		interval_id = $(this).hasClass('up') ? setInterval('move("+")',1) : setInterval('move("-")',1);
	})
	.mouseup(function(){
		stop_animations();
	})
	.hover(function(){},function(){//in case the user moves his mouse off of the arrow and never "mouseup"s it.
		stop_animations(); 
	})
	.click(function(){return false;});
	/* Scrolling Code End */
});

// Supplant function allows for variable substitution inside a string
// http://javascript.crockford.com/remedial.html
String.prototype.supplant = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
};
