var mattyParams = new Hash;				
var tmp_url_array = new Array;
var query_string = "";
var main_url = "http://www.garden-fountains.com/Catalog.bok?";

var slider_object = new Object();

slider_object['Price'] = new Bs_Slider();
slider_object['Price'].name = "Price";
slider_object['Price'].minHtmlVal = "priceLow";
slider_object['Price'].maxHtmlVal = "priceHigh";

slider_object['Height'] = new Bs_Slider();
slider_object['Height'].name = "Height";
slider_object['Height'].minHtmlVal = "heightLow";
slider_object['Height'].maxHtmlVal = "heightHigh";

slider_object['Width'] = new Bs_Slider();
slider_object['Width'].name = "Width";
slider_object['Width'].minHtmlVal = "widthLow";
slider_object['Width'].maxHtmlVal = "widthHigh";

slider_object['Depth'] = new Bs_Slider();
slider_object['Depth'].name = "Depth";
slider_object['Depth'].minHtmlVal = "depthLow";
slider_object['Depth'].maxHtmlVal = "depthHigh";

slider_object['Material'] = new Bs_Slider();
slider_object['Material'].name = "Material";
slider_object['Material'].minHtmlVal = "material";

slider_object['ShipsVia'] = new Bs_Slider();
slider_object['ShipsVia'].name = "ShipsVia";
slider_object['ShipsVia'].minHtmlVal = "shipsVia";

slider_object['Noise'] = new Bs_Slider();
slider_object['Noise'].name = "Noise";
slider_object['Noise'].minHtmlVal = "noise";

slider_object['Location'] = new Bs_Slider();
slider_object['Location'].name = "Location";
slider_object['Location'].minHtmlVal = "location";

slider_object['MadeIn'] = new Bs_Slider();
slider_object['MadeIn'].name = "MadeIn";
slider_object['MadeIn'].minHtmlVal = "madeIn";

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}


function parseUrl(){
	mattyParams['category'] = 'All';
	mattyParams['num'] = 10;
	mattyParams['page'] = 1;
	mattyParams['priceLow'] = 0;
	mattyParams['priceHigh'] = 99999;
	mattyParams['sort'] = 1;
	mattyParams['heightLow'] = 0;
	mattyParams['heightHigh'] = 99999;
	mattyParams['widthLow'] = 0;
	mattyParams['widthHigh'] = 99999;
	mattyParams['depthLow'] = 0;
	mattyParams['depthHigh'] = 99999;
	mattyParams['style'] = '';
	mattyParams['material'] = '';
	mattyParams['shipsVia'] = '';
	mattyParams['noise'] = '';
	mattyParams['location'] = '';
	mattyParams['madeIn'] = '';
	mattyParams['start'] = '';
	
	query_string = new Querystring();

	if(query_string.get('category') != '' && query_string.get('category') != null){
		mattyParams['category'] = query_string.get('category');
	}
	if(query_string.get('num') != '' && query_string.get('num') != null){
		mattyParams['num'] = query_string.get('num');
	}
	if(query_string.get('page') != '' && query_string.get('page') != null){
		mattyParams['page'] = query_string.get('page');
	}
	if(query_string.get('priceLow') != '' && query_string.get('priceLow') != null){
		mattyParams['priceLow'] = query_string.get('priceLow');
	}
	if(query_string.get('priceHigh') != '' && query_string.get('priceHigh') != null){
		mattyParams['priceHigh'] = query_string.get('priceHigh');
	}
	if(query_string.get('sort') != '' && query_string.get('sort') != null){
		mattyParams['sort'] = query_string.get('sort');
	}
	if(query_string.get('heightLow') != '' && query_string.get('heightLow') != null){
		mattyParams['heightLow'] = query_string.get('heightLow');
	}
	if(query_string.get('heightHigh') != '' && query_string.get('heightHigh') != null){
		mattyParams['heightHigh'] = query_string.get('heightHigh');
	}
	if(query_string.get('depthLow') != '' && query_string.get('depthLow') != null){
		mattyParams['depthLow'] = query_string.get('depthLow');
	}
	if(query_string.get('depthHigh') != '' && query_string.get('depthHigh') != null){
		mattyParams['depthHigh'] = query_string.get('depthHigh');
	}
	if(query_string.get('widthLow') != '' && query_string.get('widthLow') != null){
		mattyParams['widthLow'] = query_string.get('widthLow');
	}
	if(query_string.get('widthHigh') != '' && query_string.get('widthHigh') != null){
		mattyParams['widthHigh'] = query_string.get('widthHigh');
	}
	if(query_string.get('style') != '' && query_string.get('style') != null){
		mattyParams['style'] = query_string.get('style');
	}
	if(query_string.get('material') != '' && query_string.get('material') != null){
		mattyParams['material'] = query_string.get('material');
	}
	if(query_string.get('shipsVia') != '' && query_string.get('shipsVia') != null){
		mattyParams['shipsVia'] = query_string.get('shipsVia');
	}
	if(query_string.get('noise') != '' && query_string.get('noise') != null){
		mattyParams['noise'] = query_string.get('noise');
	}
	if(query_string.get('location') != '' && query_string.get('location') != null){
		mattyParams['location'] = query_string.get('location');
	}
	if(query_string.get('madeIn') != '' && query_string.get('madeIn') != null){
		mattyParams['madeIn'] = query_string.get('madeIn');
	}
	if(query_string.get('start') != '' && query_string.get('start') != null){
		mattyParams['start'] = query_string.get('start');
	}
}


