﻿function getAutostartValueFromCookie() {
    var cookieValue = $.cookie("autostartVideos");
    return (cookieValue == null) ? 'true' : cookieValue;
}

function getAutostartDisplayValue() {
    var displayValue = getAutostartValueFromCookie();
    return displayValue == 'true' ? "On" : "Off";
}

function toggleAutostartCookieValue() {
    var cookieValue = $.cookie("autostartVideos");
    if (cookieValue == null || cookieValue == "true") {
        $.cookie("autostartVideos", "false", { expires: 365, path: '/' });
    }
    else {
        $.cookie("autostartVideos", "true", { expires: 365, path: '/' });
    }
}

function setupAutostartToggleLink() {
    $("#autostart").append('<a href="#" id="toggleautoplay">Autoplaying videos: ' + getAutostartDisplayValue() + '</a> | ');
    $("#toggleautoplay").live("click", function (e) {
        e.preventDefault();
        toggleAutostartCookieValue();
        var autoplayState = getAutostartDisplayValue();
        $(this).text("Autoplaying videos: " + autoplayState);
    });
}

$(document).ready(function () {
    setupAutostartToggleLink();

    if (stylesheetOnLoad && stylesheetOnUnload) {
        stylesheetOnLoad();
        $(document).unload(function () {
            stylesheetOnUnload();
        });
    }

    if (setupPreLoad) {
        setupPreLoad();
    }
});
