function fireEvent(element,event){
	if (document.createEventObject){
		// dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on'+event,evt)
	} else {
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
}

Event.observe(window, 'load', function() {
// 	$$('.tabs_list').each(function(tab_group){
// 		new Control.Tabs(tab_group, {
// 			activeClassName: 'selected'
// 		});
// 	});

	geocoder = new GClientGeocoder();

	$$('.tabs_list').each(function(tab_group) {
		new Control.Tabs(tab_group,{
			linkSelector: 'li a',
			activeClassName: 'selected',
			beforeChange: function(old_container){}
		});
	});

	$('showNewsletter').observe('click', function(event) {
		showNewsletter();
		(event.preventDefault) ? event.preventDefault() : (event.returnValue = false);
		return false;
	});
	$('showProgram').observe('click', function(event) {
		(event.preventDefault) ? event.preventDefault() : (event.returnValue = false);
		return false;
	});

	$('pl_map').childElements().each(function(area) {
		area.onclick = function() {
			href = area.id.split('w');
			vid = href[1];

			i = 0;
			$('StationVoivodeshipId').childElements().each(function(option) {
				if (option.value == vid) {
					$('StationVoivodeshipId').selectedIndex = i;
					fireEvent($('StationVoivodeshipId'),'change');
				}
				i++;
			});

			return false;
		};
	});

	new Form.Element.EventObserver(
		'StationVoivodeshipId',
		function(element, value) {
			$('StationCityId').previousSiblings()[0].addClassName('loader');
			new Ajax.Updater(
				'StationCityId',
				root+'/fuels/getCities',
				{
					onComplete: stopCitiesLoader,
					asynchronous:true,
					evalScripts:true,
					parameters:Form.Element.serialize('StationVoivodeshipId'),
					requestHeaders:['X-Update', 'StationCityId']
				}
			);
		}
	);

	new Form.Element.EventObserver(
		'StationCityId',
		function(element, value) {
			$('StationVoivodeshipId').previousSiblings()[0].addClassName('loader');
			new Ajax.Request(root+"/fuels/getVoivodeship/"+value, {
				method: 'get',
				onSuccess: function(transport) {
					vid = transport.responseText.split(' ')[0];

					i = 0;
					$('StationVoivodeshipId').childElements().each(function(option) {
						if (option.value == vid) {
							$('StationVoivodeshipId').selectedIndex = i;
						}
						i++;
					});
					stopVoivodeshipsLoader();
				}
			});
			return false;
		}
	);

	$$('.drive_popup').each(function(popup_link) {
		popup_link.observe('click', function(event) {
			url = popup_link.href.split('/');
			sid = url[url.length - 1];

			drive_popup(sid, event);
			(event.preventDefault) ? event.preventDefault() : (event.returnValue = false);
			return false;
		});
	});

	$('driving_close').observe('click', drive_close).hide();

	var i = 4;
	$$('#averages_voiv table').each(function(voivodeship) {
		if (i < 1) {
			voivodeship.hide();
		}
		i--;
	});
	$$('#averages_voiv div')[0].update('<a href="#averages_voiv">zobacz więcej województw</a>');
	$$('#averages_voiv div a')[0].observe('click', showVoivodeships);
});

function stopCitiesLoader() {
	$('StationCityId').previousSiblings()[0].removeClassName('loader');
}

function stopVoivodeshipsLoader() {
	$('StationVoivodeshipId').previousSiblings()[0].removeClassName('loader');
}

function drive_popup(station_id, event) {
	$('driving').update('<div id="map" style="width: 1000px; height: 300px"></div>');
	map = new GMap2(document.getElementById('map'));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	var searchUrl = root+'/stations/getLocation/'+station_id;

	GDownloadUrl(searchUrl, function(data) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName('marker');

		if (markers.length == 0) {
			return false;
		}

		name = markers[0].getAttribute('name');
		url = markers[0].getAttribute('url');
		address = markers[0].getAttribute('address');
		point = new GLatLng(parseFloat(markers[0].getAttribute('lat')), parseFloat(markers[0].getAttribute('lng')));

		if (markers[0].getAttribute('lat') == '0.000000') {
			searchLocation(address);
		}

		var fs = markers[0].getElementsByTagName('fuel');
		var fuels = new Array();
		if (fs.length > 0) {
			for (var k = 0; k < fs.length; k++) {
				price = fs[k].getAttribute('price');
				if (price) {
					fuels[k] = fs[k].getAttribute('name')+' - '+fs[k].getAttribute('price')+' zł';
				} else {
					fuels[k] = fs[k].getAttribute('name');
				}
			}
		}

		map.setCenter(point, 12);

		var marker = createMarker(point, name, url, address, fuels);
		map.addOverlay(marker);

		return true;
	});

	$('driving_close').show();

	(event.preventDefault) ? event.preventDefault() : (event.returnValue = false);
	return false;
}

function drive_close() {
	$('driving').update('');
	$('driving_close').hide();
	map = null;
	return false;
}

function showVoivodeships() {
	$('averages_voiv').insert({bottom: '<div class="loader" id="showVoivodeshipsLoader">test</div>'});
	$$('#averages_voiv table').each(function(voivodeship) {
		voivodeship.show();
	});
	$$('#averages_voiv div a')[0].remove();
	$('showVoivodeshipsLoader').remove();
}