﻿
/*******************************************************************
/* Gets the initial price-min/price max and configures the slider  *
/******************************************************************/
function SetUpSliderForFirstUse() {
	
	prices_array = ConfigurePriceArray($('select[id$="MinimumPrice"] option'), $('select[id$="MaximumPrice"] option')); // Set up the prices array based off the initial minimum price dropdown choices
	// This is because the step size will need to change within the slider

	PriceMin = ($('select[id$="MinimumPrice"] option:selected')[0].text == "Min") ? 0 : $('select[id$="MinimumPrice"] option:selected').val();                                                         // The current value of the minimum price drop down
	PriceMax = ($('select[id$="MaximumPrice"] option:selected')[0].text == "Max") ? $('select[id$="MinimumPrice"] option:last-child').val() : $('select[id$="MaximumPrice"] option:selected').val();   // The current value of the maximum price drop down
	PriceActualMin = GetRealSliderValue(PriceMin);
	PriceActualMax = GetRealSliderValue(PriceMax);

	// only set up the slider if we have enough elements
	if (prices_array.length >= 3)
	{
		SetUpSlider();
	}
	else
	{
		if ($("#pmin").length > 0) $("#pmin").hide();
		if ($("#pmax").length > 0) $("#pmax").hide();
		if ($("#slider-range").length > 0)
		{
			// if we have a slider-range element
			// replace the background image to the 'greyed' out version
			$("#slider-range").replaceWith("<div id=\"disabledslider\"><img style=\"width:176px\" id=\"disabledsliderimg\" src=\"/assets/hendy/images/slider-inactive.png\" /></div>");


			if ($("div.minimumprice").length > 0 ) $("div.minimumprice").hide();
			if ($("div.maximumprice").length > 0 ) $("div.maximumprice").hide();
		}
	}
}

/******************************************
/* Configure the price array that we have *
/******************************************/
function ConfigurePriceArray(MinDropDown, MaxDropDown) {

	var mprice = new Array();
	var mxprice = new Array();
	mprice = GetPrices(MinDropDown);
	mxprice = GetPrices(MaxDropDown);

	var retval = uniqueMerge(mprice, mxprice);

	return retval;
}

/************************************************
*  Configures the slider using values from the drop down lsit
************************************************/
function SetUpSlider() {

	$("#slider-range").slider({
		range: true,
		min: 0,
		max: prices_array.length - 1,
		values: [PriceActualMin, PriceActualMax],
		step: 1,
		slide: function (event, ui) {
			// Call the slide changing function
			SlideChanging(event, ui);
		}

	});


	SetInitialStyling();

}

/**********************************************
*  Gets the prices from the drop down list 
*  and specifies the value of an array with them
**********************************************/
function GetPrices(DropDown) {
	var retval = new Array();
	var cnt = 0;
	var firsttime = true;
	$(DropDown).each(function () {
		// Never add the first item as this will always be 'min' or 'max' rather than a real value
		if (!firsttime) {
			retval[cnt] = $(this).val();
			cnt++;
		}
		firsttime = false;
	})
	return retval;
}

/***********************************
* This function keeps the slider steps up to date and sets the visual display 
* and drop down list selected item         
***********************************/
function SlideChanging(event, ui) {

	// Set which value has changed (0 for left, 1 for right)
	var currentHandle = $(".ui-slider-handle").index(ui.handle);

	// First check to make sure that the two values aren't the same
	if (ui.values[0] == ui.values[1]) {
		// stop this happening
		ui.values[currentHandle] = (currentHandle == 0) ? ui.values[currentHandle] - 1 : ui.values[currentHandle] + 1;
	}
	else {

		// Set the min / max price values depending on which handle was used to move the range indicator
		if (currentHandle == 0) {
			PriceMin = (isNaN(prices_array[ui.values[0]])) ? 0 : prices_array[ui.values[0]];
		}
		else {
			PriceMax = (isNaN(prices_array[ui.values[1]])) ? $('select[id$="MinimumPrice"] option:last-child').val() : prices_array[ui.values[1]];
		}

		// Reset the amount text box to match the new value
		$("#pmin").text('£' + PriceMin);
		$("#pmax").text('£' + PriceMax);

		// Make the drop down selected value match that which was slid to
		$('select[id$="MinimumPrice"]').val(PriceMin);
		$('select[id$="MaximumPrice"]').val(PriceMax);

	}
}



/****************************************/
/* Function to merge min and			* 
/* max array without adding duplicates  *
/* This array will be sorted            *
/****************************************/
function uniqueMerge(a, b) {
	for (var nonDuplicates = [], i = 0, l = a.length; i < l; ++i) {
		if (b.indexOf(a[i]) == -1) {
			nonDuplicates.push(a[i]);
		}
	}
	return b.concat(nonDuplicates).sort(compareNumbers);
};

/****************************/
/* Used for sorting numbers *
/****************************/
function compareNumbers(a, b) {
	return a - b;
}


/***********************************************
*   Sets the initial styling of the slider
************************************************/
function SetInitialStyling() {
	// Set the initial amount to display in the text boxes and if necessary hide the drop downs
	$("#pmin").text('£' + PriceMin);
	$("#pmax").text('£' + PriceMax);

	$("div.minimumprice").hide();
	$("div.maximumprice").hide();

}

/***********************************************
*   Sets the actual value of the slider
************************************************/
function GetRealSliderValue(pricevalue) {
	var retval = 0;
	var index = prices_array.indexOf(pricevalue);
	if (index != -1) {
		retval = index;
	}
	else {
		retval = 0;
	}
	return retval;
}

$(document).ready(function () {

	/******************************************************************
	/* ie index of fix												  *
	/******************************************************************/
	if (!Array.indexOf) {
		Array.prototype.indexOf = function (obj) {
			for (var i = 0; i < this.length; i++) {
				if (this[i] == obj) {
					return i;
				}
			}
			return -1;
		}
	}

	var prices_array;
	var PriceMin;
	var PriceMax;
	var PriceActualMin;
	var PriceActualMax;

	SetUpSliderForFirstUse();

	
});
