// vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker:             
// +-------------------------------------------------------------------------+
// | OSC Content Management System                                           |
// | Copyright (C) 2004 2005 by the OSC Development Team.                    |
// | http://www.OpenSourceClub.org/                                          |
// +-------------------------------------------------------------------------+
// | License:                                                                |
// |                                                                         |
// | This file is part of OSC.                                               |
// |                                                                         |
// | OSC is free software; you can redistribute it and/or modify             |
// | it under the terms of the GNU General Public License as published by    |
// | the Free Software Foundation; either version 2 of the License, or       |
// | (at your option) any later version.                                     |
// |                                                                         |
// | OSC is distributed in the hope that it will be useful,                  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            |
// | GNU General Public License for more details.                            |
// |                                                                         |
// | You should have received a copy of the GNU General Public License       |
// | along with OSC; if not, write to the Free Software                      |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
// +-------------------------------------------------------------------------+
// | Author: Armen Baghumian <armen@OpenSourceClub.org>                      |
// +-------------------------------------------------------------------------+
// $Id$
// ---------------------------------------------------------------------------

// Tested on:
//      Firefox 1.0.4 on Linux Debian sarge
//      Konqueror 3.2.2 on Linux Debian sarge
//      Internet Explorer 6.0 on Wine
//      Internet Explorer 5.5sp2 on Wine
//      Internet Explorer 5.01sp2 on Wine

var scrollActive = false, scrollStop = true, scrollIncrement = 10, scrollInterval = 60, viewWidth = 0, navWidth = 0;

function adjustTabbedBlock()
{
    // KNOWN BUG:
    // On firefox when you click on tap and then scroll completely left or right we have 
    // free space on left or right.

    // KNOWN BUG:
    // On Internet Explorer 5.0 we have ugly bug. right border and bottom border
    // of a tag overflowd from view.
    
    navWidth = 0;
    var ul   = xGetElementById("tabbed_block_ul");
    var li   = xFirstChild(ul);
    
    while (li) {
        navWidth += xWidth(li);
        li        = xNextSib(li);
    }
    
    viewWidth     = xWidth("tabbed_block_content");
    var nMaxWidth = Math.max(viewWidth, navWidth);
    xWidth("tabbed_block_nav", nMaxWidth);
    xWidth("tabbed_block_view", viewWidth);

    // When we are on end of right side of buffer if size of window incrise
    // we must move content of buffer to right side of window
    var nav = xGetElementById('tabbed_block_nav');
    if (xLeft(nav) < -(Math.abs(navWidth-viewWidth))) {
        xLeft(nav, -(Math.abs(navWidth-viewWidth)));
    }
    // When viewWidth >= navWidth (all content of buffer viewable) we must
    // set nav xLeft to 0
    if (viewWidth >= navWidth) {
        xLeft(nav, 0);
    }
    
    scrollOnLoad();
}

function onScrollRightStart()
{
    if (!scrollActive) {
        scrollStop = false;
        onScrollRight();
    }
    showLeftScrollButton();
}

function onScrollRight()
{
    if (!scrollStop) {
        scrollActive = true;
        setTimeout('onScrollRight()', scrollInterval);
        var nav = xGetElementById("tabbed_block_nav");
        
        var x = xLeft(nav) - scrollIncrement;

        if (x >= -(Math.abs(navWidth-viewWidth))) {
            xLeft(nav, x);
        } else {
            // This commented due bug on konqueror 3.2.2 (when right or left 
            // scroll button disappear konqueror make content of tabbed block selected)
            // onScrollStop();
        }
    }
}

function onScrollLeftStart()
{
    if (!scrollActive) {
        scrollStop = false;
        onScrollLeft();
    }
    showRightScrollButton();
}

function onScrollLeft()
{
    if (!scrollStop) {
        scrollActive = true;
        setTimeout('onScrollLeft()', scrollInterval);
        var nav = xGetElementById("tabbed_block_nav");
        
        var x = xLeft(nav) + scrollIncrement;
        if (x <= 0) {
            xLeft(nav, x);
        } else {
            // This commented due bug on konqueror 3.2.2 (when right or left 
            // scroll button disappear konqueror make content of tabbed block selected)
            // onScrollStop();
        }
    }
}

function onScrollStop()
{
    scrollStop   = true;
    scrollActive = false;

    showLeftScrollButton();
    showRightScrollButton();
}

function scrollOnLoad()
{
    var right = xGetElementById('right_btn');
    var left  = xGetElementById('left_btn');

    if (right & left) {

        right.onmousedown = onScrollRightStart;
        right.onmouseup   = onScrollStop;
        left.onmousedown  = onScrollLeftStart;
        left.onmouseup    = onScrollStop;

        showLeftScrollButton();
        showRightScrollButton();
    }    
}

function showRightScrollButton()
{
    var right = xGetElementById('right_btn');
    var nav   = xGetElementById('tabbed_block_nav');
    var tb    = xGetElementById('tabbed_block_view');

    if (xLeft(nav) <= -(Math.abs(navWidth-viewWidth)-10) || viewWidth >= navWidth) {
        xHide(right);
    } else {  
        xHeight(right, xHeight(nav));
        // KNOWN BUG:
        // We have bug on konqueror when tabbed_block width is 100% or have not fixed
        // size. when this xMoveTo call get commented this bug disapear.
        // this bug solved by decrement and then increment nav width with 1 pixel BUT 
        // we have vertical scroll bar enable without any reason!
        xWidth(nav, Math.max(viewWidth, navWidth)-1);
        xMoveTo(right, xPageX(tb) + xWidth(tb) - xWidth(right), xPageY(tb));
        xWidth(nav, Math.max(viewWidth, navWidth)+1);
        xShow(right);
    }    
}

function showLeftScrollButton()
{
    var left = xGetElementById('left_btn');
    var nav  = xGetElementById('tabbed_block_nav');
    var tb   = xGetElementById('tabbed_block_view');
    
    if (xLeft(nav) >= 0) {
        xHide(left);
    } else {
        xHeight(left, xHeight(nav));
        xMoveTo(left, xPageX(tb), xPageY(tb));
        xShow(left);
    }    
}

window.onload = function()
{
    if (window.adjustLayout) {
        xAddEventListener(window, "resize", adjustLayout, false);
        adjustLayout();
    }    

    xAddEventListener(window, "resize", adjustTabbedBlock, false);
    adjustTabbedBlock();
}
