function refreshJoke() {
    $("#joke").fadeOut("slow", function(){
        $.getJSON("refreshjoke.php", {tag: $("#current_tag").html()},
        function(data) {
            $("#joke").html(data[1]);
            $("#joke_id").html(data[0]);
            $("#joke").fadeIn("slow");
        });
    })
    return false;
}

$(function() {
	$('#googleadd').everyTime(30000, function(i) {
    	var current = (i % 3);
    	var next = current + 1;
    	if(current == 0) current = 3;
    	if(current == 3) next = 1;
    	$('#google'+current).hide();
    	$('#google'+next).show();
	});
});

$(function() {
    $('.refresh').click(function(){
        refreshJoke();
        return false;
    });
});

$(function() {
    $('.about').click(function(){
        new Boxy($('#informacija').html(), {title: "Apie anekdotai.net", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
        return false;
    });
});

$(function() {
    $('.cloud').click(function(){
        new Boxy($('#tags_div').html(), {title: "Kategorijos - raktažodžių debesėlis", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
        return false;
    });
});

$(function() {
    $('#help').click(function(){
        //new Boxy($('#pagalba').html(), {title: "Prisidėkite prie anekdotai.net tobulinimo", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
        new Boxy($('#mobili').html(), {title: "Mobili anekdotų svetainė tavo išmaniajame telefone", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
        return false;
    });
});
$(function() {
    $('#apkl').click(function(){
        new Boxy($('#apklausa').html(), {title: "Apklausa", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
        return false;
    });
});
$(function() {
    if($.cookie("mobili")==null)
        new Boxy($('#mobili').html(), {title: "Mobili anekdotų svetainė tavo išmaniajame telefone", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
    $.cookie("mobili", "taip", { expires: 28 });
    //if($.cookie("apklausa")==null)
    //    new Boxy($('#apklausa').html(), {title: "Apklausa", closeText: "Uždaryti", draggable: false, modal: true, fixed: false});
    //$.cookie("apklausa", "taip", { expires: 28 });
});

$(function() {
    var tofriend = null;
    $('.send').click(function(){
        var boxy_content;
        boxy_content += "<div style=\"width: 300px;\"><form id=\"send2friend\">";
        boxy_content += "<p>Tavo vardas<br /><input type=\"text\" name=\"your_name\" id=\"your_name\" style=\"width: 95%\" /></p><p>Tavo el. paštas<br /><input type=\"text\" name=\"your_email\" style=\"width: 95%\" /></p><p>Draugo el. paštas<br /><input type=\"text\" name=\"friend_email\"  style=\"width: 95%\" /></p><br /><input type=\"submit\" name=\"submit\" value=\"Siųsti\" />";
        boxy_content += "</form></div>";
        tofriend = new Boxy(boxy_content, {
            title: "Nusiųsk anekdotą draugui",
            closeText: "Uždaryti",
            draggable: false,
            modal: true,
            fixed: false,
            behaviours: function(c) {
                c.find('#send2friend').submit(function() {
                    Boxy.get(this).setContent("<div style=\"width: 300px;\">Siunčiam...</div>");
                    $.post("send2friend.php", { your_name: c.find("input[name='your_name']").val(), your_email: c.find("input[name='your_email']").val(), friend_email: c.find("input[name='friend_email']").val(), joke: $("#joke").html() },
                    function(data){
                        tofriend.setContent("<div style=\"width: 300px;\">"+data+"</div>");
                    });
                    return false;
                });
            } 
        });
        return false;
    });

    var share_window = null;
    $('.share').click(function(){
        var boxy_content;
        boxy_content += "<div style=\"width: 400px;\"><form id=\"share\">";
        boxy_content += "<p>Anekdotai parašyti su klaidom, be lietuviškų rašmenų bus atmesti.</p><p>Tavo vardas<br /><input type=\"text\" name=\"your_name\" id=\"your_name\" style=\"width: 95%\" /></p><p>Tavo el. paštas<br /><input type=\"text\" name=\"your_email\" style=\"width: 95%\" /></p><p>Anekdotas<br /><textarea id=\"joke_text\" name=\"joke_text\"  style=\"width: 95%\" rows=\"8\" /></p><br /><input type=\"submit\" name=\"submit\" value=\"Siųsti\" />";
        boxy_content += "</form></div>";
        share_window = new Boxy(boxy_content, {
            title: "Atsiųsk savo anekdotą",
            closeText: "Uždaryti",
            draggable: false,
            modal: true,
            fixed: false,
            behaviours: function(c) {
                c.find('#share').submit(function() {
                    Boxy.get(this).setContent("<div style=\"width: 400px;\">Siunčiam...</div>");
                    $.post("share_your_joke.php", { your_name: c.find("input[name='your_name']").val(), your_email: c.find("input[name='your_email']").val(), joke: c.find("#joke_text").val() },
                    function(data){
                        share_window.setContent("<div style=\"width: 400px;\">"+data+"</div>");
                    });
                    return false;
                });
            } 
        });
        return false;
    });

    var edit_window = null;
    $('.edit').click(function(){
        var boxy_content;
        boxy_content += "<div style=\"width: 400px;\"><p>Pastebėjai klaidų anekdote? Praleista, klaidinga raidė? Trūksta skirybos ženklų? Ištaisyk jas ir jauskis pasitarnavęs visuomenei. Mes rašom lietuviškai!</p><form id=\"edit_form\">";
        boxy_content += "<p>Žymės - vieno žodžio atskirtos tarpais (pvz. blondinės ežiukas trumpi ir pan.):<br /><input type=\"text\" id=\"tags\" name=\"tags\" maxlength=\"60\" style=\"width: 95%\" /></p>";
        boxy_content += "<p>Anekdotas<br /><textarea id=\"joke_text\" name=\"joke_text\" style=\"width: 95%\" rows=\"10\">"+$("#joke").html().replace(/<br>/gi, '\n')+"</textarea></p><br /><input type=\"submit\" name=\"submit\" value=\"Siųsti\" />";
        boxy_content += "</form></div>";
        edit_window = new Boxy(boxy_content, {
            title: "Ištaisyk anekdoto klaidas",
            closeText: "Uždaryti",
            draggable: false,
            modal: true,
            fixed: false,
            behaviours: function(c) {
                c.find('#edit_form').submit(function() {
                    Boxy.get(this).setContent("<div style=\"width: 400px;\">Siunčiam...</div>");
                    $.post("edit_joke.php", { id: $("#joke_id").html(), joke: c.find("#joke_text").val(), tags: c.find("#tags").val() },
                    function(data){
                        //edit_window.setContent("<div style=\"width: 400px;\">"+data+"</div>");
                        edit_window.hide();
                    });
                    return false;
                });
            } 
        });
        return false;
    });

    var tag_window = null;
    $('.tag').click(function(){
        var boxy_content;
        boxy_content += "<div style=\"width: 400px;\"><p>Pažymėk apie ką šis anekdotas ir įvesk kelias žymes. Mes rašom lietuviškai!</p><form id=\"edit_form\">";
        boxy_content += "<p>Žymės - vieno žodžio atskirtos tarpais (pvz. blondinės ežiukas trumpi ir pan.):<br /><input type=\"text\" id=\"tags\" name=\"tags\" maxlength=\"60\" style=\"width: 95%\" /></p>";
        boxy_content += "<input type=\"submit\" name=\"submit\" value=\"Siųsti\" />";
        boxy_content += "</form></div>";
        tag_window = new Boxy(boxy_content, {
            title: "Pažymėk anekdotą",
            closeText: "Uždaryti",
            draggable: false,
            modal: true,
            fixed: false,
            behaviours: function(c) {
                c.find('#edit_form').submit(function() {
                    Boxy.get(this).setContent("<div style=\"width: 400px;\">Siunčiam...</div>");
                    $.post("tag_joke.php", { id: $("#joke_id").html(), tags: c.find("#tags").val() },
                    function(data){
                        //tag_window.setContent("<div style=\"width: 400px;\">"+data+"</div>");
                        tag_window.hide();
                    });
                    return false;
                });
            } 
        });
        return false;
    });
});