function populateOptions(formEl, inputs){
	var theForm = jQuery(formEl)[0];
	for (i = 0; i < inputs.length; i++){
		try{
			jQuery(theForm.elements[inputs[i][0]]).addOption(inputs[i][1], false);
		} catch(e){}
	}
	preFillFormWithUrlParameters(formEl);
}

function propertySearch(theForm){
	var that = this;

	//Set up the form
	this.form = jQuery(theForm)[0];

	jQuery(this.form).submit(function(){
		return validate(this);
	});
	
	function validate(theForm){
		return true;
	}

	this.inputs = new Object();

	//Set locations
	this.inputs.locations = {
		"ct1"	: "Dublin City",
		"ct2"	: "Galway City",
		"ct3"	: "Cork City",
		"ct4"	: "Belfast City",
		"ct5"	: "Limerick City",
		"ct6"	: "Waterford City",
		""	: "-------------",
		"c27"	: "Co. Antrim",
		"c28"	: "Co. Armagh",
		"c10"	: "Co. Carlow",
		"c25"	: "Co. Cavan",
		"c16"	: "Co. Clare",
		"c15"	: "Co. Cork",
		"c31"	: "Co. Derry",
		"c24"	: "Co. Donegal",
		"c32"	: "Co. Down",
		"c1"	: "Co. Dublin",
		"c30"	: "Co. Fermanagh",
		"c19"	: "Co. Galway",
		"c14"	: "Co. Kerry",
		"c3"	: "Co. Kildare",
		"c11"	: "Co. Kilkenny",
		"c8"	: "Co. Laois",
		"c23"	: "Co. Leitrim",
		"c17"	: "Co. Limerick",
		"c5"	: "Co. Longford",
		"c9"	: "Co. Louth",
		"c20"	: "Co. Mayo",
		"c2"	: "Co. Meath",
		"c26"	: "Co. Monaghan",
		"c6"	: "Co. Offaly",
		"c21"	: "Co. Roscommon",
		"c22"	: "Co. Sligo",
		"c18"	: "Co. Tipperary",
		"c29"	: "Co. Tyrone",
		"c12"	: "Co. Waterford",
		"c7"	: "Co. Westmeath",
		"c13"	: "Co. Wexford",
		"c4"	: "Co. Wicklow"
	}

	//Set salePrices
	//*NOTE* - &euro; must be specified as \u20ac (ANSI) to display correctly!
	this.inputs.salePrices = {"100000":"\u20ac100,000","150000":"\u20ac150,000","200000":"\u20ac200,000","250000":"\u20ac250,000","300000":"\u20ac300,000","350000":"\u20ac350,000","400000":"\u20ac400,000","450000":"\u20ac450,000","500000":"\u20ac500,000","550000":"\u20ac550,000","600000":"\u20ac600,000","650000":"\u20ac650,000","700000":"\u20ac700,000","750000":"\u20ac750,000","800000":"\u20ac800,000","850000":"\u20ac850,000","900000":"\u20ac900,000","950000":"\u20ac950,000","1000000":"\u20ac1,000,000","1250000":"\u20ac1,250,000","1500000":"\u20ac1,500,000","1750000":"\u20ac1,750,000","2000000":"\u20ac2,000,000","2250000":"\u20ac2,250,000","2500000":"\u20ac2,500,000","2750000":"\u20ac2,750,000","3000000":"\u20ac3,000,000","3500000":"\u20ac3,500,000","4000000":"\u20ac4,000,000","4500000":"\u20ac4,500,000","5000000":"\u20ac5,000,000"}
	//new Array(); //this bit's buggy when there are other frameworks involved
	
	//for (var p = 100000; p <= 1000000; p += 50000){
	//	this.inputs.salePrices[p] = "\u20ac" + formatThousandSeparator(p);
	//}
	//for (var p = 1250000; p <= 5000000; p += 250000){
	//	this.inputs.salePrices[p] = "\u20ac" + formatThousandSeparator(p);
	//}
	
	//Set rentalPrices
	//*NOTE* - &euro; must be specified as \u20ac (ANSI) to display correctly!
	this.inputs.rentalPrices = {"400":"\u20ac400","500":"\u20ac500","600":"\u20ac600","700":"\u20ac700","800":"\u20ac800","900":"\u20ac900","1000":"\u20ac1,000","1100":"\u20ac1,100","1200":"\u20ac1,200","1300":"\u20ac1,300","1400":"\u20ac1,400","1500":"\u20ac1,500","1600":"\u20ac1,600","1700":"\u20ac1,700","1800":"\u20ac1,800","1900":"\u20ac1,900","2000":"\u20ac2,000","2100":"\u20ac2,100","2200":"\u20ac2,200","2300":"\u20ac2,300","2400":"\u20ac2,400","2500":"\u20ac2,500","2600":"\u20ac2,600","2700":"\u20ac2,700","2800":"\u20ac2,800","2900":"\u20ac2,900","3000":"\u20ac3,000","3100":"\u20ac3,100","3200":"\u20ac3,200","3300":"\u20ac3,300","3400":"\u20ac3,400","3500":"\u20ac3,500","3600":"\u20ac3,600","3700":"\u20ac3,700","3800":"\u20ac3,800","3900":"\u20ac3,900","4000":"\u20ac4,000","4100":"\u20ac4,100","4200":"\u20ac4,200","4300":"\u20ac4,300","4400":"\u20ac4,400","4500":"\u20ac4,500","4600":"\u20ac4,600","4700":"\u20ac4,700","4800":"\u20ac4,800","4900":"\u20ac4,900","5000":"\u20ac5,000"}
	//new Array(); //this bit's buggy when there are other frameworks involved
	//for (var p = 400; p <= 5000; p += 100){
	//	this.inputs.rentalPrices[p] = "\u20ac" + formatThousandSeparator(p);
	//}
	
	//Set sharePrices
	//*NOTE* - &euro; must be specified as \u20ac (ANSI) to display correctly!
	this.inputs.sharingPrices = {"200":"\u20ac200","250":"\u20ac250","300":"\u20ac300","350":"\u20ac350","400":"\u20ac400","450":"\u20ac450","500":"\u20ac500","550":"\u20ac550","600":"\u20ac600","650":"\u20ac650","700":"\u20ac700","750":"\u20ac750","800":"\u20ac800","850":"\u20ac850","900":"\u20ac900","950":"\u20ac950","1000":"\u20ac1,000"}
	//new Array(); //this bit's buggy when there are other frameworks involved
	//for (var p = 200; p <= 1000; p += 50){
	//	this.inputs.sharingPrices[p] = "\u20ac" + formatThousandSeparator(p);
	//}

	//Set number of bedrooms
	this.inputs.bedroomCount = {
		"1" : "1 Bed or Studio",
		"2" : "2 Bedrooms",
		"3" : "3 Bedrooms",
		"4" : "4 Bedrooms",
		"5" : "5 Bedrooms",
		"6+" : "6 or More"
	}

	this.validate = function(){
		return true;
	}
	
	this.inputList = [
		//input name : value list
		["s[cc_id]", this.inputs.locations, false, ""],
		["s[mnp]", this.inputs.salePrices, false, ""],
		["s[mxp]", this.inputs.salePrices, false, ""],
		["s[bd_no]", this.inputs.bedroomCount, false, ""]
	]

	jQuery(this.form.elements['s[search_type]']).bind('change', {}, function(event){
		jQuery(that.form.elements['s[mnp]']).removeOption(/./).addOption(that.inputs[jQuery(this).selectedValues() + 'Prices'], false);
		jQuery(that.form.elements['s[mxp]']).removeOption(/./).addOption(that.inputs[jQuery(this).selectedValues() + 'Prices'], false);
	});
	
	this.init = function(){
		populateOptions(theForm, this.inputList);
	}
}


