﻿/************************
*	NEWS ROTATOR		*
************************/
(function($) {
	$.fn.newsrotator = function(options) {

		// initialize
		var newsContainer = this;
		var defaults = { fade: 750, timeout: 9000, category: 'news' };
		var opts = $.extend(defaults, options);
		properties.fadeSpeed = opts.fade;
		properties.timeout = opts.timeout;

		// create pause and previous buttons (we'll bind them to events later)
		var html = "<div class='newsNav'><span id='newsToggle'>Pause</span><span id='newsPrev'>&laquo;</span>";

		var url;
		if (opts.category == "news")
			url = "/api/featurednews.ashx";
		else
			url = "/api/latestnews.ashx?category=" + opts.category;
		// load news from api

		$.getJSON(url, function(data) {
			$.each(data, function(i, elem) {

				// create news item from elment
				var itm = $(elem);
				var curItem = new newsItem(
				i,
				elem.ID,
				elem.NewsTitle,
				elem.Url,
				new Date(parseFloat(elem.PublicationDate.slice(6, 19))).format('dddd MMMM dd, yyyy').toLocaleString(),
				elem.Thumbnail,
				elem.Summary);

				// save to array
				properties.newsList[i] = curItem;

				// append corresponding link to navigation
				html = html.concat("<a href=\"", curItem.url, "\" title=\"", curItem.title, "\" rev=\"", i + 1, "\">", i + 1, "</a>");
			});

			// make sure there are at least 2 elements
			if (properties.newsList.length < 2) { return; }

			// create Next Button (will bind later)
			html += "<span id='newsNext'>&raquo;</span></div>";

			// refresh navigation
			$('.newsNav').replaceWith(html);

			// create pause behavior
			$("#newsToggle").click(function() {
				var btn = $(this);
				if (btn.text() == "Pause") {
					clearTimeout(timer);
					btn.text("Resume");
				}
				else {
					fadeNews();
					btn.text("Pause");
				}
			});

			// create Prev Behavior
			$("#newsPrev").click(function() {
				clearTimeout(timer);

				// move to previous image
				properties.curNews -= 2;
				if (properties.curNews < 0) properties.curNews = properties.newsList.length + properties.curNews;

				// prepare to swap image
				properties.backFrame.className = "show";
				$(properties.backFrame).html(properties.newsList[properties.curNews].tohtml());

				// set current image to hide next
				properties.frontFrame.className = "";
				properties.frontFrame.removeAttribute('style');

				fadeNews();
			});


			// create Next Behavior
			$("#newsNext").click(function() {
				clearTimeout(timer);
				fadeNews();
			});

			// create nav link behavior
			$(".newsNav a").click(function(e) {
				clearTimeout(timer);
				var lnk = $(e.target);

				// move to this image
				properties.curNews = lnk.text() - 1;

				// prepare to swap image
				properties.backFrame.className = "show";
				$(properties.backFrame).html(properties.newsList[properties.curNews].tohtml());

				// set current image to hide next
				properties.frontFrame.className = "";
				properties.frontFrame.removeAttribute('style');

				fadeNews();
				return false;
			});



			// only create element if it's not already there
			if (newsContainer.length == 1) newsContainer.append(document.createElement('div'));

			// get array of div elements to swap
			var divs = $('.newsFrame div');
			properties.frontFrame = divs[1];
			properties.backFrame = divs[0];

			$(".newsNav a[rev=1]").addClass("selected");

			// start toggling!
			toggleNews();
		});

	};

	var toggleNews = function() {
		// move to next image
		if (++properties.curNews >= properties.newsList.length) properties.curNews = 0;

		// swap frames
		var temp = properties.frontFrame;
		properties.frontFrame = properties.backFrame;
		properties.backFrame = temp;

		// set current image to hide next
		properties.frontFrame.className = "";
		properties.frontFrame.removeAttribute('style');

		// prepare to swap image
		properties.backFrame.className = "show";
		$(properties.backFrame).html(properties.newsList[properties.curNews].tohtml());

		// fade in next image and repeat
		timer = setTimeout(fadeNews, properties.timeout);
	};

	var fadeNews = function() {
		// fade in news and repeat
		$(properties.backFrame).fadeIn(properties.fadeSpeed, toggleNews);

		// bold active item
		var curlnk = $(".newsNav a[rev=" + (properties.curNews + 1) + "]");
		curlnk.addClass("selected");
		curlnk.siblings().removeClass("selected");
	};

	var properties = {
		newsList: [],
		curNews: 0,

		frontFrame: null,
		backFrame: null,

		fadeSpeed: 750,
		timeout: 1000
	};

	var timer = null;

	var newsItem = function(position, guid, title, url, date, img, summary) {
		this.position = position;
		this.guid = guid;
		this.title = title;
		this.url = url;
		this.date = date;
		this.img = img;
		this.summary = summary;
		this.tohtml = function ToHtml() {
			if (!this.html) {
				this.html = "<h3><a href=\"" + this.url + "\">" + this.title + "</a></h3>";
				this.html += "<a class=\"fullstory\" href=\"" + this.url + "\">Full Story &raquo;</a>";
				this.html += "<a class=\"sf_newsImage\" href=\"" + this.url + "\"><img src=\"" + this.img + "\" alt=\"" + this.title + "\" /></a>";
				this.html += "<p><strong>Posted " + this.date + "</strong><br />" + this.summary + "</p>";
			}
			return this.html;
		}
	};
})(jQuery);