/* CLIQUE */
/* Announcements Pattern Javascript
----------------------------------------------------------------------------- */


	(function($) {


	$.fn.cliqueAnnouncements = function(parameters) {
	
		var defaults = {
		
			fx: 'scrollVert',
			speed: 200,
			timeout: 5000
					
		};

		var parameters = $.extend(defaults, parameters);


/* PRESERVE CHAINING
----------------------------------------------------------------------------- */

	
		return this.each(function() {
	
			var $this = $(this);
	

/* FUNCTION
----------------------------------------------------------------------------- */

	
			if ($this.children('div').length > 0) {
				
		
/* Build Headlines List */
		
			
				var oldTopics = ($.cookie('announcement_alerts') ? $.cookie('announcement_alerts') : "none");
				var newTopics = "";
				$this.children('h3').after('<ul class="ticker"></ul>');
		
				// Text Posts
				$this.find('ul.announcements > li').each(function() {

					var headline = $(this).children('h4').text();
					var link = $(this).children('h4').children('a').attr('href');
					var date = $(this).children('strong').text();

					// var topic = link.split('/').pop();

					if ($(this).hasClass('news')) {

						var type = 'news';
					
					} else {
					
						var type = 'tweets';
					
					}
					
					/* if (oldTopics.match(topic)) {
					
						var newItemClass = "";

					} else {
					
						type += " new";
						$(this).addClass('new').children('h4').append(' <em class="badge">New</em>');
		
					} */

					if ($(this).hasClass('news')) {

						var html = '<li class="' + type + '"><strong>' + date + '</strong><span>/</span><h4><a href="' + link + '">' + headline + '</a></h4></li>';
					
					} else {
					
						var html = '<li class="' + type + '"><strong>' + date + '</strong><span>/</span><h4>' + headline + '</h4></li>';
					
					}
					
					$this.children('ul.ticker').append(html);

					// Truncate ticker headlines
					truncate($('ul.ticker h4'), 90);				

					/* newTopics += topic + ","; */
				
				});

				// Add social sharing links (from functions.js)
				addThisLeads('home');

				
				/* var newTopicCount = $this.children('ul.ticker').children('.new').length;
				if (newTopicCount == 0) var newTopicCount = "0";
				$this.children('h3').html('<em>' + newTopicCount.toString() + '</em>'); */
	
			
/* Slideshow */
			
				
				if ($this.children('ul.ticker').children().length > 1) {
										
					$this.children('ul.ticker').cycle({
				
						fx: parameters.fx,
						speed: parameters.speed,
						timeout: parameters.timeout
				
					});
			
				} else {
				
					$this.children('ul.ticker').children().show();
				
				}

			
/* Hover/Expand/Contract */
		
		
				// $this.children('ul.actions').clone().appendTo($this).addClass('bottom');
			
				$this.children('ul.actions, ul.ticker').hover(function() { $this.addClass('hover'); }, function() { $this.removeClass('hover'); });
			
				$this.not('.expanded').children('ul.actions, ul.ticker').click(function() {
							
					$this.children('ul.ticker').cycle('pause');		
					$this.addClass('expanded');
					// $.cookie('announcement_alerts', newTopics);
					// pageTracker._trackEvent('Announcements', 'Open', 'Announcement Alerts Bar Opened');
					return false;

				});
				
				$this.children('ul.actions').children('li').children('a.close').click(function() {
				
					$this.children('ul.ticker').cycle('resume');		
					$this.removeClass('expanded');
					// window.scrollTo(0,0);
					return false;
				
				});
				
			} else {
			
			
/* Remove Container if Empty */


				$this.remove();
			
			}


/* END PLUG-IN
----------------------------------------------------------------------------- */


		});

	};

	})(jQuery);
