/*
 * File: ScrollBar.js
 *
 *
 * Development Started at: 27.10.2006
 *
 * <script SRC="js/ScrollBar.js"></script>
 *
 * @author Daniel Lyubomirov
 */
ScrollBarV.defaultSkinConfig = {
		"padding" : 2,
		"topOffset" : 5,
		"bottomOffset" : 5,
		"scpaneOffset" : 5,
		"scpaneBorderWidth" : 0,
		"scpaneBorderColor" : "#659E00",
		"scpaneBGColor" : "#CCCCCC",
		"scBorderWidth" : 1,
		"scBorderColor" : "#659E00",
		"scBGColor" : "#659E00",
		"width" : 8,
		"butheight" : 5
}

function ScrollBarV(place, container, content, params)
{
    // used to attache events
    var self = this;

    this.place = place;
    this.container = container;
    this.content = content;
    this.params = params;

    this.params["skin"] = this.params["skin"] ? this.params["skin"] : "default";
    this.params["scrollBy"] = this.params["scrollBy"] != undefined ? this.params["scrollBy"] : 4;

    this.skinpath = this.params["path"] + "/";
    //preload image resouces
    this.images = new Object();
    this.images["upbutton"] = new Image();
    this.images["upbutton"].src = this.skinpath + "bt-up.gif";
    this.images["downbutton"] = new Image();
    this.images["downbutton"].src = this.skinpath + "bt-down.gif";
    this.images["scBGImage"] = new Image();
    this.images["scBGImage"].src = this.skinpath + "Thumb1.gif";

    if ( params["createnow"])
        this.OnLoad();
    else
        JSEvents.AttachEvent(window, "load", function(evt) { self.OnLoad(evt); }, false, false);

}

