﻿$(document).ready(function() {

    $("#MainMedia a.mediabig").fancybox({
        'hideOnContentClick': true,
        'overlayShow': true
    });

    $("#wpp a.mediabig, #imagearchive a").fancybox({
        'hideOnContentClick': true,
        'overlayShow': true
    });

    $("#WebPartsPage-One a.mediabig").fancybox({
        'hideOnContentClick': true,
        'overlayShow': true
    });

    $("#imageArchive a.img, #MainBodySubMenu div.ship-right a").fancybox({
        'hideOnContentClick': true,
        'overlayShow': true
    });

    $('input.quicksearch').keyup(function(e) {
        if (e.keyCode == 13) {
            window.location = "/en/search?quicksearchquery=" + $('input.quicksearch').value();
            window.location.href = "/en/search?quicksearchquery=" + $('input.quicksearch').value();
            return false;
        }
    });

    $("table.fleeet tbody tr").mouseover(function() {
        $(this).css({
            "background-image": 'none',
            'background-color': ''
        });
    }).mouseout(function() {
        $(this).css({
            "background-image": 'url(/Templates/Concordia/Images/bg.gif)',
            'background-color': ''
        });
    });

    $("#news-press-body li.np").mouseover(function() {
        $(this).css({ "background-color": "#ececec" });
    }).mouseout(function() {
        $(this).css({ "background-color": "#ffffff" });
    });

    $("#fleet-images li").hide();
    $("#fleet-images li:first-child").show();
    $("#fleet-images").everyTime(5000, "image-slider2", function() {
        col = $("li", this);
        next = false;

        col.each(function(i, e) {
            if ($(e).css("display") != "none") {
                $(e).fadeOut(500);
                next = i + 1;
            }
        });

        if (next + 1 == col.size()) {
            next = 0;
        }

        col.each(function(i, e) {
            if (i == next) {
                $(e).oneTime(500, "fadein", function() {
                    $(this).fadeIn(500);
                });
            }
        });
    });

    $("#news-press-slideshow li").hide();
    $("#news-press-slideshow li:first-child").show();
    $("#news-press-slideshow").everyTime(5000, "image-slider", function() {
        col = $("li", this);
        next = false;

        col.each(function(i, e) {
            if ($(e).css("display") != "none") {
                $(e).fadeOut(500);
                next = i + 1;
            }
        });

        if (next + 1 == col.size()) {
            next = 0;
        }

        col.each(function(i, e) {
            if (i == next) {
                $(e).oneTime(500, "fadein", function() {
                    $(this).fadeIn(500);
                });
            }
        });
    });

    $.getJSON("/" + current_language + "/JSON-ticker/", function(data) {

        slides = $("div.ticker ul.slides");
        content = $("div.ticker ul.content");

        $.each(data, function(i, n) {
            if (i != 0) {
                i = i - 1;

                // Create image
                img = $("<img />");
                img[0].source = n["images"];
                img[0].number = i;
                img.attr("src", n["images"][i == 0 ? "selected" : "grayed"]);

                if (i == 0) {
                    img.addClass("selected");
                }

                // Create list item
                li = $("<li />");
                li.append(img);

                // Append to list

                slides.append(li);

                url = n["url"].replace(/\.se/, ".se/" + current_language);

                // Content
                a = $('<div class="link">' + n["content"] + "</div>");

                li = $("<li onclick=\"window.location.href='"+url+"';\" style=\"cursor:pointer;\" />");
                li[0].source = n["images"];
                li[0].number = i;
                li.css({
                    "background-image": "url(" + n["images"]["background"] + ")",
                    "background-color": "#ffffff"
                });

                li.append(a)
                content.append(li);

                if (i != 0) {
                    li.css("display", "none");
                }
            }
        });

        // Mouseover-event
        $("div.ticker ul.slides li img").mouseover(function() {
            $(this).attr("src", this.source["selected"]);

            // Mouseout-event
        }).mouseout(function() {
            if (!$(this).hasClass("selected")) {
                $(this).attr("src", this.source["grayed"]);
            }

            // Click-event
        }).click(function(e) {
            $("div.ticker ul.slides img").removeClass("selected").each(function(i, e) {
                $(e).attr("src", e.source["grayed"]);
            });

            $(this).mouseover().addClass("selected");
            $("div.ticker ul.content").ticker("go", this.number).ticker("stop");
        });

        $("div.ticker ul.content").ticker("init", {
            delay: 12000, 	// Delay between switching of elements
            speed: 500, 		// Animation speed
            linked: true, 	// If stepping after last element will go to first or stop and stepping before first will goto last or stop
            selection: 'li', // The sub-selection to use for each element
            animations: {
                _in: 'fadeIn', // Animation used to fade in
                out: 'fadeOut', // Animation used to fade out
                fallback: {
                    _in: "show",
                    out: "hide",
                    speed: 0
                }
            },
            callbacks: {
                _in: function(ul) {
                    // Current (active) element is in ul.ticker.current
                    // Called when the fade in animation starts
                    $("div.ticker ul.slides img").removeClass("selected").each(function(i, e) {
                        $(e).attr("src", e.source["grayed"]);
                        if (i == ul.ticker.current.number) {
                            $(e).mouseover().addClass("selected");
                        }
                    });
                },
                out: function(ul) {
                    // Current (active) element is in ul.ticker.current
                    // Called when the fade out animations tarts
                    $("div.ticker ul.slides img").each(function(i, e) {
                        if (i == ul.ticker.current.number) {
                            $(e).mouseout();
                        }
                    });
                },
                init: function(ul) {
                    $("div.ticker ul.content li a.mediabig").fancybox({
                        'hideOnContentClick': true,
                        'overlayShow': true
                    });
                    // Current (active) element is in ul.ticker.current
                    // Called when ticker is initialized
                }
            }
            // This call enables looping for this ticker, without it's start/stop doesn't do anything and it wont loop
        }).ticker("loop").ticker("start");

    });

});

function openFancybox(foo) {
    concordia_url = foo;
    $("#view360").fancybox();
    $("#view360").click();
}