//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/string/highlight [rev. #1]

String.prototype.highlight = function(f, c, i){
    var r = this, t = /([(){}|*+?.,^$\[\]\\])/g, i = !i ? "i" : "", rf = function(t, i){
        return r.lastIndexOf("<", i) > r.lastIndexOf(">", i) ? t : c(t, p);
    };
    for(var p = -1, l = (f = f instanceof Array ? f : [f]).length; ++p < l;)
        r = r.replace(new RegExp(f[p].replace(t, "\\\$1"), "gm" + i), rf);
    return r;
}
function c(s){
    return '<span class="highlight">' + s + '</span>';
}

Ajax.Autocompleter.addMethods({
	startIndicator: function() {
		this.element.addClassName('active');
	},
	stopIndicator: function() {
		this.element.removeClassName('active');
	},
	updateElement: function(selectedElement) {
    	if (this.options.updateElement) {
    		this.options.updateElement(selectedElement);
      		return;
    	}
    	var value = '';
    	if (this.options.select) {
      		var nodes = $(selectedElement).select('.' + this.options.select) || [];
      		if(nodes.length>0) value = Element.collectTextNodes(nodes[0], this.options.select);
    	} else
      		value = Element.collectTextNodesIgnoreClass(selectedElement, 'informal');

    	var bounds = this.getTokenBounds();
    	if (bounds[0] != -1) {
      		var newValue = this.element.value.substr(0, bounds[0]);
      		var whitespace = this.element.value.substr(bounds[0]).match(/^\s+/);
      		if (whitespace)
        		newValue += whitespace[0];
	  		if(newValue + value + this.element.value.substr(bounds[1])!='')
	      		this.element.value = newValue + value + this.element.value.substr(bounds[1]);
    	} else {
      		this.element.value = value;
	    }
    	this.oldElementValue = this.element.value;
    	this.element.focus();

    	if (this.options.afterUpdateElement)
      		this.options.afterUpdateElement(this.element, selectedElement);
  }});
var SCMS = Class.create({

	LOCATION: (document.URL.indexOf('www') > -1 ? 'http://www.schuetzen-oeventrop.de/' : 'http://schuetzen-oeventrop.de/'),

	initialize: function(){
		this.query=document.URL.toQueryParams();
		this.hlString = this.query.hl;
		this.blendBlocks = $A($$('.BlendBlock'));
		this.registerBlendBlocks();
		if (this.hlString) this.highlight();
		if ($('searchfield')){
			this.createLiveSearchBox();
		}
	},
	createLiveSearchBox: function(){
		var s = $('searchfield');
		var fill = new Element('div',{id:'fill'});
		s.parentNode.insertBefore(fill,s.nextSibling);
		new Ajax.Autocompleter('searchfield','fill',this.LOCATION+'modules/Suche/livesearch.php',{minChars:3,indicator:'livesearchloader',parameters:'ref='+document.URL});
	},
	highlight: function(){
		$('content').innerHTML = $('content').innerHTML.highlight(this.hlString,c);
		var hlSwitch = new Element('div',{id:'hlswitchoff',style:'float:right;cursor:pointer;margin:1em;background:#ffe;padding:.5em;border:solid black thin'}).update('Hervorhebung ausschalten');
		hlSwitch.observe('click',function () {
			var hls = $A($$('.highlight'));
			for(var c=0,l=hls.length; c<l ; ++c){
				hls[c].replace(hls[c].innerHTML);		
			};
			$('hlswitchoff').remove();
		});
		$('content').insertBefore(hlSwitch,$('content').firstChild);
	},
	registerBlendBlocks: function(){
		for ( var c = 0, l =this.blendBlocks.length; c < l; ++c){
			if (this.blendBlocks[c].tagName === 'FIELDSET')
				this.registerBlendFS(this.blendBlocks[c]);
			else {
				var id = this.blendBlocks[c].id;
				this.toSwitch($(id+'Switch'));
				$(id+'Switch').observe('click',this.toggleView.bindAsEventListener(this,$(id+'Switch'),$(id+'Wrap')));		
			}
		}
	},
	toSwitch: function (elem){
		var col = elem.style.backgroundColor;
		elem.style.paddingLeft='15px';
		elem.style.background = "url('"+this.LOCATION+"templates/green/gr/expanded.png') 5px 7px no-repeat "+col;
		elem.title = 'Ein-/Ausblenden';
	},
	registerBlendFS: function(fs){
		var leg = $A(fs.getElementsByTagName('LEGEND'))[0];
		this.toSwitch(leg);
		var wr, c = 0;
		if($(fs.id+'Wrap')) wr = $(fs.id+'Wrap');
		else {
			wr = new Element('div',{id:fs.id+'Wrap'});
			while (fs.childNodes[c]){
				if (fs.childNodes[c] == leg) ++c;
				else wr.appendChild(fs.childNodes[c]);
			}
			fs.appendChild(wr);
		}
		leg.observe('click',this.toggleView.bindAsEventListener(this,leg,wr));
	},
	toggleView : function(event, sw,wr){
		if(wr.style.display=='none'){
    		Effect.BlindDown(wr.id);
    		sw.style.backgroundImage="url('"+this.LOCATION+"templates/green/gr/expanded.png')";
  	  	} else {
    		Effect.BlindUp(wr.id);
    		sw.style.backgroundImage="url('"+this.LOCATION+"templates/green/gr/collapsed.png')";
  		}
  	}
});



foo = new SCMS();

