function SetPopupCloseIconVisible()
{
    if($("#modalCloseButton .cll-modal-ClosePic").length > 0)
    {
        if($("#modalCloseButton .cll-modal-ClosePic").css("display") === "none")
                $("#modalCloseButton .cll-modal-ClosePic").css("display","block");
    }
    
 
} 

function MyResizeModalWindowWithAnimation(newWidth, newHeight,bModalClosePicVisible) 
{
    try 
    {
        if(bModalClosePicVisible === false)
        {
            var modalClosePic = $(".cll-modal-ClosePic");
            modalClosePic.css("display","none");
        }
        var modalWindow = $("#modalWindow");
        var leftPX = modalWindow.css("left").replace(/px/, "") * 1 - ((newWidth - modalWindow.width()) / 2);
        
        var iframeObj = $("#modalIframe");
        iframeObj.css("width", newWidth);
        iframeObj.css("height", newHeight);
        
        modalWindow.css("width", newWidth);
        modalWindow.css("height", newHeight);
        modalWindow.css("left", leftPX);
    }
    catch(e)
    {
        //alert(e.message);
    }
}

function el_fnFixPng() {
    try {
        $.each($("img[src$=.png],img[src$=.PNG]"), function () {
            var img = $(this);
            var width = img.width();
            var height = img.height();
            var m_width = getCss(this, 'width');
            var m_height = getCss(this, 'height');
            if (width < m_width) {
                width = m_width;
            }
            if (height < m_height) {
                height = m_height;
            }
            img.css({
                "width": width,
                "height": height,
                "filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.attr("src") + "', sizingMethod='scale')"
            });
            img.attr("src", "/_layouts/STYLES/1.LTR/MainCss/OpacityPNG/blank.gif");
        });
    } catch(e) {}
}
function getCss(elem, attr) {
    if (typeof(document.defaultView) != 'object') {
        switch (attr) {
        case "height":
            return parseInt(elem.clientHeight, 10);
            break;
        case "width":
            return parseInt(elem.clientWidth, 10);
            break;
        case 'padding-top':
            return parseInt(elem.currentStyle.paddingTop, 10);
            break;
        case 'padding-bottom':
            return parseInt(elem.currentStyle.paddingBottom, 10);
            break;
        default:
            return parseInt($(elem).css(attr), 10);
            break;
        }
    } else {
        return parseInt(document.defaultView.getComputedStyle(elem, '').getPropertyValue(attr), 10);
    }
}
function FixHeightRunner()
{
  var height = 0;
    var offset;
    var maintable = $(".cll-ZoneTable");
    var mainelem = $(".LastWPInZone");
    var innerelem = $(".innerLastWPInZone");
    var browser_msie = "\v" == "v";
    var browser_version = $.browser.version;
    if (flgRun != 0) {
        $.each(mainelem, function () {
            if (browser_msie && browser_version == '6.0') {
                this.style.height = "auto";
            } else {
                this.style.minHeight = "";
            }
        });
        $.each(innerelem, function () {
            if (browser_msie && browser_version == '6.0') {
                this.style.height = "auto";
            } else {
                this.style.minHeight = "";
            }
        });
    }
    flgRun = 1;
    $.each(maintable, function () {
        var obj = $(this);
        if (height < obj.height()) {
            height = getCss(obj.children("tbody").children("tr:last").get(0), 'height');
            offset = obj.children("tbody").children("tr:last").offset();
        }
    });
    $.each(maintable, function () {
        var table = $(this);
        $.each(table.children("tbody").children("tr:last").children("td").children("div.LastWPInZone"), function () {
            var div = $(this);
            if (getCss(this, 'height') != height) {
                var divoffset = div.offset();
                var padding = getCss(this, 'padding-top') + getCss(this, 'padding-bottom');
                var finalHeight = (height - (divoffset.top - offset.top) - padding);
                if (browser_msie && browser_version == '6.0') {
                    if (finalHeight > 0) {
                        this.style.height = finalHeight + "px";
                    }
                } else {
                    if (finalHeight > 0) {
                        this.style.minHeight = finalHeight + "px";
                    }
                }
            }
        });
    });
    $.each(mainelem, function () {
        var obj = $(this);
        var offset = obj.offset();
        var parentoffset = obj.parent().parent().offset();
        var rcHeight;
        if (rcHeight = obj.parent().parent().attr("rcHeight")) {} else {
            rcHeight = getCss(obj.parent().parent().get(0), 'height');
            $(this).parent().parent().attr("rcHeight", rcHeight);
        }
        var lowerborder = obj.next().length;
        var height = rcHeight - lowerborder - (offset.top - parentoffset.top);
        if (height > getCss(this, 'height')) {
            var padding = getCss(this, 'padding-top') + getCss(this, 'padding-bottom');
            var newheight = height - padding;
            if (browser_msie && browser_version == '6.0') {
                this.style.height = newheight + "px";
            } else {
                this.style.minHeight = newheight + "px";
            }
        }
    });
    mainelem.parent().parent().removeAttr("rcHeight");
    $.each(innerelem, function (i, val) {
        var parentobject;
        var object = $(val);
        if (parentobject = object.parents('.LastWPInZone')) {} else if (parentobject = object.parents('.innerLastWPInZone')) {} else {
            parentobject = null;
        }
        if (parentobject[0]) {
            objectoffset = object.offset();
            parentoffset = parentobject.offset();
            var padding = getCss(val, 'padding-top') + getCss(val, 'padding-bottom');
            var height = getCss(parentobject[0], 'height') - (objectoffset.top - parentoffset.top) - padding - $(parentobject[0]).find(".BottomDiv").length * 10;
            if (browser_msie && object.html().length == 0) {
                val.style.fontSize = "0px";
            }
            if (browser_msie && browser_version == '6.0') {
                val.style.height = height + "px";
            } else {
                val.style.minHeight = height + "px";
            }
        }
    });
    if(AnotherProcess === true)
    {
        AnotherProcess = false;
        InProc = false;
        FixHeight();
    }
    else
    {
        AnotherProcess = false;
        InProc = false;
    }
}
function FixHeight() {
  if(InProc === false)
  {
        InProc = true;
        FixHeightRunner();
  }
  else
  {
        AnotherProcess = true;
  }
}
function fixModalWindow() {
    if ($.browser.msie) {
        var mwindow = $("#modalWindow");
        var h = $(document).height() + 'px';
        var w = $(document.body).width() + 'px';
        var iframe = $(document.createElement("iframe"));
        iframe.attr({
            id: "modalIframeContainerFix",
            src: "/_layouts/PublishSite/JS/blank.htm"
        }).css({
            opacity: 0,
            position: 'absolute',
            height: h,
            width: w,
            zIndex: 1000,
            top: 0,
            left: 0
        });
        if ($.browser.version == '6.0') {
            var curh = document.documentElement.scrollTop + Math.floor(($(window).height() - mwindow.height()) / 2) + 'px';
        }
        mwindow.css({
            zIndex: 3100,
            top: curh
        });
        mwindow.after(iframe);
        $(window).scroll(function () {
            lastChance(mwindow);
        });
    }
}
function lastChance(mwindow) {
    $("#modalIframeContainerFix").hide().show();
    if ($.browser.version == '6.0') {
        var curh = document.documentElement.scrollTop + Math.floor(($(window).height() - mwindow.height()) / 2) + 'px';
        mwindow.css({
            top: curh
        });
    }
}
function closeModalFix() {
    try {
        $("#modalIframeContainerFix").remove();
    } catch(e) {}
}
function DisableForm() {}
function fixPopModalWindow() {
    try {
        if ($(".cll-modal-Background:visible").length) {
            $(".cll-modal-Background:visible").css({
                "left": "0px",
                "width": $("body").width() + "px"
            });
            $(".ShoppingAssistant").css("left", Math.floor(Math.floor($("body").width() / 2) - Math.floor($(".ShoppingAssistant").width() / 2)) + "px");
        } else if (timeout < 5) {
            timeout += 1;
            setTimeout("fixPopModalWindow()", 100);
        }
    } catch(e) {}
}
try {
    var oMainTable = document.getElementById("MSOTlPn_Tbl");
    var oToolPartContainer = document.getElementById("AllToolPartContainer");
    var oTPContainer = document.getElementById("TPContainer");
    var flgRun = 0;
    if (oMainTable == null) {
        if (oToolPartContainer) {
            oToolPartContainer.style.display = "none";
        }
        if (oTPContainer) {
            oTPContainer.style.display = "none";
        }
    }
    FixHeight();
    if ($.browser.msie == true && $.browser.version == '6.0') {
        $(document).ready(function () {el_fnFixPng();});
        var timeout = 0;
        //Enable the following line only in Authoring Env
        //setTimeout("fixPopModalWindow()", 100);
    }
} catch(e) {}
function OpenDownloadPopup(url, width) {
    var modal = document.getElementById("modalWindow");
    if (!modal) return;
    if (width) {
        var iframe = document.getElementById("modalIframe");
        modal.style.width = iframe.style.width = width;
    }
    document.getElementById("modalBackground").style.display = 'block';
    if (modal.filters) {
        modal.filters[0].enabled = true;
        modal.filters[0].Apply();
    }
    document.getElementById("modalIframeContainer").style.display = "none";
    document.getElementById("modalLoading").style.display = "block";
    document.getElementById("modalIframe").src = url + "&rand=" + Math.random();
    modal.style.display = 'block';
    OnWindowResize();
    if (modal.filters) modal.filters[0].Play();
    if (window.attachEvent) window.attachEvent("onresize", OnWindowResize);
    else if (window.addEventListener) window.addEventListener("resize", OnWindowResize, false);
    else window.onresize = OnWindowResize;
    if (document.all) document.documentElement.onscroll = OnWindowResize;
    fixModalWindow();
}
function OnWindowResize() {
    var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
    var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
    var div = document.getElementById("modalWindow");
    div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
    div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
}
function CloseDownloadPopup() {
    closeModalFix();
    document.getElementById("modalWindow").style.display = document.getElementById("modalBackground").style.display = "none";
    document.getElementById("modalIframe").src = "";
    if (window.detachEvent) window.detachEvent("onresize", OnWindowResize);
    else if (window.removeEventListener) window.removeEventListener("resize", OnWindowResize, false);
    else window.onresize = null;
}
function GetWindowWidth() {
    var width = document.documentElement && document.documentElement.clientWidth || document.body && document.body.clientWidth || document.body && document.body.parentNode && document.body.parentNode.clientWidth || 0;
    return width;
}
function GetWindowHeight() {
    var height = document.documentElement && document.documentElement.clientHeight || document.body && document.body.clientHeight || document.body && document.body.parentNode && document.body.parentNode.clientHeight || 0;
    return height;
}
function LoadingCompleted() {
    var modalIframeContainer = document.getElementById("modalIframeContainer");
    var modal = document.getElementById("modalWindow");
    if (modal && modalIframeContainer) {
        if (modal.style.display == "block") {
            modalIframeContainer.style.display = "block";
            document.getElementById("modalLoading").style.display = "none";
        }
    }
}
function ResizeModalWindow(newHeight) {
    try {
        if (newHeight < 250) {
            newHeight = 250;
        }
        document.getElementById("modalIframe").style.height = newHeight + "px";
        document.getElementById("modalWindow").style.height = newHeight + "px";
    } catch(e) {}
}

