﻿

function delItem(obj, tableName) {
    if (confirm('Biztos hogy törlöd?? Nem lehet visszavonni!!')) {
        $.ajax({
            url: obj.href,
            type: 'delete',
            success: function (result) {
                var i = obj.parentNode.parentNode.rowIndex;
                document.getElementById(tableName).deleteRow(i);
            }
        });
    }
    return false;
}

function changeContent(id, content) {
    $(id).html(content);
}

function loadImagesToSelect(id, url, selectedVal) {
    var select = $(id);
    if (selectedVal == null) {
        selectedVal = select.find('option:selected').val();
    }
    select.find('option').remove();
    $.ajax({
        url: url,
        type: 'get',
        success: function (result) {
            eval(result);
            $.each(tinyMCEImageList, function (index, item) {
                select.append($("<option></option>").
                        attr("value", item[1]).
                        text(item[0]));
            }); //each vége

            select.val(selectedVal);
            select.change();
        } //succes vége
    });    //ajax vége

}

function imageSelector(selectId, imgId) {
    var select = $(selectId);
    var img = $(imgId);
    select.change(function () {
        img.attr('src', $(selectId + ' option:selected').attr('value'));
    });
}

function initAuthorSearch(searchbox, url, resultArea, skipAddAuthor) {
    var textbox = $(searchbox);
    var resultdiv = $(resultArea);
    if (skipAddAuthor == null)
        initAddAuthor(resultArea, url);
    textbox.keyup(function (event) {

        var exp = $(this).val();
        var inp = String.fromCharCode(event.keyCode);
        if (/[a-zA-Z0-9-_öőüűáéí ]/.test(inp) || event.keyCode == 8) {
            if (exp.length > 2) {
                resultdiv.empty();
                $.ajax({
                    type: "get",
                    dataType: "json",
                    url: url + '?exp=' + exp,
                    success: function (json) {
                        if (json.length == 0) {
                            resultdiv.append(
                                $('<div/>').addClass('addauthor_box').html(
                                        exp + ' - ').append(
                                        $('<a/>').attr('href', 'javascript:void(0)')
                                        .attr('data-name',exp)
                                        .addClass('add-author')
                                        .html('Szerző felvétele')
                                        )
                                );
                        }
                        else {
                            $.each(json, function (i, element) {
                                resultdiv.append(
                                $('<div/>').html(
                                    $('<a />').attr('href', 'javascript:void(0)')
                                    .addClass('select-author').attr('data-id', element.id)
                                    .html(element.name)
                                    )
                                );
                            });
                        }
                    }
                });
            }
        }
    });
}
function initAddAuthor(result, url) {

    $('.add-author').live('click', function () {
        var name = $(this).attr('data-name');

        if (confirm('Biztosan felveszed ' + name + '-t új szerzőként?')) {
            $.ajax({
                type: "post",
                dataType: "json",
                url: url + '?add=1&exp=' + name,
                success: function (json) {
                    if (json.id > 0) {
                        var resultdiv = $(result);
                        resultdiv.empty();
                        resultdiv.append(
                            $('<div/>').html(
                                    $('<a />').attr('href', 'javascript:void(0)')
                                    .addClass('select-author').attr('data-id', json.id)
                                    .html(name)
                                    )
                            );
                    }
                }
            });
        }
        return false;

    });
}

function initSingleAuthor(target, targetfield) {

    $(".select-author").live('click', function () {
        var targetdiv = $(target);
        var hiddenfield = $(targetfield);
        var id = $(this).attr('data-id');
        if (hiddenfield.val() != id) {
            hiddenfield.val(id);
            targetdiv.html(
                $('<div/>').addClass('selectedauthor_box').attr('data-id', id)
                   .html($(this).html() + ' - ')
                        .append(
                            $('<a/>').attr('href', 'javascript:void(0)')
                            .addClass('remove-author').html('eltávolít')
                        )
                  );
        }
    });
    $(".remove-author").live('click', function () {
        var targetdiv = $(target);
        var hiddenfield = $(targetfield);
        var id = $(this).parent().attr('data-id');
        if (hiddenfield.val() == id) {
            hiddenfield.val('');
            $(this).parent().remove();
        }
    });
}
function initMultiAuthor(target, targetfield) {

    $(".select-author").live('click', function () {
        var targetdiv = $(target);
        var hiddenfield = $(targetfield);
        var id = $(this).attr('data-id');
        if (hiddenfield.val().indexOf(',' + id + ',') == -1) {
            hiddenfield.val(hiddenfield.val() + id + ',');
            targetdiv.append(
                $('<div/>').addClass('selectedauthor_box').attr('data-id', id)
                   .html($(this).html() + ' - ')
                        .append(
                            $('<a/>').attr('href', 'javascript:void(0)')
                            .addClass('remove-author').html('eltávolít')
                        )
                  );

        }
    });
    $(".remove-author").live('click', function () {
        var targetdiv = $(target);
        var hiddenfield = $(targetfield);
        var id = $(this).parent().attr('data-id');
        if (hiddenfield.val().indexOf(',' + id + ',') >= 0) {
            hiddenfield.val(hiddenfield.val().replace(id + ',', ''));
            $(this).parent().remove();
        }
    });
}

