﻿// old browsers
var fixFooterPosition = function () {
    setTimeout(function () {
        var offsetTop = $("section.f").offset() != null ? $("section.f").offset().top : 0;
        $("footer.f").css("top", parseInt(offsetTop + $("section.f").outerHeight() + 40) + "px");
    }, 25);
}

$(document).ready(function () {

    // position footer
    $(document).bind("heightUpdated", fixFooterPosition);
    fixFooterPosition(); // fix at startup
    setTimeout(fixFooterPosition, 1000); // for slow connection
    setTimeout(fixFooterPosition, 3000); // for slower connection
    setTimeout(fixFooterPosition, 10000); // for even slower connection
    $("img").load(fixFooterPosition);
    $("input").click(fixFooterPosition);

    /* image popup */
    if ($("section").imagePopup) $("section").imagePopup();

    // a title on image overlay
    $(".imageDrop a[title] img").each(function (n, img) {
        var title = $(img).parents("a").attr("title");
        if (title.length == 0) return;
        $(img).parents(".imageDrop").append("<a href=\"" + $(img).parents("a").attr("href") + "\" target=\"" + $(img).parents("a").attr("target") + "\" class=\"caption\">" + title + "</a>");
    });


    /* frontpage */

    if ($(".frontpage article").size() > 1) {
        $(".frontpage .seeMore").show();
        $(document).trigger("heightUpdated");

        $(".frontpage .seeMore a").click(function () {
            $(".frontpage .seeMore").hide();
            var interval = setInterval(function () { $(document).trigger("heightUpdated"); }, 25);
            $(".frontpage article:hidden").slideDown(500, function () { clearInterval(interval); });
            return false;
        });
    }

    /* table row hover and click */
    $("table.styled tbody tr").hover(function () {
        $(this).addClass("hover");
        updateCurrentCoworker();
    }, function () {
        $(this).removeClass("hover");
    });
    $("table.styled tbody tr").click(function () {
        var remove = $(this).hasClass("selected");
        $("table tbody tr").removeClass("selected");
        if (!remove) $(this).addClass("selected");
        updateCurrentCoworker();
    });

    /* frame header click */
    $(".framecontent .section h2").click(function () {
        var hasClass = $(this).parents(".section").hasClass("selected");
        $(".framecontent .section").removeClass("selected");
        if (!hasClass) $(this).parents(".section").addClass("selected");
        fixFooterPosition();
    });

    /* tooltip */
    $(".framecontent .section .tip").each(function (n, val) {
        var content = [];
        $(val).parents(".section").find("th").each(function (n, j) {
            content.push($(j).text());
        });
        $(val).qtip({ content: content.join(", ") });
    });

    /* co workers */
    var updateCurrentCoworker = function () {
        var current = $("table tbody tr.selected");
        if (current.size() == 0) current = $("table tbody tr.hover");
        if (!current) {
            $(".selectedCoworker").hide();
            location.href = "#";
            fixFooterPosition();
            return;
        }
        $(".selectedCoworker").find(".name").text($(current).find("td.name").text());
        $(".selectedCoworker").find(".title").text($(current).find(".title").text() + " " + $(current).find(".area").text());
        $(".selectedCoworker").find(".phone").text("Tlf: " + $(current).find("td.phone").text());
        if ($(current).find(".email").size() > 0 && $(current).find(".email").text().length > 0) $(".selectedCoworker").find(".email").html("E: <a href=\"mailto:" + $(current).find(".email").text() + "\">" + $(current).find(".email").text() + "</a>");
        $(".selectedCoworker").find(".image").html($(current).find(".image").html());
        $(".selectedCoworker").fadeIn(100);
        location.href = "#" + $(current).attr("id").replace("element", "");
        fixFooterPosition();
    }
    if (location.hash.length > 1) {
        $("#element" + location.hash.replace("#", "")).click();
    }

    /* follow at scroll */
    setTimeout(function () {
        $(".followAtScroll").each(function (n, o) {
            var _this = $(o);
            var offsettop = 202; // $(o).offset().top;
            var isFixed = false;
            var origPosition = $(_this).css("position");
            var updateMe = function () {
                var scrolltop = $(window).scrollTop();
                //console.log("update pga scroll " + scrolltop + " " + offsettop);
                if (scrolltop > offsettop && !isFixed) {
                    $(_this).css("position", "fixed");
                    $(_this).css("top", 0);
                    $(_this).css("width", $(_this).parents(".sidebar").width());
                    isFixed = true;
                    return;
                }

                if (scrolltop <= offsettop && isFixed) {
                    $(_this).css("position", origPosition);
                    isFixed = false;
                }
            }
            $(window).scroll(updateMe);
        });
    }, 100);

    /* dropdown */

    var dropInEffect = false;
    $(".dropdownmenu").hover(function () {
        if (dropInEffect) return;
        dropInEffect = true;
        $(this).find("ul").slideDown(100, function () { dropInEffect = false; });
    }, function () {
        if (dropInEffect) return;
        dropInEffect = true;
        $(this).find("ul").slideUp(100, function () { dropInEffect = false; });
    });

    /* sidebarlist */
    $(".sidebarlist .category .c").click(function () {
        var hasClass = $(this).parents(".category").hasClass("selected");
        $(".sidebarlist .category").removeClass("selected");
        if (!hasClass) $(this).parents(".category").addClass("selected");
        return false;
    });

    /* map */
    $(".sidebarlist .elements a").click(function () {
        var id = parseInt($(this).attr("href").replace("#", ""));
        map.map.setZoom(7);
        map.click(id);
    });

    if ($(".livesearch").size() > 0) {
        var removeOverlayText = function () {
            $(".livesearch input").val("");
            removeOverlayText = function () { };
        }

        $(".livesearch input").one("click", removeOverlayText);
        $(".livesearch input").one("keydown", removeOverlayText);

        $(".livesearch input").keyup(function () {
            // todo: remove overlay text
            var keyword = $(this).val().toLowerCase();
            $("#" + $(this).attr("data-targettable") + " tbody tr").each(function (n, row) {
                if ($(row).text().toLowerCase().indexOf(keyword) < 0) {
                    $(this).hide();
                } else {
                    $(this).show();
                }
            });
            fixFooterPosition();
        });

    }

});
