var evts = [];
function addListener(evt, func, priority)
{
    if(typeof evts[evt] == 'undefined')
    {
        evts[evt] = [];

        if (window.attachEvent) 
        { 
            window.attachEvent("on" + evt, eventHandler);
        } 
        else
        {
            window.addEventListener(evt, eventHandler , false);
        }
    }
    evts[evt].push([priority, func]);
}

function compare(a, b)
{
    return a[0] - b[0];
}

function eventHandler(evt)
{
    var evt = (!evt) ? window.event : evt;
    evts[evt.type].sort(compare);
    for(var i=0; i<evts[evt.type].length; i++)
    {
        var evnt = evts[evt.type][i][1];
        if (evnt != null)
          evnt();
    }
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
    while(1) 
    {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function formatFloat(pFloat, pDp){
    var m = Math.pow(10, pDp);
    return parseInt(pFloat * m, 10) / m;
}


function slider(div)
{
    this.divSlider = div;

    for(var i=0;i<div.childNodes.length;i++)
    {
        if(div.childNodes[i].className=="track")
        {
            this.divTrack = div.childNodes[i];
        }          
        else if(div.childNodes[i].className=="handle")
        {
            this.divHandle = div.childNodes[i];
        }          
    }

    var instance = this; 
    if (document.attachEvent) 
    { 
        document.attachEvent("onmousedown", function(evnt) { instance.sliderMouseDown(evnt) });
    } 
    else
    {
        document.addEventListener("mousedown", function(evnt) { instance.sliderMouseDown(evnt) } , false);
    }
    this.tracking = false;
    this.left = 0;
    this.x = 0;
}


slider.prototype.sliderMouseDown = function(evnt)
{
    this.left = findPosX(this.divTrack) + 8;
    var evnt = (!evnt) ? window.event : evnt; // The mousemove event
    this.tracking = true;
    var instance = this; 
    if (document.attachEvent) 
    { 
        document.attachEvent("onmousemove", function(evnt) { instance.sliderMouseMove(evnt) });
        document.attachEvent("onmouseup", function() { instance.sliderMouseUp() });
    } 
    else
    {
        document.addEventListener("mousemove", function(evnt) { instance.sliderMouseMove(evnt) } , false);
        document.addEventListener("mouseup", function() { instance.sliderMouseUp() } , false);
    }
}
slider.prototype.sliderMouseMove = function(evnt)
{
    var evnt = (!evnt) ? window.event : evnt; // The mousemove event
    if (this.tracking)
    {
        var tmp = this.x;
        this.setAbsolute(evnt.clientX - this.left);
        if(tmp != this.x && this.onChange != null)
        {
            this.onChange((100 * this.x) / parseInt(this.divTrack.style.width) );
        }
        return false
    }
}
slider.prototype.sliderMouseUp = function()
{
    this.tracking = false;
    if (document.detachEvent) 
    { 
        document.detachEvent("onmousemove", function(evnt) { instance.sliderMouseMove(evnt) });
        document.detachEvent("onmouseup", function() { instance.sliderMouseUp() });
    } 
    else
    {
        document.removeEventListener("mousemove", function(evnt) { instance.sliderMouseMove(evnt) } , false);
        document.removeEventListener("mouseup", function() { instance.sliderMouseUp() } , false);
    }
}
slider.prototype.setPercent = function(val)
{
    this.setAbsolute(Math.round(parseInt(this.divTrack.style.width) * val / 100));
}
slider.prototype.setAbsolute = function(val)
{
    if(val>0 && val<parseInt(this.divTrack.style.width))
    {
        this.x = val;
        this.divHandle.style.left = this.x + "px";
    }
}

function invokePostBack(target) {
    var pattern = /_/g;
    var a = target.id.replace(pattern, '$');
    __doPostBack(a, '');
}