Event.observe(window, 'load', function() {
	$$('.rating a').each(function(a) {
		a.observe('click', rate);
	});

	$$('.addPrice').each(function(a) {
		a.observe('click', addPriceForm);
	});
});

function rate(event) {
	var a = event.element();

	rater_id = a.parentNode.parentNode.getAttribute('id').split('_rating');
	subject = rater_id[0];

	anchor_class = a.className.split('star');
	rate = anchor_class[1];

	new Ajax.Request(root+"/stations/rate/"+id, {
		method: 'post',
		parameters: {subject: subject, rate: rate},
		onSuccess: function(transport) {
			response = transport.responseText.split(' ');
			//response[0] = response[0].replace("\n", '');

			if (response[0] == 'OK' || response[0] == 'BADPARAMS' || response[0] == 'ERROR' || response[0] == 'ALREADYRATEDERROR') {
				/**
				 * Szerokość wskaźnika aktualnej oceny to ocena * szerokość grafiki
				 */
				width = response[1] * 30;
				$$('#'+subject+'_rating li.rating_current')[0].writeAttribute('style', 'width: '+width+'px;');

				/**
				 * Usuwamy linki (przestają więc działać ich observery)
				 * li.rating_current zostaje, pokazując aktualną ocenę
				 */
				$$('#'+subject+'_rating li a').each(function(a) {
					a.remove();
				});
			}
			return false;
		}
	});
	(event.preventDefault) ? event.preventDefault() : (event.returnValue = false);
	return false;
}

function addPriceForm(event) {
	var a = event.element();

	id = a.getAttribute('id').split('fuel');
	fuel_id = id[1];

	var form = new Element('form', { 'method': 'post', 'action': '/paliwa/ceny/dodaj/'+fuel_id });

	var fieldset1 = new Element('fieldset', {'style': 'display:none;'});
	var input1 = new Element('input', {'type': 'hidden', 'name': '_method', 'value': 'POST'});

	var input2 = new Element('input', {'name': 'data[FuelPrice][price]', 'type': 'text', 'maxlength': '7', 'value': '', 'class': 'FuelPricePrice', 'style': 'width: 35px'});

	var subm = new Element('input', {'type': 'submit', 'value': '»', 'style': 'margin-left: 10px'});

	fieldset1.insert({bottom: input1});

	form.insert({bottom: fieldset1});
	form.insert({bottom: input2});
	input2.insert({after: 'zł'});
	form.insert({bottom: subm});

	td = a.parentNode;
	Element.extend(td);
	td.update(form);

	form.observe('submit', addPrice);

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

function addPrice(event) {
	var form = event.element();

	cell = form.parentNode;

	action = form.getAttribute('action').split('/');
	fuel_id = action[action.length-1];

	cell.addClassName('loader');
	params = form['data[FuelPrice][price]'].serialize();
	form.remove();

	new Ajax.Request(
		root+'/fuel_prices/add/'+fuel_id,
		{
			onComplete: function(transport) {
				refreshPrice(cell, fuel_id, transport.responseText);
			},
			asynchronous:true,
			evalScripts:true,
			parameters: params,
			requestHeaders:['X-Update', 'FuelPricePrice']
		}
	);

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

function refreshPrice(cell, fuel_id, response) {
	//response = response.split("\n")[1];
	response = response.split(' ');

	status = response[0];
	price = response[1];
	date = response[2];

	if (status == 'OK') {
		status = 'Cena dodana';
	} else if (status == 'BADPARAMS') {
		newlink = new Element('a', {href: '#', 'class': 'addPrice', 'id': 'fuel'+fuel_id});
		newlink.update('Błąd w cenie, spróbuj ponownie').observe('click', addPriceForm);

		status = newlink;
	} else {
		status = 'Błąd';
	}

	if (price == '-') {
		price = '';
	}

	row = cell.parentNode.childElements();

	row[1].update(price);
	row[2].update(date);
	row[3].update(status);

	if (response[0] == 'BADPARAMS') {
		
	}

	cell.removeClassName('loader');
}

