/*
 * Ajax Pagination System
 *
 */
$(function()
{
    if(!$.browser.msie)
    {
        $("#pagination a").live('click', function(e)
        {
            e.preventDefault();

            var pagination = {
                content : $("#paginationContainer"),
                target : $(this).attr("href") + " #paginationContainer"
            };

            pagination.content.animate({opacity:0}, 400, "easeInOutCubic", function()
            {
                pagination.content.load(pagination.target, function()
                {
                    pagination.content.animate({opacity:1}, 400, "easeInOutCubic");
                    Cufon.replace('.headline',{textShadow:'1px 1px rgba(255, 255, 255, 1)'})('.ribbon span',{hover:true,textShadow:'-1px -1px rgba(0, 0, 0, 0.4)'})
                });
            });
        });
    }
});

$(document).ready(function(){
    Cufon.replace('.headline',{textShadow:'1px 1px rgba(255, 255, 255, 1)'})('.ribbon span',{hover:true,textShadow:'-1px -1px rgba(0, 0, 0, 0.4)'})
    $("#liite_napit img").each(function(){
       $(this).css("border", "1px solid #f5f2eb")
       $(this).css("background-color", "#f5f2eb")
       $(this).css("cursor", "pointer")
       $(this).css("padding", "4px 4px 0 4px")
    })

    $("#open_modal").click(function(e){
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);
        
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($(id).html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$(id).height()/2);
        $("#facebox").css('left', winW/2-$(id).width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })
    //
    //--- Viestin lähettäminen
    //
    $("#viesti_peruuta").live("click", function(){
        $("#facebox_overlay").hide();
        $("#facebox").hide();
    })
    $("#liite_kuva_nappi").live("mouseover", function(){
       $(this).css("border", "1px solid #ccc")
       $(this).css("background-color", "#efefef")
       $(this).css("cursor", "pointer")
       $(this).css("padding", "4px 4px 0 4px")
    });
    $("#liite_kuva_nappi").live("mouseout", function(){
       $(this).css("border", "1px solid #f5f2eb")
       $(this).css("background-color", "#f5f2eb")
       $(this).css("cursor", "pointer")
       $(this).css("padding", "4px 4px 0 4px")
    });
        $("#liite_linkki_nappi").live("mouseover", function(){
       $(this).css("border", "1px solid #ccc")
       $(this).css("background-color", "#efefef")
       $(this).css("cursor", "pointer")
       $(this).css("padding", "4px 4px 0 4px")
    });
    $("#liite_linkki_nappi").live("mouseout", function(){
       $(this).css("border", "1px solid #f5f2eb")
       $(this).css("background-color", "#f5f2eb")
       $(this).css("cursor", "pointer")
       $(this).css("padding", "4px 4px 0 4px")
    });
    $("#liite_kuva_nappi").live("click", function(){
        $(this).parent().html('<div id="liite_kuva"><div id="liite_kuva_x" style="float:right;color:#fff;background-color:#6e84b5;border:1px solid #46587e;padding:0 4px;cursor:pointer;margin-top:-3px;"><b>X</b></div><p style="border-bottom: 1px solid #ccc;"><img src="http://www.royalrangers.fi/Kehitys/images/image.png" alt="Kuva" /> <b>Kuva</b></p> <p align="center"><b>Valitse kuva koneeltasi.</b><br /><input type="file" name="liite_kuva" /></p></div>')
    });
    $("#liite_linkki_nappi").live("click", function(){
        $(this).parent().html('<div id="liite_kuva"><div id="liite_kuva_x" style="float:right;color:#fff;background-color:#6e84b5;border:1px solid #46587e;padding:0 4px;cursor:pointer;margin-top:-3px;"><b>X</b></div><p style="border-bottom: 1px solid #ccc;"><img src="http://www.royalrangers.fi/Kehitys/images/television.png" alt="Video" /> <b>YouTube linkki</b></p> <p align="center"><b>Liit&auml; YouTube-videon osoite.</b><br />http://www.youtube.com/watch?v=<input type="text" name="liite_linkki" /></p></div>')
    });
    $("#liite_kuva_x").live("click", function(){
        $(this).parent().parent().html('<img src="http://www.royalrangers.fi/Kehitys/images/image.png" id="liite_kuva_nappi" alt="Kuva" style="padding:4px 4px 0 4px;border:1px solid #f5f2eb" /><img src="http://www.royalrangers.fi/Kehitys/images/television.png" id="liite_linkki_nappi" alt="Video" style="padding:4px 4px 0 4px;border:1px solid #f5f2eb" />');
    })

    //
    //--- LISÄÄ ALBUMI TAI KUVA
    //
    $("#lisaa_albumi").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#albumin_lisaaminen").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#albumin_lisaaminen").height()/2);
        $("#facebox").css('left', winW/2-$("#albumin_lisaaminen").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //--- LISÄÄ PROFIILIKUVA
    //
    $("#lataa_profiilikuva_linkki").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#lataa_profiilikuva").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#lataa_profiilikuva").height()/2);
        $("#facebox").css('left', winW/2-$("#lataa_profiilikuva").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //--- LIITY/POISTU RYHMÄSTÄ
    //
    $("#en_kuulu_ryhmaan").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#poistu_ryhmasta").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#poistu_ryhmasta").height()/2);
        $("#facebox").css('left', winW/2-$("#poistu_ryhmasta").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })
    $("#kuulun_ryhmaan").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#liity_ryhmaan").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#liity_ryhmaan").height()/2);
        $("#facebox").css('left', winW/2-$("#liity_ryhmaan").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //--- Muokkaa ryhmän tietoja
    //
    $("#muokkaa_ryhmaa").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#ryhman_tiedot").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#ryhman_tiedot").height()/2);
        $("#facebox").css('left', winW/2-$("#ryhman_tiedot").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //-- MUOKKAA TUKIKOHDAN TIETOJA
    //
    $("#muokkaa_tukikohtaa").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#tukikohdan_tiedot").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#tukikohdan_tiedot").height()/2);
        $("#facebox").css('left', winW/2-$("#tukikohdan_tiedot").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //-- LISÄÄ RYHMÄ TUKIKOHTAAN
    //
    $("#ryhman_lisaaminen").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#lisaa_ryhma").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#lisaa_ryhma").height()/2);
        $("#facebox").css('left', winW/2-$("#lisaa_ryhma").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //-- LISÄÄ TAPAHTUMA
    //
    $("#lisaa_tapahtuma").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#tapahtuman_lisaaminen").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#tapahtuman_lisaaminen").height()/2);
        $("#facebox").css('left', winW/2-$("#tapahtuman_lisaaminen").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })
    //
    //-- LISÄÄ UUTINEN
    //
    $("#lisaa_uutinen").click(function(){
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#facebox_overlay').css({'width':maskWidth,'height':maskHeight});

        //transition effect
        $('#facebox_overlay').fadeIn(1000);
        $('#facebox_overlay').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        $("#facebox .body").html($("#uusi_uutinen").html());
        //Set the popup window to center
        $("#facebox").css('top',  winH/2-$("#uusi_uutinen").height()/2);
        $("#facebox").css('left', winW/2-$("#uusi_uutinen").width()/2);

        //transition effect
        $("#facebox").fadeIn(200);
    })

    //
    //-- CONFIRM DELETE (GENERAL)
    //
    $('.delete').click(function()
    {
        var answer = confirm('Haluatko varmasti poistaa kohteen?');
        return answer; // answer is a boolean
    })

    //
    //-- EXTERNAL LINK WARNING (GENERAL)
    //
    $('.external').click(function()
    {
        var answer = confirm('Tämä linkki ohjaa ulkoiselle sivulle. RRF ei vastaa sivun sisällöstä. Haluatko jatkaa linkin seuraamista?');
        return answer;
    })

    //
    //-- AJAX TAPAHTUMAN KOMMENTOINTI
    //
    $("#tapahtuma_kommentoi").click(function(e)
    {
        e.preventDefault();

        var tapahtuma = $('input[name=tapahtuma]').val(),
            viesti = $("#viesti").val(),
            target_url = $("#kommentointi").attr("action");

        if(viesti === "")
        {
            alert("Kommentissasi ei ole sisältöä.");
            return false;
        }

        $.ajax({
           type: "POST",
           url: target_url,
           data: "tapahtuma=" + tapahtuma + "&viesti=" + viesti,
           success: function(msg){
               $("#viesti").val("");
               //var obj = jQuery.parseJSON(msg);
               $("#comment_list").prepend(msg);
           }
        });

       return false;
    });
});
