/*
	This JavaScript file supports custom functionality
	for brand campaign landing pages.
*/

/**
 * Plugin: jquery.zRSSFeed
 * 
 * Version: 1.1.2
 * (c) Copyright 2010-2011, Zazar Ltd
 * 
 * Description: jQuery plugin for display of RSS feeds via Google Feed API
 *              (Based on original plugin jGFeed by jQuery HowTo. Filesize function by Cary Dunn.)
 * 
 * History:
 * 1.1.2 - Added user callback function due to issue with ajaxStop after jQuery 1.4.2
 * 1.1.1 - Correction to null xml entries and support for media with jQuery < 1.5
 * 1.1.0 - Added support for media in enclosure tags
 * 1.0.3 - Added feed link target
 * 1.0.2 - Fixed issue with GET parameters (Seb Dangerfield) and SSL option
 * 1.0.1 - Corrected issue with multiple instances
 *
 **/

var allData = new Array();
var z = 0;
var tempNumberOfFeeds = 0;
var month=new Array(12);
month[0]="Jan";
month[1]="Feb";
month[2]="Mar";
month[3]="Apr";
month[4]="May";
month[5]="Jun";
month[6]="Jul";
month[7]="Aug";
month[8]="Sep";
month[9]="Oct";
month[10]="Nov";
month[11]="Dec";

	function mySorting(a,b) {
		a = a[2];
		b = b[2];
		return a == b ? 0 : (a < b ? -1 : 1)
	}

