/********************************************************************

	FOREX.COM - ARTICLE FEED
	
	=========================================
	Author: Dave Bobak (bobak@teehanlax.com)
	Agency: teehan+lax
	Date: July 15, 2008
	=========================================	
	
	The articleFeed JSON object allows for periodical updates from a source and appends an update to a scrollable div layer.
			
	Public Methods
	-------------------------------------------------------------
	init (pseudo-constructor)
		- Arguments:
			- container (html div): The HTML container to push the data into
			- feed (URL): The URL of the page or script to access
			- interval (integer): The time in milliseconds to perform the periodical update
		- Returns: null
	
	checkArticle
		- Description: Check current element and if see if it meets the criteria, it returns a boolean with the result			
		- Arguments:
			- elem (html element):
		- Returns: boolean
	
	checkAllArticles 
		- Description: Checks all articles. Calls "checkArticle" to test all articles to ensure they meet the winnow and pair criteria and shows or hides them
		- Arguments:
		- Returns: null
		
	checkNewArticles 
		- Description: Check new articles coming in to ensure they meet the winnow and pair criteria
		- Arguments: 
			-elem (html element)
		- Returns: null
		
	togglePairs
		- Description: This function toggles the pairs filter. When on, only articles related to your currency pairs will be displayed. 
		- Arguments: 
		- Returns: null
	
	toggleWinnow
		- Description: This function is accessed when toggling between article content types (all, news, forex insider, market update)
		- Arguments: 
			-winnow (string): The "tag" of the winnow type, used by other methods to determine which content to display
		- Returns: null

	HTML fragment example
	-------------------------------------------------------------
	Each fragment would have a unique ID such as article_20080715121234 in the example below (article_+timestamp)
	The fragment would be "tagged" with relavent classes: news, USD, CAD (the article type, currency pairs)
	
	<!-- Start ARTICLE BLOCK container -->
	<div id="article_20080715121234" class="article_block news USD CAD">
		<div class="float_wrapper">
			<div class="news_article">
				<a href="#" class="title">USD Dollar hits 50-day high</a>
				<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut laboris nisi ut aliquip ex ea commodo consequat... <a href="#">full article</a></p>
				<span class="related_pair"><a href="#">View AUS/USD</a></span>
		  </div>
			<div class="time">20:03:40 <img src="/resources/images/icons/icn_article_news.gif" alt="News" /></div>
		</div>
	</div>
	<!-- End ARTICLE BLOCK container -->	
	
	Dependencies
	-------------------------------------------------------------
	- MooTools 1.2
	
*********************************************************************/

var articleFeed = {
	
	container : null,	
	feed : null,
	blnFilterPairs: false,
	currWinnow: 'all',
	blnUsePair : false,
	arrCurrencyPair : null,
	blnUpdate: true,
	
    init : function (container, feed, interval) {
		
		articleFeed.container = $(container);
		articleFeed.feed = feed;
		articleFeed.interval = interval;		
		
		if (articleFeed.blnUpdate) {
			
			var req = new Request.HTML({url:articleFeed.feed, 
				onSuccess: function(tree,elements) {
									
					// Check if element exists. If request was empty, do not continue
					if (elements) {		
				
						// Store the first element from the result (the div container)
						var elem = elements[0];
	
						// Check if we should only display pairs (eg: for currency pair summary page)
						if (!articleFeed.blnUsePair || (articleFeed.blnUsePair && ( elem.hasClass(articleFeed.arrCurrencyPair[0]) || elem.hasClass(articleFeed.arrCurrencyPair[1]) ) )) {
							
								articleFeed.checkNewArticles(elem); 
	
								// Start the new element at 0 opacity (invisible)				
								elem.setStyle('opacity','0');					
								
								// Add the new element to the top of the article container
								articleFeed.container.grab(elem, 'top')
	
								// Get the id from the new element
								var newID = elem.id;
								
								// Fade the new element in
								$(newID).fade('in');					
								
								// Figure out the last element
								var elemLast = $(articleFeed.container).getLast('.article_block');
								
								// Remove the last element
								elemLast.destroy();
						}
						
					}					
				},
				//Our request will most likely succeed, but just in case, we'll add an
				//onFailure method which will let the user know what happened.
				onFailure: function() {
					
				}
			});
			
			var sendRequest = function() { req.send() };
							
			sendRequest.periodical(articleFeed.interval);		
		}
    },
	
	checkArticle : function(elem) {

		// If not set to all, make sure this is the proper type
		if (articleFeed.currWinnow == 'all' || elem.hasClass(articleFeed.currWinnow)) {		

			// If quickpairs toggle is set, ensure this is related to one of our quick pairs
			if (!articleFeed.blnFilterPairs) {		
				return true;
			}
			else {
				var blnMatchFound = false;
				var cookiePairs = null;
				
				if (Cookie.read('quickpairs')) {
					var cookiePairs = Cookie.read('quickpairs');
				
					var arrPairs = cookiePairs.split('|');
					
					arrPairs.each(function(item, index) {
						
						arrCurrencies = item.split('_');
						
						arrCurrencies.each(function(item, index) {
							if (elem.hasClass(item)) {
								blnMatchFound = true;
							}
						});						
					});						
					
					if (blnMatchFound) {
						return true;
					}
				}			
				else {
					return false;
				}				
			}
		}
		else {
				return false;
		}
		
	},
	
	checkAllArticles : function() {
		
		var arrCurrArticles = articleFeed.container.getElements('div.article_block');
		
		arrCurrArticles.each(function(item,index) {
			if (!articleFeed.checkArticle(item)) {
				item.addClass('hidden');
			}
			else {
				item.removeClass('hidden');				
			}
		});
		
	},
	
	checkNewArticles : function(elem) {
				
		var blnVisible = articleFeed.checkArticle(elem);
		
		if (!blnVisible) {
			
			elem.addClass('hidden');
			
		} 
		
		return blnVisible;
		
	},
	
	togglePairs : function() {
		articleFeed.blnFilterPairs = !articleFeed.blnFilterPairs;
		articleFeed.checkAllArticles();
	},
	
	toggleWinnow : function(winnow) {
		articleFeed.currWinnow = winnow;
		articleFeed.checkAllArticles();		
	}
}		
