﻿/// <reference path="jquery-vsdoc.js" />
function ScrollHorizon(selector)
{
    var startindex = 0;
    var lastindex = 0;
    var root;
    var toleft, toright;
    var rootwidth = 0;
    var items = new Array();

    function OnToLeftClick()
    {
        if (startindex == 0)
            return;

        while (GetCanUseWidth() < $(items[startindex - 1]).width())
        {
            if (lastindex >= 0)
            {                
                $(items[lastindex]).hide();
                lastindex--;
            }
            else
            {
                return;
            }
        }
        $(items[startindex - 1]).show();
        startindex--;
        OnChnage();
    }
    function OnToRightClick()
    {
        if (lastindex == items.length - 1)
            return;

        while (GetCanUseWidth() < $(items[lastindex + 1]).width())
        {
            if (startindex <= items.length - 1)
            {
                $(items[startindex]).hide();
                startindex++;
            }
            else
            {
                return;
            }
        }
        $(items[++lastindex]).show();

        OnChnage();
    }
    function GetCanUseWidth()
    {
        var width = 0
        for (i = startindex; i <= lastindex; i++)
        {
            width += $(items[i]).width() + 10;
        }
        return rootwidth - width;
    }
    function OnChnage()
    {
        if (lastindex == items.length - 1)
        {
            toright[0].className = "scrollhorizontoright1";
        }
        else
        {
            toright[0].className = "scrollhorizontoright";
        }

        if (startindex == 0)
        {
            toleft[0].className = "scrollhorizontoleft1";
        }
        else
        {
            toleft[0].className = "scrollhorizontoleft";
        }
    }
    function Init()
    {
        root = $(selector);
        root.addClass("scrollhorizon");

        rootwidth = $(root).width() - 60;
        lastindex = -1;

        $("div", root).each(function()
        {
            items[items.length] = this;
            $(this).addClass("scrollhorizonitem");
            var itemwidth = (asclen($.trim($(this).text())) + 1) * 6;
            $(this).width(itemwidth);

            //alert(width + " " + usedwidth + " " + itemwidth);            
            if (GetCanUseWidth() >= itemwidth)
            {
                lastindex++;
                $(this).show();
            }
            else
            {
                $(this).hide();
            }
        });


        toleft = $("<div class=\"scrollhorizontoleft1\">&nbsp;</div>");
        toright = $("<div class=\"scrollhorizontoright\">&nbsp;</div>");

        root.prepend(toleft);
        root.append(toright);

        toleft.bind("click", null, OnToLeftClick);
        toright.bind("click", null, OnToRightClick);

        OnChnage();
    }

    Init();
}
