var ScrollLinks = {
        currentHash: false,
        start: function(){
        this.scroll = new fx.Scroll({duration: 500, onComplete: function(){ScrollLinks.end();}});
        this.allinks = document.getElementsByTagName('a');
        for (i=0; i<this.allinks.length; i++){
            var lnk = this.allinks[i];
            if ((lnk.href && lnk.href.indexOf('#') != -1) && ( (lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {
                lnk.onclick = function(){
                    ScrollLinks.scroll.clearTimer();
                    this.initialHref = this.href;
                    this.initialHash = this.hash;
                    this.href = "javascript:void(0)";
                    setTimeout(function(){this.href = this.initialHref;}.bind(this), 200);
                            ScrollLinks.click(this);
                }
            }
        }
    },

    click: function(link){
        this.currentHash = link.initialHash.substr(1);
        if (this.currentHash) {
            for (j=0; j<this.allinks.length; j++){
                if (this.allinks[j].id == this.currentHash){
                    if (!window.opera) this.scroll.scrollTo(this.allinks[j]);
                    else this.scroll.scrollTo(this.allinks[j].parentNode);
                    break;
                }
            }
        }
    },

    end: function(){
        window.location.href = "#"+this.currentHash;
        this.currentHash = false;
    }
}

function startScroll() { ScrollLinks.start(); }


function activateSearch() {
if ($('searchform')) {
 $('s').value = 'Search...';
 $('searchform').onsubmit = function() { doSearch();return false; };
 $('s').onfocus = focusS;
 $('s').onblur = blurS;
 var s = $('search-results');
 var f = $('searchform');
 is_searching = false;
 $('search-results').innerHTML = '';
 }
}

function init(){
var myMenu = document.getElementsByClassName('menu');
var mySelector = document.getElementsByClassName('selector');
var menuEffect = new fx.Accordion(mySelector, myMenu, {opacity: true, duration: 300});
resultsHeight = new fx.Combo('search-results', {duration: 800});
menuEffect.showThisHideOpen = function(toShow){
 this.elements.each(function(el, i){
 if (el.offsetHeight > 0 && el != toShow) this.clearAndToggle(el);
 }.bind(this));
 setTimeout(function(){this.clearAndToggle(toShow)  ;}.bind(this), this.options.delay);
 };
activateSearch();
resultsHeight.toggle();
}

function doOpacity() {
resultsOpacity = new fx.Opacity('search-results', {duration: 800});
}

function doSearch() {
if (is_searching) return false;
s = $F('s');
if (s == '' || s == 'Live Search...') return false;
$('loader').innerHTML = "<img src=\"http://riding-shotgun.de/themes/Citrus/images/ajax-loader.gif\" alt=\"Loading...\" />";
is_searching = true;
pars = 's=' + escape(s) + '&ajax';

var myAjax = new Ajax.Request('http://riding-shotgun.de', {method: 'get', parameters: pars, onComplete:doSearchResponse});
}

function doSearchResponse(response) {
$('search-results').innerHTML = response.responseText;
resetForm();
$('loader').innerHTML = "";
resultsHeight.toggle();
$('s').blur();
}

function resetForm() { is_searching = false; }

function clearResults() {
resultsHeight.toggle();
setTimeout("$('search-results').innerHTML = '';", 800);
$('s').value = 'Search...';
}

function focusS() {
if ($F('s') == 'Search...') {
 $('s').value = '';
 }
else {
 $('s').value = '';
 resultsHeight.toggle();
 }
}

function blurS() {
if ($F('s') == '') {
 $('s').value = 'Search...';
 }
}