var CompareCards = function(){
	var this_CompareCards = this;

	this.table = $('.compare-table');
	
	this.config = {
		defaultState: false,
		stateCookie: 'compare_cards_state',
		compareCookie: 'compare_cards_compared',
		cookieParams: {
			domain: '.capitalone.ca'
		},
		minCards: 2,
		expandText: function(input){
			return 'See ' + input.hiddenCount + ' more ' + input.section.capitalize() + ' Credit Card' + (input.hiddenCount > 1 ? 's' : '');
		},
		expandTitle: function(input){
			return 'Currently showing Top ' + (input.cardCount - input.hiddenCount) + ' ' + input.section.capitalize() + ' Credit Cards';
		},
		collapseText: function(input){
			return 'See Top ' + (input.cardCount - input.hiddenCount) + ' ' + input.section.capitalize() + ' Credit Card' + (input.cardCount - input.hiddenCount > 1 ? 's' : '');
		},
		collapseTitle: function(input){
			return 'Currently showing all ' + input.section.capitalize() + ' Credit Cards';
		}
	};
	
	this.populateTableSections = function(){
		var sectionsArray = $('.compare-table-section',this.table);
		sectionsArray.each(
			function(){
				var cardsArray = $('.compare-table-section-card',this);
				$(this).data('cards',cardsArray);
				$(this).data('hide-cards',cardsArray.filter('.hide-card'));
				$(this).data('name',$(this).attr('section'));
			}
		);
		this.table.data('sections',sectionsArray);
	}
	
	this.createCollapsibleLinks = function(){
		this.table.data('sections').each(
			function(){
				var this_section = $(this);
				var collapseAreaLink = $('.expand-collapse-area-link',this);
				

				var expand = function(){
					this_section.data('state',true);
					var textOptions = {
						cardCount:		this_section.data('cards').length,
						hiddenCount:	this_section.data('hide-cards').length,
						section:		this_section.data('name')
					};
					$(collapseAreaLink).html(
						this_CompareCards.config.collapseText(textOptions)
					).attr('title',this_CompareCards.config.collapseTitle(textOptions));
					this_section.data('hide-cards').show();
					collapseAreaLink.addClass('expanded');
				}
				
				var collapse = function(){
					this_section.data('state',false);
					var textOptions = {
						cardCount:		this_section.data('cards').length,
						hiddenCount:	this_section.data('hide-cards').length,
						section:		this_section.data('name')
					};
					$(collapseAreaLink).html(
						this_CompareCards.config.expandText(textOptions)
					).attr('title',this_CompareCards.config.expandTitle(textOptions));
					this_section.data('hide-cards').hide();
					collapseAreaLink.removeClass('expanded');
				}
				
				var toggle = function(event){
					if(this_section.data('state')){
						collapse();
					}
					else {
						expand();
					}
					this_CompareCards.save();
				}
				
				this_section.data('collapse',collapse);
				this_section.data('expand',expand);
				this_section.data('toggle',toggle);
				collapseAreaLink.parent().click(toggle);
				collapseAreaLink.html('Click Me!');
			}
		);
	}
	
	this.initialize = function(){
		this.populateTableSections();
		this.createCollapsibleLinks();
		this.restore();
		this.setupFormSubmit();
		$('.compareCheckbox').customCheckbox();
		$('.cardTypeDropdown').selectbox();
		$('.compareButton').cluetip(
			$.extend({},tooltipDefaults,
				{
					activation: false
				}
			)
		);
	}
	
	this.setupFormSubmit = function(){
		$('.compareButton').click(
			function(){
				if($('.compareCheckbox:checked').length < this_CompareCards.config.minCards){
					$(this).trigger('showCluetip');
					return false;
				}
				else {
					var values = new Array;
					$('input.compareCheckbox:checked').each(function(){
						values.push($(this).val());
					});
					$.cookie(this_CompareCards.config.compareCookie,values.join('|'),this_CompareCards.config.cookieParams);
				}
				return true;
			}
		);
	}
	
	this.restore = function(){
		var state = this_CompareCards.config.defaultState;
		var savedState = $.cookie(this.config.stateCookie);
		if(savedState != null){
			state = savedState.fromQuery();
		}
		
		this.table.data('sections').each(
			function(){
				if(
					(typeof state == 'object' && state[$(this).data('name')]) == 'true' ||
					(typeof state == 'boolean' && state)
				){
					$(this).data('expand')();
				}
				else {
					$(this).data('collapse')()
				}
			}
		);
		
		var comparingCards = $.cookie(this.config.compareCookie);
		if(comparingCards != null){
			comparingCards = comparingCards.split('|');
			for(var index in comparingCards){
				$('#compareCheckbox_' + comparingCards[index]).attr('checked',true);
			}
		}
	}
	
	this.save = function(){
		var state = {};
		this.table.data('sections').each(
			function(){
				state[$(this).data('name')] = $(this).data('state');
			}
		);
		$.cookie(this_CompareCards.config.stateCookie,jQuery.param(state),this.config.cookieParams);
	}
	
	this.initialize();
}

$(function(){
	new CompareCards;
});
