/* 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>');
		
				// Friend Requests
				if ($this.find('ul.members > li').length > 0) {

					var headline = $('div.confirmation > h3').text();
					var type = "member";
					var html = '<li class="' + type + ' new"><h4><a href="#">' + headline + '</a></h4></li>';
					$this.children('ul.ticker').append(html);
					
					$this.find('ul.members > li').each(function() {
						
						var topic = "friend_request";						
						newTopics += topic + ",";
					
					});
				
				}
		
				// Text Posts
				$this.find('ul.stories > li, ul.leads > li').each(function() {
				
					var headline = $(this).children('h4').text();
					var link = $(this).children('h4').children('a').attr('href');
					var topic = link.split('/').pop();
					var type = $(this).parent().attr('class');
					
					if (oldTopics.match(topic)) {
					
						var newItemClass = "";

					} else {
					
						type += " new";
						$(this).addClass('new').children('h4').append(' <em class="badge">New</em>');
		
					}
					
					var html = '<li class="' + type + '"><h4><a href="' + link + '">' + headline + '</a></h4></li>';
					$this.children('ul.ticker').append(html);
					newTopics += topic + ",";
				
				});
				
				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.hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
			
				$this.not('.expanded').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.close').children('a').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);
