function parseResponse(response) {
    if (!response) return;
    var rates = $(response).children('.rate');
    var intervals = $(response).children('.interval');

//  odkomentowac
    jQuery.each(intervals, function(){
        $("span#"+$(this).children(".name").text()).html($(this).children('.intervalVal').text());
    });

    jQuery.each(rates, function() {
		var region = $(this).children(".region").text();
		var ragionValue = $(this).children(".rateVal").text();
		if(!parseInt(ragionValue)) {
			$("#" + region).css('background-image', 'none');
		} else {
			$("#" + region).css('background-image', 'url(/images/barometr/'+ region + ragionValue + '.png)');
		}

//  zakomentowac
//        $("span#"+$(this).children(".name").text()).html(ragionValue);

		

    });

}

function sendRequest() {
    var type   = $("#barometer .types img[alt=On]");
    var period = $("#barometer .periods img[alt=On]");
// don't work on ie :S
//  var type   = $("#barometer .types img[src*='on.png']"); 
//  var period = $("#barometer .periods img[src*='on.png']");
    if(type.length){
        if ($(type).attr('id').length == 0 || $(period).attr('id').length == 0) return;

//    $.get(BASE + '/barometer/index/p/' + $(period).attr('id') + "/t/" + $(type).attr('id'), { }, parseResponse);
        $.get(BASE + '/barometer/index/p/' + $(period).attr('id').replace('l','') + "/t/" + $(type).attr('id'), function(response){$("#barometerContent").html(response);$("*").ifixpng();});
    }
}

function assignListeners() {
    $("#barometer .types img").click(function() {
        // set default bg
        jQuery.each($("#barometer .types img"), function() {
            $(this).css('filter','');
            $(this).attr('src', BASE + '/images/barometr/off.png');
            $(this).attr('alt', 'Off');

        });

        $(this).attr('src', BASE + '/images/barometr/on.png');
        $(this).attr('alt', 'On');

        sendRequest();
    });

    $("#barometer .periods img").click(function() {
        // set default bg
        jQuery.each($("#barometer .periods img"), function() {
            $(this).css('filter','');
            $(this).attr('src', BASE + '/images/barometr/off.png');
            $(this).attr('alt', 'Off');
        });

        $(this).attr('src', BASE + '/images/barometr/on.png');
        $(this).attr('alt', 'On');

        sendRequest();
    });
}

$(document).ready(function() {
    assignListeners();

    // init bar
    sendRequest();
});