﻿var $loading = $('<center><img src="/Content/themes/base/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon"></center>');
var $dialog = $('<div></div>');
var $myx = ($(window).width() / 2) - 300;
var $myy = ($(window).height() / 8);
var $height = 150;
var $width = 600;
$(document).ready(function () {
    $('a.aCreate').live('click', function (event) { addCreate(this, event, '#Info'); });
    $('a.aEdit').live('click', function (event) { addEdit(this, event, '#Info'); });
    $('a.aSearch').live('click', function (event) { addSearch(this, event, '#Info'); });
    $('a.aLogin').live('click', function (event) { addLogin(this, event, '#Login'); });
    $('a.aRead').live('click', function (event) { addRead(this, event, '#Info'); });
    $('a.aDelete').live('click', function (e) {
        e.preventDefault();
        var $btn = $(this);
        var $msg = $(this).attr('title');
        confirmDelete($msg, function () {
            deleteRow($btn);
        });
    });
    $('a.aPage').live('click', function (e) {
        e.preventDefault();
        var $btn = $(this);
        page($btn)
    });
    $('a.aExit').live('click', function (e) {
        e.preventDefault();
        var $btn = $(this);
        exit($btn)
    });
    $('a.aPuan').live('click', function (e) {
        e.preventDefault();
        var $btn = $(this);
        puan($btn)
    });
});
function displayError(message, status) {
    var $dialog = $(message);
    $dialog
        .dialog({
            modal: false,
            position: [$myx + 100, $myy],
            width: $width - 200,
            show: 'fade',
            hide: 'fade',
            title: status + ' Error',
            buttons: {
                'Kapat': function () {
                    $(this).dialog('close');
                    $(this).empty();
                }
            }
        });
    return false;
};
function page($btn) {
    $.ajax({
        url: $btn.attr('onclick'),
        success: function (data) {
            $('#Info').html(data);
            $('#ajaxResult').hide().html($btn.attr('title') + ' Sayfaya Geçildi').fadeIn(300, function () {
                var e = this;
                setTimeout(function () { $(e).fadeOut(400); }, 2500);
            });
        },
        error: function (xhr, status) {
            if (xhr.status == 400)
                dlg.html(xhr.responseText, xhr.status);
            else
                displayError(xhr.responseText, xhr.status);
        }
    });
    return false;
};
function exit($btn) {
    $.ajax({
        url: $btn.attr('onclick'),
        success: function (data) {
            $('#Login').html(data);
            $('#ajaxLogin').hide().html($btn.attr('title') + ' Yapıldı').fadeIn(300, function () {
                var e = this;
                setTimeout(function () { $(e).fadeOut(400); }, 2500);
            });
        },
        error: function (xhr, status) {
            if (xhr.status == 400)
                dlg.html(xhr.responseText, xhr.status);
            else
                displayError(xhr.responseText, xhr.status);
        }
    });
    return false;
};
function puan($btn) {
    $.ajax({
        url: $btn.attr('onclick'),
        success: function (data) {
            $('#' + $btn.attr('id') + 'Info').html(data);
            $('#' + $btn.attr('id') + 'Result').hide().html($btn.attr('title') + ' Puan Verildi').fadeIn(300, function () {
                var e = this;
                setTimeout(function () { $(e).fadeOut(400); }, 2500);
            });
        },
        error: function (xhr, status) {
            if (xhr.status == 400)
                dlg.html(xhr.responseText, xhr.status);
            else
                displayError(xhr.responseText, xhr.status);
        }
    });
    return false;
};
function addCreate(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('onclick'))
        .dialog({ autoOpen: true, modal: false, title: $(tag).attr('title'), position: [$myx, $myy], minHeight: $height, width: $width, show: 'fade', hide: 'fade' });
    $dialog.dialog('option', 'buttons', {
        'Kaydet': function () {
            var dlg = $(this);
            var $frm = $('#frmData');
            $.ajax({
                type: 'POST',
                url: $frm.attr('action'),
                data: $frm.serialize(),
                success: function (data, textStatus, xhr) {
                    $('#Info').html(data);
                    $('#ajaxResult').hide().html('Kayıt Eklendi').fadeIn(300, function () {
                        var e = this;
                        setTimeout(function () { $(e).fadeOut(400); }, 2500);
                    });
                    dlg.dialog('close');
                    dlg.empty();
                },
                error: function (xhr, status) {
                    if (xhr.status == 400)
                        dlg.html(xhr.responseText, xhr.status);
                    else if (xhr.status == 404) {
                        dlg.dialog('close');
                        dlg.empty();
                    }
                    else
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'Kapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function addEdit(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('onclick'))
        .dialog({ autoOpen: true, modal: false, title: $(tag).attr('title'), position: [$myx, $myy], minHeight: $height, width: $width, show: 'fade', hide: 'fade' });
    $dialog.dialog('option', 'buttons', {
        'Düzenle': function () {
            var dlg = $(this);
            var $frm = $('#frmData');
            $.ajax({
                type: 'POST',
                url: $frm.attr('action'),
                data: $frm.serialize(),
                success: function (data, textStatus, xhr) {
                    $('#Info').html(data);
                    $('#ajaxResult').hide().html('Kayıt Düzenlendi').fadeIn(300, function () {
                        var e = this;
                        setTimeout(function () { $(e).fadeOut(400); }, 2500);
                    });
                    dlg.dialog('close');
                    dlg.empty();
                },
                error: function (xhr, status) {
                    if (xhr.status == 400)
                        dlg.html(xhr.responseText, xhr.status);
                    else if (xhr.status == 404) {
                        dlg.dialog('close');
                        dlg.empty();
                    }
                    else
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'Kapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function addSearch(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('onclick'))
        .dialog({ autoOpen: true, modal: false, title: $(tag).attr('title'), position: [$myx + 100, $myy], minHeight: $height, width: $width - 200, show: 'fade', hide: 'fade' });
    $dialog.dialog('option', 'buttons', {
        'Arama Yap': function () {
            var dlg = $(this);
            var $frm = $('#frmData');
            $.ajax({
                type: 'POST',
                url: $frm.attr('action'),
                data: $frm.serialize(),
                success: function (data, textStatus, xhr) {
                    $('#Info').html(data);
                    $('#ajaxResult').hide().html('Arama Yapıldı').fadeIn(300, function () {
                        var e = this;
                        setTimeout(function () { $(e).fadeOut(400); }, 2500);
                    });
                    dlg.dialog('close');
                    dlg.empty();
                },
                error: function (xhr, status) {
                    if (xhr.status == 400)
                        dlg.html(xhr.responseText, xhr.status);
                    else if (xhr.status == 404) {
                        dlg.dialog('close');
                        dlg.empty();
                    }
                    else
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'Kapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function addLogin(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('onclick'))
        .dialog({ autoOpen: true, modal: false, title: $(tag).attr('title'), position: [$myx + 100, $myy], minHeight: $height, width: $width - 200, show: 'fade', hide: 'fade' });
    $dialog.dialog('option', 'buttons', {
        'Giriş Yap': function () {
            var dlg = $(this);
            var $frm = $('#frmData');
            $.ajax({
                type: 'POST',
                url: $frm.attr('action'),
                data: $frm.serialize(),
                success: function (data, textStatus, xhr) {
                    $('#Login').html(data);
                    $('#ajaxLogin').hide().html('Giriş Yapıldı').fadeIn(300, function () {
                        var e = this;
                        setTimeout(function () { $(e).fadeOut(400); }, 2500);
                    });
                    dlg.dialog('close');
                    dlg.empty();
                },
                error: function (xhr, status) {
                    if (xhr.status == 400)
                        dlg.html(xhr.responseText, xhr.status);
                    else if (xhr.status == 404) {
                        dlg.dialog('close');
                        dlg.empty();
                    }
                    else
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'Kapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function confirmDelete(message, callback) {
    var $deleteDialog = $('<div><b style="color:red">' + message + '</b> <br/> Kaydı Silmek İstiyormusunuz</div>');
    $deleteDialog
        .dialog({
            title: 'Kaydı Sil',
            resizable: false,
            modal: false,
            position: [$myx + 150, $myy],
            minHeight: $height,
            width: $width - 300,
            buttons: {
                'Sil': function () {
                    $(this).dialog('close');
                    callback.apply();
                    $(this).empty();
                },
                'Kapat': function () {
                    $(this).dialog('close');
                    $(this).empty();
                }
            }
        });
};
function deleteRow($btn) {
    $.ajax({
        url: $btn.attr('onclick'),
        success: function (data) {
            $('#Info').html(data);
            $('#ajaxResult').hide().html('Kayıt Silindi').fadeIn(300, function () {
                var e = this;
                setTimeout(function () { $(e).fadeOut(400); }, 2500);
            });
        },
        error: function (xhr, status) {
            if (xhr.status == 400)
                dlg.html(xhr.responseText, xhr.status);
            else if (xhr.status == 404) {
                dlg.dialog('close');
                dlg.empty();
            }
            else
                displayError(xhr.responseText, xhr.status);
        }
    });
    return false;
};
function addRead(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('onclick'))
        .dialog({ autoOpen: true, modal: false, title: $(tag).attr('title'), position: [$myx, $myy], minHeight: $height, width: $width, show: 'fade', hide: 'fade' });
    $dialog.dialog('option', 'buttons', {
        'Kapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
