﻿/// <reference path="/JS/lib/animatedcollapse.js" />

// Utility functions

// Browser detection (start)
var isMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
var NS4 = (document.layers) ? true : false;
var IEmac = ((document.all) && (isMac)) ? true : false;
var IE4plus = (document.all) ? true : false;
var IE4 = ((document.all) && (navigator.appVersion.indexOf("MSIE 4.") != -1)) ? true : false;
var IE5 = ((document.all) && (navigator.appVersion.indexOf("MSIE 5.") != -1)) ? true : false;
var IE6 = ((document.all) && (navigator.appVersion.indexOf("MSIE 6.") != -1)) ? true : false;
var IE7 = ((document.all) && (navigator.appVersion.indexOf("MSIE 7.") != -1)) ? true : false;
var IE8 = ((document.all) && (navigator.appVersion.indexOf("MSIE 8.") != -1)) ? true : false;
var IE9 = ((document.all) && (navigator.appVersion.indexOf("MSIE 9.") != -1)) ? true : false;
var ver4 = (NS4 || IE4plus) ? true : false;
var NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape') != -1) ? true : false;
var IE = (IEmac || IE4plus || IE4 || IE5 || IE6 || IE7 || IE8 || IE9) ? true : false;
// Browser detection (end)

// Body onload utility (supports multiple onload functions) (start)
var gSafeOnload = new Array();

function safeAddOnLoad(f) {
    if (IEmac && IE4) {
        // IE 4.5 blows out on testing window.onload    
        window.onload = safeOnload;
        gSafeOnload[gSafeOnload.length] = f;
    }

    else if (window.onload) {
        if (window.onload != safeOnload) {
            gSafeOnload[0] = window.onload;
            window.onload = safeOnload;
        }
        gSafeOnload[gSafeOnload.length] = f;
    }

    else {
        window.onload = f;
    }
}

function safeOnload() {
    for (var i = 0; i < gSafeOnload.length; i++) {
        gSafeOnload[i]();
    }
}
// Body onload utility (supports multiple onload functions) (end)

// The terms of the Community Edition of Sitefinity requires a "powered
// by Sitefinity" logo to be displayed in the footer of every page. We've
// done this, but in IE7 and below, an *additional* logo is displayed (not
// visible in FF or Safari, nor IE8 when not in compatiblity mode). Hide this
// logo!
function hideDefaultSitefinityLogo() {
    var telerikLogo = document.getElementById('ctl00_sitefinityLogo');

    if (telerikLogo) {
        telerikLogo.style['display'] = 'none';
    }
}

/// <summary>
///     Returns true if a particular string is null or empty.
/// </summary>
/// <param name="theString" type="String Data Type">
///     The string to be checked.
/// </param>
function stringIsNullOrEmpty(theString) {
    return !theString || theString == '';
}

/// <summary>
///     Sets the visiblity of further details associated with/controlled by a toggling element.
/// </summary>
/// <param name="togglingElement">
///     Reference to the element which is toggling the details element's visibility
/// </param>
/// <param name="detailsId" type="String Data Type">
///     ID of the block element (containing the associated details) to be altered
/// </param>
/// <param name="isVisible" type="boolean">
///     Whether the details element should be visible or not
/// </param>
/// <param name="isAnimatedToggle" type="boolean">
///     Whether the details element's visibility should be toggled smoothly or snap-like.
/// </param>
/// <param name="detailsCueId" type="String Data Type">
///     Optional. The ID of an inline "cue" element which should only be shown when the details are hidden.
/// </param>
function setAssociatedDetailsVisibility(togglingElement, detailsId, isVisible, isAnimatedToggle, detailsCueId ) {
    if (!togglingElement || stringIsNullOrEmpty(detailsId)) {
        return;
    }

    togglingElement.setAttribute('title', isVisible ? 'Hide' : '');
    setBlockElementVisibility(detailsId, isVisible, isAnimatedToggle);    

    if (detailsCueId) {
        setInlineElementVisibility(detailsCueId, !isVisible);
    }
}

/// <summary>
///     Sets the visibility of a block element with a particular id according to a
///     supplied value indicating if it should be visible
/// </summary>
/// <param name="elementId" type="String Data Type">
///     ID of the element to be altered
/// </param>
/// <param name="isVisible" type="boolean">
///     Whether the element should be visible or not
/// </param>
/// <param name="isAnimatedToggle" type="boolean">
///     Whether the element's visibility should be toggled smoothly or snap-like.
/// </param>
function setBlockElementVisibility(elementId, isVisible, isAnimatedToggle) {
    var blockElement = document.getElementById(elementId);

    if (blockElement) {
        var requiredDisplay = isVisible ? 'block' : 'none';

        if (!blockElement.style.display || blockElement.style.display != requiredDisplay) {
            if (isAnimatedToggle) {
                animatedcollapse.toggle(elementId);
            }

            else {
                blockElement.style.display = requiredDisplay;
            }            
        }
    }
}

/// <summary>
///     Sets the visibility of an inline element with a particular id according to a
///     supplied value indicating if it should be visible
/// </summary>
/// <param name="elementId" type="String Data Type">
///     ID of the element to be altered
/// </param>
/// <param name="isVisible" type="boolean">
///     Whether the element should be visible or not
/// </param>
function setInlineElementVisibility(elementId, isVisible) {
    var inlineElement = document.getElementById(elementId);

    if (inlineElement) {
        inlineElement.style.display = isVisible ? 'inline' : 'none';
    }
}

/* Execute on js load: */
safeAddOnLoad(hideDefaultSitefinityLogo);