if (document.all) { 
	try { 
		document.execCommand("BackgroundImageCache", false, true); 
	} catch(oe) { } 
}

Event.observe(
	window,
	'load',
	init
);

function init() {
	Event.observe(
		$('region'),
		'change',
		function() {
			if(this.value) {
				$('municipality').show();
				$('municipality').enable();
						
				$('municipality').options[0].value = $('region').value;
				
				$('municipality').options.length = 1;
				
				var i = 1;
				regions.get(this.value).each(
					function(item) {
						$('municipality').options[i] = new Option(item.value, item.key);
						i++;
					}
				);
			} else {
				$('municipality').options.length = 1;
				$('municipality').hide();
				$('municipality').disable();
			}
		}
	)
	
	
	if($('searchFilterForm')) {
		/* Search filter form submit */
		Event.observe(
			$('searchFilterForm'),
			'submit',
			searchSubmit.bindAsEventListener($('searchFilterForm'))
		)
		
		if($('age')) {
			/* Age slider control */ 	
			var	ageSlider		= $('age');
			ageSlider.show();
    	
			var span			= $$('div#age div.span')[0];
			
			var handleMin	= $$('div#age div.handle.min')[0];
			var plateMin	= $$('div#age div.handle.min div')[0];
			var inputMin	= $$('input[type="hidden"]')[0];
			
			var handleMax	= $$('div#age div.handle.max')[0];
			var plateMax	= $$('div#age div.handle.max div')[0];
			var inputMax	= $$('input[type="hidden"]')[1];
    	
			var currMin		= (inputMin.value)?inputMin.value:'1920';
			var currMax		= (inputMax.value)?inputMax.value:year;

			plateMin.update(currMin);
			plateMax.update(currMax);
			
  		var slider = new Control.Slider(
				[handleMin,handleMax],
				ageSlider,
				{
					range:				$R(1920,year),
					sliderValue:	[currMin,currMax],
					restricted:		true,
  		    spans:				[span]
				}
			);
			
			slider.options.onSlide = function(values) {
				plateMin.update(values[0].toFixed());
				plateMax.update(values[1].toFixed());		
			}
			
			slider.options.onChange = function(values) {
				inputMin.value = values[0].toFixed();
				inputMax.value = values[1].toFixed();
				
				searchSubmit.bind($('searchFilterForm'), null)();
			}
		}

		/* Form Ajax control */
		var formControls = $('searchFilterForm').getElements();
		formControls.each(
			function(formControl) {
				Event.observe(
					formControl,
					'click',
					searchSubmit.bind($('searchFilterForm'), null)
				);
			}
		);
	}

	if($('pageNav'))				initPageNav();
	if($('adForm'))					initAdForm();
	/*
	if($('recruiterForm'))	initRecruiterForm();
	*/
}


function initPageNav() {
	$('spinner').hide();
	
	var links = $$('#pageNav a');
	links.each(
		function(link) {
			Event.observe(
				link,
				'click',
				function(event) {
					Event.stop(event);

					new Ajax.Updater(
						$('rightCol'),
						link.href.replace('resultat/', 'get.searchResult'),
						{
							onComplete: initPageNav 
						}
					);
				}
			);
		}
	);
}

function initAdForm() {
	/* Create ad step 2 form */
	$('loader').hide();
	
	Event.observe(
		$('adForm'),
		'submit',
		function() { $('loader').show() }
	);
	
	var sports = $$('input.sport, input.type');
	sports.each(
		function(sport) {
			Event.observe(
				sport,
				'click',
				adUpdate.bind(sport)
			);
		}
	);

	if($('regions')) {
		Event.observe(
			$('regions'),
			'change',
			adUpdate.bind($('regions'))
		);
	}
}

function initRecruiterForm() {
	Event.observe(
		$('sport'),
		'change',
		function() {
			var form = $('sport').form;

			new Ajax.Updater(
				document.documentElement,
				form.action,
				{
					method:			form.method,
					parameters:	form.serialize(),
					onComplete:	init
				}
			);
		}
	);
}

function searchSubmit(event) {
	if(event) Event.stop(event);
	
	new Ajax.Updater(
		$('rightCol'),
		this.action,
		{
			method:			this.method,
			parameters:	this.serialize(),
			onCreate:		function() { $('spinner').show() },
			onComplete: initPageNav 
		}
	);
}

function adUpdate() {
	var form = $(this.form);

	new Ajax.Updater(
		$('mainCol'),
		form.action,
		{
			method:			form.method,
			parameters:	form.serialize(),
			onCreate:		function() { $('loader').show() },
			onComplete:	initAdForm
		}
	);
}

function showAd(zoneId)
{
	var m3_u = (location.protocol=='https:'?'https://ads.sportsverige.com/www/delivery/ajs.php':'http://ads.sportsverige.com/www/delivery/ajs.php');
	var m3_r = Math.floor(Math.random()*99999999999);
	if (!document.MAX_used) document.MAX_used = ',';
	document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
	document.write ("?zoneid="+zoneId);
	document.write ('&amp;cb=' + m3_r);
	if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
	document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
	document.write ("&amp;loc=" + escape(window.location));
	if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
	if (document.context) document.write ("&context=" + escape(document.context));
	if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
	document.write ("'><\/scr"+"ipt>");
}
