$(document).ready(function(){
    wrapHideable();
    
    $("#toggle").click(function() { $("#about-us").toggle(); return false; });

    
    $("input.sell").click(function(){
       
        if($(this).attr("checked")){
            $("input.sell").attr('checked', 'checked');
            $('label[for="p_10"]').text('cena');
            $('label[for="p_13"]').text('cena za 1 m²');
        }
    });
    
    $("input.rent").click(function(){
        if($(this).attr("checked")){
            $("input.rent").attr('checked', 'checked');
            $('label[for="p_10"]').text('czynsz');
            $('label[for="p_13"]').text('czynsz za 1 m²');
            
        }
    });
        
    $("a.hide").click(hideBlock);
    $("input").focusin(
        function() {
            var input = $(this);
            var input_value = input.attr("value");
            var input_title = input.attr("title");
            if (input_value==input_title) {
                $(this).attr("value", "")
            }
        }
        );
    $("input").focusout(
        function() {
            var input = $(this);
            var input_value = input.attr("value");
            var input_title = input.attr("title");
            if (input_value=="") {
                $(this).attr("value", input_title)
            }
        }
        );
	
    $("input.multiple").click(
        function() {
            $(".fieldset .close").click();
            var fieldset = $(this).prev('.fieldset');
            fieldset.slideDown(100);
        }
        );
    $(".fieldset .close").click(
        function() {
            var fieldset = $(this).parent(),
            inputs = fieldset.find("input"),
            values = "";
            inputs.each(
                function(index) {
                    if ($(this).attr("checked")) {
                        values += $(this).attr("value")+", ";
                    }
                }
                );
            $(this).parent('.fieldset')
            .next('input')
            .attr('value', values);
            fieldset.slideUp(100);
            return false
        }
        );
	
    $("#agency").selectbox({
    onChange: function (val, inst) {
      if(val=='Toruń') location.href='http://www.rezydent.nieruchomosci.pl';
    }
    });
    
    $.get('/ajax/notes/get', function(data){
        if(!data)
            $("#notes_entry").append('<tr class="empty"><td colspan="3">Brak ofert w notesie</td></tr>');
        else
            for(i in data){
                $("#notes_entry").append('<tr id="r_'+data[i].id+'"><td class="first_col"><a href="/oferta/'+data[i].id+'"><img src="'+data[i].thumb+'" width="85" /></a></td><td class="second_col">'+data[i].row1+'<br />'+data[i].row2+'</td><td class="third_col remove"><a class="rem_from_notes" href="#"><img src="/rezydent/gfx/x_btn.gif" /></a></td></tr>');
            }
    }, "json");
    
    $(".add_to_notes").click(
        function(){
            var result = $(this).attr("id").split("_");
                       
            if($("#notes_entry tr#r_"+result[1]).length){
                alert('Wybrana oferta już jest w notesie'); 
                return false;
            }
            
            $("#notes_entry tr.empty").hide();
            
            $.post('/ajax/notes/add', {
                offer_id: result[1]
            }, function(data) {
                $("#notes_entry").append('<tr id="r_'+data.id+'"><td class="first_col"><a href="/oferta/'+data.id+'"><img src="'+data.thumb+'" width="85" /></a></td><td class="second_col">'+data.row1+'<br />'+data.row2+'</td><td class="third_col remove"><a class="rem_from_notes" href="#"><img src="/rezydent/gfx/x_btn.gif" /></a></td></tr>');
            }, "json");
            $(this).html("dodano do notesu");
            return false;    
        });
    
    
    $("#notes_entry").delegate("a.rem_from_notes", "click", function(){  
        var result = $(this).parent().parent().attr("id").split("_");  
        if($("#notes_entry tr:not(.empty)").length==1) $("#notes_entry tr.empty").show();
        $('#r_'+result[1]).remove();
          
        $.post('/ajax/notes/remove', {
            offer_id: result[1]
        });
        return false;
    });
    
});

function wrapHideable() {
    $(".hideable").each(
        function() {
            var elem_id = $(this).attr("id");
            $(this).children().wrapAll('<div class="wrapper" />');
            $(this).append('<a href="#'+elem_id+'" class="hide"><img src="/rezydent/gfx/hide_btn.gif" /></a>');
        }
        );
}

function hideBlock() {
    var parent = $(this).parent();
    var wrapper = $(this).parent().find(".wrapper");
    if (parent.find("h3.hidden").length>0) {
        parent.find("h3.hidden").remove()
    } else {
        var title;
        if ($(this).parent().attr("title")) {
            title = $(this).parent().attr("title");
        } else {
            title = wrapper.find("h3").text();
        }
        $('<h3 class="hidden">'+title+'</h3>').insertAfter(wrapper);	
    }
    wrapper.slideToggle();
    return false;
}

function compute() {
    var form = document.getElementById('calculator');
    price  = form.price.value;
    agency_perc = form.agency_perc.value;
    type = form.type.selectedIndex;
    agency = (price * agency_perc) / 100;
    agency_vat = agency * 0.23;
    tax = price * 0.02;
    notar = 0.0;
    if (type != 4) {
        if ((price > 0) && (price <= 3000))			notar = 50.0;
        if ((price > 3000) && (price <= 10000))		notar = (100 + ((price - 3000)*0.03))/2;
        if ((price > 10000) && (price <= 30000))	notar = (310 + ((price - 10000)*0.02))/2;
        if ((price > 30000) && (price <= 60000))	notar = (710 + ((price - 30000)*0.01))/2;
        if ((price > 60000) && (price <= 1000000))	notar = (1010 + ((price - 60000)*0.005))/2;
        if (price > 1000000)						notar = (5710 + ((price - 1000000)*0.0025))/2;
    } else {
        if ((price > 0) && (price <= 3000))			notar = 100.0;
        if ((price > 3000) && (price <= 10000))		notar = (100 + ((price - 3000)*0.03));
        if ((price > 10000) && (price <= 30000))	notar = (310 + ((price - 10000)*0.02));
        if ((price > 30000) && (price <= 60000))	notar = (710 + ((price - 30000)*0.01));
        if ((price > 60000) && (price <= 1000000))	notar = (1010 + ((price - 60000)*0.005));
        if (price > 1000000)						notar = (5710 + ((price - 1000000)*0.0025));
    }
    notar_vat = notar * 0.23;
    if (type == 0)
        just = 0.0;
    else
        just = 200.0;
    add = tax + notar + notar_vat + agency + agency_vat + just;
    form.agency.value = (Math.round(agency * 100) / 100) + ' zł';
    form.notar.value = (Math.round(notar * 100) / 100) + ' zł';
    form.notar_vat.value = (Math.round(notar_vat * 100) / 100) + ' zł';
    form.agency_vat.value = (Math.round(agency_vat * 100) / 100) + ' zł';
    form.just.value = (Math.round(just * 100) / 100) + ' zł';
    form.tax.value = (Math.round(tax * 100) / 100) + ' zł';
    form.sum_add.value = (Math.round((add) * 100) / 100) + ' zł';
    form.sum.value = (Math.round(((price * 1.0) + add) * 100) / 100) + ' zł';
}
