function uniqid() {
    var newDate = new Date;
    return newDate.getTime();
}

function goToByScroll(id) {
    //$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
    $('html,body').animate({
        scrollTop: $("#" + id).offset().top
    }, 'slow');
}

function showBubble(el, szoveg) {
    var id = uniqid();
    $(el).html('<div class="bubble" id="bubble' + id + '"></div>' + $(el).html());
    $(el).attr("id", id);
    $("#bubble" + id).html(szoveg);
    $("#bubble" + id).stop(true, true).animate({
        opacity: "show",
        "margin-top": '-50px'
    }, "slow");
}

function hideBubble(el) {
    id = "#bubble" + $(el).attr('id');
    $(id).stop(true, true).delay(2000).animate({
        opacity: "hide",
        "margin-top": '-60px'
    }, "slow");
    $(id).remove();
}

function bubble(e) {
    showBubble($(e), $(e).attr('title'));
}

function bubbleout(e) {
    hideBubble($(e));
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}

var clicked = true;
$(document).ready(function () {
    var currentPage = document.location.toString();
    if (currentPage.match('#')) {
        var tmp_torrent_id = currentPage.split('#');
        for (var tzi = 0; tzi <= tmp_torrent_id.length; tzi++) {
            if (tmp_torrent_id[tzi] !== false && tmp_torrent_id[tzi] != null) {
                tmp_torrent_id[tzi] = parseInt(tmp_torrent_id[tzi]);
                if ($("#" + tmp_torrent_id[tzi]).length == 1) {

                    var leiras = tmp_torrent_id[tzi] + "_leiras";
                    var le = $("#" + leiras);

                    if (le.css("display") == "none") {
                        $("#" + tmp_torrent_id[tzi]).addClass("selected");
                        le.slideDown(200);
                        goToByScroll(tmp_torrent_id[tzi]);
                    }


                }
            }
        }
    }
    $(".chat_datum").mouseover(function () {
        showBubble($(this), $(this).attr('title'));
    }).mouseout(function () {
        hideBubble($(this));
    });
    if (document.URL.match(/\#/)) {
        var melyik = document.URL.replace(/.*\#(.*)$/i, "$1");
        $(".allink").removeClass("fekete");
        $("span[alt=" + melyik + "]").addClass("fekete");
        view_switcher(melyik);
    }

    $(".allink").click(function () {
        if ($(this).attr('id') != null && $(this).attr('id') != false && $(this).attr('id') != "") {
            var id = parseInt($(this).attr('id'));
            var type = $(this).attr('id').substr(String(id).length);
            var answer = true;
            if (type.match("delete")) {
                answer = confirm("Biztosan törlöd?");
            }
            if (answer === true) {
                window.location = "index.php?id=" + id + "&p=" + type;
                clicked = false;
            } else {
                clicked = true;
            }

            return clicked;
        }
    });

    $(function () {
        $.Lightbox.construct();

        $('*[rel=tooltip]').tooltip({
            track: false,
            delay: 2,
            showURL: false,
            showBody: " - ",
            fade: 400,
            positionLeft: false,
            width: "300px"
        });
    });

});
var oldalresz = document.URL;
if (oldalresz.match('feltoltes') || oldalresz.match('hirek_szerkesztese') || oldalresz.match('torrent_edit')) {
    $(document).ready(function () {
        $("#block_right").html('<div id="preview"></div>');

    });
    $(document).ready(function () {
        $('#seed_server').bind("change", function () {
            if ($(this).is(':checked') == true) {
                $('#seed_ido').attr('disabled', 'disabled');
                $('#seed_ido').val('24/24h 5 seeder');
            } else {
                $('#seed_ido').removeAttr('disabled');
                $('#seed_ido').val('24/24h 5 seederig');
            }
        });
    });

}
if (oldalresz.match('torrentek')) {
    $(document).ready(function () {

        if (clicked === true) {

            $(".torrent_entry").click(function (e) {
                var leiras = $(this).attr("id") + "_leiras";
                var le = $("#" + leiras);

                if (le.css("display") == "none") {
                    $(this).addClass("selected");
                 //   le.slideDown(200);
	le.animate({
		height: 'slide',
		opacity: 'show'
	},{duration: 400});
                    goToByScroll($(this).attr("id"));
                } else {
                    //le.slideUp(200);
	le.animate({
		height: 'slide',
		opacity: 'hide'
	},{duration: 400});
                    $(this).removeClass("selected");
                }
            });
        }
    });
}

function elfogadom() {
    var torrid = document.URL.replace(/(.*)\?id\=(.*)\&(.*)/, "$2");
    $.ajax({
        url: document.URL,
        data: "rendben=" + torrid,
        success: function (data) {
            alert(data);
            window.location = 'index.php?p=torrentek#' + torrid;
        }
    });
}

function nemfogadomel() {
    var torrid = document.URL.replace(/(.*)\?id\=(.*)\&(.*)/, "$2");
    var miert = prompt("Miért nem fogadod el?", "Ide az okot!");
    if (miert.length > 2) {
        $.ajax({
            url: document.URL,
            data: "ok=" + miert + "&trid=" + torrid,
            success: function (data) {
                alert(data);
                window.location = 'index.php?p=torrentek#' + torrid;
            }
        });
    } else {
        alert("Legalább egy okot írj!");
    }

}

function view_switcher(melyik) {

    var peers_e = $("#peers");
    var desc_e = $("#description");
    var allomanyok_e = $("#files");
    var nfo_e = $("#nfo");

    if (melyik == "leiras" || melyik == null) {
        peers_e.fadeOut(0);
        nfo_e.fadeOut(0);
        allomanyok_e.fadeOut(0);
        if (desc_e.css("display") == "none") {
            desc_e.fadeIn(800);
        }
    } else if (melyik == "peerlista") {
        allomanyok_e.fadeOut(0);
        desc_e.fadeOut(0);
        nfo_e.fadeOut(0);
        peers_e.fadeIn(800);
    } else if (melyik == "allomanyok") {
        desc_e.fadeOut(0);
        peers_e.fadeOut(0);
        nfo_e.fadeOut(0);
        allomanyok_e.fadeIn(800);
    } else if (melyik == "nfo") {
        desc_e.fadeOut(0);
        peers_e.fadeOut(0);
        allomanyok_e.fadeOut(800);
        nfo_e.fadeIn(800);
    }
}

function show_detail(e) {
    var melyik = $(e).attr("alt");
    var locat = "index.php?id=" + gup('id') + "&p=torrent#" + melyik
    $(".allink").removeClass("fekete");
    $(e).addClass("fekete");
    view_switcher(melyik);
    window.location = locat;
}

function mutikereso() {
    var kereso_e = $("#kereso");
    if (kereso_e.css("display") == "none") {
        kereso_e.slideDown(400);
    } else {
        kereso_e.slideUp(400);
    }
}

function chat_userszam() { //userszam numeric

        $.ajax({
            url: '/chat.php',
            type: 'POST',
            data: "userszam=1",
            async: true,
            success: function (data) {
                if (data == "exit") {
                    //clearInterval(chatint);
                    //clearInterval(userszam);
                    $("#container").fadeOut(600);
                    setInterval(function () {
                        window.location = document.URL;
                    }, 400);
                }
                if ($("#userszam").html() != data) {
                    $("#userszam").fadeOut(100).fadeIn(200).delay(100).html(data);
                }
            }

        });
}

function chat() {
        var block = $("#chat");
        $("#resp").html("");
        $.ajax({
            url: '/chat.php',
            dataType: "json",
            async: true,
            ifModified: true,
            type: 'POST',
            success: function (data) {
                var update = "";
                var i = 0;
                //alert(Base64.decode(data));
                $.each(data, function (key, value) {
                    value.class2 = '';
                    if (value.keresnek === true) {
                        value.class2 = 'bcg_yellow';
                    }
                    if (value.for_uid != 0) {
                        value.class1 = 'bcg_green';
                    }
                    update += "<div id='szoveg" + i + "' class='szoveg " + value.class1 + "'>";
                    update += "<span class='chat_datum' title='" + value.elido + "'>::" + (value.del != null ? value.del : "") + "::" + value.time + "</span>";
                    update += "<a href='index.php?id=" + value.uid + "&p=profil'><span class='chat_username' style='color: " + value.color + ";'>" + value.username + "</span></a>";
                    if (value.for_uid != 0) {
                        update += "<span class='red' style='font-size: 8px'>(P)</span>-><a href='index.php?id=" + value.for_uid + "&p=profil'><span class='chat_username' style='color: " + value.for_color + "'>" + value.for_username + "</span></a>";
                    }
                    update += "<span class='chat_szoveg " + value.class2 + "'>:" + value.szoveg + "</span></div>";
                    i++;
                });

                if (update != block.html()) {
                    block.html(update);
                }



            }

        });
}

$(document).ready(function () {

if (gup("p")=="csevego") {
    var params = {};
    params.host               = "sexmodell.hu";
    params.language           = "hu";
    params.nickselect         = "no";
    params.nick               = chat_username;
    params.autojoin           = "#exanova";
    params.showNickChangeButton = "no";
    params.showPartChannelButton = "no";
    params.showChannelCentralButton = "no";
    params.showButtonTexts = "no";
    // params.webcam           = "yes";
    params.utf8CompatibilityMode = "yes";
    params.styleURL			= "http://exanova.eu/irc/styles/yahoo.swf";
	swfobject.embedSWF("http://exanova.eu/irc/lightIRC.swf", "lightIRC", "640", "404", "9.0.0", null, params, null);
}

    if (document.URL.match(/\#kimeno/)) {
        var bejovo = $("#bejovo");
        var kimeno = $("#kimeno");
        if (bejovo.css('display') != 'none') {
            bejovo.fadeOut(0);
        }
        if (kimeno.css('display') == 'none') {
            kimeno.fadeIn(400);
        }
    }
    if (document.URL.match(/\#bejovo/)) {
        var bejovo = $("#bejovo");
        var kimeno = $("#kimeno");
        if (kimeno.css('display') != 'none') {
            kimeno.fadeOut(0);
        }
        if (bejovo.css('display') == 'none') {
            bejovo.fadeIn(400);
        }
    }


    //üzenetek
    $('*[rel=bejovouzi]').click(function () {
        var bejovo = $("#bejovo");
        var kimeno = $("#kimeno");
        if (kimeno.css('display') != 'none') {
            kimeno.fadeOut(0);
        }
        if (bejovo.css('display') == 'none') {
            bejovo.fadeIn(400);
        }
        window.location = document.URL.replace(/\#bejovo/, "").replace(/\#kimeno/, "") + "#bejovo";
    });
    $('*[rel=kimenouzi]').click(function () {

        var bejovo = $("#bejovo");
        var kimeno = $("#kimeno");
        if (bejovo.css('display') != 'none') {
            bejovo.fadeOut(0);
        }
        if (kimeno.css('display') == 'none') {
            kimeno.fadeIn(400);
        }
        window.location = document.URL.replace(/\#bejovo/, "").replace(/\#kimeno/, "") + "#kimeno";
    });

    //bbcode-ok
    if ($("textarea").length > 0) {
        $(document).ready(function () {
            $('textarea').markItUp(mySettings);
        });
    }
    //bbcode-ok

    $("#reset_kereso").click(function () {
        mutikereso();
        if ($(e).val() == "keresés törlése") {
            $(e).click();
        }
        return false;
    });

    $("#keresogomb").click(function () {
        mutikereso();
        return false;
    });

    var focusFlag = 0;
    jQuery(window).bind("focus", function (event) {
        //if ($("#chat").length > 0) {
		//var userszam = setInterval("chat_userszam();", 10000);
		//var chatint = setInterval("chat()", 3000);
       // }
    })

    jQuery(window).bind("blur", function (event) {
        if ($("#chat").length > 0) {
			if (typeof(userszam)!=undefined) {
				clearInterval(userszam);
			}
			if (typeof(chatint)!=undefined) {
				clearInterval(chatint);
			}
            $("#chat").html("<p style='width: 100%; text-align: center;'>Senki nem figyel, nem frissítek...</p>");
        }
    });
    $("input[name=chatkuldes]").click(function () {
        var szoveg = $("#chatuzenet").val();
        $("#chatuzenet").attr("disabled", "disabled");
        $("#chatuzenet").val('');
        $.ajax({
            url: '/chat.php',
            dataType: "json",
            type: "POST",
            data: "send=1&szoveg=" + szoveg,
            success: function (data) {
                $("#resp").html("Elküldve!");
            }, complete: function () {
                $("#chatuzenet").removeAttr("disabled");
                $("#chatuzenet").focus();
            }

        });
    });

    $("#chatuzenet").bind("keyup", function (e) {
        if (e.keyCode == 13) {
            var szoveg = $("#chatuzenet").val();

            $("#chatuzenet").attr("disabled", "disabled");
            $("#chatuzenet").val('');

            $.ajax({
                url: '/chat.php',
                dataType: "json",
                async: true,
                type: "POST",
                data: "send=1&szoveg=" + szoveg,
                success: function (data) {
                    $("#resp").html("Elküldve!");
                    $("#chatuzenet").val('');
                }, complete: function () {
                    $("#chatuzenet").removeAttr("disabled");
                    $("#chatuzenet").focus();
                }

            });
        }
    });

});

function tema_atnevezem(h, oldname) {
    var ujnev = prompt("Mi legyen az új címe a témának?", oldname);

    if (ujnev !== null) {
        $.ajax({
            url: 'index.php?p=forum',
            dataType: "json",
            async: false,
            type: "POST",
            data: "atnevezemaforumtemat=" + h + "&ujneve=" + ujnev,
            success: function () {
                window.location = document.URL;
            }

        });
    }
}

function tema_torlom(h) {
    var biztos = confirm("Valóban törlöd ezt a témát?");
    if (biztos === true) {
        $.ajax({
            url: 'index.php?p=forum',
            dataType: "json",
            async: false,
            type: "POST",
            data: 'torlomaforumtemat=' + h,
            success: function (data) {

                if (data == 0) {
                    alert("Nincs ilyen téma!");
                }
                if (data == 1) {
                    alert('A téma törölve!');
                    window.location = document.URL;
                }
                if (data == 2) {
                    alert("Nem áll jogodban törölni a témát!");
                }


            }

        });
    }
}

function torles_hozzaszolas(id) {
    var valoban = confirm('Valóban törlöd a hozzászólást?');
    if (valoban === true) {
        $.ajax({
            url: 'index.php?p=forum',
            dataType: "json",
            async: false,
            type: "POST",
            data: 'torlomaposztot=' + id,
            success: function (data) {
                if (data == 0) {
                    alert("Nincs ilyen hozzászólás!");
                }
                if (data == 1) {
                    alert('A hozzászólás törölve!');
                    if (gup("page") != "") {
                        window.location = "index.php?id=" + gup("id") + "&p=forum&page=" + gup("page");
                    } else {
                        window.location = "index.php?id=" + gup("id") + "&p=forum";
                    }
                }
                if (data == 2) {
                    alert("Nem áll jogodban törölni a hozzászólást!");
                }
            }

        });
    }
}

function tema_nyitas(id, title) {
    var biztos = confirm("Biztos kinyitod ezt a fórumot (" + title + ") ?");
    if (biztos === true) {
        $.ajax({
            url: 'index.php?p=forum',
            dataType: "json",
            async: false,
            type: "POST",
            data: 'nyitomaforumot=' + id,
            success: function (data) {
                if (data == 0) {
                    alert("Nincs ilyen téma!");
                }
                if (data == 1) {
                    alert('A téma mostantól nyitott!');
                    window.location = document.URL;
                }
                if (data == 2) {
                    alert("Nem áll jogodban kinyitni a témát!");
                }
            }

        });

    }
}

function tema_zaras(id, title) {
    var biztos = confirm("Biztos lezárod ezt a fórumot (" + title + ") ?");
    if (biztos === true) {
        $.ajax({
            url: 'index.php?p=forum',
            dataType: "json",
            async: false,
            type: "POST",
            data: 'zaromaforumot=' + id,
            success: function (data) {
                if (data == 0) {
                    alert("Nincs ilyen téma!");
                }
                if (data == 1) {
                    alert('A téma lezárva!');
                    window.location = document.URL;
                }
                if (data == 2) {
                    alert("Nem áll jogodban lezári a témát!");
                }
            }

        });


    }
}

function ujtema() {
    var neve = prompt("Mi legyen az új téma címe?");
    if (neve != null) {
        $.ajax({
            url: 'index.php?p=forum',
            dataType: "json",
            async: false,
            type: "POST",
            data: 'ujtema=' + neve,
            success: function (data) {
                if (data == 0) {
                    alert("Nem sikerült valamiért létrehozni!");
                }
                if (data == 1) {
                    alert('A téma elkészült!');
                    window.location = document.URL;
                }
                if (data == 2) {
                    alert("Nem áll jogodban új témát létrehozni!");
                }
            }

        });
    }
}

function mozgatas_le(id, order) {
    order--;
    $.ajax({
        url: 'index.php?p=forum',
        dataType: "json",
        async: true,
        type: "POST",
        data: 'rendezes=' + order + "&id=" + id,
        success: function (data) {
            if (data == 0) {
                alert("Nem sikerült valamiért létrehozni!");
            }
            if (data == 1) {
                window.location = document.URL;
            }
            if (data == 2) {
                alert("Nem áll jogodban sorbarendezni!");
            }
        }

    });
}

function mozgatas_fel(id, order) {
    order++;
    $.ajax({
        url: 'index.php?p=forum',
        dataType: "json",
        async: true,
        type: "POST",
        data: 'rendezes=' + order + "&id=" + id,
        success: function (data) {
            if (data == 0) {
                alert("Nem sikerült valamiért létrehozni!");
            }
            if (data == 1) {
                window.location = document.URL;
            }
            if (data == 2) {
                alert("Nem áll jogodban sorbarendezni!");
            }
        }

    });
}

function uzitorles(id) {
    var valoban = confirm("Valóban törlöd az üzenetet?\nAz üzenet csak nálad törlődik, a másik félnél megmarad!");
    if (valoban === true) {
        $.ajax({
            url: 'index.php?p=uzenetek',
            dataType: "json",
            async: true,
            type: "POST",
            data: 'torles=' + id,
            success: function (data) {
                if (data == 0) {
                    alert("Nem töröltem, mert: vagy nem te küldted/fogadtad, vagy mert nem létezik!");
                } else {
                    window.location.reload();
                }
            }

        });
    }
}

function double_up(tid) {
    $.ajax({
        url: 'index.php?p=torrent_edit',
        async: true,
        type: "POST",
        data: 'id=' + tid + '&double_up=1',
        success: function (data) {
            alert(data);
        }

    });
}

function freeleech(tid) {
    $.ajax({
        url: 'index.php?p=torrent_edit',
        async: true,
        type: "POST",
        data: 'id=' + tid + '&freeleech=1',
        success: function (data) {
            alert(data);
        }

    });
}

function half(tid) {
    $.ajax({
        url: 'index.php?p=torrent_edit',
        async: true,
        type: "POST",
        data: 'id=' + tid + '&half=1',
        success: function (data) {
            alert(data);
        }

    });
}