/*
THE STRUCTURE OF JSON ARTICLE OBJECT
{
	"urgent":true,
	"important":false,
	"alertLevel":"urgent",
	"contentHtml":"For the remainder of 2009, Brizo is offering incentives to consumers, 
					showroom sales associates and rep agencies to purchase and sell Vesiâ?¢ and TresaÂ® bath 
					products. There are three components to the programâ??and three reasons to make someoneâ??s 
					dream bath a reality. Visit <a href=\"http://www.brizo.com/rebate\">www.brizo.com/rebate<\/a> to 
					learn more.<br />\n",
	"locationHtml":"",
	"date":{"time":1250568000000,
			"minutes":0,
			"seconds":0,
			"hours":0,
			"month":7,
			"timezoneOffset":240,
			"year":109,
			"day":2,
			"date":18},
	"bizLink":"/news/2009/Vesi+or+Tresa+Mail-In+Rebate+Offer.html",
	"contacts":[
				{"phone":"(317) 571.6516",
				 "email":"jai.massela@brizo.com",
				"name":"Jai Massela"},
				{..}...
				],
	"keywordsHtml":"",
	"categories":["company","".....],
	"attachments":[],
	"titleHtml":"Vesiâ?¢ or TresaÂ® Mail-In Rebate Offer\n",
	"subtitleHtml":"",
	"prettyDateForEmail":"08/18/2009","
	descriptionHtml":""
}
*/

var ticker = {
	
	setGlobals: function() {
		$ticker = $('div.ticker-placeholder');
		tickerItems = [];
	},
	
	// load JSON data and build ticker
	buildTicker: function() {
		var d = new Date();
		$.getJSON('/news/ticker.json?nocache=' + d.getTime(),
			function(data, status){
				if(status === 'success'){
					var articles = data.articles;
					// create ticker container ul
					var $headlines = $('<ul id="headlines-list" class="marquee"></ul>');
					// make sure there are headlines to display in the ticker
					if(articles.length >= 1) {
						$(articles).each(function(){
							var $linkText = $.trim(this.titleHtml).replace('<br />', '');
							$headlines.append('<li><a href="' + this.bizLink + '">' + $linkText + '</a> - <a href="#" class="ignore">[ignore]</a></li>');
						});
						// add id of 'ticker' to pick up styles
						$ticker.attr('id', 'ticker').html($headlines);
						// activate ignore links
						ticker.setIgnore();
						// insert HTML above ticker which would be useless without the ticker
						$ticker.before('<div id="ticker-info" class="clearfix"><h3>Latest News</h3> <p class="manage-subscription-ticker"><a class="thickbox" href="/subscription-center/index.html?TB_iframe=true&amp;height=470&amp;width=600">Manage my news subscription</a></p></div>');
						// add slight delay to first headline appearing - overcomes intermittent hiccups in presentation
						$('body#news-main #content p.manage-subscription-tag').hide();
						setTimeout(function(){
							tb_init('a.thickbox, area.thickbox, input.thickbox');							
							$('#headlines-list').marquee({ showSpeed: 1500, pauseSpeed: 4000, delay: 500 });
						}, 1000);
					}
				}
			}
		);		
	},
	
	
	// set up ignore links for headlines
	setIgnore: function() {
		$('a.ignore').each(function(){
			var $this = $(this);
			// strip out host as well as protocol that IE "helpfully" inserts for us
			var bizLink = $this.prev().attr('href').replace(location.host, '').replace('http://', '');			
			$this.click(function(){				
				var d = new Date();				
				$.getJSON('/news/markIgnore.json?nocache=' + d.getTime() + '&bizLink=' + encodeURIComponent(bizLink), function(data, status){
					if(status === 'success'){
						$('#headlines-list').marquee('resume');
						$this.parents('li').fadeOut('fast').remove();
						var mItems = $('#headlines-list li');
						setTimeout(function() {		
							$('#headlines-list').marquee('update');
							if(mItems.length < 1){
								$('#ticker, #ticker-info').remove();
								$('body#news-main #content p.manage-subscription-tag').show();
							}
						}, 1000);						
					}
				});
				return false;
			});
		});
	}
	
};		

$(document).ready(function(){
	ticker.setGlobals();
	ticker.buildTicker();
});