ScrollBarV.prototype.OnLoad = function(event)
{
    // TODO : there will be dynamic lodaing of js scripts
    //LoadJScript("default/config.js");
    if ( typeof ScrollBarV[this.params["skin"] + "SkinConfig"] == "undefined" )
    {
        alert("ScrollBarV[Error] => Cannot load skin config file.");
        return;
    }
    this.skinConf = ScrollBarV[this.params["skin"] + "SkinConfig"];

    this.place = GetElement(this.place);
    this.container = GetElement(this.container);
    this.content = GetElement(this.content);


    if ( (!this.place) || (!this.container) || (!this.content))
    {
        alert("ScrollBarV[Error] => Required objects not supplied.");
        return;
    }

    //debug.Dump(GetElementClientRect(this.place));
    this.crClientHeight = GetElementClientRect(this.container).height-20;
    var containerwidth = GetElementClientRect(this.container).width;
    //debug.Dump(GetElementClientRect(this.content));
    this.ctHeight = GetElementRect(this.content).height;
    this.height =  this.crClientHeight - 2*this.skinConf["padding"];//GetElementClientRect(this.place).height;	
    this.place.style.height = this.height + "px";
    this.place.style.padding = this.skinConf["padding"] + "px";
    this.place.style.width = this.skinConf["width"] + "px";

    //debug.Dump(this.ctHeight);
    //debug.Dump(this.crClientHeight);

    if ( this.crClientHeight+20 >= this.ctHeight )
    {
        //this.visible = true;
        //this.place.style.visibility = "hidden";
		this.place.style.display = "none";
        return;
    }else{
		this.place.style.display = "block";
	}


    //if ( this.params[""])
    this.scpaneSize = this.height - 2*this.skinConf["scpaneBorderWidth"] - 2*this.skinConf["butheight"] - 2*this.skinConf["scpaneOffset"];
    this.scrollDelta = 100 - this.crClientHeight*100/this.ctHeight;
    this.scrollSize = Math.round(this.scpaneSize*this.crClientHeight/this.ctHeight);// - this.skinConf["scBorderWidth"]*2;
    this.scrollPathLength = this.scpaneSize - this.scrollSize;//  + this.skinConf["scBorderWidth"]*2;
    // create document objects
    this.upimg = document.createElement("img");
    this.upimg.src = this.images["upbutton"].src;
    this.upimg.style.position = "relative";
    this.upimg.style.top = "0px";
    this.upimg.style.left = "0px";
    this.upimg.style.cursor = "pointer";
    this.upimg.style.border = "none";
    this.upimg.style.display = "block";
    this.place.appendChild(this.upimg);

    this.scpane = document.createElement("div");
    this.scpane.style.position = "relative";
    this.scpane.style.height = this.scpaneSize + "px";
    this.scpane.style.border = "solid";
    this.scpane.style.borderWidth = this.skinConf["scpaneBorderWidth"] + "px";
    this.scpane.style.borderColor = this.skinConf["scpaneBorderColor"];
    this.scpane.style.backgroundColor = this.skinConf["scpaneBGColor"];
    this.scpane.style.display = "block";
    this.scpane.style.left = "7px";
    this.scpane.style.top = this.skinConf["scpaneOffset"] + "px";
    this.place.appendChild(this.scpane);

    this.scroll = document.createElement("div");
    this.scroll.style.position = "relative";
    this.scroll.style.height = (this.scrollSize - this.skinConf["scBorderWidth"]*2)  + "px";
    this.scroll.style.border = "solid";
    this.scroll.style.borderWidth = this.skinConf["scBorderWidth"] + "px";
    this.scroll.style.borderColor = this.skinConf["scBorderColor"];
    this.scroll.style.background = this.skinConf["scBGColor"];
    this.scroll.style.top = "0px";
    this.scroll.style.overflow = "hidden";
    //this.scroll.style.cursor = "pointer";
    this.scpane.appendChild(this.scroll);

    this.downimg = document.createElement("img");
    this.downimg.src = this.images["downbutton"].src;
    this.downimg.style.position = "relative";
    this.downimg.style.top = this.skinConf["scpaneOffset"]*2 + "px";//this.height - this.images["downbutton"].height*2 + "px";
    this.downimg.style.left = "0px";
    this.downimg.style.display = "block";
    this.downimg.style.cursor = "pointer";
    this.place.appendChild(this.downimg);

    if ( this.params["sweepcontent"] )
        this.container.style.width = containerwidth - GetElementRect(this.place).width + "px";
    //this.content.style.width = contentwidth - GetElementRect(this.place).width + "px";

    this.pos = 0;

    if ( this.params["position"] )
        this.SetAbsolutePosition(this.params["position"]);
    else
        this.SetPosition(0);
    this.dragging = false;
    this.scrolling = false;
    // Attach event handlers
    var self = this;
    JSEvents.AttachEvent(this.scroll, "mousedown", function (evt) { self.ScrollOnMouseDown(evt); }, false, false);
    JSEvents.AttachEvent(document, "mouseup", function(evt) { self.ScrollOnMouseUp(evt); }, false, false);
    JSEvents.AttachEvent(document, "mousemove", function(evt) {self.OnMouseMove(evt);}, false, false );
    JSEvents.AttachEvent(this.scpane, "mousedown", function(evt) {self.scpaneOnMouseDown(evt); } , false, false);
    JSEvents.AttachEvent(this.scpane, "mouseout", function(evt) {self.StopScrolling(evt); }, false, false);
    JSEvents.AttachEvent(this.scpane, "mouseup", function(evt) {self.StopScrolling(evt); }, false, false);
    JSEvents.AttachEvent(this.downimg, "mousedown", function(evt) {self.Scroll(evt, 1);}, false, false);
    JSEvents.AttachEvent(this.upimg, "mousedown", function(evt) {self.Scroll(evt, -1);}, false, false);
    JSEvents.AttachEvent(this.downimg, "mouseup", function(evt) {self.StopScrolling(evt) }, false, false);
    JSEvents.AttachEvent(this.upimg, "mouseup", function(evt) {self.StopScrolling(evt) }, false, false);
    JSEvents.AttachEvent(this.downimg, "mouseout", function(evt) {self.StopScrolling(evt) }, false, false);
    JSEvents.AttachEvent(this.upimg, "mouseout", function(evt) {self.StopScrolling(evt) }, false, false);
    JSEvents.AttachEvent(this.content, "DOMMouseScroll" , function(evt) {self.OnMouseWheel(evt); }, false, false);
    /** IE/Opera. */
    //this.content.scroller = this;
    this.container.onmousewheel = function(evt) { self.OnMouseWheel(evt);};


    /*var win = document.createElement("div");
    win.style.position = "absolute";
    win.style.overflow = "hidden";
    win.style.border = "none";
    win.style.backgroundColor = "red";
    document.body.appendChild(win);
    SetElementRect(win, GetElementRect(this.scroll));*/


}

ScrollBarV.prototype.SetPosition = function(value)
{
    if ( (value == -1) )
    {
        value = this.scrollDelta;
    }

    value = Math.round(value*this.scpaneSize/100);

    this.SetAbsolutePosition(value);
}

