﻿/// <reference path="jquery.min-vsdoc2.js" />

function bmcInit() {

    // admin footer
    $("#footpanel li").hover(function() {
        $(this).children("div.footerSubMenu").css("display", "block");
        $(this).children("a").children("small").css("display", "block");
    }, function() {
        $(this).children("div.footerSubMenu").css("display", "none");
        $(this).children("a").children("small").css("display", "none");
    });
    $("#AddNoteIcon").bind("click", function() { showNoteForm(); return false; });
    $("#GetNotesIcon").bind("click", function() { getFooterNotes(); return false; });
    $("#footerNoteList li").live("click", function() { var url = $(this).attr("class"); showNoteForm(url); return false; });
    $("#footerLogin img").bind("click", function() { showLoginForm(); });
    $("#chatpanel").adjustPanel(); //Run the adjustPanel function on #chatpanel
    $("#alertpanel").adjustPanel(); //Run the adjustPanel function on #alertpanel

    $("img.EditNoteButton").die("click.EditNoteButton").live("click.EditNoteButton", function(e) {
        var url = "/admin/noteedit.aspx?id=" + $(this).attr("id");
        showNoteForm(url);
        return false;
    });

    $("div .closeDialog").live("click", function() { $(this).parent().fadeOut('slow').hide(); });
    bindShortcutKeys();
}

function bindShortcutKeys() {
    $(document).bind('keydown', { combi: 'alt+ctrl+L', disableInInput: true }, function() { showLoginForm(); });
    //$(document).bind('keydown', { combi: 'alt+N+shift', disableInInput: true }, function() { showNoteForm(); });
}

function hideAttachmentWindow() {
    $("#frmAttach").toggleClass("displayNone");
}

function initCommentForm() {
    $("[ID$='btnPostComment']").bind("click", function() { postComment(); });
}

function LoadAttachmentPage() {
    var attachRefID = $("#attachContainer [ID$='hidAttachmentRefID']").val();
    //$(window.frames['frmAttach'].document).load("../admin/attach.aspx?refid=" + attachRefID);
    var url = 'attachfile.aspx?refid=' + attachRefID;
    $("#frmAttach").attr("src", url).toggleClass("displayNone");
}

function postComment() {
    //$("[ID$='btnPostComment']").attr("disabled") = true;
    var comment = new Object();
    comment.ID = "00000000-0000-0000-0000-000000000000";
    comment.ContentID = $("#contentID").html();
    comment.Author = $("[ID$='txtAuthorName']").val();
    comment.AuthorEmail = $("[ID$='txtAuthorEmail']").val();
    comment.HTML = $("[ID$='txtComment']").val();
    comment.Timestamp = "1/1/1900";
    comment.ClientIP = "";
    comment.Active = true;
    var DTO = { "comment": comment };
    // send list to server
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/blog.aspx/PostComment",
        data: JSON.stringify(DTO),  // use JSON2 script to encode transfer object to JSON
        dataType: "json",
        success: commentSuccess,
        failure: commentError
    });
    return false;
}
function clearCommentForm() {
    $("[ID$='txtAuthorName']").val("");
    $("[ID$='txtAuthorEmail']").val("");
    $("[ID$='txtComment']").val("");
    //$("[ID$='btnPostComment']").attr("disabled") = false;
}
function commentSuccess(result) {
    if (result.hasOwnProperty("d")) { result = result.d; }
    $("#contentComments").append(result);
    clearCommentForm();
}

function reportError(error) {
    alert(error.get_message());
}

