﻿var CATEGORY_FIELD_ID = "#ctl00_Content_CategoryDD_CategoryField";
var NEW_CATEGORY_FORM = "#newCategoryForm";
var NEW_CATEGORY_ADD = "#ctl00_Content_CategoryDD_newCategoryAdd";
var NEW_CATEGORY_SAVE = "#newCategorySave";
var NEW_CATEGORY_CANCEL = "#newCategoryCancel";
var NEW_CATEGORY_NAME = "#newCategoryField";
var NEW_CATEGORY_PARENT = "#ctl00_Content_CategoryDD_NewCategoryDD";
var CATEGORY_DD = "#ctl00_Content_CategoryDD_CategoryDD";
var NO_NAME_ERROR = "Please fill in a name to add a category";
var SEARCH_ID = '#query';

var imgBrowser = null;
var defaultText = '';
var searchFocused = false;
var autoCompleteURL = '';
var THUMB_DIR = 'thumbnail/';
var GALLERY_DIR = 'admin-gallery-thumb/';

var tiny_mce_settings = {
    // General options
    theme: "advanced",
    skin: "bbox",
    mode: "specific_textareas",
    editor_selector: "mceEditor",
    height: "550px",
    width: "650px",
    plugins: "contextmenu,flash,inlinepopups,paste,safari,spellchecker,table,imageselect,galleryselect,media,break",
    // Theme options
    theme_advanced_buttons1: "bold,italic,underline,formatselect,|,bullist,numlist,hr,break,outdent,indent,blockquote,|,link,unlink,anchor,|,imageselect,galleryselect,|,undo,redo,|,cleanup,code",
    theme_advanced_buttons2: "table,delete_table,row_props,cell_props,delete_col,col_after,col_before,delete_row,row_after,row_before,split_cells,merge_cells,|,selectall,pastetext,pasteword,spellchecker,charmap",
    theme_advanced_buttons3: "",

    theme_advanced_toolbar_location: "top",
    theme_advanced_toolbar_align: "left",
    theme_advanced_statusbar_location: "bottom",
    theme_advanced_resizing: true,
    theme_advanced_resize_horizontal: "",

    paste_create_linebreaks: false,
    paste_create_paragraphs: true,
    paste_auto_cleanup_on_paste: true,
    paste_convert_middot_lists: true,
    paste_convert_headers_to_strong: false,
    paste_remove_spans: true,
    paste_remove_styles: true,
    paste_strip_class_attributes: "all",
    paste_use_dialog: false,
    content_css: "/stylesheets/tinyMCE.css",
    galleryselect_css: "/style",
    imageselect_css: "/style",
    extended_valid_elements: "script[language|type|src],script[type|src],script[src|type],object[classid|codebase|width|height|align|type|data],param[id|name|type|value|valuetype<DATA?OBJECT?REF],iframe[src|style|width|height|scrolling|marginwidth|marginheight|frameborder],form[name|id|action|method|enctype|accept-charset|onsubmit|onreset|target],input[id|name|type|value|size|maxlength|checked|accept|src|width|height|disabled|readonly|tabindex|accesskey|onfocus|onblur|onchange|onselect],textarea[id|name|rows|cols|disabled|readonly|tabindex|accesskey|onfocus|onblur|onchange|onselect],option[name|id|value],select[id|name|type|value|size|maxlength|checked|accept|src|width|height|disabled|readonly|tabindex|accesskey|onfocus|onblur|onchange|onselect|length|options|selectedIndex]",
    relative_urls: false,
    spellchecker_languages: "+English=en",
    spellchecker_rpc_url: "/javascripts/tiny_mce/plugins/spellchecker/Default.aspx"
}

var tiny_mce_settings_small = {
    theme: "advanced",
    mode: "specific_textareas",
    editor_selector: "mceEditorSmall",
    height: "200px",
    width: "270px",
    plugins: "inlinepopups,paste,safari",
    theme_advanced_buttons1: "bold,italic,underline,|,link,unlink,|,undo,redo,|,code",
    theme_advanced_buttons2: "",
    theme_advanced_buttons3: "",
    theme_advanced_toolbar_location: "top",
    theme_advanced_toolbar_align: "left",
    theme_advanced_path_location: "bottom",
    invalid_elements: "span",
    paste_create_linebreaks: false,
    paste_create_paragraphs: true,
    paste_auto_cleanup_on_paste: true,
    paste_convert_middot_lists: true,
    paste_convert_headers_to_strong: false,
    paste_remove_spans: true,
    paste_remove_styles: true,
    paste_strip_class_attributes: "all",
    paste_unindented_list_class: "unindentedList",
    paste_use_dialog: false,
    content_css: "/stylesheets/tinyMCE.css",
    relative_urls: false,
    debug: false
}
function initAddCategory() {
    if (!$(CATEGORY_DD).length) return false;

    $(NEW_CATEGORY_ADD).click(function() {
        $(NEW_CATEGORY_NAME).val('')
        $(NEW_CATEGORY_PARENT).val('0')
        $(NEW_CATEGORY_FORM).slideToggle();
    });
    $(NEW_CATEGORY_CANCEL).click(function() {
        $(NEW_CATEGORY_FORM).slideUp();
    });
    $(NEW_CATEGORY_SAVE).click(function() {
        if ($(NEW_CATEGORY_NAME).val() == "") return alert(NO_NAME_ERROR);

        $(NEW_CATEGORY_FORM).slideUp();
        $(CATEGORY_DD).html('<option value="0">updating...</option>');
        $.getJSON('/admin/categories/create', { name: $(NEW_CATEGORY_NAME).val(), parentId: $(NEW_CATEGORY_PARENT).val() }, function(data) {
            $(NEW_CATEGORY_PARENT).html(data.options);
            $(CATEGORY_DD).html(data.options);
            $(CATEGORY_FIELD_ID).val($(CATEGORY_DD).val());
        });
    });
    $(CATEGORY_DD).change(function() {
        $(CATEGORY_FIELD_ID).val($(CATEGORY_DD).val());
    });
}

function initAutoComplete() {
    if ($(SEARCH_ID).length < 1) return;

    defaultText = $(SEARCH_ID).val();
    $(SEARCH_ID).focus(function() {
        searchFocused = true;
        $(this).val('');
    });
    $(SEARCH_ID).blur(function() {
        if ($(this).val() == '') $(this).val(defaultText);
        searchFocused = false;
    });
    $(SEARCH_ID).autocomplete({
        serviceUrl: '/admin/autocomplete/' + autoCompleteURL,
        onSelect: function(value, data) {
            if (data == "") return;
            location.href = data;
        }
    });
    /* ADD HOTKEY */
    $(document).keyup(function(e) {
        if ((e.which == 83 || e.which == 115) && !searchFocused) $(SEARCH_ID).focus();
    });
}

/** WINDOW ONLOAD **/
$(document).ready(function() {
    $('.overview:visible').corners('6px');
    $('a.tip').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    });
    $('#inUseList a').click(function() {
        var id = '#' + $(this).attr('id') + '_list';
        var pos = $(this).position();
        $(this).toggleClass('on');
        $(id).slideToggle();
        if ($(this).is('.on')) $('html, body').animate({ scrollTop: pos.top }, 250);
    });
    initAddCategory();
    initAutoComplete();


    if ($('.success').length > 0) setTimeout(hideSuccess, 2500);
});
function hideSuccess() {
    $('.success').fadeOut("slow");
}
$(window).unload(function() {
    if (imgBrowser) imgBrowser.close();
    imgBrowser = null;
});