function motoringSearch(theForm){
	var that = this;

	//Set up the form
	this.form = jQuery(theForm)[0];
	
	//update form based on new/used car search selection - change form action and make/model dropdowns
	jQuery(document.motoringSearchBarForm.elements['searchType']).bind('change', {}, function(event){
		var formActionUrl = "http://motoring.eircom.net/%1/index.cfm";
		var searchType = jQuery(this).selectedValues().toString();
		that.form.action = formActionUrl.replace('%1', searchType);
		if (searchType == 'newcars'){
			jQuery('#msb-motoring-newcars').show();
			jQuery('#msb-motoring-usedcars').hide();
			init_makemodelbody(that.form, MakeModelBodyList_SSCIRL2002);
		}else{
			jQuery('#msb-motoring-usedcars').show();
			jQuery('#msb-motoring-newcars').hide();
			init_makemodelyear(that.form);
		}
	});
	
	this.init = function(){
		var successCount = 0;
		var motoringJs = ((window.location.protocol.indexOf('https') > -1) ? 'https://secure.eircom.net' : 'http://siteassets.eircom.net'); //handle local files
		motoringJs += "/assets/static/js/motoring/carzone/";
/*		script urls as defined by carzone
		<!-- NEW search		script src="http://eircom.webzone.ie/JavaScript/index.cfm?fuseaction=NewMakeModelBody&StrSpecs=SSCIRL2002"></script -->
		<!-- USED search	script src="http://eircom.webzone.ie/sausage/javascript/index.cfm?fuseaction=makemodelyear&groupid=738"></script -->
		<!-- BODYTYPES search	script src="http://eircom.webzone.ie/JavaScript/index.cfm?fuseaction=BodyTypes&StrSpecs=SSCIRL2002"></script -->
*/		
		
		//load the main carzone search functions (dropdown manipulation)
		jQuery.ajax({
			type: "GET",
			url: motoringJs + "searchFunctions.js", 
			dataType: "script",
			success : function(){ that.jsReady(++successCount) },
			cache: true
		});
		//load used car list
		jQuery.ajax({
			type: "GET",
			url: motoringJs + "usedMakeModelYear.js", 
			dataType: "script",
			success : function(){ that.jsReady(++successCount) },
			cache: true
		});
		//load new car list
		jQuery.ajax({
			type: "GET",
			url: motoringJs + "newMakeModelBody.js", 
			dataType: "script",
			success : function(){ that.jsReady(++successCount) },
			cache: true
		});
		//load body abbreviation/fullname mapping
		jQuery.ajax({
			type: "GET",
			url: motoringJs + "bodyTypes.js", 
			dataType: "script",
			success : function(){ that.jsReady(++successCount) },
			cache: true
		});
		//Once all 4 js files have loaded we can set the right values
		this.jsReady = function(successCount){
			if(successCount == 4){
				//setTimeout(function(){
					preFillFormWithUrlParameters(theForm);
					jQuery(document.motoringSearchBarForm.elements['searchType']).change();
				//}, 1000) //It seems in Firefox 3 and Safari Webkit, objects loaded via jQuery.getScript() aren’t instantly available when called via the callback function. Adding a setTimeout seems to get around this.
			}
		}
	}
}

