﻿var tinyMCELoaded = false;

function InitTinyMCEEditor(cssFile, managedMode, disablePlugins, extendedElementsType)
{
    if (disablePlugins != undefined && disablePlugins == true)
    {
        var enabledPlugins = "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave";
    }
    else
    {
        var enabledPlugins = "filemanager,imagemanager,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave";
    }

    if (tinyMCELoaded)
        return;

    tinyMCELoaded = true;

    var iframe = "iframe[src|style|width|height|scrolling|marginwidth|marginheight|frameborder]";
    var js = "script[type|src]";

    //none
    if (extendedElementsType == 0)
        var extendedValidElements = "";
    else if (extendedElementsType == 1)
        var extendedValidElements = js + "," + iframe;
    else if (extendedElementsType == 2) //just iframe
        var extendedValidElements = iframe
    else if (extendedElementsType == 2) //just js
        var extendedValidElements = js

    tinyMCE_GZ.init({
        plugins: enabledPlugins,
        themes: 'simple,advanced',
        languages: 'en',
        disk_cache: true,
        debug: false
    });

    mode = "none";

    if (!managedMode)
        mode = "textareas"

    tinyMCE.init({
        // General options
        mode: mode,
        theme: "advanced",
        editor_selector: "tiny-mce-editor",
        plugins: enabledPlugins,

		autosave_ask_before_unload:false,

        // Theme options
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,insertimage,insertfile,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,pagebreak,restoredraft",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true,

        extended_valid_elements: extendedValidElements,

        // Example content CSS (should be your site CSS)
        content_css: cssFile,

        // Drop lists for link/image/media/template dialogs
        template_external_list_url: "lists/template_list.js",
        external_link_list_url: "/Admin/TinyMCEExternalLinkList/",
        external_image_list_url: "lists/image_list.js",
        media_external_list_url: "lists/media_list.js"
    });
}

function InitSubmitButton(id)
{
    $("#" + id).hover(
	    function ()
	    {
	        $(this).addClass("ui-state-hover");
	    },
	    function ()
	    {
	        $(this).removeClass("ui-state-hover");
	    });
}

function YesNoDialog(linkedDivId, linkedButtonId, width, height, isModal, id)
{
    $(linkedDivId).dialog(
    {
        autoOpen: false,
        buttons: {
            'Yes': function ()
            {
                $("#submitter-" + id).val(linkedButtonId);
                $(linkedButtonId).parents('form:first').submit();
                $(this).dialog('close');
            },
            'No': function ()
            {
                $(this).dialog('close');
            }
        },
        resizable: false,
        height: height,
        width: width,
        modal: isModal,
        draggable: false,
        bgiframe: true
    });

    $(linkedButtonId).click(function ()
    {
        $(linkedDivId).dialog('open');
        return false;
    });
}

function OkDialog(linkedDivId, linkedButtonId, width, height, isModal, id)
{
    $(linkedDivId).dialog(
    {
        autoOpen: false,
        buttons: {
            'OK': function ()
            {
                $(this).dialog('close');
            }
        },
        resizable: false,
        height: height,
        width: width,
        modal: isModal,
        draggable: false,
        bgiframe: true
    });

    $(linkedButtonId).click(function ()
    {
        $(linkedDivId).dialog('open');
        return false;
    });
}

function CheckUrlFor301(urlId, origUrlId)
{

    var currentUrl = $("#" + urlId).val();
    var originalUrl = $("#" + origUrlId).val();

    if (currentUrl.toLowerCase() != originalUrl.toLowerCase())
    {
        $("#check-redirect-301").dialog('open');
    }
}

function InitAutoRedirectionUrlCheck(urlId, origUrlId, redirectFlagId)
{

    if ($("#check-redirect-301").length > 1)
        alert('You can only have once instance of a UrlTextbox per page.');

    $("#" + urlId).blur(function ()
    {
        CheckUrlFor301(urlId, origUrlId);
    });

    InitAutoRedirectionUrlCheckDialog(redirectFlagId);
}

function InitAutoRedirectionUrlCheckDialog(redirectFlagId)
{
    $("#check-redirect-301").dialog(
    {
        autoOpen: false,
        buttons: {
            "Yes": function ()
            {
                $("#" + redirectFlagId).val("true");
                $(this).dialog("close");
            },            
            "No": function ()
            {
                $("#" + redirectFlagId).val("false");
                $(this).dialog("close");
            }
        },
        resizable: false,
        height: 200,
        width: 350,
        modal: true,
        draggable: false,
        bgiframe: true
    });
}

function InitTagControl(name)
{
    $("#tag-add").click(function ()
    {
        $.ajax({
            type: "POST",
            url: "/Admin/Tags/Add/" + $("#newTag").val(),
            data: "{}",
            dataType: "text",
            success: function (data)
            {
                data = JSON.parse(data);

                if (data.Error)
                    alert("Error adding tag.  Either the tag entered is blank or already created.");
                else
                {
                    newCheckmark = '<input type="checkbox" name="' + name + '" id="tag-item-' + data.Id + '" checked="checked" value="' + data.Id + '"/> <label for="tag-item-' + data.Id + '"> ' + data.Tag + '</label> <br />';
                    $("#tag-list").prepend(newCheckmark);
                    $("#newTag").val("")
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown)
            {
                alert("Error adding tag.");
            }
        });

        return false;
    });
}


function SetActiveRadio(radioButton, associatedControl)
{
    //get control by id because names are not unique
    radioControl = document.getElementById(radioButton);
    switch (associatedControl.type)
    {
        case "checkbox":
            if (associatedControl.checked)
            {
                radioControl.checked = true;
            }
            break;
        case "text":
            if (associatedControl.value.length > 0)
            {
                radioControl.checked = true;
            }
            break;
        case "select-one":
            radioControl.checked = true;
            break;
    }
}

function AddTempAccess() {
	$.ajax({
		type: "POST",
		url: "/Admin/TempAccess/Add",
		dataType: "text"
	});
}

function RemoveTempAccess() {
	$.ajax({
		type: "POST",
		url: "/Admin/TempAccess/Remove",
		dataType: "text"
	});
}

var cache = [];
function PreLoadImage(path) {
    var cacheImage = document.createElement('img');
    cacheImage.src = path;
    cache.push(cacheImage);
}
