
// Sportspages.com javascript
// Requires Prototype, Scriptaculous

function toggleDisplay(id) {
	var el = document.getElementById(id);
	el.style.display = (el.style.display == 'block')?'none':'block';
}

function doPostOld(formId, fieldName, value) {
	var form = document.getElementById(formId);
	if(typeof fieldName != 'undefined')
		form[fieldName].value = value;
	form.submit();
}

function doPost(url, params) {
	var formEl = new Element('form', {action:url, method:'post'});
	
	$H(params).each(function(pair) {
		var field = new Element('input', {type:'hidden',name:pair.key,value:pair.value});
		formEl.insert(field);
	});
	
	document.body.appendChild(formEl);
	formEl.submit();
}

function clicker(fn) {
	return function(event) {event.stop(); fn(event);};
}

// Signup

function selectSignupSport(selectBox, suffix) {
	if(typeof suffix == 'undefined') suffix = '';
	
	selectSport(selectBox, 'mailingType'+suffix, true);
	var updater = new Ajax.Updater('signupListsbox'+suffix, SCRIPTS_DIR+'signupType?sport='+currentSport, { method: 'get' });
}

var emptyInterestLabel;

function addInterest(suffix) {
	
	if(typeof suffix == 'undefined') suffix = '';
	
	var sportBox = $('mailingSport'+suffix);
	var typeBox = $('mailingType'+suffix);
	var selectedBox = $('selectedBox'+suffix);
	
	if(!sportBox.value)
		return;
	
	if(selectedBox.options[0].value == '') {
		emptyInterestLabel = selectedBox.options[0].innerHTML;
		selectedBox.innerHTML = '';
	}
	
	var catLabel = sportBox.options[sportBox.selectedIndex].innerHTML;
	var id = sportBox.value;
	
	if(typeBox.value) {
		catLabel += ' ' + typeBox.options[typeBox.selectedIndex].innerHTML;
		id = typeBox.value;
	}
	
	var addCount = 0;
	
	$('signupListsbox'+suffix).select('tr').each(function(row) {
		var name = row.down('td.listName').innerHTML;
		var selectBox = row.down('select');
		var valueName = selectBox.options[selectBox.selectedIndex].innerHTML;
		var value = selectBox.value;
		
		if(value) {
			addCount++;
			optValue = id + ':' + value;
			if(!checkExists(selectedBox, optValue))
				addOption(selectedBox, optValue,  name + ': ' + valueName + ' (' + catLabel + ')');
		}
	});
	
	if(!addCount && !checkExists(selectedBox, id))
		addOption(selectedBox, id, catLabel);
	
	if($('noInterestsSelected'+suffix))
		$('noInterestsSelected'+suffix).fade({duration:0.2});
}

function addOption(selectBox, value, label) {
	$(selectBox).insert(new Element('option', {value: value}).update(label));
}

function removeInterest(suffix) {
	if(typeof suffix == 'undefined') suffix = '';
	
	var selectedBox = $('selectedBox'+suffix);
	$A(selectedBox.options).findAll(function(opt) {return opt.value != "" && opt.selected;}).invoke('remove');
	
	if(!selectedBox.options.length)
		addOption(selectedBox, '', emptyInterestLabel);
}

function checkExists(selectBox, value) {
	return $A($(selectBox).options).find(function(opt) {return (opt.value.indexOf('%') > -1 ? opt.value.substr(0,opt.value.indexOf('%')) : opt.value) == value;});
}

function submitInterestForm() {
	$A($('selectedBox').options).each(function(opt) {opt.selected = true;});
}

// RSS

function submitRSSForm() {
	if($('selectedBox').options[0].value == '') {
		$('noInterestsSelected').appear({duration:0.2});
		return false;
	}
	
	submitInterestForm();
	
	$('noInterestsSelected').fade({duration:0.2});
}

// Search

var currentSport;

function selectSport(selectBox, typeBox, signup) {
	
	var extraGet = '';
	
	if(signup)
		extraGet = 'signup&'
	
	$(typeBox).selectedIndex=0;
	$(typeBox).options[0].text = "Loading...";
	
	setSearchFormActions();
	
	currentSport = selectBox.value;
	
	var updater = new Ajax.Updater(typeBox, SCRIPTS_DIR+'searchTypeDropdown?' + extraGet + 'sport='+selectBox.value, { method: 'get' });
	
	if(!updater)
		selectBox.form.submit();
}

