/*
 SlideTab
 
 Author: MIURA Kazki
 
 Copyright 2007 HMDT. All rights reserved.
*/

// Configurations
var _duration = 400;
var _tabBarName = "tabBar";
var _scrollerName = "scroller";
var _tabTag = "-tab";       // must start with "-"
var _paneTag = "-pane";     // must start with "-"
var _activeTabClassName = "active";
var _inactiveTabClassName = "inactive";

// Global variables
var _currentPaneName = null;
var _animator = null;
var _startPosition = null;
var _goalPosition = null;

function Init(initialPaneName)
{
    // Set current pane name
    _currentPaneName = initialPaneName;
    
    // Scroll to Initial position
    var scroller;
    var position, scrollerPos;
    scroller = document.getElementById(_scrollerName);
    position = elementPosition(document.getElementById(_currentPaneName));
    scrollerPos = elementPosition(scroller)
    scroller.scrollLeft = position[0] - scrollerPos[0];
    scroller.scrollTop = position[1] - scrollerPos[1];
}

function ScrollToPane(paneName)
{
    // Filter
    if (paneName == _currentPaneName) {
        return;
    }
    
    // Store last pane name and update current pane name
    var lastPaneName;
    lastPaneName = _currentPaneName;
    _currentPaneName = paneName;
    
    // Toggle active/inactive
    var tabName;
    tabName = _currentPaneName.split("-")[0] + _tabTag;
    document.getElementById(tabName).className = _activeTabClassName;
    if (lastPaneName) {
        var lastTabName;
        lastTabName = lastPaneName.split("-")[0] + _tabTag;
        document.getElementById(lastTabName).className = _inactiveTabClassName;
    }
    
    // Get scroller
    var scroller;
    scroller = document.getElementById(_scrollerName);
    
    // Store start position
    _startPosition = new Array(scroller.scrollLeft, scroller.scrollTop);
    
    // Store goal position
    var position;
    var scroller;
    var scrollerPos;
    scrollerPos = elementPosition(scroller)
    position = elementPosition(document.getElementById(paneName));
    position[0] = position[0] - scrollerPos[0];
    position[1] = position[1] - scrollerPos[1];
    _goalPosition = position;
    
    // Start animation
    if (_animator) {
        _animator.stopAnimation();
    }
    _animator = new Animator();
    _animator.setDuration(_duration);
    _animator.setHandler(animatorStep0);
    _animator.setFinishHandler(animatorStep1);
    _animator.startAnimation();
}

function ScrollArrow(direction)
{
    // Check arguments
    if (direction != "prev" && direction != "next") {
        return;
    }
    
    // Get tab bar
    var tabBar;
    tabBar = document.getElementById(_tabBarName);
    if (!tabBar.hasChildNodes()) {
        return;
    }
    
    // Get names
    var names;
    var children;
    names = new Array();
    children = tabBar.childNodes;
    for (var i = 0; i < children.length; i++) {
        var childID;
        childID = new String(children[i].id);
        if (childID.match(_tabTag + "$")) {
            names.push(childID.split("-")[0]);
        }
    }
    
    // Determine index to go to
    var i;
    for (i = 0; i < names.length; i++) {
        if (names[i] == _currentPaneName.split("-")[0]) {
            break;
        }
    }
    if (direction == "prev") {
        i = (i - 1 + names.length) % names.length;
    }
    else {
        i = (i + 1 + names.length) % names.length;
    }
    
    // Scroll
    ScrollToPane(names[i] + _paneTag);
}

function elementPosition(element)
{
    var x = 0, y = 0;
    do {
        x += element.offsetLeft;
        y += element.offsetTop;
    } while (element = element.offsetParent);
    
    return Array(x, y);
}

function animatorStep0(animator)
{
    // Scroll
    var scroller;
    scroller = document.getElementById(_scrollerName);
    scroller.scrollLeft = _startPosition[0] + (_goalPosition[0] - _startPosition[0]) * animator.currentValue;
    scroller.scrollTop = _startPosition[1] + (_goalPosition[1] - _startPosition[1]) * animator.currentValue;
}

function animatorStep1(animator)
{
    // Scroll
    var scroller;
    scroller = document.getElementById(_scrollerName);
    scroller.scrollLeft = _goalPosition[0];
    scroller.scrollTop = _goalPosition[1];
}