ScrollBarV.prototype.SetAbsolutePosition = function(value)
{
    if ( value < 0 )
        value = 0;
    if ( value > this.scrollPathLength )
        value = this.scrollPathLength;

    if ( this.pos != value )
    {
        this.pos = value;
        this.scroll.style.top = value + "px";
        this.SetContentPos();
    }
}


ScrollBarV.prototype.GetAbsolutePosition = function()
{
    return this.pos;
}

ScrollBarV.prototype.SetContentPos = function()
{
    var pos = Math.round((this.pos*this.ctHeight/this.scpaneSize));

    if ( pos < 0 ) pos = 0;
    if ( pos > (  this.ctHeight ))
        pos = this.ctHeight;

    this.content.style.top = -pos + "px";
}



ScrollBarV.prototype.ScrollOnMouseDown = function(evt)
{
    this.dragging = true;
    this.dy = this.pos - evt.screenY;

    JSEvents.StopPropagation(evt);
    JSEvents.PreventDefault(evt);
}

ScrollBarV.prototype.ScrollOnMouseUp = function(evt)
{
    this.dragging = false;
}

ScrollBarV.prototype.OnMouseMove = function(evt)
{

    if ( !this.dragging )
        return;

    this.SetAbsolutePosition(this.dy + evt.screenY);
    JSEvents.StopPropagation(evt);
    JSEvents.PreventDefault(evt);
}

ScrollBarV.prototype.scpaneOnMouseDown = function(evt)
{
    if ( !this.scrolling )
    {
        var self = this;
        this.scrolling = true;
        var delta = JSEvents.GetMousePos(evt).top < GetElementRect(this.scroll).top ? -this.scrollSize : this.scrollSize;
        this.ScrollBy(delta);

        setTimeout(function() {
            if ( self.scrollingInterval )
                clearInterval(self.scrollingInterval);
            self.scrollingInterval = setInterval(function() {self.ScrollBy(delta); }, 10);
        }, 200);
    }

    JSEvents.StopPropagation(evt);
    JSEvents.PreventDefault(evt);
}

ScrollBarV.prototype.Scroll = function(evt, direction)
{
    if ( !this.scrolling )
    {
        var self = this;
        this.scrolling = true;
        var delta = this.params["scrollBy"]*direction;
        this.ScrollBy(delta);

        setTimeout(function() {
            if ( self.scrollingInterval )
                clearInterval(self.scrollingInterval);
            self.scrollingInterval = setInterval(function() {self.ScrollBy(delta); }, 20);
        }, 200);
    }

    evt.returnValue = false;
    evt.cancelBubble = true;

    JSEvents.StopPropagation(evt);
    JSEvents.PreventDefault(evt);
}

ScrollBarV.prototype.StopScrolling = function(evt)
{
    this.scrolling = false;
}

ScrollBarV.prototype.ScrollBy = function(delta)
{
    var self = this;
    if ( this.scrolling && ((delta > 0 && this.pos < this.scrollPathLength )||( delta < 0  && this.pos > 0)) )
    {
        this.SetAbsolutePosition(this.pos + delta);
    }
    else
    {
        clearInterval(this.scrollingInterval);
    }

}


ScrollBarV.prototype.OnMouseWheel = function(event)
{
    var delta = 0;
    if (!event) /* For IE. */
            event = window.event;
    if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta/120;
            /** In Opera 9, delta differs in sign as compared to IE.
             */
            if (window.opera)
                    delta = -delta;
    } else if (event.detail) { /** Mozilla case. */
            /** In Mozilla, sign of delta is different than in IE.
             * Also, delta is multiple of 3.
             */
            delta = -event.detail/3;
    }
    /** If delta is nonzero, handle it.
     * Basically, delta is now positive if wheel was scrolled up,
     * and negative, if wheel was scrolled down.
     */
    var self = this;//JSEvents.GetEvtTarget(event);//.scroller;
    //self = self.scroller;
    //debug.Dump(self);
    if (delta < 0)
    {
        //alert("leli");
        self.SetAbsolutePosition(self.GetAbsolutePosition() + 10);
    }
    else if ( delta > 0 )
    {
        self.SetAbsolutePosition(self.GetAbsolutePosition() - 10);
    }
    /** Prevent default actions caused by mouse wheel.
     * That might be ugly, but we handle scrolls somehow
     * anyway, so don't bother here..
     */
    if (event.preventDefault)
            event.preventDefault();
	event.returnValue = false;
}