function setupContentAdd() {
    $("[ID$='txtTitle']").bind("keyup", function() {
        var title = $("[ID$='txtTitle']").val().replace(/ /g, '-').replace(/\'/g, '').replace(/\"/g, '').replace(/\./g, '-');
        var alias = $("#urlAliasDate").text();
        alias = alias + title;
        $("[ID$='txtURLAlias']").val(alias.toLowerCase());
    });
}

function showLoginForm() {
    $("#footerLoginPanel").toggleClass("displayNone");
    $("#footerLoginPanel input:text[ID$='login_UserName']").focus();
}

function showNoteForm(url) {
    var noteFormURL = "/admin/noteedit.aspx";
    if (url != null) noteFormURL = url;
    var form = $("#EditNoteForm");
    if (form.length > 0) {
        if (form.parent().css('display') === 'none') {
            $(form).children("iframe").attr("src", noteFormURL);
            //form.fadeIn("slow");
            form.dialog('open');
        } else {
            //form.fadeOut('slow');
            form.dialog('close');
        }
    } else {
        var div = $("<div id='EditNoteForm' class=''></div>");
        //div.append("<div class='closeDialog'>X</div>");
        div.append("<iframe src='" + noteFormURL + "' id='EditNoteFrame'></iframe>");
        //div.hide();
        $("#siteFrame").append(div);
        div.dialog({ autoOpen: false, minHeight: 390, modal: true, position: 'center', resizable: false, width: 645  });
        //form.fadeIn("slow");
        div.dialog('open');
    }
    return false;
}

function getFooterNotes() {
    var DTO = { "count": 6 };
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/admin/notelist.aspx/GetActiveNotes",
        data: JSON.stringify(DTO),
        dataType: "json",
        success: getFooterNotesSuccess,
        failure: reportError
    });

}

function getFooterNotesSuccess(msg) {
    if (msg.hasOwnProperty("d")) msg = msg.d;
    $("#footerNoteList").html(msg);
}

//Adjust panel height
$.fn.adjustPanel = function() {
    $(this).find("ul, .subpanel").css({ 'height': 'auto' }); //Reset sub-panel and ul height

    var windowHeight = $(window).height(); //Get the height of the browser viewport
    var panelsub = $(this).find(".subpanel").height(); //Get the height of sub-panel
    var panelAdjust = windowHeight - 100; //Viewport height - 100px (Sets max height of sub-panel)
    var ulAdjust = panelAdjust - 25; //Calculate ul size after adjusting sub-panel

    if (panelsub > panelAdjust) {	 //If sub-panel is taller than max height...
        $(this).find(".subpanel").css({ 'height': panelAdjust }); //Adjust sub-panel to max height
        $(this).find("ul").css({ 'height': panelAdjust }); ////Adjust subpanel ul to new size
    }
    else { //If sub-panel is smaller than max height...
        $(this).find("ul").css({ 'height': 'auto' }); //Set sub-panel ul to auto (default size)
    }
};

//Each time the viewport is adjusted/resized, execute the function
$(window).resize(function() {
    $("#chatpanel").adjustPanel();
    $("#alertpanel").adjustPanel();
});
//Click event on Chat Panel and Alert Panel	
$("#chatpanel a:first, #alertpanel a:first").click(function() { //If clicked on the first link of #chatpanel and #alertpanel...
    if ($(this).next(".subpanel").is(':visible')) { //If subpanel is already active...
        $(this).next(".subpanel").hide(); //Hide active subpanel
        $("#footpanel li a").removeClass('active'); //Remove active class on the subpanel trigger
    }
    else { //if subpanel is not active...
        $(".subpanel").hide(); //Hide all subpanels
        $(this).next(".subpanel").toggle(); //Toggle the subpanel to make active
        $("#footpanel li a").removeClass('active'); //Remove active class on all subpanel trigger
        $(this).toggleClass('active'); //Toggle the active class on the subpanel trigger
    }
    return false; //Prevent browser jump to link anchor
});

//Click event outside of subpanel
$(document).click(function() { //Click anywhere and...
    $(".subpanel").hide(); //hide subpanel
    $("#footpanel li a").removeClass('active'); //remove active class on subpanel trigger
});
$('.subpanel ul').click(function(e) {
    e.stopPropagation(); //Prevents the subpanel ul from closing on click
});

//Show/Hide delete icons on Alert Panel
$("#alertpanel li").hover(function() {
    $(this).find("a.delete").css({ 'visibility': 'visible' }); //Show delete icon on hover
}, function() {
    $(this).find("a.delete").css({ 'visibility': 'hidden' }); //Hide delete icon on hover out
});