﻿lw.AppendInit(function () {

    $(".menu>ul>li").menu({ parent: $(".menu") });
    $("#pikame").PikaChoose({ transition: [-1] });

    $(document).ready(function () {
        lw.galleryButton = $(".photo-gallery");
        lw.mask = $(".mask");
        lw.galleryButtonVisible = false;
        var mouseMove = function (e) {

            var pos = { left: e.pageX + 15, top: e.pageY + 15 };
            if (!lw.galleryButtonVisible) {
                if (!$.browser.msie) {
                    lw.galleryButton.css({ "display": "block", opacity: 0 });
                    lw.galleryButton.animate({ opacity: 1 }, 300);
                }
                else
                    lw.galleryButton.css("display", "block");
            }
            lw.galleryButtonVisible = true;
            var offset = lw.mask.offset();
            lw.galleryButton.css(pos);
        };

        lw.mask.bind("mousemove", mouseMove);
        lw.mask.bind("mouseout", function (e) {
            if (lw.noHideGB)
                return;
            lw.galleryButtonVisible = false;
            if (!$.browser.msie)
                lw.galleryButton.stop().animate({ opacity: 0 }, 300);
            else
                lw.galleryButton.css("display", "none");
        });

    });

    lw.home = $("#home");
    if (lw.home && lw.home.length > 0) {
        lw.newsButtons = [];
        function mycarousel_initCallback(carousel) {
            var len = $(".carousel ul li").length;
            var ol = $("<ol />");
            for (var i = len; i >= 1; i--) {
                var li = $("<li />");
                var a = $("<a />");
                a.addClass("carousel-index-" + i);
                a[0].href = "#";
                a.html(i);
                a.click(function () {
                    clearTimeout(lw.automateNews);
                    var $this = $(this);
                    if ($this.html() == $("._active").html())
                        return;
                    $("._active").removeClass("_active");
                    $this.addClass("_active");
                    $(".carousel").jcarousel("scroll", parseInt($this.html()));

                    return false;
                });
                li.click(function () {
                    clearTimeout(lw.automateNews);
                });
                if (i == 1)
                    a.addClass("_active");
                li.append(a);
                lw.newsButtons.push(a);
                ol.append(li);
            }
            $("#homenews .f").append(ol);
            return false;
        }
        $(".carousel").jcarousel({
            scroll: 1,
            initCallback: mycarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null,
            circular: true
        });
        lw.lastNewsButton = 1;
        function AutomateNews() {
            if (!lw.automateNews)
                return;
            lw.lastNewsButton++;
            if (lw.lastNewsButton == lw.newsButtons.length + 1)
                lw.lastNewsButton = 1;
            $(".carousel").jcarousel("scroll", lw.lastNewsButton);

            $("._active").removeClass("_active");
            $(".carousel-index-" + lw.lastNewsButton).addClass("_active");
            lw.automateNews = setTimeout(AutomateNews, 5000);
        }
        lw.automateNews = setTimeout(AutomateNews, 5000);
    }
      });



      /*

      highlight v3

      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>

      */

      jQuery.fn.highlight = function (pat) {
      	function innerHighlight(node, pat) {
      		var skip = 0;
      		if (node.nodeType == 3) {
      			var pos = node.data.toUpperCase().indexOf(pat);
      			if (pos >= 0) {
      				var spannode = document.createElement('span');
      				spannode.className = 'highlight';
      				var middlebit = node.splitText(pos);
      				var endbit = middlebit.splitText(pat.length);
      				var 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 += innerHighlight(node.childNodes[i], pat);
      			}
      		}
      		return skip;
      	}
      	return this.each(function () {
      		innerHighlight(this, pat.toUpperCase());
      	});
      };

      jQuery.fn.removeHighlight = function () {
      	return this.find("span.highlight").each(function () {
      		this.parentNode.firstChild.nodeName;
      		with (this.parentNode) {
      			replaceChild(this.firstChild, this);
      			normalize();
      		}
      	}).end();
      };
