﻿/*
 * Search functionality
 */

function searchComplete(searcher) {
    var contentDiv = $('#searchresult')[0];

    if (contentDiv.hasChildNodes()) {
        while (contentDiv.childNodes.length >= 1) {
            contentDiv.removeChild(contentDiv.firstChild);
        }
    }

    if (searcher.results && searcher.results.length > 0) {
        if (gotoFirstResult) {
            window.location = searcher.results[0].unescapedUrl;
            gotoFirstResult = false;
        }
        
        var ul = document.createElement("ul");
        for (var i = 0; i < searcher.results.length && i < 10; i++) {
            var li = document.createElement("li");
            var result = searcher.results[i];

            var link = document.createElement('a');
            link.href = result.unescapedUrl;
            link.innerHTML = result.titleNoFormatting.replace(/ - .*/g, "");
            link.title = result.content.replace(/<[^<>]*>/g, "").replace(/&[^&;]*;/g, "");
            li.appendChild(link);
            ul.appendChild(li)
        }
        contentDiv.appendChild(ul);
    }
    else {
        contentDiv.innerHTML = "<span>No results!</span>"
    }

    
}

function Search() {
    var query = $('#query')[0];
    
    var webSearch = new google.search.WebSearch();
    webSearch.setSearchCompleteCallback(this, searchComplete, [webSearch]);
    webSearch.setSiteRestriction("EpiWiki.se");
    webSearch.setResultSetSize(GSearch.LARGE_RESULTSET); 
    webSearch.execute(query.value);
}

var hasStartingSearch = false;
var gotoFirstResult = false;
function OnLoad() {
    var searchDiv = $('#searchcontrol')[0];
    searchDiv.style.display = "block";

    $('#query').keydown(function(event) {
        if (!hasStartingSearch) {
            $('#query')[0].style.color = '#3A3733';
            $('#query').attr("value", "");
        }
        hasStartingSearch = true;
        if (event.keyCode == 13) {
            gotoFirstResult = true;
            return false;
        }
    });
    
    $('#query').keyup(function(event) {
        if (event.keyCode == 27) {
            $('#query').attr("value", "");
            return false;
        }

        Search();
    });

    if (window.location.href.indexOf("Edit") < 0) {
        $('#query')[0].focus();
    }
    else {
        $('textarea')[0].focus();
    }

    var email = $("#email");
    if (email != '') {
        var title = "mattias[dot]lovstrom[at]sverige[dot]nu";
        title = title.replace("[at]", "@").replace(/\[dot\]/g, ".");
        email.attr('title', title);
        email.attr('href', "mailto:" + title);
    }
}


/*
* Droppy 0.1.2
* (c) 2008 Jason Frame (jason@onehackoranother.com)
*/
$.fn.droppy = function(options) {

    options = $.extend({ speed: 250 }, options || {});

    this.each(function() {

        var root = this, zIndex = 1000;

        function getSubnav(ele) {
            if (ele.nodeName.toLowerCase() == 'li') {
                var subnav = $('> ul', ele);
                return subnav.length ? subnav[0] : null;
            } else {
                return ele;
            }
        }

        function getActuator(ele) {
            if (ele.nodeName.toLowerCase() == 'ul') {
                return $(ele).parents('li')[0];
            } else {
                return ele;
            }
        }

        function hide() {
            var subnav = getSubnav(this);
            if (!subnav) return;
            $.data(subnav, 'cancelHide', false);
            setTimeout(function() {
                if (!$.data(subnav, 'cancelHide')) {
                    $(subnav).slideUp(options.speed);
                }
            }, 500);
        }

        function show() {
            var subnav = getSubnav(this);
            if (!subnav) return;
            $.data(subnav, 'cancelHide', true);
            $(subnav).css({ zIndex: zIndex++ }).slideDown(options.speed);
            if (this.nodeName.toLowerCase() == 'ul') {
                var li = getActuator(this);
                $(li).addClass('hover');
                $('> a', li).addClass('hover');
            }
        }

        $('ul, li', this).hover(show, hide);
        $('li', this).hover(
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
    );

    });

};


/*SitemapLink*/
$(document).ready(function() {

    $('.PopUpTrigger').mouseenter(
        function(event) {
            var o = $(this).offset();
            var h = $(this).css("height");
            var popup = $(this).next('div');
            popup.removeClass("Hide");
            popup.css("top", o.top - 10);
            popup.css("left", o.left + 30);

            $(".PopUpTriggerLayer").each(function() {
                $(this).css("top", o.top);
                $(this).css("left", o.left);
                $(this).css("height", h);
                $(this).css("width", 200);
                $(this).removeClass("Hide");
            });

            $(".FadeLayer").each(function() {
                $(this).removeClass("Hide");
                $(this).css("height", "2000px");
            });
        });

    $('.FadeLayer').mouseenter(
        function(event) {
            $(".PopUpTriggerLayer,.PopUp,.FadeLayer").each(function() {
                $(this).addClass("Hide");
            });
        });
});


