/*

highlight v2

Highlights arbitrary terms.

<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

MIT license.

Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>

*/

$(function() {
    jQuery.highlight = document.body.createTextRange ?

    /*
    Version for IE using TextRanges.
    */
  function(node, te) {
      var r = document.body.createTextRange();
      r.moveToElementText(node);
      for (var i = 0; r.findText(te); i++) {
          r.pasteHTML('<span class="searchterm">' + r.text + '<\/span>');
          r.collapse(false);
      }
  }

 :

    /*
    (Complicated) version for Mozilla and Opera using span tags.
    */
  function(node, te) {
      var pos, skip, spannode, middlebit, endbit, middleclone;
      skip = 0;
      if (node.nodeType == 3) {
          pos = node.data.toUpperCase().indexOf(te);
          if (pos >= 0) {
              spannode = document.createElement('span');
              spannode.className = 'searchterm';
              middlebit = node.splitText(pos);
              endbit = middlebit.splitText(te.length);
              middleclone = middlebit.cloneNode(true);
              spannode.appendChild(middleclone);
              middlebit.parentNode.replaceChild(spannode, middlebit);
              skip = 1;
          }
      }
      else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
          for (var i = 0; i < node.childNodes.length; ++i) {
              i += $.highlight(node.childNodes[i], te);
          }
      }
      return skip;
  }

 ;
});

jQuery.fn.removeHighlight = function() {
    this.find("span.searchterm").each(function() {
        with (this.parentNode) {
            replaceChild(this.firstChild, this);
            normalize();
        }
    });
    return this;
};

jQuery(function() {
    //remove the extra "extraKnop" css classes in the homepage left menu
    $(".extraKnop").not(".extraKnop:first").removeAttr("class");
    //remove the img attributes that morello includes in the banners in the footer
    $("#banner a img").removeAttr("style");
    //change the homepage image based on the pool
    $("#visual").each(function(i) {
        if ($(this).text().length > 0) {
            $(this).css({ "background-image": "url(" + $(this).text() + ")", "display": "block" });
            $(this).text("");
            //$(this).show();
        }
    });

    $('.profilecheck').children(":first").click(function() {
        $.blockUI({
            message: 'Een moment geduld alstublieft.',
            css: { top: '20%' }
        });

        setTimeout($.unblockUI, 10000);
    });

    $("input[@value=rdbAnswer]").removeAttr("checked");

    $(".alphabet ul li a").each(function(link) {
        var linkUrl = $(this).attr("href");
        if (linkUrl == null || linkUrl.length == 0) {
            $(this).addClass("removeBold");
        }
    });

    function isLinkFromCurrentHost(hostname) {
        var currentHost = hostname.substr(0, hostname.indexOf(":"));
        return (currentHost == window.location.host);
    }

    $(".sub [class != 'gepensionnerde'] #content:has(#sidebar)").removeClass("standardPage");

    function getheight() {
        var bodyheight = $("#mainContent").height() + $("#footWrapper").height();
        if (bodyheight >= $(window).height()) {
            return bodyheight;
        }
        return $(window).height();
    }

    $(".openanswertextarea").hide();

    $(".openanswer").click(function() {
        $(".openanswertextarea").show();
    });

    $(".normalanswer").click(function() {
        $(".openanswertextarea").hide();
    });

    $(".sitemap .links li a[href='javascript:window.print();']").parent().hide();

    $('.administrationnumber').mask('999-9999999999-99');
    $('.dateMask').mask('99-99-9999');
    $("#footLogo a").attr("target", "_blank");
    $(".logosideimage").css("top", $(".logo2img").offset().top + "px");
    $(".sub").css("height", getheight());
    $(".home").css("height", getheight());
    $("a > img").addClass("imageUnderLink"); // each(function() { $(this).parent().css("border", "none"); });
    $(".imageUnderLink").parent("a").attr("style", "border-width: 0 !important; border-style: none !important;")
    $(".imageUnderLink").attr("style", "border-width: 0 !important; border-style: none !important;")
    $("a.unreadDocument").click(function() {
        $(this).removeClass("unreadDocument");
        $(this).addClass("readDocument");
    });

    function hasSearchString() {
        var qrStr = window.location.search;
        var spQrStr = qrStr.substring(1);
        var arrQrStr = new Array();
        // splits each of pair
        var arr = spQrStr.split('&');

        for (var i = 0; i < arr.length; i++) {
            // splits each of field-value pair
            var index = arr[i].indexOf('=');
            var key = arr[i].substring(0, index);
            if (key == 'zoekterm') {
                return true;
            }
        }
        return false;
    }

    function getSearchString() {
        var qrStr = window.location.search;
        var spQrStr = qrStr.substring(1);
        var arrQrStr = new Array();
        // splits each of pair
        var arr = spQrStr.split('&');

        for (var i = 0; i < arr.length; i++) {
            // splits each of field-value pair
            var index = arr[i].indexOf('=');
            var key = arr[i].substring(0, index);
            if (key == 'zoekterm') {
                return arr[i].substring(index + 1);
            }
        }
        return "";
    }
    //highlight keywords based on the request query string
    /*
    Hilighting:         $('#content').each(function() { $.highlight(this, 'AZL'); });
    Removing highlitht: $('#content').removeHighlight();          
    */

    $('#content').each(function() {
        if (hasSearchString() == true) {
            var searchstring = getSearchString();
            if (searchstring != "") {
                $.highlight(this, getSearchString().toUpperCase());
            }
        }
    });

    $(".TreeView ul li").each(function(listitem) {
        if (!$(this).hasClass("expandable")) {
            $(this).addClass("remove20PixelsMarginLeft");
        }
    });
});


$(window).load(function() {
    //retieve image object of hidden image which has the same source URL as the div background image (HACK!)
    var imgSrc = $('#hidden-background-image').attr('src');
    if (imgSrc.length > 0) {
        var pic = $('#hidden-background-image');
        var minimumHeight = pic.height();
        //set div minimum height to the background image height
        $('#content').css('min-height', minimumHeight + 'px'); 
        if($.browser.msie && $.browser.version=="6.0"){
            $('#content').css('height', minimumHeight + 'px'); 
        }
    }
});

