Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
  
function get_offer(efficiency,tank){

    new Ajax.Request(encodeURI('/products/proposition/'+parseInt(efficiency)+'.'+parseInt(tank)), {
        requestHeaders: {
            Accept: 'application/json'
        },
        onSuccess: function(transport){
            var result = transport.responseText.evalJSON(true);
            render_offer(result,efficiency);
        }
    });
}
/* var for graph */
var global_price=40000;


function render_offer(offer,efficiency){
    var html='';
    sum_price= 0;
    //template for product
    var str = '<div class="span-8 #{last}">'
    +'<div>'
    +'<h3 class="bottom">#{name}</h3>'
    +'<p class="align-right"><strong>#{price} PLN</strong></p>'
    +'<p class="small align-justify">'
    +'<img src="/img/Bjorn.jpg" width="100" alt="#{name}" class="top pull-1 left" />'
    +'#{description}'
    +'</p>'
    //+'<br class="clear"/>'
    //+'<div>'
   //+'<table border="0">'
   // +'<thead>'
    //+'<tr>'
    //+'<th class="span-6">#{name}</th>'
    //+'<th class="span-2">#{price} PLN</th>'
    //+'</tr>'
    //+'</thead>'
    //+'<tbody>'
    //+'#{relation_template}'
    //+'<tr>'
    //+'<td colspan="2"><strong>#{sum_price} PLN</strong></td>'
    //+'</tr>'
    //+'</tbody>'
    //+'</table>'
    //+'</div>'
    +'<div class="align-center top"><a class="button_red fit" href="/pompa-ciepla/#{id}.#{tank_id}.'+parseInt(parseInt($F('area')) + parseInt($F('extra_area')))+'">Dostosuj ofert\u0119</a></div>'
    +'</div>'
    +'</div>';
    var strr = ''
    +'<tr>'
    +'<td><span class="more">#{name}</span><br /><span class="small quiet desc" style="display:none;">#{description}</span></td>'
    +'<td><strong class="small">#{price} #{currency}</strong></td>'
    +'</tr>';

    var template = new Template(str);
    var templater = new Template(strr);

    //to foreach products
    offer.each(function(o) {
        o.relation_template='';
        o.tank_id=0;
        if(offer.last()==o){
            o.last='last';
        }else{
            o.last='';
        }
        sum_price=parseInt(sum_price)+parseInt(o.price);
        o.price=(parseInt(o.price)).formatMoney(0, ' ', ' ');
        //to foreach relation and assign to product template
        o.relation.each(function(or) {
            if(or.check_to==1){
                sum_price=parseInt(sum_price)+parseInt(or.price);
                or.price=(parseInt(or.price)).formatMoney(0, ' ', ' ');
                if(or.type=='tank'){
                    o.tank_id=or.id;
                }
                o.relation_template+=templater.evaluate(or);
            }
        });
        //faster but less idiotproof version of money format
        //o.price=o.price.split("").reverse().join("").replace(/(\d{3})/g, '$1 ').split("").reverse().join("");
        o.sum_price=(parseInt(sum_price)).formatMoney(0, ' ', ' ');
        if(offer[1].id ==  o.id) {
            global_price = parseInt(sum_price);
	    //alert(global_price);
        }
        html+=template.evaluate(o);
        sum_price=0;

    });
    $('offer').innerHTML=html;
     //fill_thinker_html();
    draw_slider(offer.first().value,offer[1].value,offer.last().value,efficiency);
    //fill_offer_html.delay(1.1,html);
    //function fill_offer_html(h) {
        //$('offer').innerHTML=h;
        $$('.fit').invoke('observe','click',function(e) {
            e.stop();
            fill_waiter_html();
            jumpto.delay(1.1,e.target.href);
        })
    //}
    function jumpto(href) {
        document.location.href=href;
    }
        function fill_thinker_html() {
            $('offer').innerHTML='<div class="span-24"><div class="barchart" style="margin:0 auto;"><p>Dobieranie pompy ciep\u0142a \u2026</p></div></div>';
    }
           function fill_waiter_html() {
            $('offer').innerHTML='<div class="span-24"><div class="barchart" style="margin:0 auto;"><p>Kalkulacja oferty \u2026</p></div></div>';
    }
    /*
    $('offer').fade({
        duration: 1.0,
        from: 0,
        to: 1
    });
    */
       //observing buttons for thinker
    //

    //return global_price;
}
function draw_slider(min,mid,max,value) {
    //point 0 on scale (value > mid)50+ (value > mid)50-
    var percent=50;
    if(value < min){
        percent=0;
    }else if(value > max) {
        percent=100;
    }else if(value > mid) {
        percent=Math.round(50 + ((value -mid)/(max-mid))*50);
    }else if (value < mid){
        percent=Math.round(((value -min)/(mid-min))*50);
    }
    new Effect.Morph('slider', {
        style: 'background-position:'+percent+'%',
        duration: 0.3
    });
    max=parseFloat(max/1000);
    mid=parseFloat(mid/1000);
    min=parseFloat(min/1000);
    
    $('scale').innerHTML='<div class="span-16 small bottom"><span id="smin">'+min +' kW</span><span id="smid">'+mid+' kW</span><span id="smax">'+max + ' kW</span></div>';
}


/*
 *for observing and showing details
    $$('.more').invoke('observe','click',function(e) {
        Element.extend(e.target.next('span')).toggle();
    })
*/

