YAHOO.namespace('squarecompare');

YAHOO.squarecompare.initVpsHostingDataTable = function(options) {

	var fields = [
	    {
	    	name: 'VpsHostingProductId',
	    	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: 'Host',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'merchantSiteName'
	   	},
	    {
	   		name: 'PlanName',
	   		title: 'Plan',
	   		sortable: true,
	  		sizable: true
	   	},
    	{
	   		name: 'DiskspaceGb',
	   		title: 'Disk',
	   		group: 'basic',
	 		numberDecimals: 2,
	 		numberPrefix: '',
	 		numberSuffix: ' GB',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number',
	  		magicValues: {
	   			999999.0: 'Unlimited|Be sure to check the fine print to determine if restrictions apply to this <i>Unlimited</i> value.'
	   		}
	   	},
	 	{
	 		name: 'TransferGb',
	 		title: 'Transfer',
	   		group: 'basic',
	 		numberDecimals: 2,
	 		numberPrefix: '',
	 		numberSuffix: ' GB',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number',
	  		magicValues: {
	   			999999.0: 'Unlimited|Be sure to check the fine print to determine if restrictions apply to this <i>Unlimited</i> value.'
	   		}
	 	},
	 	{
	 		name: 'MonthlyFee',
	 		title: 'Monthly',
	   		group: 'basic',
	 		numberDecimals: 2,
	 		numberPrefix: '$',
	 		numberSuffix: '',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'monthlyFee'
	 	},
	 	{
	 		name: 'SetupFee',
	 		title: 'Setup',
	   		group: 'basic',
	 		numberDecimals: 2,
	 		numberPrefix: '$',
	 		numberSuffix: '',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number'
	 	},
	 	{
	 		name: 'GuaranteedRamGb',
	 		title: 'Guaranteed RAM',
	   		group: 'advanced',
	 		numberDecimals: 2,
	 		numberPrefix: '',
	 		numberSuffix: ' GB',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number'
	 	},
	 	{
	 		name: 'BurstableRamGb',
	 		title: 'Burstable RAM',
	   		group: 'advanced',
	 		numberDecimals: 2,
	 		numberPrefix: '',
	 		numberSuffix: ' GB',
	   		sortable: true,
	  		sizable: true,
	  		formatter: 'number'
	 	},
	 	{
	 		name: 'DedicatedIpCount',
	 		title: 'IPs',
	   		group: 'advanced',
	 		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.'
	   		}
	 	}
	];

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

	var filterInputs = [
	    {
	    	paramName: 'diskspaceGb',
	    	input: YAHOO.util.Selector.query('#filter-diskspace-gb')[0]
	    },
	    {
	    	paramName: 'transferGb',
	    	input: YAHOO.util.Selector.query('#filter-transfer-gb')[0]
	    },
	    {
	    	paramName: 'dedicatedIpCount',
	    	input: YAHOO.util.Selector.query('#filter-ips-count')[0]
	    },
	    {
	    	paramName: 'guaranteedRamGb',
	    	input: YAHOO.util.Selector.query('#filter-guaranteed-ram-gb')[0]
	    },
	    {
	    	paramName: 'burstableRamGb',
	    	input: YAHOO.util.Selector.query('#filter-burstable-ram-gb')[0]
	    },
	    {
	    	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: 'windowsVista',
	    	input: YAHOO.util.Selector.query('#filter-os-windows-vista')[0]
	    },
	    {
	    	paramName: 'windows2008',
	    	input: YAHOO.util.Selector.query('#filter-os-windows-2008')[0]
	    },
	    {
	    	paramName: 'windows2003',
	    	input: YAHOO.util.Selector.query('#filter-os-windows-2003')[0]
	    },
	    {
	    	paramName: 'windowsXp',
	    	input: YAHOO.util.Selector.query('#filter-os-windows-xp')[0]
	    },
	    {
	    	paramName: 'centOs',
	    	input: YAHOO.util.Selector.query('#filter-os-cent-os')[0]
	    },
	    {
	    	paramName: 'ubuntu',
	    	input: YAHOO.util.Selector.query('#filter-os-ubuntu')[0]
	    },
	    {
	    	paramName: 'redhat',
	    	input: YAHOO.util.Selector.query('#filter-os-red-hat')[0]
	    },
	    {
	    	paramName: 'arch',
	    	input: YAHOO.util.Selector.query('#filter-os-arch')[0]
	    },
	    {
	    	paramName: 'debian',
	    	input: YAHOO.util.Selector.query('#filter-os-debian')[0]
	    },
	    {
	    	paramName: 'gentoo',
	    	input: YAHOO.util.Selector.query('#filter-os-gentoo')[0]
	    },
	    {
	    	paramName: 'slackware',
	    	input: YAHOO.util.Selector.query('#filter-os-slackware')[0]
	    },
	    {
	    	paramName: 'mandrake',
	    	input: YAHOO.util.Selector.query('#filter-os-mandrake')[0]
	    },
	    {
	    	paramName: 'freeBsd',
	    	input: YAHOO.util.Selector.query('#filter-os-free-bsd')[0]
	    },
	    {
	    	paramName: 'openBsd',
	    	input: YAHOO.util.Selector.query('#filter-os-open-bsd')[0]
	    },
	    {
	    	paramName: 'solaris',
	    	input: YAHOO.util.Selector.query('#filter-os-solaris')[0]
	    },
	    {
	    	paramName: 'flagged',
	    	input: YAHOO.util.Selector.query('#filter-flagged input')
	    }
	];

	var dataTableOptions = YAHOO.lang.merge(options, {
		keyFieldName: 'VpsHostingProductId',
		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-vps-hosting-plans'
	});
	var dataTable = YAHOO.squarecompare.DataTable(dataTableOptions);

	// Check all Fixed Term Types
	var osCheckAll = YAHOO.util.Selector.query('#filter-os-check-all')[0];
	YAHOO.util.Event.addListener(osCheckAll, 'click', function(e) {
		YAHOO.util.Event.stopEvent(e);
		for (var i=0; i<filterOsInputs.length; i++) {
			filterOsInputs[i].checked = true;
		}
		dataTable.requery();
	});

	// Uncheck all Fixed Term Types
	var osUncheckAll = YAHOO.util.Selector.query('#filter-os-uncheck-all')[0];
	YAHOO.util.Event.addListener(osUncheckAll, 'click', function(e) {
		YAHOO.util.Event.stopEvent(e);
		for (var i=0; i<filterOsInputs.length; i++) {
			filterOsInputs[i].checked = false;
		}
		dataTable.requery();
	});

	// 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);
		});
    }

}
