// AB testing - ujzp, 7/29/10, testing "sort options" feature on search results page
var AbTestingSortOptions = {
	initialize: function() {
		AbTestingSortOptions.testingSortOptions = $('div.testingSortOptions');

		AbTestingSortOptions.changeDefaultViewLength();
		AbTestingSortOptions.adjustSortDropDown;
	},

	adjustSortDropDown: function() {
		var dropDown = $('div.sortBar div#divMenuHide select');
		if(dropDown != null && dropDown.length) {
			dropDown[0].selectedIndex = 0;	// select "Best Match"
		}
	},

	changeDefaultViewLength: function() {
		if(AbTestingSortOptions.testingSortOptions.length) {
			var queryStringRedirect = null;

			switch(AbTestingSortOptions.testingSortOptions[0].id)
			{
				case 'natSortOriginal':
					queryStringRedirect = 'Ns=pSalesVolume|1';
					$(document).ready(AbTestingSortOptions.adjustSortDropDown);
					break;
				case 'natSortNewArrivalsThenTopSellers':
					queryStringRedirect = 'Ns=pNewArrivals|1||pSalesVolume|1';
					break;
				default:
					break;
			}

			if(queryStringRedirect && (window.location.href.match(/(&|\?)Ns=/i) == null || window.location.href.match(/(&|\?)Ns=/i).length == 0)) {
				// for debugging
				var tagLessUrl = window.location.href.match(/#.+/);
				if(tagLessUrl != null && tagLessUrl.length) {
					tagLessUrl = window.location.href.replace(tagLessUrl[0], '');
				}
				else {
					tagLessUrl = window.location.href;
				}

				window.location.href = tagLessUrl + ((tagLessUrl.indexOf('?') > 0) ? '&' : '?') + queryStringRedirect;
			}
		}
	}
};

AbTestingSortOptions.initialize();


