﻿$(document).ready(function () {
//    $('.submit').click(function () {
//        $('.processing').show();
//        var form = $(this).parents('form:first');
//        var submitted = false;

//        $(form).validate({
//            showErrors: function (errorMap,
//             errorList) {
//                if (submitted) {
//                    var summary = "";
//                    $.each(errorList, function () { summary += " * " + this.message + "\n"; });
//                    $(".errors").text(summary);
//                    submitted = false;
//                }
//                $('.processing').hide();
//                //this.defaultShowErrors();
//            },
//            invalidHandler: function (form, validator) {
//                submitted = true;
//            }
//        });
//    });

    $(".feed-list li").click(function (event) {
        $(".feed-list li").removeClass();
        $(this).addClass("feed-selected");
        $("#feed-name").text($(this).text());
        GetFeed($(this).attr('id'));
    });

    $("#add-feed").click(function (event) {
        event.preventDefault();
        $.get('/Feed/Add', function (content) {
            jQuery.fancybox({
                'content': $(content).find('#add-feed-panel').html(),
                'autoDimensions': false,
                'padding': '0px',
                'width': 420,
                'height': 'auto',
                'onComplete': function () {
                    $('#confirm').click(function (event) {
                        $('.processing').show();
                        event.preventDefault();
                        AddFeed($('#feed-name').val(), $('#feed-url').val());
                    });
                }
            });
        });
    });

    $('.feed-list li').hover(
        function () {
            $(this).find('a').css('display', 'block');
        },
        function () {
            $(this).find('a').css('display', 'none');
        }
    );

    $('.remove-feed').click(function (event) {
        event.preventDefault();
        var id = $(this).attr('href').replace('#', '');
        $.get('Content/Confirm.htm', function (content) {
            var msg = content.replace('msg', 'Are you sure you want to delete this?');
            fancyConfirm(msg, function (ret) {
                if (ret) RemoveFeed(id);
            });
        });
    });
});

function GetFeed(id) {
    $.ajax({
        type: 'GET',
        cache: false,
        data: '{"feedid":"' + id + '"}',
        url: '/Feed/LoadFeed/' + id,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
            var items = "";
            for (var i in data.Items) {
                items += "<li><a href='" + data.Items[i].Url + "' title='" + data.Items[i].Summary + "'>" + data.Items[i].Title + "</a></li>";
            }
            $(".feed").html(items);
        },
        error: function (xhr) {
            alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
        }
    });
    $(".processing").hide();
}

function AddFeed(name, url) {
    $.ajax({
        type: 'POST',
        cache: false,
        data: '{"feedname":"' + name + '", "feedurl":"' + url + '"}',
        url: '/Feed/Add/',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
            if (data.Advisory != null) {
                $("#errors").html(data.Advisory.AdvisoryMessage);
            } else {
                window.location.reload(true);
            }
        },
        error: function (xhr) {
            alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
        }
    });
    $(".processing").hide();
}

function RemoveFeed(id) {
    $.ajax({
        type: 'POST',
        cache: false,
        data: '{"feedid":"' + id + '"}',
        url: '/Feed/Remove/',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
            if (data.Advisory != null) {
                $("#errors").html(data.Advisory.AdvisoryMessage);
            } else {
                window.location.reload(true);
            }
        },
        error: function (xhr) {
            alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
        }
    });
    $(".processing").hide();
}

function fancyConfirm(content, callback) {
    var ret;
    jQuery.fancybox({
        modal: true,
        content: content,
        onComplete: function () {
            jQuery("#confirm_cancel").click(function () {
                ret = false;
                jQuery.fancybox.close();
            })
            jQuery("#confirm_ok").click(function () {
                ret = true;
                jQuery.fancybox.close();
            })
        },
        onClosed: function () {
            callback.call(this, ret);
        }
    });
}