function refreshUrl(div, url) {
    $.ajax({ url: url, type: 'get', success: function (result) { div.html(result); } });
}

function initIIAsync(divid,refreshurl) {
    var div = $(divid);
    $(".ii-async-submit").live('click', function () {
        var form = $('#' + $(this).attr("data-formid"));
        var url = $(this).attr("href");
        $.post(url, form.serialize(), function (result) {
            form.remove();
            refreshUrl(div, refreshurl);
        });
        return false;
    });
    $(".archive-auto-save").live('click', function () {
        $(".async-auto-save").each(function () {
            var isClear = $(this).hasClass('archive-auto-save-clear');
            var url = $(this).attr("data-posturl");
            var form = $(this).parent();

            var title = form.find('.text-box[name*="Title"]').val();
            if (title && title !== "") {
                $.post(url, form.serialize(), function () {
                    refreshUrl(div, refreshurl);
                    if (isClear) form.remove();
                });
            }
            return false;
        });
    });
    $(".archive-async-delete").live('click', function () {
        if (confirm('Biztosan törlöd ezt az elemet (és gyerekeit?)')) {
            var id = $(this).attr("data-id");
            $.ajax({ url: $(this).attr("href"), type: 'delete', success: function () {
                refreshUrl(div, refreshurl);
            }
            });
        }
        return false;
    });
    $(".archive-async-move").live('click', function () {
        $.ajax({ url: $(this).attr("href"), type: 'put', success: function () {
            refreshUrl(div, refreshurl);
        }
        });
        return false;
    });
}

var activefeatured;
var timer;
var lock;
var fadetime = 2000;
var visibletime = 10000;

function SetUpFeatured() {
    lock = false;
    activefeatured = $(".featuredcontent").first().fadeIn(fadetime, function () {
        timer = setTimeout("SwitchFeatured()", visibletime);
    }
    );
}

function SwitchFeatured() {
    if(!lock){
        activefeatured.fadeOut(fadetime, function () {
            if (activefeatured.next().length == 0) { activefeatured = $(".featuredcontent").first(); }
            else { activefeatured = activefeatured.next(); }

            activefeatured.fadeIn(fadetime, function () {
                timer = setTimeout("SwitchFeatured()", visibletime);
            });
        }
        );
    }
}

//function StopFeatured() {
//    lock = true;
//    clearTimeout(timer);
//    activefeatured.stop(false, true);
//    activefeatured.fadeIn(0, function () { });
//}
//function ResumeFeatured() {
//    lock = false;
//    timer = setTimeout("SwitchFeatured()", visibletime);
//}


function OpenCloseCart() {
    var bottom = $("#dash").css("bottom");
    if (bottom == "0px") $("#dash").css("bottom", "-128px");
    else $("#dash").css("bottom", "0");
}

function switchCartClass(from, to) {
    $("#cartbutton").removeClass(from);
    $("#cartbutton").addClass(to);
}

var left;
function DashLeft() {
    if (left == null) left = 0;
    var itemcount = $("#dashboard_scrollable").children().length;
    if (left - 168 > -1 * itemcount * 168) {
        left -= 168;
        $("#dashboard_scrollable").css("marginLeft", left + "px");
    }
}

function DashRight() {
    if (left == null) left = 0;

    if (left + 168 < 4 * 168) {
        left += 168;
        $("#dashboard_scrollable").css("marginLeft", left + "px");
    }
}

function initUserAdmin(subscription_url,writer_url,author_url) {
    $(".subscription-selector").change(function () {
        var id = $(this).attr("data-user-id");
        var chkbox = $(this);
        var value = chkbox.is(':checked');
        $.ajax({
            url: subscription_url + "?id=" + id + "&value=" + value,
            type: 'put',
            success: function (result) {
                alert(result.msg);
            }
        });
    });
    $(".article-writer-selector").change(function () {
        var id = $(this).attr("data-user-id");
        var chkbox = $(this);
        var value = chkbox.is(':checked');
        $.ajax({
            url: writer_url + "?id=" + id + "&value=" + value,
            type: 'put',
            success: function (result) {
                alert(result.msg);
            }
        });
    });

    $(".author-selector").change(function () {
        var id = $(this).attr("data-user-id");
        var drop = $(this);
        var value = drop.val();
        var url = author_url + "?id=" + id + "&value=" + value;
        if (value == '') url = author_url + "?id=" + id;
        $.ajax({
            url: url,
            type: 'put',
            success: function (result) {
                alert(result.msg);
            }
        });
    });

}
