﻿function hideLegacyFlashAds() {
    $('object.legacy').hide();
}

function showLegacyFlashAds() {
    $('object.legacy').show();
}

$(document).ready(function () {
    $("a.jTooltip").simpletooltip();
}); 

jQuery.preloadImages = function () {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

$(window).bind('load', function () {
    $.preloadImages(
        '/Images/SearchHover.png');
});

$(document).ready(function () {
    var maxlines = 12;
    var lineheight = 15; // line height in 'px'
    var maxheight = (maxlines * lineheight);
    var allowedExtraLines = 3;
    var showText = "Læs mere...";
    var hideText = "Skjul tekst...";

    $('.Truncate').each(function () {
        var text = $(this);
        if (text.height() > maxheight + allowedExtraLines * lineheight) {
            text.css({ 'overflow': 'hidden', 'line-height': lineheight + 'px', 'height': maxheight + 'px' });

            var link = $('<a href="#">' + showText + '</a>');
            link.click(function (event) {
                event.preventDefault();

                if (text.css('height') == 'auto') {
                    $(this).html(showText);
                    text.css('height', maxheight + 'px');
                } else {
                    //$(this).remove();
                    $(this).html(hideText);
                    text.css('height', 'auto');
                }
            });

            var linkDiv = $('<div></div>');
            linkDiv.append(link);

            $(this).after(linkDiv);
        }
    });
});

//Links opens in a new window when they are relative and genereted with wymeditor
$(function () {
    $(".Generated a[href^='http']").click(function () {
        window.open(this.href);
        return false;
    });
});

function linksNewWindow() {
    $(".NewWindow").click(function () {
        window.open($(this).attr("href")); return false;
    });
}

$(document).ready(function () {
    $(".Ad").click(function () {
        window.open($(this).attr("href")); return false;
    });

    linksNewWindow();

    $(".MessageDialog").dialog({
        autoOpen: true,
        bgiframe: true,
        resizable: false,
        draggable: true,
        width: 350,
        height: 140,
        modal: true,
        open: function (event, ui) { hideLegacyFlashAds(); },
        close: function (event, ui) { showLegacyFlashAds(); },
        buttons: {
            'Ok': function () {
                $(this).dialog('close');
            }
        }
    });
});

$(document).ready(function () {

    $("#SearchDialog #SearchButton").hover(
        function () {
            $(this).addClass("ui-state-hover");
        },
        function () {
            $(this).removeClass("ui-state-hover");
        });

    $("#SearchDialog").dialog({
        autoOpen: false,
        bgiframe: true,
        resizeable: true,
        draggable: true,
        height: 550,
        width: 450,
        modal: true,
        open: function (event, ui) { $('object.legacy').hide(); },
        close: function (event, ui) { $('object.legacy').show(); },
        buttons: {
            'Luk': function () {
                $(this).dialog('close');
            }
        }
    });

    $("#FloatingSearch a").click(function () {
        $("#SearchDialog").dialog('open');
        $("#SearchDialog #SearchQuery").focus();
        return false;
    });

    $("#SearchDialog #SearchForm").submit(function () {
        var q = $("#SearchDialog #SearchQuery").val();

        if (q == "") return false;

        function highlight() {
            if (typeof jQuery.fn.highlight == "undefined") {
                $.getScript('Scripts/jquery.highlight-3.js', function () {
                    $('#SearchDialog #SearchResults').highlight(q);
                    $('#SearchDialog #SearchResults .no-highlight').removeHighlight();
                });
            }
            else {
                $('#SearchDialog #SearchResults').highlight(q);
                $('#SearchDialog #SearchResults .no-highlight').removeHighlight();
            }
        }

        function loadPage(url) {
            $("#SearchDialog #SearchResults").load(url, function () {
                highlight();
                remapLinks();
            });
        }

        function remapLinks() {
            $("#SearchDialog #SearchResults #ResultPager a").click(function (event) {
                loadPage($(this).attr('href'));
                $("#SearchDialog #SearchResults #ResultContainer").html('<em>Henter side...</em>');
                event.preventDefault();
            });

            linksNewWindow();
        }

        $("#SearchDialog #SearchResults").html('<em>Søger...</em>');

        loadPage("Search.aspx?" + $.param({ q: q }));

        return false;
    });
});

$(document).ready(function () {
    var title = $('body').attr('title');

    if (title == undefined || title == '')
        return;

    $('body').css('cursor', 'pointer');

    $('body').click(function (event) {
        if (event.target == event.currentTarget)
            window.open(title);
    });

    $('body > *').live('mouseout', function () {
        $('body').css('cursor', 'pointer');
    });

    $('body > *').live('mouseover', function () {
        $('body').css('cursor', 'auto');
    });

    $('body').attr('title', '');
});
