(function($) {

	$.fn.news_scroller = function(settings) {
	
		settings = jQuery.extend({
			// Configuration related to overlay
			url: 		'shared/inc/news.php?site=site_gshs&element=web',
			activeColour:	'#000'
		},settings);
			
		$(this).append("<div id='news_window'><div id='news_reel'></div></div><div id='news_list'><ul></ul></div>");
		
		$.ajax({
			type: "GET",
			url: settings.url,
			dataType: "xml",
			success: parseXml
		});
		
		function parseXml(xml) {
				
			var start=Math.floor(Math.random()*$(xml).find("item").length)+1;
			var office='false';
			var emergency='false';
			
			$(xml).find("item").each(function() {
				if ($(this).find("image").text().indexOf("swf") != -1 || $(this).find("image").text().indexOf("mp4") != -1 || $(this).find("image").text().indexOf("flv") != -1) {
					$("#news_reel").append("<a href='"+$(this).find("large_image").text()+"' target='_blank'><span id='movie_"+$(this).attr("id")+"'></span></a>");		
					var so = new SWFObject('http://www.greatsankey.org/shared/flash/player.swf','ply','460','345','9','#000000');
					so.addParam('allowfullscreen','true');
					so.addParam('allowscriptaccess','always');
					so.addParam('wmode','opaque');
					so.addVariable('file',$(this).find("image").text());
					so.addVariable('controlbar','over');
					so.addVariable('image','http://www.greatsankey.org/shared/js/scroller/images/film.jpg');
					so.write('movie_'+$(this).attr("id"));
				} else {
					$("#news_reel").append("<a href='"+$(this).find("large_image").text()+"' target='_blank'><img src='" + $(this).find("image").text() + "' /></a>");			
				}	
				$("#news_reel").append("<div class='news_article'><h2>" + $(this).find("title").text() + "</h2>"+$(this).find("article").text()+"</div>")		
				$("#news_list ul").append("<li id='" + $(this).attr("id") + "' rel='"+$(this).attr("id")+"'><h2>" + $(this).find("title").text() + "</h2>" + $(this).find("date").text() + " (" + $(this).attr("department") + ")</li>");
				if ($(this).attr("department") == "Office" && office != 'true') {
					start=$(this).attr("id");
					office = 'true';					
				} else if ($(this).attr("department") == "Emergency Notification" && emergency != 'true') {
					start=$(this).attr("id");
					emergency = 'true';
				}
			});
			scroller(start);	
		}	
		function scroller(start) {
			//Set Default State of each portfolio piece
			$("#news_list").show();
			
			//$("#news_list li:first").addClass("active");
			$("#news_list li:nth-child("+start+")").addClass("active");
			$active = $('#news_list li.active');
			
			//Get size of images, how many there are, then determin the size of the image reel.
			var imageWidth = $("#news_window").width();
			var imageSum = $("#news_reel img,div").size();
			var imageReelWidth = imageWidth * imageSum;

			//Adjust the image reel to its new size
			$("#news_reel").css({'width' : imageReelWidth});

			//Paging + Slider Function
			rotate = function(){	
				var triggerID = $active.attr("rel") - 1; //Get number of times to slide
				var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

				$("#news_list li").removeClass('active'); //Remove all active class
				$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
				
				//Slider Animation
				$("#news_reel").animate({ 
					left: -image_reelPosition
				}, 500 );
							
				$('#news_list').animate({
					scrollTop: ($("#"+$active.attr("rel")).offset().top-345)+$('#news_list').scrollTop()
				}, 500);			
				
			}; 

			//Rotation + Timing Event
			rotateSwitch = function(){		
				play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
					$active = $('#news_list li.active').next();
					if ( $active.length === 0) { //If paging reaches the end...
						$active = $('#news_list li:first'); //go back to first
					}
					rotate(); //Trigger the paging and slider function
				}, 8000); //Timer speed in milliseconds (3 seconds)	
				
			};
			
			rotate();
			rotateSwitch();

			//On Hover
			$("#news_window").hover(function() {
				clearInterval(play); //Stop the rotation
			}, function() {
				rotateSwitch(); //Resume rotation
			});	

			//On Click
			$("#news_list li").click(function() {	
				$active = $(this); //Activate the clicked paging
				//Reset Timer
				clearInterval(play); //Stop the rotation
				rotate(); //Trigger rotation immediately
				rotateSwitch(); // Resume rotation - turned off so when they click, it stops
				return false; //Prevent browser jump to link anchor
			});
		};	
	};
})(jQuery);