function selectType(selectBox) {
	setSearchFormActions();
}

function setSearchFormActions() {
	var basicForm = $('basicSearchForm');
	var advancedForm = $('advancedSearchForm');
	
	if(basicForm)
		setSearchFormAction(basicForm);
	if(advancedForm)
		setSearchFormAction(advancedForm);
}

function setSearchFormAction(form) {
	var sportBox = $('basicSearchSport');
	var typeBox = $('basicSearchType');
	
	form.action = ROOT;
	
	if(sportBox.value)
		form.action += sportBox.value + '/';
	
	if(typeBox.value)
		form.action += typeBox.value + '/';
}

function basicSearch() {
	$('basicSearchType').disabled = true;
	$('basicSearchSport').disabled = true;
	$('basicSearchButton').disabled = true;
}

function advancedSearch() {
	var form = $('advancedSearchForm');
	
	form.getElements().each(function(el) {
		if(el.type == 'checkbox' && el.checked)
			return;
		if(el.value == '' && el.name != 'search' || el.value == 'Min' || el.value == 'Max')
			el.disable();
	});
}

/* Sliders */

// Price slider

var PriceSlider = {
	
	min:null,
	max:null,
	selectedMin:null,
	selectedMax:null,
	seperation:0.12,
	power:2,
	
	add: function(min, max, selectedMin, selectedMax) {
		
		this.min = min;
		this.max = max;
		
		addRangeSlider('pricetrack', this.normalise(selectedMin), this.normalise(selectedMax), this.seperation, this.priceHandler.bind(this), 'priceField', 'priceDisplay');
	},
	
	normalise: function(x) {
		return Math.pow((x - this.min) / (this.max - this.min), 1/this.power);
	},
	
	unnormalise: function(x) {
		return Math.pow(x, this.power) * (this.max - this.min) + this.min;
	},
	
	priceHandler: function(x) {
		
		if(x == 1)
			return 'Max';
		
		if(x == 0)
			return 'Min';
		
		var rawVal = this.unnormalise(x);
		
		var interval = 5;
		if(rawVal > 100)
			interval = 10;
		if(rawVal > 300)
			interval = 50;
		if(rawVal > 1000)
			interval = 100;
		if(rawVal > 3000)
			interval = 500;
		
		var newVal = Math.floor(rawVal/interval)*interval;
		
		return newVal;
	}
}

// Generic slider class

var addRangeSlider = function(track, minStartValue, maxStartValue, seperation, resultHandler, formField, displayField) {
	
	var currentMinValue = minStartValue;
	var currentMaxValue = maxStartValue;
	
	var updateValues = function(minVal, maxVal) {
		var newMin = resultHandler(minVal);
		var newMax = resultHandler(maxVal);
		$(formField + '-1').value = newMin;
		$(formField + '-2').value = newMax;
		$(displayField + '-1').innerHTML = newMin;
		$(displayField + '-2').innerHTML = newMax;
	};
	
	updateValues(minStartValue, maxStartValue);
	
	var minSlider = new Control.Slider(track+'-handle1', track, {
		onSlide: function(v,s) {
			currentMinValue = v;
			if(currentMaxValue < v+seperation) {
				if(currentMaxValue == 1)
					s.setValue(1-seperation);
				else
					maxSlider.setValue(v+seperation);
			}
			updateValues(currentMinValue, currentMaxValue);
		},
		onChange: function(v,s) {
			if(v > 1-seperation)
				s.setValue(v-seperation);
			
			if(currentMaxValue == v)
				maxSlider.setValue(v+seperation);
			
			currentMinValue = v;
			updateValues(currentMinValue, currentMaxValue);
		},
		range: $R(0,1),
		sliderValue: minStartValue
	});
	
	var maxSlider = new Control.Slider(track+'-handle2', track, {
		onSlide: function(v,s) {
			currentMaxValue = v;
			if(currentMinValue > v-seperation) {
				if(currentMinValue == 0)
					s.setValue(0+seperation);
				else
					minSlider.setValue(v-seperation);
			}
			updateValues(currentMinValue, currentMaxValue);
		},
		onChange: function(v,s) {
			if(v < seperation) {
				s.setValue(v+seperation);
			}
			
			if(currentMinValue == v)
				minSlider.setValue(v-seperation);
			
			currentMaxValue = v;
			updateValues(currentMinValue, currentMaxValue);
		},
		sliderValue: maxStartValue,
		range: $R(0, 1)
	});
}