﻿$(document).ready(function () {

    var fadeSpeed = 50;

    ////////////////////////////////////////////////////////////////////////////
    //
    //
    //  NewsFlash
    //
    //

    if ($("#NewsFlash").length > 0) {
        $("DIV#Content>DIV.ProjectItem")
        .each(function (i) {
            $(this).fadeTo('fast', 0.4);
        });

        setTimeout(function () {
            $('#NewsFlash').fadeOut('fast', function () { $('#NewsFlash').remove() });
            Fade();
            Highlight();

            $("DIV#Content>DIV.ProjectItem")
            .each(function (i) {
                $(this).fadeTo('slow', 1);
            })
        }, 3500);
    }
    else {
        Fade();
        Highlight();

    }


    ////////////////////////////////////////////////////////////////////////////
    //
    //
    //  Fade
    //
    //
    var description = "";

    function Fade() {

        $("DIV#ProjectFocusOuter").hover(function () {
            $(this).fadeOut(fadeSpeed, function () {
                $("#ProjectFocusInner IMG").remove();
            });
        });

        // Fade out image on mouse
        $("DIV#Content")
    .hover(function () {
        $("#ProjectFocusOuter").fadeOut(fadeSpeed);
    });

        // Images
        $("DIV#Content>DIV.ProjectItem")
    .each(function (i) {
        // add pos in attribute title
        //$(this).attr("title", i + 1);
        $(this).attr("id", i + 1);
    })
    .hover(function () {
        var pos = parseInt($(this).attr("id"));
        var src = $("IMG", this).attr("longdesc");
        description = $("IMG", this).attr("alt");

        // disable alt and title
        $("IMG", this).attr("alt", "");
        $("A", this).attr("title", "");

        // Determine position
        if ((pos >= 4 && pos <= 6) || (pos >= 10 && pos <= 12)) {
            $('#ProjectFocusOuter').css("left", 1);
        }
        else
            $('#ProjectFocusOuter').css("left", 427);

        // Add loader
        //$('#ProjectFocusInner').append("<div class='ProjectFocusLoading'></div>");

        var img = new Image();
        $(img).load(function () {
            // Fade in container
            $('#ProjectFocusOuter').fadeIn(fadeSpeed);

            // Hide image and append element
            $(this).hide();
            $('#ProjectFocusInner').append(this);

            // Fade images and remove any old description and loader
            $(this).fadeIn(fadeSpeed, function () {
                if ($("#ProjectFocusInner IMG").length > 1) {
                    $("#ProjectFocusInner IMG:first-child").remove();
                    $("#ProjectFocusInner SPAN:first-child").remove();
                }
                // Remove loader
                //$("#ProjectFocusInner DIV.ProjectFocusLoading").remove();

            });

            // Append description
            $('#ProjectFocusInner').append("<span>" + description + "</span>");

        })
        .error(function () {
            alert("Bilden kunde inte laddas.");
            // Remove loader
            //$("#ProjectFocusInner DIV.ProjectFocusLoading").remove();
        })
        .attr('src', src);
        return false;
    }
    , function () {
        // Add alt and title
        $("IMG", this).attr("alt", description);
        $("A", this).attr("title", description);
    });

    }

    ////////////////////////////////////////////////////////////////////////////
    //
    //
    //  Highlight
    //
    //
    var Title





    function Highlight() {

        var mouseid = 0
        var DivID = 1;



        $("#ProjectNavLevelTwo")
        .mouseleave(function () {
            $("#ProjectNavLevelTwo UL LI").each(function (i) {
                $("DIV#Content DIV#" + $(this).attr("id")).animate({ opacity: 1 }, fadeSpeed);
            });
        })
        .mouseenter(function () {
            $("#ProjectNavLevelTwo UL LI")
            .mouseenter(function () {
                DivID = $(this).attr("id");
                //$("DIV#Content DIV#" + $(this).attr("title")).animate({ opacity: 0.6 }, fadeSpeed);
                $("#ProjectNavLevelTwo UL LI").each(function (i) {
                    if (DivID != $(this).attr("id")) {
                        $("DIV#Content DIV#" + $(this).attr("id")).animate({ opacity: 0.4 }, fadeSpeed);
                    }
                    else {
                        $("DIV#Content DIV#" + $(this).attr("id")).animate({ opacity: 1 }, fadeSpeed);
                    }
                });
            });


        });







        $('#Navigation').mouseleave(function () {
            //$("#ProjectNavLevelTwo").html("");
            $("DIV#Content>DIV.ProjectItem")
            .each(function () {
                jQuery("A IMG", this).animate({ opacity: 1 }, fadeSpeed);
            });
        });

        $("#ProjectNav A").mouseenter(function () {
            // Match id with classname 
            var define = $(this).attr("id");

            var ProjectNavLevelTwo = "<ul>";

            if (define != "") {
                $("DIV#Content>DIV.ProjectItem")
                .each(function () {
                    if (!$(this).hasClass(define))
                        jQuery("A IMG", this).animate({ opacity: 0.4 }, fadeSpeed);
                    else {
                        jQuery("A IMG", this).animate({ opacity: 1 }, fadeSpeed);
                        id = jQuery(this).attr("id");
                        text = jQuery("A", this).attr("title");
                        href = jQuery("A", this).attr("href");
                        ProjectNavLevelTwo += "<li id=\"" + id + "\"><a href=\"" + href + "\">" + text + "</li>";
                    }
                });
            }
            ProjectNavLevelTwo += "</ul>";
            //$("#ProjectNavLevelTwo").html(ProjectNavLevelTwo);

            $('#ProjectNavLevelTwo').mouseleave(function () {
                $("#ProjectNavLevelTwo UL LI").each(function (i) {
                    $("DIV#Content DIV#" + $(this).attr("id")).animate({ opacity: 1 }, fadeSpeed);
                });
            }), function () { };


            $("#ProjectNavLevelTwo UL LI")
            .mouseenter(function () {
                DivID = $(this).attr("id");

                //$("DIV#Content DIV#" + $(this).attr("title")).animate({ opacity: 0.6 }, fadeSpeed);
                $("#ProjectNavLevelTwo UL LI").each(function (i) {
                    if (DivID != $(this).attr("id"))
                        $("DIV#Content DIV#" + $(this).attr("id")).animate({ opacity: 0.4 }, fadeSpeed);
                    else
                        $("DIV#Content DIV#" + $(this).attr("id")).animate({ opacity: 1 }, fadeSpeed);
                });
            });

        },
        function () {
            $("DIV#Content>DIV.ProjectItem")
            .each(function () {
                jQuery("A IMG", this).animate({ opacity: 1 }, fadeSpeed);
            });
        });
    }
});
