﻿function HighLight() {
    //Highlight top menu items
    var url = window.location.pathname;
    $('#topmenu li [href|="' + url + '"]').addClass("highlight");
}

function Search(sUrl) {
    var url = sUrl;
    $('#request').keyup(function() {
        setTimeout(function() { searchAjax(url); }, 1000);
    });

    $('form[action="' + sUrl + '"]').submit(function() {
        return false;
    });
}

function searchAjax(url) {
    var requestData = "request=" + $('#request').attr('value');
    $.ajax({
            url: url,
            data: requestData,
            success: function(data) {
                $('#content').html(data);
                DeletePost();
            }
        });
}

function AjaxProgress(url) {
    // Setup the ajax indicator
    $('#request').parent('div').append('<span id="ajaxBusy"><img src="' + url+ '"></span>');

    $('#ajaxBusy').css({
        display: "none",
        margin: "0px",
        paddingLeft: "10px",
        paddingRight: "0px",
        paddingTop: "0px",
        paddingBottom: "0px",
        right: "3px",
        top: "3px",
        width: "auto"
    });
}

// Ajax activity indicator bound to ajax start/stop document events
$(document).ajaxStart(function () {
    $('#ajaxBusy').show();
}).ajaxStop(function () {
    $('#ajaxBusy').hide();
});

function DeletePost() {
    //Ajax delete
    $('a[href^="/Home/Delete/"]').click(function() {
        var thisLink = $(this);
        var linkHref = $(thisLink).attr("href");
        var postContainer = $(thisLink).parents('.post');
        var postName = $(postContainer).find('h2').text();

        if (confirm("Are you sure you want to delete the " + postName + "?")) {
            $.get(linkHref, function() {
                postContainer.remove();
            });
        }
        return false;
    });
}

function DeleteCategory() {
    //Ajax delete
    $('a[href^="/Category/Delete/"]').click(function() {
        var thisLink = $(this);
        var linkHref = $(thisLink).attr("href");
        var postContainer = $(thisLink).parents('.post');
        var postName = $(postContainer).find('h2').text();

        if (confirm("Are you sure you want to delete the " + postName + "?")) {
            $.get(linkHref, function() {
                postContainer.remove();
            });
        }
        return false;
    });
}