//alert(msbDatingSearch.hasOwnProperty('locations'));
	
jQuery(document).ready(function($){
	//Make the search bar fade on tab change :)
	$('#msb-tabs').parent().tabs({ fx: { opacity: 'toggle', duration: 65 } });
	
	//Change hand to cursor on button mouseover
	$('.msb-submit').hover(
		function(){ $(this).addClass('msb-button-hover') },
		function(){ $(this).removeClass('msb-button-hover') }
	);
	
	//Select a tab if there's one defined in the url (eg search-web)
	//var selectTab = 'search-' + (getUrlParameters()['searchTab']);
	//$('#msb-tabs').tabs('select', selectTab);
	
	/* The 'more' drop down menu on Google Search */
	$('#multi-search-bar-v2 li.more ul').hide();
	$('#multi-search-bar-v2 li.more').hover(
	  function () {
		$('#multi-search-bar-v2 li.more ul').slideDown('fast');
	  }, 
	  function () {
		$('#multi-search-bar-v2 li.more ul').hide(); //slideUp('fast');
	  }
	);
	$('#multi-search-bar-v2 li.more ul li a').click(
	  function () {
		$('#multi-search-bar-v2 li.more ul').hide();
	  }
	);

	//Load search options (init includes preFillFormWithUrlParameters function)
	new motoringSearch('#search-motoring form').init();
	new propertySearch('#search-property form').init();
	//There are no search settings for the following
	preFillFormWithUrlParameters($('#search-web form')[0]);
	preFillFormWithUrlParameters($('#search-music form')[0]);
	preFillFormWithUrlParameters($('#search-supportsite form')[0]);
	preFillFormWithUrlParameters($('#search-phonebook form')[0]);
});