﻿/// <reference path="jquery-1.4.1.js" />

jQuery(window).load(
    function () {
        WizardBuild();
        WizardShow();
    }
);

jQuery(document).ready(
    function() {
        WizardBuild();      
        $.localScroll({ hash: true });
        SetLeftPanelVisibility();
        WrapDivAroundCheckBoxes();
        PositionLists();
        showSelectedNodeChilds();
    }
);

function WizardShow() {
    var $container = $(".mainWizardContainerDiv");
    $container.addClass('mainWizardShow');
    
    var $tbltopWizardSteps = jQuery(".topWizardSteps td");
    $tbltopWizardSteps.each(function () {
        tdWidth = jQuery(this).width();
        jQuery(this).find(".topBg").css("width", tdWidth-1);
    });

}

function WrapDivAroundCheckBoxes() {
    var Checkbox = jQuery('input[type=checkbox]');
    CheckboxLabel = jQuery('.checkBoxContainer label');
    Checkbox.wrap("<div class='chb'></div>");
    CheckboxLabel.addClass("lbl lblChb");
}

function SetLeftPanelVisibility() {
    if ($.cookie("CookieName.LeftPanel.Visible") == 'false')
        HideLeftPanel();
}

function PositionLists() {
    var Block = jQuery(".controlContentContainerLeft");
    var BlockAmount = Block.length;
    var HalfBlockAmount = BlockAmount / 2;
    var HalfBlockAmountRounded = Math.round(HalfBlockAmount);

    jQuery('.controlContentContainerLeft:gt(' + (HalfBlockAmountRounded - 1) + ')').addClass("controlContentContainerRight");
}

function setMaxImageSize() {
    var image = jQuery('.sanaHtml img');
    var leftPane = document.getElementById('leftPane');
    if (leftPane != null && leftPane.style.display != 'none') {
        image.css('max-width', '500px');
        image.css('width', 'auto');
        image.css('height', 'auto');
    }
    else {
        image.css('max-width', '700px');
        image.css('width', 'auto');
        image.css('height', 'auto');
    }
}

function switchMenu() {
    var image = jQuery('.sanaHtml img');
    var leftPane = document.getElementById('leftPane');
    if (leftPane.style.display != 'none') {
        HideLeftPanel();
        image.css('max-width', '700px');
        image.css('width', 'auto');
        image.css('height', 'auto');
    }
    else {
        ShowLeftPanel()
        image.css('max-width', '500px');
        image.css('width', 'auto');
        image.css('height', 'auto');
    }
    $.cookie("CookieName.LeftPanel.Visible", leftPane.style.display != 'none', { expires: 365, path: '/' });
}
function HideLeftPanel() {
    var expandCollapseIcon = $('#collapseExpandIcon');

    $('#leftPane').hide();
    $('#rightPane').css('width', '100%');
    $('#rightPaneContainer').css('borderLeft', '0');
    expandCollapseIcon.attr('title', 'Expand');
    expandCollapseIcon.css('left', '0%');
    expandCollapseIcon.css('backgroundPosition', '0px 21px');
}

function ShowLeftPanel() {
    var expandCollapseIcon = $('#collapseExpandIcon');

    $('#leftPane').show();
    $('#rightPane').css('width', '75%');
    $('#rightPaneContainer').css('borderLeft', '1px solid');
    expandCollapseIcon.attr('title', 'Collapse');
    expandCollapseIcon.css('left', '25%');
    expandCollapseIcon.css('backgroundPosition', '0 0px');
}

function showSelectedNodeChilds() {
    var SelectedNode = $("AspNet-TreeView-Root AspNet-TreeView-Selected");
    var SelectedNodeChilds = $(".AspNet-TreeView-Selected").children('ul');
    var SelectedNodeCollapseExpandIcon = $(".AspNet-TreeView-Selected").children('span');

    if (SelectedNodeChilds.parent().className = SelectedNode) {
        SelectedNodeChilds.removeClass("AspNet-TreeView-Hide");
        SelectedNodeChilds.addClass("AspNet-TreeView-Show");
        SelectedNodeCollapseExpandIcon.removeClass("AspNet-TreeView-Expand");
        SelectedNodeCollapseExpandIcon.addClass("AspNet-TreeView-Collapse");
    }
}


function scrollToAnchor(name) {
    $.scrollTo($('a[name=' + name + ']'), 1000, { onAfter: function() {
        location = '#' + name;
    }
    })
}

function WizardBuild() {
    var $wizardHolder = jQuery(".mainWizardContainer");
    var mainContHeight = $wizardHolder.height();
    var mainContWidth = $wizardHolder.width();
    var addHeight = 0;
    var addHeightLi = 0;
    if ($.browser.msie && $.browser.version == 7) {
        addHeight = 4;
        addHeightLi = -1;
    }

    var count = jQuery('.firstLevel', '.mainWizardContainer').size();
    if (count <= 3) {
            itemHeight = 366 / count; 
        } else { 
            itemHeight = 91;
            jQuery('.mainWizardContainer li a img').css('maxHeight', 88) 
        }
    jQuery('.firstLevel', '.mainWizardContainer').css('height', itemHeight);

    jQuery("li", $wizardHolder).each(function() {
        var $children = jQuery(this).children('ul');
        var $childrens = $children.children('li');
        var liNumber = $childrens.length;
        if (liNumber > 0) {
            if (liNumber == 2)
                $children.find('li span:first').css("padding-bottom", "5px");
            var spanHeights = 0;
            $childrens.each(function() {
                spanHeights += jQuery(this).find("span:first").height() + 7 + addHeight;
            });
            //imgMaxHeight = (mainContHeight - spanHeights) / liNumber;
            imgMaxHeight = (mainContHeight - 40) / (liNumber-1);
            $childrens.each(function() {
               jQuery(this).find("img:first").css("max-height", imgMaxHeight);
            });
            var caption = $children.children('li.CaptionOfChildren');
            var captionHeight = caption.height() - 1;
            if (caption != "undefined" && caption.length > 0) {
                $children.children('li').height((mainContHeight - captionHeight) / (liNumber - 1) + addHeightLi);
                caption.css("height", captionHeight);
            }
            else {
                $children.children('li').height((mainContHeight) / liNumber + addHeightLi);
            }

            $children.children('li:first').height($children.children('li:first').height() - addHeightLi); /*ie7 FIX*/

            if ($.browser.msie && $.browser.version == 8) { /*ie8 FIX*/
                $children.children('li').each(function() {
                    var currentLink = jQuery(this).find('a');
                    currentLink.css("margin-top", (jQuery(this).height() - currentLink.height()) / 2);
                    currentLink.css("margin-bottom", (jQuery(this).height() - currentLink.height()) / 2);
                });
            }
        }
    });

    SetArrowTop($wizardHolder);
}

jQuery(window).load(function() {
    var container = jQuery(".mainWizardContainer");
    if (container != "undefined" && container.length > 0) {
            var arrowChoise = jQuery(".arrowChoise");
            arrowChoise.css("top", container.height() / 2 - 10);
    }
});

function SetArrowTop(container) {
    if (jQuery(".childrenContainer").length > 0)
        jQuery(".arrowChoise").css("display", "none");
    else {
        var arrowChoise = jQuery(".arrowChoise");
        var childrens = jQuery(".childrenContainer");
        var needWidth = container.find("li:first").width() * (jQuery(childrens).length + 1);
        arrowChoise.css("top", container.height() / 2 - 10);
        arrowChoise.css("width", container.width() - needWidth - 20);
        arrowChoise.css("left", 120 + (container.width() + needWidth - arrowChoise.width()) / 2);
    }
}