var Site = new Class({
	initialize: function(){
		this.panel_functions();
	},
	panel_functions: function(){
	
		/*** Clear and Go Buttons ***/
		var matty_clear_and_go = $$('#mattySubmit img');
		matty_clear_and_go.each(function(actual_element, index_element){
			actual_element.addEvent('click', function(e){
				if (index_element == 0) {
					window.location.href = "/Catalog.bok";
				}
				else {
					processRun();
				}
			});
		});

	
	
		/*** Pages ***/
		var matty_pages_select = $$('#mattyPagesFakeSelectWrapper div');
		matty_pages_select.each(function(actual_element, index_element){
			actual_element.addEvent('click', function(e){
				var tmp_pages_val = actual_element.title.split('-');
				mattyParams['num'] = tmp_pages_val[1];
				mattyParams['start'] = tmp_pages_val[0];
				processRun2();
			});
		});


		/*** Sort ***/
		var matty_sort_select = $$('#mattySortFakeSelectWrapper div');
		matty_sort_select.each(function(actual_element, index_element){
			actual_element.addEvent('click', function(e){
				mattyParams['sort'] = actual_element.title;
				processRun();
			});
		});

		
		/*** Style ***/
		var matty_style_checks = $$('#mattyStyleChecksWrapper input');
		matty_style_checks.addEvent('change', function(e){
			var tmp_style_array = new Array();
			
			matty_style_checks.each(function(actual_element, index_element){
				if(actual_element.checked){
					tmp_style_array.push(actual_element.value);
				}
			});
			if(tmp_style_array.length == 1){
				mattyParams['style'] = tmp_style_array[0];
			}
			else{
				mattyParams['style'] = tmp_style_array.join("-");
			}
			//processRun();
		});
	}
});

function processRun(){
	mattyParams['page'] = 1;
	mattyParams['start'] = 1;
	processRun2();
}

function processRun2(){
	full_url = main_url + mattyParams.toQueryString();
	window.location.href = full_url;
}

function afterAjaxFunctions(){
	for(var sliderItemName in slider_object){
		sliderItem = slider_object[sliderItemName];
		
		createSlider(sliderItem, 80, 160, parseInt($('matty' + sliderItem.name + 'SliderdefaultMin').innerHTML), parseInt($('matty' + sliderItem.name + 'SliderdefaultMax').innerHTML), parseInt($('matty' + sliderItem.name + 'SlideractualMin').innerHTML), parseInt($('matty' + sliderItem.name + 'SlideractualMax').innerHTML));
	}

	var new_Class = new Site();
	
	if(readCookie('mattyResultsHelp_status') == ''){
		toggleHelp(1,'mattyResultsHeader', 'mattyResultsHelp');
	}
	else{
		toggleHelp(readCookie('mattyResultsHelp_status'),'mattyResultsHeader', 'mattyResultsHelp');	
	}
}


function toggleDrops(toggleVictim){
	if(toggleVictim.className=='show'){
		toggleVictim.className = 'hide';
	}
	else{
		toggleVictim.className = 'show';
	}
}

function valChange(sliderObj, val, newPos, knobNumber) { 
	if(sliderObj.totalKnobs == 2){
		if(knobNumber == 1){
			$('matty' + sliderObj.name + 'SlideractualMin').innerHTML = val;
			mattyParams[sliderObj.minHtmlVal] = val;
		}
		if(knobNumber == 2){
			$('matty' + sliderObj.name + 'SlideractualMax').innerHTML = val;
			mattyParams[sliderObj.maxHtmlVal] = val;
		}
	}
	else{
		$('matty' + sliderObj.name + 'SlideractualMin').innerHTML = val;
		mattyParams[sliderObj.minHtmlVal] = val;
	}
}
function valEnd(sliderObj, val, newPos) { 
	//processRun();
}

/*  For the love of all that is good, the doctype must be transitional */
function createSlider(val, width, height, sliderMin, sliderMax, actualMin, actualMax){
	val.width         = width;
	val.height        = height;
	val.imgDir   = '/images/NeoSet2/';
	val.direction = 1;
	val.attachOnChange(valChange);
	val.attachOnSlideEnd(valEnd);		
	val.fieldName     = 'val_start';
	val.minVal        = sliderMin;
	val.maxVal        = sliderMax;
	val.valueInterval = 1;
	val.valueDefault  = actualMin;
	val.useInputField = 0;
	val.styleValueFieldClass = 'sliderInput';		
	if(actualMax != -1){
		val.totalKnobs = 2;
		val.useSecondKnob        = true;
		val.preventValueCrossing = true;
		val.wheelAmount        = 0; //disable mouse wheeling cause we have 2 knobs.		
		val.fieldName2     = 'val_end';
		val.minVal2        = sliderMin;
		val.maxVal2        = sliderMax;
		val.valueInterval2 = 1;
		val.valueDefault2  = actualMax;
		val.setSliderIcon2('up.gif', 20, 15);//file,width,height
		val.useInputField2 = 0;
		val.styleValueFieldClass2 = 'sliderInput';	
		val.setSliderIcon('down.gif', 20, 15);
	}
	else{
		val.totalKnobs = 1;
		val.setSliderIcon('slider_large.gif', 80, 21);
	} 
	val.drawInto('matty' + val.name + 'Slider');
	
}
