//kirjautuminen
function logIn(user,passwd)
{
    var type = $("#mailClient").val();

    //pyöreä kuutio
    if(type == "2")
    {
        $("#loginForm").attr("action", "https://mail.opiferum.com/roundcube/");
        $("#passwd1").attr("name", "_pass");
        $("#user1").attr("name","_user");
        $("#passwd1").val(passwd);
        $("#user1").val(user);

        // Tyhjennetään fieldit
        $("#user").val("");
        $("#passwd").val("");
        return true;
    }
	else
    {
        $("#loginForm").attr("action", "https://mail.opiferum.com/src/redirect.php");
        $("#passwd1").attr("name", "secretkey");
        $("#user1").attr("name","login_username");
        $("#user1").val(user);
        $("#passwd1").val(passwd);

        // Tyhjennetään fieldit
        $("#user").val("");
        $("#passwd").val("");
        return true;
    }
    return false;
}

//tarkistetaan kirjautuminen
function checkLogin()
{	
    if($("#user").val()=="" && $("#passwd").val()=="")
    {
        alert("Tunnus ja salasana on pakollinen");
        return false;
    }
    else
    {
        return logIn($("#user").val(),$("#passwd").val());
    }
    return true;
}

//togglaataan fake radiobuttonin arvoa ja kuvaa
function toggleRadioButton(el)
{
    var site_main_url = $("#site_main_url").val();
    for(var i=0; i<2; i++)
    {
        if((i+1)==el)
        {
            $("#radio_"+(i+1)).attr("src",site_main_url+"kuvat/radio_checked.png");
            $("#mailClient").val(el);
        }
        else
        {
            $("#radio_"+(i+1)).attr("src",site_main_url+"kuvat/radio.png");
        }
    }
}

//alasvetovalikko
function toggleMenuVisible(el)
{
    //poistetaan aluksi perinteinen mouseover
    $("#"+el).parent(".sLMFix").parent(".etu_valikkolinkki").mouseover("");
    $("#"+el).parent(".sLMFix").parent(".etu_valikkolinkki").removeAttr('onmouseover');

    //avataan valikko ja lisätään mouseenter + leave
    $("#"+el).slideDown("slow");
    $('#'+el).parent(".sLMFix").parent(".etu_valikkolinkki").mouseenter(function(event) {
            event.stopPropagation();
            $("#"+el).stop(true,true).delay(500).slideDown("slow");
        }
    );

    $('#'+el).parent(".sLMFix").parent(".etu_valikkolinkki").mouseleave(function(event) {
            event.stopPropagation();
            $("#"+el).stop(true,true).slideUp("slow");
        }
    );
}

//togglataan bloginäkymän alasvetolikkoa
function toggleSubMenu(el)
{
    var site_main_url = $("#site_main_url").val();
    var el2 = el.replace("subLink","subMenu");
    var el3 = el.replace("subLink","rssl");

    //suljetaan aluksi auki olevat valikot
    $(".submenuContainer:not(#"+el2+")").slideUp("slow",function(){
        $(".rightSideSubLink:not(#"+el3+") img").attr("src",site_main_url+"kuvat/plus.png");
    });

    //ja avataan sitten nykyinen
    $("#"+el).attr("src",site_main_url+($("#"+el).attr("src")==site_main_url+"kuvat/plus.png"?"kuvat/minus.png":"kuvat/plus.png"));
    $("#"+el2).slideToggle("slow");
}

//Facebook-linkki
function facebookLink()
{
    window.open('http://www.facebook.com/pages/Opiferum/146630925393774');
}

//Twitter-linkki
function twitterLink()
{
    //lisätäänkö tweettikohtainen linkki (?)
    if($("#currentTweet").length==1 && $("#currentFrame").length==1)
    {
        var currentTweet = parseInt($("#currentTweet").val());
        var currentFrame = parseInt($("#currentFrame").val());
        if(currentFrame>2 && currentFrame!=5)
        {
            if($("#tweetlink"+currentTweet).length==1)
            {
                var tweetlink = $("#tweetlink"+currentTweet).val();
            }
        }
    }
    window.open('http://twitter.com/#!/Opiferum'+(tweetlink?"/status/"+tweetlink:""));
}

//animoidun taustan asetukset
var scrollSpeed = 70;       // Speed in milliseconds
var step = 1; // How many pixels to move per step
var cookien_current = $.cookies.get('cloudPos');
var current = (cookien_current?cookien_current:-858); // The current pixel row
var imageHeight = 429;     // Background image height
var headerHeight = 1000;     // How tall the header is.

//The pixel row where to start a new loop
var restartPosition = 0;// -(imageHeight - headerHeight);

//liikutetaan taustaa
function scrollBg(){

    //Go to next pixel row.
    current += step;
    window.current = current;
    //$("#user").val(current);

    //If at the end of the image, then go to the top.
    if (current == restartPosition){
        current = -429;
        window.current = current;
    }

    //Set the CSS of the header.
    $('#cloud1').css("left",""+current+"px");
}

//tallennetaan taustan sijainti seuraavalle sivulle
$(window).unload(function()
{
    $.cookies.set('cloudPos', ''+window.current+'');
});

