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 () {
    YAHOO.namespace('example');

    var Dom = YAHOO.util.Dom;

    // Jangkauan Slider 500 pixels
    var range = 500;

    // Jarak Pergeseran ketika Panah diGeser
    var tickSize = 0;

    // Jarak Antar Panah
    var minThumbDistance = 10;

    // Jarak Awal Dua Panah
    var initValues = [0,500];

    var cf = 1000/(range - 10);

    // Set up a function to convert the min and max values into something useful
    var convert = function (val) {
        return Math.round(val * cf + 0);
    };

    // Slider set up is done when the DOM is ready
    YAHOO.util.Event.onDOMReady(function () {
        var demo_bgtanah = Dom.get("demo_bgtanah"),
            info    = Dom.get("demo_infotanah"),
            eng     = Dom.get("eng_contanah"),
            from    = Dom.get("demo_fromtanah"),
            to      = Dom.get("demo_totanah"),
            tanahmin      = Dom.get("demo_tanahmin"),
            tanahmax      = Dom.get("demo_tanahmax");

        // Create the DualSlider
        var slider = YAHOO.widget.Slider.getHorizDualSlider(demo_bgtanah,
            "demo_min_thumbtanah", "demo_max_thumbtanah",
            range, tickSize, initValues);

        slider.minRange = minThumbDistance;

        // ======= Tambahan Untuk Menambahkan Warna Kedua Agar bisa Berubah-ubah == Awal
        YAHOO.lang.augmentObject(slider, {
            _status : 'ok',
            _highlight : Dom.get("demo_highlighttanah"),

            getStatus : function () { return this._status; },

            updateHighlight : function () {
                var delta = this.maxVal - this.minVal,
                    newStatus = 'ok';

                if (delta < 13) {
                    newStatus = 'boom';
                } else if (delta < 75) {
                    newStatus = 'danger';
                } else if (delta < 150) {
                    newStatus = 'caution';
                }

                if (this._status !== newStatus) {
                    // Update the highlight class if status is changed
                    Dom.replaceClass(this._highlight,this._status,newStatus);
                    this._status = newStatus;
                }
                if (this.activeSlider === this.minSlider) {
                    // If the min thumb moved, move the highlight's left edge
                    Dom.setStyle(this._highlight,'left', (this.minVal + 0) + 'px');
                }
                // Adjust the width of the highlight to match inner boundary
                Dom.setStyle(this._highlight,'width', Math.max(delta + 10,0) + 'px');
            }
        },true);
        // Attach the highlight method to the slider's change event
        slider.subscribe('change',slider.updateHighlight,slider,true);
        // ======= Tambahan Untuk Menambahkan Warna Kedua Agar bisa Berubah-ubah == Akhir

        // Custom function to update the text fields, the converted value
        // report and the slider's title attribute
        var updateUI = function () {
        	sqft1 = 10.764,
            from.value = slider.minVal;
            to.value   = slider.maxVal;
             tanahmin.value   = convert(slider.minVal);
            tanahmax.value   = convert(slider.maxVal-10);



            // Update the converted values and the slider's title.
            // Account for the thumb width offsetting the value range by
            // subtracting the thumb width from the max value.
            var gw_min,
                sqft = 10.764,
                min = convert(slider.minVal),
                max = convert(slider.maxVal - 10);

            if( max == 0){
                max = 2;
            }

            // Konversi Ke Square Feet
            gw_min = min * sqft;
            gw_max = max * sqft;
            info.innerHTML = "<strong><font style=color:#ff9d01;>Pilih Luas Tanah :</font> " + min + "</strong>&nbsp;M<sup>2</sup>&nbsp;("+ (gw_min).formatMoney(0, '.', ',')+"&nbsp;Sq.Ft)"+
                             "&nbsp;<strong>-</strong>&nbsp;<strong>" + max + "</strong>&nbsp;M<sup>2</sup>&nbsp;(" + (gw_max).formatMoney(0, '.', ',') + "&nbsp;Sq.Ft)</br>";
            demo_bgtanah.title  = "";
        };

        // Subscribe to the dual thumb slider's change and ready events to
        // report the state.
        slider.subscribe('ready', updateUI);
        slider.subscribe('change', updateUI);

        // Wire up the button to update the slider
        YAHOO.util.Event.on('demo_btn','click',function () {
            // Get the int values from the inputs
            var min = Math.abs(parseInt(from.value,10)|0),
                max = Math.abs(parseInt(to.value,10)|0);

            if (min > max) {
                var hold = min;
                min = max;
                max = hold;
            }

            // Verify the values are in range
            min = Math.min(min,range - 30);
            max = Math.max(Math.min(max,range),min + 20 + minThumbDistance);

            // Set the new values on the slider
            slider.setValues(min,max);
        });
        // Attach the slider to the YAHOO.example namespace for public probing
        YAHOO.example.slider = slider;
    });
})();