﻿function Slider(config) {
    this.config = config;
    this.init();
}

Slider.prototype = {
    version : {
        author : 'ggkl',
        date : '2009-09-26'
    },

    config : null,

    init : function() {
        if (typeof(this.config.cobj) == "string") {
            this.config.cobj = document.getElementById(this.config.cobj);
        }

        this.removeChild(this.config.cobj);

        if (this.config.maxvalue <= this.config.minvalue) {
            return;
        }

        obj1 = createNode('div');
        this.config.cobj.appendChild(obj1);
        obj1.className = this.config.scn;
        obj1.style.width = this.config.siw + 'px';
        obj1.style.height = this.config.sih + 'px';

        this.config.sobj = obj1;
        this.config.cobj.style.cursor = 'pointer';

        var sliderobj = this;
        this.config.cobj.onclick = function(event){
            sliderobj.onclick.call(sliderobj, event);
        };
        this.config.sobj.onmousedown = function(event){
            sliderobj.ondragsart.call(sliderobj, event);
        };

        this.config.ch = this.config.ch || this.config.cobj.offsetHeight;
        this.config.cw = this.config.cw || this.config.cobj.offsetWidth;
        this.config.cobj.style.width = this.config.cw + 'px';
        this.config.cobj.style.height = this.config.ch + 'px';

        this.config.hv = this.config.hv || 'horizonal';
        if (this.config.hv == 'vertical') {
            this.config.sl = (this.config.ch - this.config.sih) / (this.config.maxvalue - this.config.minvalue);
        } else {
            this.config.sl = (this.config.cw - this.config.siw) / (this.config.maxvalue - this.config.minvalue);
        }

        this.config.maxvalue = this.config.maxvalue || 10;
        this.config.minvalue = this.config.minvalue || 1;
        this.value = this.value || this.config.minvalue;

        this.setstep();

        this.autostart();
    },

    setstep : function(value) {
        value = isNaN(Number(value)) ? this.value : value;

        this.oldvalue = this.value || this.config.minvalue;
        this.value = value || this.value || this.config.value;

        this.value = Math.min(this.value, this.config.maxvalue);
        this.value = Math.max(this.value, this.config.minvalue);
        if (this.config.hv == 'vertical')
            this.config.sobj.style.marginTop = parseInt((this.value - this.config.minvalue) * this.config.sl) + 'px';
        else
            this.config.sobj.style.marginLeft = parseInt((this.value - this.config.minvalue) * this.config.sl) + 'px';

        if (this.oldvalue == this.value) {
            return;
        }

        this.onchange.call(this);
    },

    movepos : function(hvpos) {

        if (this.config.hv == 'vertical')
            this.config.sobj.style.marginTop = hvpos + 'px';
        else
            this.config.sobj.style.marginLeft = hvpos + 'px';
    },

    onchange : function() {

    },

    onclick : function(event) {
        event = event || window.event;
        target = event.target || event.srcElement;

        if (target == this.config.sobj) {
            return;
        }

        coffset = getOffset(this.config.cobj);
        poffset = getPageScroll();

        hvpos = this.config.value * this.config.sl;
        if (this.config.hv == 'vertical') {
            hvpos = event.clientY + poffset.offsetTop - coffset.offsetTop;
        } else {
            hvpos = event.clientX + poffset.offsetLeft - coffset.offsetLeft;
        }

        step = hvpos / this.config.sl;
        if (step > parseInt(step)) {
            step = parseInt(step) + 1;
        }
        this.setstep(step);
    },

    ondragsart : function(event) {
        event = event || window.event;
        target = event.target || event.srcElement;
        this.autostop();

        sliderobj = this;
        document.onmouseup = function(event){
            sliderobj.ondragstop.call(sliderobj, event);
        };

        document.onmousemove = function(event){
            sliderobj.ondrag.call(sliderobj, event);
        };
    },

    ondragstop : function(event) {
        this.setstep();
        this.autostart();
        document.onmouseup = null;
        document.onmousemove = null;
    },

    ondrag : function(event) {
        event = event || window.event;
        target = event.target || event.srcElement;

        coffset = getOffset(this.config.cobj);
        poffset = getPageScroll();
        if (this.config.hv == 'vertical') {
            hvpos = event.clientY + poffset.offsetTop - coffset.offsetTop;
            hvpos = Math.min(hvpos, this.config.ch);
            hvpos = Math.max(0, hvpos);

            rhvpos = event.clientY + poffset.offsetTop - coffset.offsetTop - (this.config.sih / 2);
            rhvpos = Math.min(rhvpos, this.config.ch - this.config.sih);
            rhvpos = Math.max(0, rhvpos);
        } else {
            hvpos = event.clientX + poffset.offsetLeft - coffset.offsetLeft;
            hvpos = Math.min(hvpos, this.config.cw);
            hvpos = Math.max(0, hvpos);

            rhvpos = event.clientX + poffset.offsetLeft - coffset.offsetLeft - (this.config.siw / 2);
            rhvpos = Math.min(rhvpos, this.config.cw - this.config.siw);
            rhvpos = Math.max(0, rhvpos);
        }

        step = hvpos / this.config.sl;
        if (step > parseInt(step)) {
            step = parseInt(step) + 1;
        }
        this.setstep(step);
        this.movepos(rhvpos);
    },

    autostart : function() {
        if (this.config.autotimeout == undefined) {
            return;
        }
        var sliderobj = this;
        this.timer = setInterval(
            function(){
                sliderobj.autostep.call(sliderobj);
            },
            sliderobj.config.autotimeout);
    },

    autostop : function() {
        clearInterval(this.timer);
        this.timer = null;
    },

    autostep : function() {
        nextsetp = this.value + 1;
        if (nextsetp > this.config.maxvalue) {
            nextsetp = this.config.minvalue
        }
        this.setstep(nextsetp);
    },

    removeChild : function(obj) {
        while( obj.firstChild ) {
            this.removeChild(obj.firstChild);
            obj.removeChild(obj.firstChild);
        }
    }
};

function createNode(tagname, tagtype) {
    var str = '<' + tagname;
    if (tagname == 'input') {
        str += ' type="' + tagtype + '"';
    }
    str += '>';
    return document.createElement(tagname);
}

function getOffset(obj) {
    var offset = {offsetLeft: 0, offsetTop: 0};
    while (obj) {
        offset.offsetLeft += obj.offsetLeft;
        offset.offsetTop += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return offset;
}

function getPageScroll() {
   var XScroll,YScroll;
   var wObj = window;
   if(wObj.pageYOffset)
   {
       XScroll = wObj.pageXOffset;
       YScroll = wObj.pageYOffset;
   }
   else if(wObj.document.documentElement && wObj.document.documentElement.scrollTop)
   {
      XScroll = wObj.document.documentElement.scrollLeft;
      YScroll = wObj.document.documentElement.scrollTop;
   }else if(wObj.document.body)
   {
      XScroll = wObj.document.body.scrollLeft;
      YScroll = wObj.document.body.scrollTop;
   }
   return {offsetLeft: XScroll, offsetTop: YScroll};
}



