$(document).ready(function() {
	// Make each news item totally clickable
 	$("#newsBox ul li").click(function() {
 		url = $(this).find("a").attr("href");
 		window.location = url;
 	});
 	
 	
	// Count the Panels
	$("#feature div.feature").hide();
	$("#feature div.feature:first").show();
	totalPanels = $("#feature div.feature").length;
	
	currentPanel = 1;
	panelSpeed = 6000;						
	
	
	// Pause Effect
	$("#feature").mouseover(function() {
		stopPanels();
	});
	
	$("#feature").mouseout(function() {
		startPanels();
	});
	
	
	// Create Paginator
	for (i=1; i<=totalPanels; i++) {
		$("#paginator").append('<li><a href="javascript:void(0);">' + i + '</a></li>');
	}
	$("#paginator li a:first").addClass("current");
	
	$("#paginator li a").live("click",function() {
		stopPanels();
		panelNumber = $(this).html();
		
		if (currentPanel == panelNumber) {
			return false;
		}
		else {
			$("#paginator li a").removeClass("current");
			$(this).addClass("current");
			$("#feature div.feature").fadeOut(200);
			$("#feature div.feature:eq(" + (panelNumber - 1) + ")").fadeIn(1000);
			currentPanel = panelNumber;
		}
	});
	
	
	// Functions
	function showPanel() {
		if (currentPanel > (totalPanels - 1)) {
			currentPanel = 0;
			$("#feature div.feature").fadeOut(200);
			$("#paginator li a").removeClass("current");
			$("#paginator li a:eq(" + currentPanel + ")").addClass("current");
			$("#feature div.feature:eq(" + currentPanel + ")").fadeIn(1000);
			currentPanel++;
		}
		else {
			$("#feature div.feature").fadeOut(200);
			$("#paginator li a").removeClass("current");
			$("#paginator li a:eq(" + currentPanel + ")").addClass("current");
			$("#feature div.feature:eq(" + currentPanel + ")").fadeIn(1000);
			currentPanel++;
		}
	}	
	
	function startPanels() {
		featurePanel = setInterval(showPanel, panelSpeed);
	}
	
	function stopPanels() {
		clearInterval(featurePanel);
	}
	
	
	startPanels();
	
	
	
	// -----------------------------------------------------------------------------
	//  INDUSTRY NEWS
	// -----------------------------------------------------------------------------
	
	var newsItems = [];
	
	$.ajax({
		url: "assets/includes/feed.php?url=http://www.govinfosecurity.com/rssFeeds.php?type=main",
		dataType: "xml",
		error: function(error, xhr, text) {
			console.log(error);
		},
		success: function(data) {	
						
			$(data).find("item").each(function() {
				var object = {
					title : $(this).find("title").text(),
					link : $(this).find("link").text()
				};
				newsItems.push(object);
			});
		}
	});
	
	$.ajax({
		url: "assets/includes/feed.php?url=http://feeds2.feedburner.com/GovconWire",
		dataType: "xml",
		error: function(error, xhr, text) {
			console.log(error);
		},
		success: function(data) {	
						
			$(data).find("item").each(function() {
				var object = {
					title : $(this).find("title").text(),
					link : $(this).find("link").text()
				};
				newsItems.push(object);
			});
		}
	});
	
	function shuffle(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}

	$("#rssFeed").ajaxStop(function() {
		newsItems = shuffle(newsItems);
		
		$(newsItems).each(function(index) {
			if(index < 8) $("#rssFeed").append('<li><a href="' + this.link + '" target="_blank">' + this.title + '</a></li>');
		});
		
		var tallestItem = 0;
		$("#rssFeed li").each(function() {
			if($(this).height() > tallestItem) tallestItem = $(this).height();
			
			$(this).hide();
		});
		$("#rssFeed").height(tallestItem);
		$("#rssFeed li:first").addClass("active").css({ top: "15px" }).animate({ opacity: "show", top: "0px" }, 500);
		
		setInterval(function() {
			$this = $("#rssFeed li.active");
			
			$this.removeClass("active").animate({ top: "-15px", opacity: "hide" }, 500);
			
			var nextLi = ($this.next('li').length) ? $this.next('li') : $("#rssFeed li:first");
			
			nextLi.addClass("active").css({ top: "15px" }).animate({ top: "0px", opacity: "show" }, 500);
			
		}, 5000);
	

	});
	
	
	
});
