﻿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;
$(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)
    });
});
function displayError(message, status) {
    var $dialog = $(message);
    $dialog
        .dialog({
            modal: true,
            title: status + ' Error',
            buttons: {
                'kapatkapat': function () {
                    $(this).dialog('close');
                    $(this).empty();
                }
            }
        });
    return false;
};
function page($btn) {
    $.ajax({
        url: $btn.attr('href'),
        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('href'),
        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 addCreate(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('href'))
        .dialog({ autoOpen: true, modal: true, 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
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'kapatkapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function addEdit(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('href'))
        .dialog({ autoOpen: true, modal: true, 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
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'kapatkapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function addSearch(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('href'))
        .dialog({ autoOpen: true, modal: true, title: $(tag).attr('title'), position: [$myx + 150, $myy], minHeight: $height, width: $width - 300, 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
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'kapatkapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
function addLogin(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('href'))
        .dialog({ autoOpen: true, modal: true, title: $(tag).attr('title'), position: [$myx + 150, $myy], minHeight: $height, width: $width - 300, 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
                        displayError(xhr.responseText, xhr.status);
                }
            });
        },
        'kapatkapat': 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: true,
            position: [$myx + 150, $myy],
            minHeight: $height, 
            width: $width - 300,
            buttons: {
                'Sil': function () {
                    $(this).dialog('close');
                    callback.apply();
                    $(this).empty();
                },
                'kapatkapat': function () {
                    $(this).dialog('close');
                    $(this).empty();
                }
            }
        });
};
function deleteRow($btn) {
    $.ajax({
        url: $btn.attr('href'),
        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
                displayError(xhr.responseText, xhr.status);
        }
    });
    return false;
};
function addRead(tag, event, target) {
    event.preventDefault();
    $dialog.empty();
    $dialog
        .append($loading)
        .load($(tag).attr('href'))
        .dialog({ autoOpen: true, modal: true, title: $(tag).attr('title'), position: [$myx, $myy], minHeight: $height, width: $width, show: 'fade', hide: 'fade' });
    $dialog.dialog('option', 'buttons', {
        'kapatkapat': function () {
            $(this).dialog('close');
            $(this).empty();
        }
    });
};
$.backstretch("/Content/images/arka" + (Math.floor(Math.random() * 6)) + ".jpg", { speed: 1000 });
var satir = 0;
var dizim = new Array();
$(document).ready(function () {
    var satirsay = $(".esecilen div").length;
    for (var i = 0; i < satirsay; i++) {
        dizim[i] = $(".esecilen div:eq(" + i + ")").html();
    }
    $(".esecilen").html("");
    for (var i = 0; i < satirsay; i++) {
        $(".esecilen").append("<div class=\"anablok\">" + dizim[i] + "</div>");
    }
    setInterval(degis, 4000);
    function degis() {
        satir = (satir + 1) % satirsay;
        $(".esecilen").html("");
        var ilk = satir - 1;
        if (ilk < 0) ilk = ilk + satirsay;
        $(".esecilen").append("<div class=\"anablok\">" + dizim[ilk] + "</div>");
        for (var i = satir; i < satir + satirsay; i++) {
            $(".esecilen").append("<div class=\"anablok\">" + dizim[(i % satirsay)] + "</div>");
        }
        $(".esecilen div:first").slideUp(400);
    }
});