$(function()
{
    //logon fade in/out
    $("#logoEffect").css({opacity: 0});

    $( ".logo" ).mouseover( function() {
        $("#logoEffect").stop( true, true ).animate({
            opacity: 1
        }, 200, "swing");
    });
    $( ".logo" ).mouseout( function() {
        $("#logoEffect").stop( true, true ).animate({
            opacity: 0
        }, 200, "swing");
    });

    //animoitu tausta
    var init = setInterval("scrollBg()", scrollSpeed);

    //ja sienestykset
    /*setInterval(function(){
    $("#ufos").css({opacity:0});
    $("#ufos").delay(1000).animate({
        left: '104px',
        top: '100px',
        width: '28px',
        height: '10px',
        opacity: 1
    },2000,function(){
        $("#ufos").animate({
            left: '604px',
            top: '-100px',
            width: '28px',
            height: '10px',
            opacity: 0
        },2000);
    });
    },10000);*/

    //haetaan twiitit
    var tcount = 0;
    var tweets = new Array();
    var site_main_url = $("#site_main_url").val();
    $("#tux, #tux_txt").css({opacity:0});
    $("#tweet1,#tweet2,#tweet3,#tweet4,#tweet5,#tweet6,#tweet7,#tweet8,#tweet9,#tweet10").each(function()
    {
        tweets[tcount] = $(this).val();
        tcount++;
    });

    //aloitetaan "twitter" animaatio
    setTimeout(function(){swapTux(site_main_url,tweets,1,1,0);},1000);

    //tux pingviinin seikkailut
    function swapTux(site_main_url,tweets,i,i2,i3)
    {
        i = parseInt(i);
        i2 = parseInt(i2);
        i3 = parseInt(i3);

        //tallennetaan tieto nykyisestä twiitistä
        $("#currentFrame").val(i);
        $("#currentTweet").val((i3+1));
        
        if(i==1)
        {
            $("#tux").attr("src", site_main_url+"kuvat/tux"+i2+".png");
            $("#tux").animate({
                opacity: 1
            },1000,function(){setTimeout(function(){swapTux(site_main_url,tweets,(i+1),(i2+1),i3);},1000);});
        }
        else if(i==2)
        {
            $("#tux").attr("src", site_main_url+"kuvat/tux"+i2+".png");
            setTimeout(function(){swapTux(site_main_url,tweets,(i+1),(i2+1),i3);},500);
        }
        else if(i==3 || i==6)
        {
            $("#tux").attr("src", site_main_url+"kuvat/tux3.png");
            $("#tux_txt").html(tweets[i3]);
            $("#tux_txt").animate({opacity:1}, 1000,function()
            {
                setTimeout(function(){swapTux(site_main_url,tweets,(i+1),(i2+1),(i3+1));},4000);
            });
        }
        else if(i==5)
        {
            $("#tux_txt").animate({opacity:0}, 1000,function()
            {
                $("#tux").attr("src", site_main_url+"kuvat/tux"+i2+".png");
                setTimeout(function(){swapTux(site_main_url,tweets,(i+1),(i2-1),i3);},4000);
            });
        }
        else if(i==4 || i>6)
        {
            $("#tux_txt").html(tweets[i3]);
            if((i3+1)<tweets.length)
            {
                setTimeout(function(){swapTux(site_main_url,tweets,(i+1),i2,(i3+1));},4000);
            }
            else
            {
                setTimeout(function(){swapTux(site_main_url,tweets,3,3,0);},4000);
            }
        }
    }

    //scrollaus keksi
    $.cookies.get('myCookie');

    //set window scroll position if cookie is set
    window.scroll(0, parseFloat($.cookies.get('myCookie')));

    //unset cookie after setting scroll position
    $.cookies.del('myCookie');

    //make this class objects keep page scroll position
    $('.linkki, .linkki2, .sivulinkki, .sivulinkki2, .ut_link').each(function() {
        $(this).click(function() {
            $.cookies.set('myCookie', ""+getPageScroll()+"");
        });
    });

    //siirrytään bloginäkymässä oikeaan kohtaan kun joku uutinen valittuna...
    if($("#fullContent").length==1)
    {
        var offset = $("#newsAnchor").offset().top;
        window.scroll(0, offset);
    }

    //this function simply gets the window scroll position, works in all browsers
    function getPageScroll() {
        var yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            yScroll = document.documentElement.scrollTop;
        } else if (document.body) {
            yScroll = document.body.scrollTop;
        }
        return yScroll
    }

    //vaihdetaan lyhyen tekstin tilalle koko sisältö (silmänkääntötemppu)
    if($("#shortContent").length==1 && $("#fullContent").length==1 && $("#hideLink").length==1)
    {
        $("#shortContent").animate({opacity:0},function(){
            $("#hideLink").animate({opacity:0},function(){
                $("#shortContent").html($("#fullContent").val());
            });
        });
        $("#shortContent").animate({opacity:1});
    }

    //kohdistus kirjautumiseen
    //if($("#user").length==1){$("#user").focus();}
});
