$(document).ready(function() {
	
	//Sliding Banners Stuff
	
	$("#gcontainer").css("overflow", "hidden");
	
	var slider = $('#gcontainer').cycle({ 
		fx:      'scrollHorz', 
		speed:    1500, 
		timeout:  6000,
		prev:   'a.prev', 
    	next:   'a.next'
	});
	
	$('#banner-controls a.switch').click(function() { 
		$('#gcontainer').cycle('toggle');
		$(this).toggleClass("pause play");
	});
	
	//News Slider Stuff
	
	var narticles = $("#slide-container .article").length;
	var npos = 1;
	
	$("#slide-container").stop().scrollLeft(0);
	
	$(".left a").click(function() {
		if(npos > 1) {
			$("#slide-content").animate({scrollLeft: "-=200"}, 500);
			npos = npos - 1;
			console.log(npos);
		}
		return false;
	});
	
	$(".right a").click(function() {
		if(npos < (narticles-2)) {
			$("#slide-content").animate({scrollLeft: "+=200"}, 500);
			npos = npos + 1;
			console.log(npos);
		}
		return false;
	});
	
	//Events Slider
	
	var spos = 0;
	var nevents = $(".event").length;
	var eheight = (120 * nevents)-240;
	$("#newsbody").scrollTop(0);
	
	$("#news .next a").click(function() {
		if(spos < eheight) {
			spos = spos+240;
			$("#newsbody").stop().animate({scrollTop: spos}, 1000);
		}
		return false;
	});
	
	$("#news .previous a").click(function() {
		if(spos > 0) {
			spos = spos-240;
			$("#newsbody").stop().animate({scrollTop: spos}, 1000);
		}
		return false;
	});

	//Ticker Stuff

	$.ajax({
		type: "GET",
		url: "/news/rss",
		dataType: "xml",
		data: "limit=6",
		success: parseXml
	});

	function parseXml(xml)
	{
		$(xml).find('item').each(function() {
			var title= $(this).find('title').text();
			var link = $(this).find('link').text();
			var anchor = "<a href=\"" + link + "\">" + title + "</a>"; 
			if(link == 'http://' || link == '') {
				$('#js-news').append($('<li>', {text: title}));
			} else {
				$('#js-news').append($('<li>' + anchor + '</li>'));
			}
		});
	
        $('#js-news').ticker({
            speed: 0.10,
            htmlFeed: true,
            fadeInSpeed: 600,
			controls: true,
            titleText: ''
        });
	}
	
	function throttle() {
		clearTimeout(timer);
		timer = setTimeout(function() {autoslide = true;}, 5000);
	}
	
});
