function initWebcamsRefresh(refreshAlways, refreshTime) {
    
    if (refreshAlways == null) {
        refreshAlways = false;
    }

    refreshWebcams();

    if (refreshAlways) {
        setInterval(function() {

            refreshWebcams();

        }, refreshTime * 1000);
    }

}

function refreshWebcams() {
    var webcamElements = $('#content-camviews');

    webcamElements.each(function() {
        var webcamElement = this;

        // webcamsAjaxUrl, webcamsAjaxUrlParams - global variables
        $.get(
            webcamsAjaxUrl,
            webcamsAjaxUrlParams,
            function(responseHtml) {

                webcamElement.innerHTML = responseHtml;

            }
        );
    });
}

function initSpireRefresh(refreshTime) {

    refreshSpire();

    setInterval(function() {

        refreshSpire();

    }, refreshTime * 1000);
    
}

function refreshSpire() {
    var spireElements = $('#content-spire .info-wrapper');

    spireElements.each(function() {
        var activeSpire = [];
        var activeStatsSpire = [];
        var activeStatsSpireDates = [];
        $('#content-spire .info-wrapper .main-info.active-row').each(function() {
            var spira = this.id.substring(this.id.indexOf('_') + 1, this.id.length);
            activeSpire[activeSpire.length] = spira;

            if ($("#spira-stats-" + spira).hasClass('stats-active')) {
                activeStatsSpire[activeStatsSpire.length] = spira;
                activeStatsSpireDates[activeStatsSpireDates.length] =
                    $("#spira-stats-date-" + spira).val();
            }
        });

        var spireElement = this;

        // spireAjaxUrl - global variable
        $.get(
            spireAjaxUrl,
            {
                'zona_node_id' : $('#operator-zona-node-id')[0].value,
                'active_spire[]' : activeSpire,
                'active_stats_spire[]' : activeStatsSpire,
                'active_stats_spire_dates[]' : activeStatsSpireDates
            },
            function(responseHtml) {

                spireElement.innerHTML = responseHtml;

            }
        );
    });
}

function showSpiraDailyStatistics(spira) {
    // spiraDailyStatisticsUrl -- global variable
    var statsDate = $("#spira-stats-date-" + spira).val();
    if (spira) {
        $.get(
            spiraDailyStatisticsUrl,
            {
                'spira' : spira,
                'stats_date' : statsDate
            },
            function(responseHtml) {
                $("#spira-stats-" + spira).html(responseHtml).addClass('stats-active');
            }
        );
    }
}

function isBrowserCompatible() {
    var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

    return !(jQuery.browser.msie && (ie55 || ie6));
}

function toggleContactForm(link) {
    alert(link.style.className);
    //link.addClass(".contact-link-open");
}

function drawPodcastControl(audioUrl, elemId, imagePath) {
    var iconUrl = imagePath + 'multicast.png';
    var playerUrl = imagePath + 'PodcastPlayer.swf';
    var alternateUrl = imagePath + 'expressInstall.swf';
    var iconWidth = 21;
    var iconHeight = 21;
    var flashvars = {
        podcastURL: audioUrl, 
        iconTintColor: 0x33FF33, 
        iconURL: iconUrl
    };
    swfobject.embedSWF(playerUrl, elemId, iconWidth, iconHeight, "9.0.0", alternateUrl, flashvars, {wmode: "transparent"});
}

function getCookie(name, defaultValue) {
    var value = $.cookie(name);
    return (value == null) ? defaultValue : value;
}

function setCookie(name, value) {
    $.cookie(name, value, { expires: 365, path: '/' });
}

function initConsultaCollapsibleElements(collapseInfo) {
    var element = null;
    for (var i = 0; i < collapseInfo.length; i++) {
        (function() {
            var _i = i;
            element = $(collapseInfo[_i]['elementQuery']).click(function() {
                setCookie(
                    collapseInfo[_i]['collapseCookieName'],
                    ($(this).next().css('display') == 'none') ? 0 : 1
                );
                $(this).next().toggle('fast');
                return false;
            });
            if (
                parseInt(
                    getCookie(
                        collapseInfo[_i]['collapseCookieName'],
                        collapseInfo[_i]['defaultCollapsed']
                    )
                )
            ) {
                element.next().hide();
            }
        })();
    }
}
