﻿$(document).ready(function() {
    var count = 0;
    var size = $("#wrapper #bgImg .bgImg").size();
    $("#totalImg").html(size);
    $("#wrapper #bgImg").cycle({
        fx: 'scrollDown'
        , speed: 2000
        , before: onBefore
        , timeout: 15000
        , random: 1
    });

    function onBefore() {
        if (count < size) {
            count = count + 1;
        } else {
            count = 1;
        }

        $("#currImg").html(count);
        $("#imgCaption").html(" - " + $(this).html());

    };

    $("#loginarea input").focus(function() {
        if ($(this).val() == $(this).attr("rel")) {
            $(this).val("");
        }
        if ($(this).hasClass("wrongPass")) {
            $(this).removeClass("wrongPass");
        }
    }).blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("rel"));
        }
    });

    $("#maincontent .readMore").click(function() {
        if (!$(this).hasClass("close")) {
            var thisA = this;
            $("#maincontent .contentbox").fadeOut("fast", function() {
                $(thisA).addClass("close").html("Tillbaka").closest(".contentbox").addClass("readMore");
                $(".contentbox.readMore").css({
                    height: $("#maincontentwrapper").height() - 51
                    , width: 960 - $("#quotes").width() - 30
                }).fadeIn("fast");
            });

        } else {
            $(this).removeClass("close").html("Läs mer").closest(".contentbox").removeClass("readMore").css({
                height: 180
                , width: 300
            });
            $("#maincontent .contentbox").fadeIn("fast");
        }
        return false;
    });

    $("#headerbyline .readMore, #information").click(function() {
        var thisA;
        if ($(this).attr("id") != "information") {
            thisA = this;
        } else {
            thisA = $("#headerbyline .readMore");
        }

        if (!$(this).hasClass("close") && !$("#information").hasClass("closeNav")) {
            $("#information").addClass("closeNav");
            $(thisA).addClass("close").html("Tillbaka").parent().addClass("readMore").find("p, a").hide();
            $("#headerbyline").animate({
                marginLeft: 0
                    , width: 960
                    , height: 250
            }, "500", function() {
                $("#headerbyline").find("p.moreCont,a").fadeIn();
            }).next().fadeOut();


        } else {
            $("#headerbyline").find("p,a").hide();
            $("#information").removeClass("closeNav");
            $(thisA).removeClass("close").html("Läs mer").parent().removeClass("readMore").animate({
                height: 160
                , width: 480
                , marginLeft: 480
            }, "500", function() {
                $("#headerbyline").find("p:not(.moreCont),a").fadeIn();
            }).next().fadeIn();
        }
        return false;
    });

    $("a#forgotPass").click(function() {
        $("ul#loginForm").fadeOut("fast", function() {
            $("ul#remindForm").fadeIn()
        });
        return false;
    });

    $("a#cancelRemind, p#message a").click(function() {
        $("p#message").hide();
        $("ul#remindForm").fadeOut("fast", function() {
            $("ul#loginForm").fadeIn()
        });
        return false;
    });

    if ($("#state").val() == "wrongEmail") {
        $("ul#loginForm").fadeOut("fast", function() {
            $("ul#remindForm").fadeIn()
        });
    } else if ($("#state").val() == "mailSent") {
        $("ul#loginForm").fadeOut("fast", function() {
            $("p#message").html('Dina uppgifter har skickats till den angivna e-postadressen.<a href="#">Tillbaka</a>').fadeIn();

            $("p#message a").click(function() {
                $("p#message").hide();
                $("ul#remindForm").fadeOut("fast", function() {
                    $("ul#loginForm").fadeIn()
                });
                return false;
            });
        });
    }

});