function ResizeModalWindowWithAnimation(newWidth, newHeight) {
    try {
        var modalWindow = $("#modalWindow");
        var leftPX = modalWindow.css("left").replace(/px/, "") * 1 - ((newWidth - modalWindow.width()) / 2);
        modalWindow.animate({
            width: newWidth,
            height: newHeight,
            left: leftPX
        },
        300, "linear", function () {
            var iframeObj = $("#modalIframe");
            iframeObj.css("width", newWidth);
            iframeObj.css("height", newHeight);
            iframeObj.animate({
                opacity: 1
            },
            "slow");
        });
    } catch(e) {}
}
function HideContent() {
    $("#modalIframe").css("opacity", 0);
}
function GetModalWidth() {
    return $("#modalWindow").width();
}
function mdl_RefreshMe() {
    try {
        window.parent.location.reload(true);
    } catch(e) {}
}
function mdl_RedirectTo(url) {
    try {
        window.location.href = url;
    } catch(e) {}
}
function ResizeWindowOnOverflow() {
    var scrollHeight = document.documentElement.clientHeight;
    var modalBottomPosition = window.document.getElementById('modalWindow').offsetTop + window.document.getElementById('modalWindow').offsetHeight;
    if (modalBottomPosition > scrollHeight) {
        OnWindowResize();
    }
}
function AttachRefreshToCloseButton() {
    $(document).ready(function () {
        $('#modalCloseButton input').click(function () {
            CloseDownloadPopup();
            document.location.reload();
        });
    });
}
function ClickCloseDownloadPopup() {
    $('#modalCloseButton input').click();
}

