﻿function getContent(subject, target) {
    $(target).html('');
    $.ajax({
        type: "POST",
        data: "{'subject': '" + subject + "'}",
        contentType: "application/json; charset=utf-8",
        url: "ContentFetcher.asmx/GetContent",
        dataType: "json",
        success: function (data) { $(target).html(data.d); },
        error: function (e) { $(target).html("An Error Occured"); }
    });
}

function getCheckStateTableString(subject, target, animate) {
    if (animate) {
        showLoader(".loader");
        $(target).html('');
        $(target).hide();
    }
    $.ajax({
        cache: false,
        type: "GET",
        url: "ContentHandler.axd?type=checkstate&subject=" + subject + "",
        success: function (data) {
            $(target).html(data);
            slideOpenAnimation(target);
            hideLoader(".loader");
        },
        error: function (e) {$(target).html("An Error Occured"); }
    });
}

function changeCheckStateTable(subject, target, obj) {
    $.ajax({
        cache:false,
        //type: "GET",
        url: "ContentHandler.axd?type=checkstateChange&subject=" + subject + "&object=" + obj,
        success: function (data) {
            getCheckStateTableString(subject, target, true);
            //getCheckStateTableString(subject, target, false);
        },
        error: function (e) { $(target).html("An Error Occured"); }
    });
}


function getContentAnimationLoader(subject, target) {
    showLoader(".loader");
    $(target).html('');
    $.ajax({
        type: "POST",
        data: "{'subject': '" + subject + "'}",
        contentType: "application/json; charset=utf-8",
        url: "ContentFetcher.asmx/GetContent",
        dataType: "json",
        success: function (data) {
            $(target).html(data.d);
            slideOpenAnimation(target);
            hideLoader(".loader");
        },
        error: function (e) { $(target).html("An Error Occured"); }
    });
}

function getFileTableString(subject, target) {
    showLoader(".loader");
    $(target).html('');
    $(target).hide();
    $.ajax({
        type: "POST",
        data: "{'subject': '" + subject + "', 'procedureName':'sp_GetFiles'}",
        contentType: "application/json; charset=utf-8",
        url: "ContentFetcher.asmx/GetTable",
        dataType: "json",
        success: function (data) {
            $(target).html(data.d);
            slideOpenAnimation(target);
            hideLoader(".loader");
        },
        error: function (e) { $(target).html("An Error Occured"); }
    });
}

function getFileTableWithFormat(subject, target) {
    showLoader(".loader");
    $(target).html('');
    $.ajax({
        type: "POST",
        data: "{'subject': '" + subject + "', 'procedureName':'sp_GetFiles'}",
        contentType: "application/json; charset=utf-8",
        url: "ContentFetcher.asmx/GetList",
        dataType: "json",
        success: function (data) {
            var files = data.d;
            var bool = true;
            $.each(files, function (index, files) {
                if (bool) {
                    $(target).append("<tr><td><a href='FileHandler.axd?type=file&id=" + files.Id + "'>" + files.Title + "</a></td><td class='center'>" + files.PublishDate + "</td><td class='center'><img src='Images/" + files.FileType + ".png'></td></tr>");
                    bool = false;
                }
                else {
                    $(target).append("<tr class='alt'><td><a href='FileHandler.axd?type=file&id=" + files.Id + "'>" + files.Title + "</a></td><td class='center'>" + files.PublishDate + "</td><td class='center'><img src='Images/" + files.FileType + ".png'></td></tr>");
                    bool = true;
                }
            });

            slideOpenAnimation(target);
            hideLoader(".loader");
        },
        error: function (e) { $(target).html("An Error Occured"); }
    });
}

function slideOpenAnimation(target) {
    //$(target).animate({ height: $(target).height() + 50 + "px" }, { queue: false, duration: 300 })
    //alert($(target).height());
    $(target).parent().animate({ height: $(target).height() + 50 + "px" }, { queue: false, duration: 300 })
    $(target).show();
}

function showLoader(target) {
    $(target).html('<center><img src="Images/ajax-loader.gif" /></center>');
}

function hideLoader(target) {
    $(target).html('');
}

function setColor(target) {
    $(target).children(".download").css("color","#000");
}

function scrollToTop(){
    $(document).ready(function () {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    });
}

function openSite(url) {
    open(url, "yes")
}

function onmodal(subject) {
    $("#modal").modal({
        minHeight: 500,
        overlayClose: true,
        resize: true
    });
    $(".modalHeader").html("<h2>" + subject + "</h2>");
    showLoader(".modalBody");
    getContent(subject, ".modalBody");
}
