﻿(function() {

    try {
        var YMD = YAHOO.My.Debug;
        var Debug = function(sText, nType) {
            YMD.trace(sText, nType);
        };
    } catch (err) { Debug = function() { }; };


    var YUE = YAHOO.util.Event;
    var YUD = YAHOO.util.Dom;
    var YUC = YAHOO.util.CustomEvent;
    var isDrag = false;

    if (window.opera) {
        YUD.addClass(document.documentElement, 'borwser-opera');
    };




    var stopAction = function(e) {
        YUE.stopEvent(e);
        return false;
    };

    var setValue = function(nFrom, nTo, dFromBtn, bNotChangePosition) {

        var nOldFrom = this.from;
        var nOldTo = this.to;

        var sElementId = this.id;
        var dEl = document.getElementById(sElementId);
        var dBox = document.getElementById(sElementId + '-box');
        var dC0 = document.getElementById(sElementId + '-ctrl-0');
        var dC1 = document.getElementById(sElementId + '-ctrl-1');
        var dC2 = document.getElementById(sElementId + '-ctrl-2');
        var dLn1 = document.getElementById(sElementId + '-line-1');
        var nSliderWidth = dBox.offsetWidth;

        var nf = Math.min(nTo, Math.max(0, nFrom));
        var nt = Math.min(100, Math.max(nFrom, nTo));
        if (isNaN(nf)) { nf = 0 };
        if (isNaN(nt)) { nf = 100 };
        nf = Math.round(nf);
        nt = Math.round(nt);




        this.from = nf;
        this.to = nt;

        if (!bNotChangePosition) {
            var nX1 = (nSliderWidth * (this.from / 100));
            var nX2 = (nSliderWidth * (this.to / 100)) - dC2.offsetWidth;

            if (dFromBtn == dC1) {
                dC1.style.left = nX1 + 'px';
            }
            else if (dFromBtn == dC2) {
                dC2.style.left = nX2 + 'px';
            }
            else {
                dC1.style.left = nX1 + 'px';
                dC2.style.left = nX2 + 'px';
            };

            var nLeft = parseInt(dC1.style.left) - 1;
            var nRight = parseInt(dC2.style.left) + dC2.offsetWidth - 1;
            dC0.style.left = dC1.style.left;
            dC0.style.width = nRight - nLeft + 'px';
        };

        //Debug(  ['from:' , this.from  , ' ; to:' , this.to ].join('') ) ;
        if (!!(nOldFrom - nf) || !!(nOldTo - nt)) {
            this.onChange.fire(nf, nt);
        };
        return [nX1, nX2];
    };

    var onMouseDown = function(e, oSelf) {

        var dBtn = this;

        var sElementId = oSelf.id;
        var dEl = document.getElementById(sElementId);
        var dBox = document.getElementById(sElementId + '-box');
        var dC0 = document.getElementById(sElementId + '-ctrl-0');
        var dC1 = document.getElementById(sElementId + '-ctrl-1');
        var dC2 = document.getElementById(sElementId + '-ctrl-2');
        var dLn1 = document.getElementById(sElementId + '-line-1');
        var nSliderWidth = dBox.offsetWidth;
        var nOffsetLeft = YUD.getX(dBox) || YUD.getX(dEl);
        var nLimitLeft = 0;
        var nLimitRight = nSliderWidth - dBtn.offsetWidth;
        var nSliderWidth = nSliderWidth;


        if (dBtn.className == 'ctrl-2') {
            nLimitLeft = parseInt(dC1.style.left) + dC1.offsetWidth;
        }
        else {
            nLimitRight = parseInt(dC2.style.left) - dC1.offsetWidth;
        };

        var onMouseUp = function(e) {

            YUE.stopEvent(e);
            YUE.removeListener(document, 'mousemove', onMouseMove);
            YUE.removeListener(document, 'mouseup', onMouseUp);
            isDrag = false;
            oSelf.onMouseUp.fire(e);


            return false;
        };

        var onMouseMove = function(e) {

            var nEx = e.clientX - nOffsetLeft;
            /*
            Debug( 'nOffsetLeft = ' + nOffsetLeft  );
            Debug( 'limit = ' + [ nLimitLeft , nLimitRight ] ,2 );
            Debug( 'e.clientX = ' + e.clientX  );
            Debug( 'nEx = ' + nEx );
            */

            if (!isDrag) {
                onMouseUp.call(document, e, aArg);
                return;
            };

            nEx = Math.max(nLimitLeft, nEx);
            nEx = Math.min(nEx, nLimitRight);
            if (dBtn == dC1) {
                oSelf.from = Math.abs((nEx / (nSliderWidth - dC1.offsetWidth - dC2.offsetWidth)) * 100);
            }
            else {
                oSelf.to = Math.abs((nEx / (nSliderWidth - dC1.offsetWidth - dC2.offsetWidth)) * 100);
            };

            if (Math.abs(oSelf.from - oSelf.to) <= 3) {
                oSelf.from = oSelf.to
            };




            setValue.call(oSelf, oSelf.from, oSelf.to, dBtn, true);
            if (dBtn == dC1) {
                dBtn.style.left = nEx + 'px';
                var nLeft = parseInt(dC1.style.left) - 1;
                var nRight = parseInt(dC2.style.left) + dC2.offsetWidth - 1;
                dC0.style.left = dC1.style.left;
                dC0.style.width = nRight - nLeft + 'px';

            }
            else {
                dBtn.style.left = nEx + 'px';
                var nLeft = parseInt(dC1.style.left) - 1;
                var nRight = parseInt(dC2.style.left) + dC2.offsetWidth - 1;
                dC0.style.left = dC1.style.left;
                dC0.style.width = nRight - nLeft + 'px';

            };




            YUE.stopEvent(e);
            oSelf.onSlide.fire(e, oSelf.from, oSelf.to);
            return false;
        };


        YUE.on(document, 'mousemove', onMouseMove);
        YUE.on(document, 'mouseup', onMouseUp);
        isDrag = true;
        YUE.stopEvent(e);
        oSelf.onMouseDown.fire(e);
        return false;

    };

    var onMouseDownOnBackground = function(e, oSelf) {
        var sElementId = oSelf.id;

        var dBox = document.getElementById(sElementId + '-box');
        var dC0 = this;
        var dC1 = document.getElementById(sElementId + '-ctrl-1');
        var dC2 = document.getElementById(sElementId + '-ctrl-2');
        var dLn1 = document.getElementById(sElementId + '-line-1');
        var nSliderWidth = dBox.parentNode.offsetWidth || dBox.offsetWidth;
        var nAvailRangeWidth = nSliderWidth - dC1.offsetWidth - dC2.offsetWidth;
        var nOffsetLeft = YUD.getX(dBox);

        var ndC0Width = dC0.offsetWidth;
        var ndC2Width = dC2.offsetWidth;
        var nLimitRight = nSliderWidth - ndC0Width;
        var nMouseDownX = e.clientX - YUD.getX(dC0);
        var nRange = oSelf.to - oSelf.from;

        var onMouseMove = function(e) {

            if (!isDrag) {
                onMouseUp.call(document, e);
                return;
            };
            var nEx = Math.max(0, e.clientX - nOffsetLeft - nMouseDownX);
            if (nEx < nLimitRight) {

                var nTo = Math.min(100, Math.round(((nEx + ndC0Width) / nAvailRangeWidth) * 100));
                var nFrom = nTo - nRange;

                setValue.call(oSelf, nFrom, nTo, null, true);
                dC0.style.left = nEx + 'px';
                dC0.style.width = ndC0Width + 'px';
                dC2.style.left = nEx + ndC0Width - ndC2Width + 1 + 'px';
            }
            else {
                dC0.style.left = nLimitRight + 'px';
                dC0.style.width = ndC0Width + 'px';
                dC2.style.left = nLimitRight + ndC0Width - ndC2Width + 1 + 'px';
            };
            dC1.style.left = dC0.style.left;
            oSelf.onSlide.fire(e, oSelf.from, oSelf.to);


        };

        var onMouseUp = function(e) {
            YUE.removeListener(document, 'mousemove', onMouseMove);
            YUE.removeListener(document, 'mouseup', onMouseUp);
            isDrag = false;
        };


        YUE.on(document, 'mousemove', onMouseMove);
        YUE.on(document, 'mouseup', onMouseUp);
        isDrag = true;
        YUE.stopEvent(e);


        oSelf.onMouseDown.fire(e);
        return false;
    };



    YAHOO.widget.RangeSlider = function(sElementId, oAttr) {

        var oSelf = this;

        oSelf.onMouseDown = new YUC('mousedown');
        oSelf.onMouseUp = new YUC('mouseup');
        oSelf.onSlide = new YUC('slide');
        oSelf.onLoad = new YUC('load');
        oSelf.onChange = new YUC('change');

        if (typeof (oAttr) != 'object') {
            oAttr = {};
        };

        var init = function() {
            var dEl = this;

            dEl.className = 'yui-range-slider';
            if (dEl.tagName != 'SPAN') {
                throw Error('YAHOO.widget.RangeSlider Error: \n Element[' + sElementId + ']\'s tagName is not SPAN');
                return;
            };


            oSelf.type = (YUD.hasClass(dEl, 'yui-range-slider-v')) ? 2 : 1;



            YUD.addClass(dEl, 'yui-range-slider yui-range-slider-' + oSelf.type);

            var sTagName = window.opera ? 'span' : 'button';
            dEl.innerHTML =
        [
            '<span class="s-hd">',

            '<span class="s-line-1" id="', sElementId, '-line-1"></span>',
            '<', sTagName, ' class="ctrl-0" id="', sElementId, '-ctrl-0" ></', sTagName, '>',
            '<button  id="', sElementId, '-ctrl-1" class="ctrl-1">&nbsp;</button>',
            '<button   id="', sElementId, '-ctrl-2" class="ctrl-2">&nbsp;</button>',
            '</span>',
            '<', sTagName, ' hidefocus="hidefocus" class="s-bd" id="', sElementId, '-box" >&nbsp;</', sTagName, '>',

        ].join('');




            var dBox = document.getElementById(sElementId + '-box');
            dBox.style.width = (oAttr.width || 200) + 'px';
            dBox.style.height = (oAttr.height || 23) + 'px';

            var dC0 = document.getElementById(sElementId + '-ctrl-0');
            var dC1 = document.getElementById(sElementId + '-ctrl-1');
            var dC2 = document.getElementById(sElementId + '-ctrl-2');
            var dLn1 = document.getElementById(sElementId + '-line-1');
            var nSliderWidth = dBox.offsetWidth;



            dLn1.style.width = parseInt(dBox.style.width) - 4 + 'px';
            dC0.style.height = dC1.style.height = dC2.style.height = (parseInt(dBox.style.height) - 4) + 'px';

            dLn1.style.top = (oAttr.height || 23) / 2 + 'px';


            YUE.on(dC1, 'mousedown', onMouseDown, oSelf);
            YUE.on(dC2, 'mousedown', onMouseDown, oSelf);
            YUE.on(dC0, 'mousedown', onMouseDownOnBackground, oSelf);
            YUE.on(dEl, 'selectstart', stopAction);
            YUE.on(dBox, 'mousedown', stopAction);


            setValue.call(oSelf, oSelf.from, oSelf.to);
            oSelf.readyState = 'complete';

            oSelf.onLoad.fire();
            //Debug('slidebar element:  ' + sElementId + ' complete' , 2);




        };

        this.id = sElementId;
        YUE.onAvailable(sElementId, init);
    };

    YAHOO.widget.RangeSlider.prototype =
{
    id: '',
    readyState: 'uninitialized',
    type: 1,
    from: 10,
    to: 90,
    setValue: function(nFrom, nTo) {
        var oSelf = this;
        nFrom = isNaN(nFrom) ? 0 : (nFrom || 0);
        nTo = isNaN(nTo) ? 100 : (nTo || 100);

        nFrom = Math.max(0, nFrom);
        nTo = Math.min(100, nTo);
        nFrom = Math.min(nFrom, nTo);

        var fAction = function() {
            setValue.call(oSelf, nFrom, nTo);
        };

        if (oSelf.readyState != 'complete') {
            oSelf.onLoad.subscribe(fAction);
        }
        else {
            fAction();
        };


    },

    getValue: function() {
        return [this.from, this.to];
    },

    getElement: function() {
        return document.getElementById(this.id);
    }
};
})();
