YAHOO.namespace('squarecompare');

YAHOO.squarecompare.initOnlineBackupDataTable = function(options) {

	var fields = [
	    {
	    	name: 'OnlineBackupProductId',
	    	internal: true
	    },
	    {
	    	name: 'MerchantSiteId',
	    	internal: true
	    },
	    {
	    	name: 'ContractMonths',
	    	internal: true
	    },
	    {
	    	name: 'MerchantSiteListingTypeId',
	    	internal: true
	    },
	    {
	    	name: 'EnhancedListingScore',
	    	internal: true
	    },
	    {
	    	name: 'FeePerClickTypeId',
	    	internal: true
	    },
	  	{
	   		name: 'Flagged',
	   		title: '<img src="/ext/silk/icons/flag_yellow.png" width="12" height="12">',
	   		width: 12,
	   		sortable: false,
	  		sizable: false,
	  		formatter: 'flagged'
	   	},
	    {
	   		name: 'MerchantSiteName',
	   		title: 'Company',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'merchantSiteName'
	   	},
	    {
	   		name: 'PlanName',
	   		title: 'Plan',
	   		group: 'basic',
	   		sortable: true,
	  		sizable: true,
	  		maxLength: 18,
	  		formatter: 'text'
	   	},
	 	{
	 		name: 'MonthlyFee',
	 		title: 'Monthly',
	   		group: 'basic',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'monthlyFee'
	 	},
	 	{
	 		name: 'SetupFee',
	 		title: 'Setup',
	   		group: 'basic',
	 		numberDecimals: 0,
	 		numberPrefix: '$',
	 		numberSuffix: '',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number'
	 	},
	 	{
	 		name: 'DiskSpaceGb',
	 		title: 'Space',
	   		group: 'basic',
	 		numberDecimals: 0,
	 		numberPrefix: '',
	 		numberSuffix: ' GB',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number',
	  		magicValues: {
	   			999999: 'Unlimited|Be sure to check the fine print to determine if restrictions apply to this <i>Unlimited</i> value.'
	   		}
	 	},
	 	{
	 		name: 'FreeTypeName',
	 		title: 'Trial',
	   		group: 'feature-a',
	   		sortable: true,
	  		sizable: true
	 	},
	 	{
	 		name: 'VersionCount',
	 		title: 'Versions',
	   		group: 'feature-a',
	 		numberDecimals: 0,
	 		numberPrefix: '',
	 		numberSuffix: '',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number',
	  		magicValues: {
	   			999999: 'Unlimited|Be sure to check the fine print to determine if restrictions apply to this <i>Unlimited</i> value.'
	   		}
	 	},
	 	{
	 		name: 'SearchFiles',
	 		title: 'Search Files',
	   		group: 'feature-a',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'WebAccess',
	 		title: 'Web Access',
	   		group: 'feature-a',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'FileSharing',
	 		title: 'File Sharing',
	   		group: 'feature-b',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'MultiComputer',
	 		title: 'Multi-Computer',
	   		group: 'feature-b',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'DataCompression',
	 		title: 'Compression',
	   		group: 'feature-b',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'TransferEncryption',
	 		title: 'Encryption',
	   		group: 'feature-b',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'OpenedFileBackup',
	 		title: 'Open Files',
	   		group: 'feature-c',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'ContinuousBackup',
	 		title: 'Continuous',
	   		group: 'feature-c',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'IncrementalBackup',
	 		title: 'Incremental',
	   		group: 'feature-c',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'BandwidthThrottling',
	 		title: 'Throttling',
	   		group: 'os',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'Windows',
	 		title: 'Windows Client',
	   		group: 'os',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'Unix',
	 		title: 'Linux Client',
	   		group: 'os',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	},
	 	{
	 		name: 'Mac',
	 		title: 'Mac Client',
	   		group: 'os',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'checked'
	 	}
	];

	var filterColumnInputs = YAHOO.util.Selector.query('#filter-columns input');

	var filterInputs = [
	    {
	    	paramName: 'monthlyFee',
	    	input: YAHOO.util.Selector.query('#filter-monthly-fee')[0]
	    },
	    {
	    	paramName: 'contractMonths',
	    	input: YAHOO.util.Selector.query('#filter-contract-months input')
	    },
	    {
	    	paramName: 'setupFee',
	    	input: YAHOO.util.Selector.query('#filter-setup-fee')[0]
	    },
	    {
	    	paramName: 'freeTypeIds',
	    	input: YAHOO.util.Selector.query('#filter-free-type-id input')
	    },
	    {
	    	paramName: 'diskSpaceGb',
	    	input: YAHOO.util.Selector.query('#filter-disk-space-gb')[0]
	    },
	    {
	    	paramName: 'versionCount',
	    	input: YAHOO.util.Selector.query('#filter-version-count')[0]
	    },
	    {
	    	paramName: 'searchFiles',
	    	input: YAHOO.util.Selector.query('#filter-search-files')[0]
	    },
	    {
	    	paramName: 'webAccess',
	    	input: YAHOO.util.Selector.query('#filter-web-access')[0]
	    },
	    {
	    	paramName: 'fileSharing',
	    	input: YAHOO.util.Selector.query('#filter-file-sharing')[0]
	    },
	    {
	    	paramName: 'multiComputer',
	    	input: YAHOO.util.Selector.query('#filter-multi-computer')[0]
	    },
	    {
	    	paramName: 'dataCompression',
	    	input: YAHOO.util.Selector.query('#filter-data-compression')[0]
	    },
	    {
	    	paramName: 'transferEncryption',
	    	input: YAHOO.util.Selector.query('#filter-transfer-encryption')[0]
	    },
	    {
	    	paramName: 'bandwidthThrottling',
	    	input: YAHOO.util.Selector.query('#filter-bandwidth-throttling')[0]
	    },
	    {
	    	paramName: 'openedFileBackup',
	    	input: YAHOO.util.Selector.query('#filter-opened-file-backup')[0]
	    },
	    {
	    	paramName: 'continuousBackup',
	    	input: YAHOO.util.Selector.query('#filter-continuous-backup')[0]
	    },
	    {
	    	paramName: 'incrementalBackup',
	    	input: YAHOO.util.Selector.query('#filter-incremental-backup')[0]
	    },
	    {
	    	paramName: 'windows',
	    	input: YAHOO.util.Selector.query('#filter-windows')[0]
	    },
	    {
	    	paramName: 'unix',
	    	input: YAHOO.util.Selector.query('#filter-unix')[0]
	    },
	    {
	    	paramName: 'mac',
	    	input: YAHOO.util.Selector.query('#filter-mac')[0]
	    }
	];

	var dataTableOptions = YAHOO.lang.merge(options, {
		keyFieldName: 'OnlineBackupProductId',
		fields: fields,
		initialGroup: 'basic',
		filterInputs: filterInputs,
		defaultSort: {key:'MonthlyFee', dir: YAHOO.widget.DataTable.CLASS_ASC},
		customFormatters: {
			monthlyFee: function(elLiner, oRecord, oColumn, oData) {
	    		var monthlyFee = oRecord.getData('MonthlyFee');
		    	var contractMonths = oRecord.getData('ContractMonths');

		    	var value = YAHOO.squarecompare.formatCurrency(monthlyFee, 2) + '&#47;mo (' + contractMonths + ')';
		    	elLiner.innerHTML = value;
				YAHOO.util.Dom.setStyle(elLiner, 'text-align', 'right');

		    	var toolTipText = YAHOO.squarecompare.formatCurrency(monthlyFee, 2) + ' per month with a ' + contractMonths + ' month commitment or prepayment';
				YAHOO.util.Dom.addClass(elLiner, 'info-icon');
				elLiner.setAttribute('title', toolTipText);
		    }
		},
		getDataStep: 'get-online-backup-plans'
	});
	var dataTable = YAHOO.squarecompare.DataTable(dataTableOptions);

	// Listen for column group changes
    for (var i=0; i<filterColumnInputs.length; i++) {
		YAHOO.util.Event.addListener(filterColumnInputs[i], 'click', function(e) {
			dataTable.showColumnGroup(this.value);
		});
    }
};
