// JavaScript Document
function parseUrl1(data) {
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}

function selectcompany(companyid) {
	$$('.company-info').invoke('hide');
	$(companyid).show();
	$(companyid).scrollTo();
	return false;
}

Event.observe(window, 'load', function() {
	$$('ul.companies a').each(function(element) {
		element.observe('click', function(e) {
			var element = $(e.target);
			var url = parseUrl1(element.href);
			var companyid = url.hash.gsub('#','');
			selectcompany(companyid);
			e.stop();
			return false;
		});
	});
	$('county').observe('change', function(e) {
		var url;
		if ($('county').getValue() == '') {
			url = '/county_companies.php';
		} else {
			url = '/companies/' + $('county').getValue() + '.php';
		}
		window.location = url;
	});

	var purl = parseUrl1(document.URL);
	if (purl.file == 'county_companies.php') {
		$('county').setSelectedValue('');
	} else {
		$('county').setSelectedValue(purl.file.gsub('.php', ''));
	}
});