﻿//-----------------------------------------------
var LineHeight = 28;
var BlockHeight = 226 - LineHeight;
var BlockWidth = 323;
var ContentHeight = 0;
var ColumsNum = 0;
var ColumnsMarginLeft = 0;
var AnimateBy = 0;
var RightMostPosition = 0;
var InSequenceHebrew = false;
var Actions = "DisableHebrewNextBtn,DisableHebrewPreviousBtn";
//-----------------------------------------------

//-----------------------------------------------
function InitHtmlHoster() {
    
    // Initilizg the grampa and parent of the ColumnsWrapper
    $(".ColumnsWrapperGrampa, .ColumnsWrapperParent").css({
        'width': BlockWidth,
        'height': BlockHeight,
        'overflow': 'hidden'
    });

    // Calculating how much columns should appear:
    ContentHeight = $('.ColumnsWrapper').height();
    ColumsNum = Math.ceil(ContentHeight / BlockHeight);

    // Doing the columnize:
    $(".ColumnsWrapperParent").width(ColumsNum * BlockWidth);

    // Setting the hebrew navigation controls if there is one column only.
    // Note: Columinze does not execute if there is only one column, so 
    // the "doneFunc" does not execute.
    if (ColumsNum == 1) {
        AnimationFinished("DisableHebrewNextBtn,DisableHebrewPreviousBtn");
        return (false);
    }

    $(".ColumnsWrapper").columnize({
        float: 'right',
        columns: ColumsNum,
        doneFunc: function() {

            // Initilizg the grampa and parent of the ColumnsWrapper:  
            // Fixing IE bug that does not hide text
            $(".ColumnsWrapperGrampa, .ColumnsWrapperParent").css({
                'width': BlockWidth,
                'height': BlockHeight + LineHeight
            });
        
            // Removing empty columns
            $(".column").each(function() {
                if (this.innerHTML == "") {
                    $(this).remove();
                    return (false);
                }
            });

            // Setting the actual colums number (for scrolling purpose)
            ColumsNum = $(".column").size();

            // Setting css values for scrolling animation (Cannot be set up in the css file
            // because the columnize function can not handle the proprety position absolute so it 
            // must be setup afterwards).
            $(".ColumnsWrapper").css({
                'position': 'absolute',
                'top': '0',
                'right': '0'
            });

            // Setting Columns Margin Left
            ColumnsMarginLeft = Number($(".first.column").css("margin-left").replace("px", ""));

            // Setting the move lenght of the columns which the scroll animation should do.
            AnimateBy = BlockWidth + ColumnsMarginLeft;

            // Calculating the right most position that the div that contains the columns can be animated to.
            RightMostPosition = (-1) * (ColumsNum - 1) * (BlockWidth + ColumnsMarginLeft);

            // Setting the coumns in their right position
            // This avoids/escapes/fxises allot (very allot) of bugs/errors/etc like:
            // 1) Setting the last column in his rightfull position (it always floats to some weired position)
            // 2) In order that the component and columns will show in IE (6/7/8).
            // 3) etc (just take these rows out and check it yourself - booom)
            var i = 0;
            $(".column").each(function() {
                $(this).css({
                    'position': 'absolute',
                    'top': '0',
                    'right': i * AnimateBy
                });
                i++;
            });

            try {
                // Attaching the mouse wheel event to work with html and not silverlight
                $(".ColumnsWrapper").mousewheel(function(objEvent, intDelta) {
                    try {
                        var arrActions = Actions.split(",");
                        if (arrActions[1] != "DisableHebrewPreviousBtn" && intDelta >= 0)
                            MoveBlock("Previous")
                        else if (arrActions[0] != "DisableHebrewNextBtn" && intDelta < 0)
                            MoveBlock("Next");
                    }
                    catch (err) { }
                });
            }
            catch (err) { }

            // Setting the hebrew navigation controls
            AnimationFinished("EnableHebrewNextBtn,DisableHebrewPreviousBtn");
        }
    });
}
//-----------------------------------------------

//-----------------------------------------------
function MoveBlock(direction) {
    if (InSequenceHebrew == true)
        return (false);

    InSequenceHebrew = true;
    
    var action = "";

    switch (direction) 
    {
        case "Next": action = "-"; break;
        case "Previous": action = "+"; break;
    }

    $(".ColumnsWrapper").animate({ "right": action + "=" + AnimateBy + "px" }, "fast", function() {
        var currentRightPosition = Number($(".ColumnsWrapper").css('right').replace("px", ""));
        if (currentRightPosition == null || isNaN(currentRightPosition))
            currentRightPosition = 0;
        
        if (currentRightPosition < 0 && currentRightPosition > RightMostPosition)
            AnimationFinished("EnableHebrewNextBtn,EnableHebrewPreviousBtn");
        else if (currentRightPosition == 0)
            AnimationFinished("EnableHebrewNextBtn,DisableHebrewPreviousBtn");
        else if (currentRightPosition <= RightMostPosition)
            AnimationFinished("DisableHebrewNextBtn,EnableHebrewPreviousBtn");
        else
            AnimationFinished("DisableHebrewNextBtn,DisableHebrewPreviousBtn");
    });
}
//-----------------------------------------------

//-----------------------------------------------
function AnimationFinished(actions) {
    Actions = actions;
    if (SLControl != null)
        SLControl.Content.PaginatedHtmlViewer.HebrewAnimationCompleted(actions);
    InSequenceHebrew = false;
}
//-----------------------------------------------

//-----------------------------------------------
function ToggleHtmlHoster(boolVal) {
    boolVal == "true" ? $(".ColumnsWrapperGrampa").css("display", "block") :
                        $(".ColumnsWrapperGrampa").css("display", "none");
}
//-----------------------------------------------

//-----------------------------------------------
function GetBrowserVersion() { return ($.browser.version); }
//-----------------------------------------------

//-----------------------------------------------
function RemoveHtmlHoster() { $(".HtmlHoster").remove(); }
//-----------------------------------------------