// Accessibility: Ensure H1 exists on every page.
$(document).ready(function() {
    if ($("h1").length < 1) {
        $("<h1/>").text(document.title).hide().insertBefore($(document.body.firstChild));
    }
});

// Accessibility: Insert a "lang" attribute for top menu links
function getLanguageForString(str) {
    var counters = new Object();
    counters['en'] = 0;
    counters['he'] = 0;
    counters['ru'] = 0;
    counters['ar'] = 0;
    for (var i = 0; i < str.length; i++) {
        var ch = str[i];
        if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) {
            counters['en']++;
        } else if ((ch >= 'א') && (ch <= 'ת')) {
            counters['he']++;
        } else if (((ch >= 'а') && (ch <= 'я')) || ((ch >= 'А') && (ch <= 'Я'))) {
            counters['ru']++;
        } else if ((ch >= 'ا') && (ch <= 'ي‎')) {
            counters['ar']++;
        }
    }

    if ((counters['en'] > counters['he']) && (counters['en'] > counters['ru']) && (counters['en'] > counters['ar'])) {
        return 'en';
    } else if ((counters['ru'] > counters['he']) && (counters['ru'] > counters['en']) && (counters['ru'] > counters['ar'])) {
        return 'ru';
    } else if ((counters['ar'] > counters['he']) && (counters['ar'] > counters['en']) && (counters['ar'] > counters['ru'])) {
        return 'ar';
    } else return 'he';
}
$(document).ready(function() {
    $('.divHeaderLinks a').each(function(index) {
        var link = $(this);
        var lang = getLanguageForString(link.text());
        link.attr('lang', lang);
    });
});