(function($){

	$.fn.rssCiscoCAFeed = function(url, options, fn) {	
	
		// Set pluign defaults
		var defaults = {
			limit: 2,
			header: true,
			titletag: 'h4',
			date: true,
			content: true,
			snippet: true,
			showerror: true,
			errormsg: '',
			key: null,
			ssl: false,
			linktarget: '_self',
			numberOfFeeds: 1,
			dateLang: "EN"
		};  
		var options = $.extend(defaults, options); 
		
		// Functions
		return this.each(function(i, e) {
			var $e = $(e);
			var s = '';

			// Check for SSL protocol
			if (options.ssl) s = 's';
			
			// Add feed class to user div
			if (!$e.hasClass('rssFeed')) $e.addClass('rssFeed');
			
			// Check for valid url
			if(url == null) return false;
			
			// Create Google Feed API address
			var api = "http"+ s +"://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + encodeURIComponent(url);
			if (options.limit != null) api += "&num=" + options.limit;
			if (options.key != null) api += "&key=" + options.key;
			api += "&output=json_xml"

			// Send request
			$.getJSON(api, function(data){
				
				// Check for error
				if (data.responseStatus == 200) {
	
					// Process the feeds
					_process(e, data.responseData, options);

					// Optional user callback function
					if ($.isFunction(fn)) fn.call(this,$e);
					
				} else {
					// Handle error if required
					if (options.showerror)
						if (options.errormsg != '') {
							var msg = options.errormsg;
						} else {
							var msg = data.responseDetails;
						};
						$(e).html('<div class="rssError"><p>'+ msg +'</p></div>');
				};
			});				
		});
	};
	
	// Function to create HTML result
	var _process = function(e, data, options) {

		// Get JSON feed data
		var feeds = data.feed;
		if (!feeds) {
			return false;
		}

		// Add feeds
		for (var i=0; i<feeds.entries.length; i++) {

			// Get individual feed
			var entry = feeds.entries[i];
		
			// Format published date
			var entryDate = new Date(entry.publishedDate);
			//var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
			
			allData[z] = new Array();
			allData[z][0]=entry.title;
			allData[z][1]=entry.link;
			allData[z][2]=entryDate;
			allData[z][3]=feeds.title;
			allData[z][4]=options.dateLang;
			allData[z][5]=entry.author;
			z++;
		}
		tempNumberOfFeeds++;
		if(tempNumberOfFeeds == options.numberOfFeeds)
		{
			var tempBrowser = navigator.userAgent + "";
			if(tempBrowser.toLowerCase().indexOf("firefox") != -1)
				var t=setTimeout("showNarrowTicker()",5000)
			/*else if(tempBrowser.toLowerCase().indexOf("chrome") != -1)
				var t=setTimeout("showNarrowTicker()",10000)*/
			else
				showNarrowTicker();
			//$(e).find('div.rssBody').vTicker();
		}
	};

})(jQuery);

	function showNarrowTicker()
	{
		//Set up Ticker
		if( $('#socialmediaticker').length){
			
			var html = '';	
			var itemsCount = 0;
			allData.sort(mySorting);
			allData.reverse();
			var j = 0;
			// Add body
			for (var i=0; i<allData.length; i++)
	
			{
				if(allData[i][0] == "")
					continue;
				// Add feed row
				var entryDate = new Date(allData[i][2]);
				var pubDate = "";
				var linkText = "";
				var icon = "news";
				pubDate = entryDate.getDate() + "-" + month[entryDate.getMonth()] + "-" + entryDate.getFullYear();
					if(allData[i][3] == "StratApps's Facebook Wall")
					{
						icon = "facebook";
					}
					else if(allData[i][3].indexOf("Twitter") != -1)
					{
						icon = "twitter";
					}
				html += '<li class="t1m-ticker-' + icon + '" data-index="'+j+'"><a name="&amp;lpos=socialdialog_hn" href="'+allData[i][1]+'" target="_blank" class="forcedNoUSFlag">' + ( (allData[i][0].length>70)?allData[i][0].substr(0,70)+'...':allData[i][0] )  + '</a><span class="date"> - '+pubDate+'</span></li>';
				j++;
				itemsCount++;
				if(itemsCount == 50) break;
			}
			var items = html;
	
				$('#socialmediaticker ol').empty().append(items);
				
				$('#socialmediaticker').data('ticker',{
					list$: $('#socialmediaticker ol'),
					button_next$: $('#socialmediaticker .newsitem-next a'),
					button_prev$: $('#socialmediaticker .newsitem-previous a'),
					length: $('#socialmediaticker ol li').length,
					index: -1,
					interval: null,
					onInterval: null,
					onNext: null,
					onPrev: null,
					goTo: null
				});
				
				var ticker = $('#socialmediaticker').data('ticker');
				
				ticker.goTo = function(i) {
					ticker.list$.find('li').eq(ticker.index).fadeOut('slow');
					ticker.index = i;
					ticker.list$.find('li').eq(ticker.index).fadeIn('slow');
				};
				
				ticker.onNext = function(e,isLoop){ 
					if(isLoop == undefined){
						//This was not a loop trigger
						clearInterval(ticker.interval);
						ticker.interval = setInterval(ticker.onInterval,5000);
					}
					ticker.goTo( (ticker.index+1) % ticker.length );
					return false;
				}
				
				ticker.onPrev = function(e,isLoop){ 
					if(isLoop == undefined){
						//This was not a loop trigger
						clearInterval(ticker.interval);
						ticker.interval = setInterval(ticker.onInterval,5000);
					}
					ticker.goTo ( ((ticker.index==0)?ticker.length:ticker.index)-1 ); //  Math.max(ticker.index-1) % ticker.length;
					return false;
				}
				
				ticker.onInterval = function() {
					ticker.button_next$.triggerHandler('click',true);
				}
				
				//Wire Buttons
				ticker.button_next$.click( ticker.onNext );
				ticker.button_prev$.click( ticker.onPrev );
				
				//Initially Hide all other items
				ticker.list$.find('li:gt(0)').hide();
				
				//Start Auto Ticker
				ticker.button_next$.trigger('click');
			
		}
	}

$(document).ready(function(){
					$('#boxContent').rssCiscoCAFeed('https://www.facebook.com/feeds/page.php?id=158382980890719&format=rss20', {
						limit: 5,
						header: false,
						titletag: 'h3',
						content: false,
						errormsg: "Oops! seems like some problem with Facebook Feed",
						ssl: true,
						numberOfFeeds: 1
					});
});



