var MsdsTechData = Class.create();
MsdsTechData.prototype = {

// URL requested to pull DuSpec product data.
AJAX_URL : '../ajax/duspecProducts.do',

productsArrayByCode : new Array(),
productsArrayByName : new Array(),

currentSortBy : 'code',

form                   : $( 'prodDataForm' ),
documentType_msds      : $( 'prodDataDocumentType_msds' ),
documentType_datasheet : $( 'prodDataDocumentType_datasheet' ),
productCode            : $( 'prodDataProductCode' ),
dataSort_code          : $( 'prodDataSort_code' ),
dataSort_name          : $( 'prodDataSort_name' ),
submit                 : $( 'prodDataSubmit' ),

initialize : function() {

	// Give the sort radio buttons their function.
	Event.observe( this.dataSort_code, 'click', this.sortProducts.bind( this, 'code' ), false );
	Event.observe( this.dataSort_name, 'click', this.sortProducts.bind( this, 'name' ), false );

	// Give the submit button its function.
	Event.observe( this.submit, 'click', this.onSubmit.bind( this ), false );

	// Polite load the products list.
	Event.observe( window, 'load', this.loadProducts.bind( this ), false );

}, // End initialize().

loadProducts : function() {

	var thisObj = this;
	new Ajax.Request( this.AJAX_URL, {
		method : 'get',
		onSuccess : function( transport ) {
			thisObj.onProductsAjaxComplete( transport );
		}
	});

}, // End loadProducts().

onProductsAjaxComplete : function( transport ) {

	var products = transport.responseText.evalJSON();	

	// Initialize and clear the lists.
	var productsByCode = new Array();
	var productsByName = new Array();

	// Perform a deep copy of the products array (do not use prototype's clone method).
	// Also create a label for each product.
	products.each(
		function( product ) {

			var codeProduct = {
				code : product.code,
				name : product.name,
				label : product.code + ' (' + product.name + ')'
			};
			productsByCode[ productsByCode.length ] = codeProduct;

			var nameProduct = {
				code : product.code,
				name : product.name,
				label : product.name + ' (' + product.code + ')'
			};
			productsByName[ productsByName.length ] = nameProduct;

		}
	);

	// Sort each product array -- one by code, one by name.
	productsByCode.sort(
		function( a, b ) {
			if ( a.code < b.code ) return -1;
			if ( a.code > b.code ) return 1;
			return 0;
		}
	);
	productsByName.sort(
		function( a, b ) {
			if ( a.name < b.name ) return -1;
			if ( a.name > b.name ) return 1;
			return 0;
		}
	);

	this.productsArrayByCode = productsByCode;
	this.productsArrayByName = productsByName;

	this.update();

}, // End setProducts().

sortProducts : function( sortBy ) {

	this.currentSortBy = sortBy;
	this.update();

}, // End sortProducts().

update : function() {

	// Clear the list.
	this.clearOptions( this.productCode );

	// Determine which array to load into the list.
	var arr = ( this.currentSortBy == 'name' ) ? this.productsArrayByName : this.productsArrayByCode;

	// Add the array's items to the list.
	for ( var i = 0; i < arr.length; i++ ) {
		var p = arr[ i ];
		this.addOption( this.productCode, p.label, p.code );
	} // End for.

}, // End update().

clearOptions : function( selectObject ) {

	if ( !selectObject ) return;
	while ( selectObject.options.length > 0 ) {
		selectObject.remove( 0 );
	} // End while.

}, // End clearOptions().

addOption : function( selectObject, name, value ) {

	if ( !selectObject ) return;
	selectObject.options[ selectObject.options.length ] = new Option( name, value );

}, // End addOption().

onSubmit : function() {

	if ( this.productCode.selectedIndex < 0 ) {
		alert( 'Please select a product.' );

	} else {

		// Track which document is selected.
		var documentType = this.form.serialize().toQueryParams()[ 'documentType' ];
		var prodNum = this.productCode.options[ this.productCode.selectedIndex ].value;
		pageTracker._trackPageview( '/products/tracking/' + documentType + '/' + prodNum );

		// Submit the form.
		this.form.submit();

	} // End if.

} // End onSubmit().

} // End class MsdsTechData.

new MsdsTechData();
