/********************************************************************

	FOREX.COM - QUICK PAIRS
	
	=========================================
	Author: Chris Erwin (chris@teehanlax.com)
	Agency: teehan+lax
	Date: July 30, 2008
	=========================================
	
	
	Public Methods
	-------------------------------------------------------------
	initialize (constructor)
		- Arguments:
			- maxPairs (integer): the maximum number of the quick pairs you can choose.
		- Returns: null
	
	parseCookie
		- Description: this grabs the cookie and parses the id's into an array
		- Arguments: none
		- Returns: null
		
	saveCookie
		- Description: this iterates through the quickpairs array and builds the string which gets stored in the cookie
		- Arguments: none
		- Returns: null
	
	loadQuickPairs 
		- Description: this iterates throught the quickparis array and calls the show method for each one, and then calls the checkMaxPairs method at the end
		- Arguments:
		- Returns:
	
	togglePair
		- Description: this show/hides the selected quickpair, adds or removes it from the array, calls saveCookie, and calls checkMaxPairs
		- Arguments:
		- Returns:
	
	showPair
		- Description: displays the link in the quickpairs nav (<ul id="quick_pair_links"> inside the "nav_bar" div), and sets the check box to checked
		- Arguments:
		- Returns:
	
	hidePair
		- Description: hides the link in the quickpairs nav (<ul id="quick_pair_links"> inside the "nav_bar" div), and unchecks the check box
		- Arguments:
		- Returns:
	
	checkMaxPairs
		- Description: checks to see if the number of quickpairs in the array exceeds the maximum number defined when instansiating
		- Arguments:
		- Returns:
		
	toggleChooser
		- Description: this simply opens & closes the quick pairs chooser
		- Arguments:
		- Returns:
		
		
	Quick Pairs Cookies
	-------------------------------------------------------------
	The quickpairs cookie stores the id's of the quickpairs the user has chosen. The id's are stored in a single string seperated by the vertical bar character '|'.
	When the cookie is saved it is saved for 14 days
	
	ie: quickpairs = usd_cad|chf_jpy|gbp_chf|jpy_usd
	
	
	HTML Files
	-------------------------------------------------------------
	See the /includes/quick_pairs_nav.shtml file for the quick pairs chooser
	See the /includes/nav_bar.shtml file for the nav
	
	
	CSS Files
	-------------------------------------------------------------
	all styles for the quickpairs nave and the quickpairs chooser reside in the global css file /resources/css/structure.css
	
	
	Dependencies
	-------------------------------------------------------------
	- MooTools 1.2
	
*********************************************************************/

var QuickPairs = new Class({
    initialize: function(maxPairs){	
		this.maxPairs = (maxPairs == null) ? 6 : maxPairs;
		
		this.quickPairsChooser = $('quick_pairs_chooser');
		this.arrCheckboxes = this.quickPairsChooser.getElements('.pairs_check');
		
		this.arrQuickPairs = [];
		
		// animation properties
		this.animation = new Fx.Tween(this.quickPairsChooser, {duration: 500, transition: Fx.Transitions.Quad.easeOut});
		this.currAnimating = false;
		this.openHeight = 145;
		this.openState = false;
		
		// clear all check boxes
		for (var i=0, arrCheckboxesLen=this.arrCheckboxes.length; i<arrCheckboxesLen; i++) {
			this.arrCheckboxes[i].checked = false;
		}
		
		// parse cookie and load pairs
		this.parseCookie();
		this.loadQuickPairs();
		
		// add event listeners
		$('edit_quick_pairs_link').addEvent('click', this.toggleChooser.bind(this));
		$('quick_pairs_close_link').addEvent('click', this.toggleChooser.bind(this));
		this.arrCheckboxes.addEvent('click', this.togglePair.bind(this));
    },
	
	parseCookie : function() {
		var quickPairsCookie = Cookie.read('quickpairs');
		if (quickPairsCookie) {
			this.arrQuickPairs = quickPairsCookie.split('|');
		}
		//console.log(this.arrQuickPairs);
	},
	
	saveCookie : function() {
		var cookieString = '';
		for (var i=0, arrQuickPairLen=this.arrQuickPairs.length; i<arrQuickPairLen; i++) {
			cookieString = cookieString + this.arrQuickPairs[i] + '|';	
		}
		cookieString = cookieString.substring(0, (cookieString.length - 1))
		var quickpairs = Cookie.write('quickpairs', cookieString, {duration: 14});
		//console.log(cookieString)
	},
	
	loadQuickPairs : function() {		
		for (var i=0, arrQuickPairLen=this.arrQuickPairs.length; i<arrQuickPairLen; i++) {
			var pairID = this.arrQuickPairs[i].toLowerCase();
			this.showPair(pairID);
		}
		this.checkMaxPairs();
	},
	
	togglePair : function(e) {
		var checkedElement = e.target;
		var checkedID = checkedElement.id;
		var pairID = checkedID.replace('_check', '');
		
		if (checkedElement.checked) {
			this.arrQuickPairs.include(pairID.toUpperCase());
			this.showPair(pairID);
		}
		else {
			this.arrQuickPairs.erase(pairID.toUpperCase());
			this.hidePair(pairID);
		}
		
		this.checkMaxPairs();
		this.saveCookie();
	},
		
	showPair : function(pairID) {
		var pairLinkID =  'link_' + pairID;
		var pairLink = $(pairLinkID);
		var pairCheckbox = $(pairID + '_check');
		
		if(pairLink) {
			pairLink.inject('quick_pair_links', 'bottom');
			pairLink.setStyle('display', 'block');
			pairCheckbox.checked = true;
		}
	},
	
	hidePair : function(pairID) {
		var pairLinkID =  'link_' + pairID;
		var pairLink = $(pairLinkID);
		var pairCheckbox = $(pairID + '_check');
		
		if(pairLink) {
			pairLink.setStyle('display', 'none');
			pairCheckbox.checked = false;
		}
	},
	
	checkMaxPairs : function() {
		var blnDisabled = false;
		
		//console.log('max pairs');
		
		if(this.arrQuickPairs.length >= this.maxPairs) {
			blnDisabled = true;
		}
		
		for (var i=0, arrCheckboxesLen=this.arrCheckboxes.length; i<arrCheckboxesLen; i++) {
			if (! this.arrCheckboxes[i].checked ) {
				this.arrCheckboxes[i].disabled = blnDisabled;
			}
		}
	},
	
	toggleChooser :function() {		
		if(this.openState) {
			this.animation.start('height', 0).chain(
				function(){ this.openState = false; this.animation.callChain(); }.bind(this),
				function(){ $('edit_quick_pairs_link').getParent().removeClass('open'); this.animation.callChain(); }.bind(this)
			);
		}
		else {
			this.animation.start('height', this.openHeight).chain(
				function(){ this.openState = true; }.bind(this)
			);
			 $('edit_quick_pairs_link').getParent().addClass('open');
		}
		
		return false;
	}
});

