jQuery.fn.blaetterbox = function(options) {
	
	if( options == 'next' || options == 'prev')
	{
		return this.each(function() {
			var $this = jQuery(this);
			var li = $this.find('li:visible')[0];
			
			if( li != null && li != undefined )
			{
				var current = getPageNum( li.className );
				var next = current;
				if( options == 'prev' ) { next--; } else { next++; }
				if( hasPage( $this, next ) )
				{
					$this.find('li.page' + current).hide();
					$this.find('li.page' + next).show();
				}
			}
			$this.blaetterbox('updateButtons');
		});
	}
	else if( options == 'aktuell')
	{
		return this.each(function() {
			var $this = jQuery(this);
			var li = $this.find('li.aktuell')[0];
			
			if( li != null && li != undefined )
			{
				var goto = getPageNum( li.className );
				if( hasPage( $this, goto ) )
				{
					$this.find('li:not(.page' + goto + ')').hide();
					$this.find('li.page' + goto).show();
				}
			}
			$this.blaetterbox('updateButtons');
		});
	}
	else if( options == 'initButtons' )
	{
		var $this = jQuery( this );
		var left = $this.find('.left')[0];
		var right = $this.find('.right')[0];
		$this.blaetterbox('updateButtons');
		if(left != null)
		{
			jQuery(left).show();
		}
		if(right != null)
		{
			jQuery(right).show();
		}
	}
	else if( options == 'updateButtons' )
	{
		var $this = jQuery( this );
		var left = $this.find('.left')[0];
		var right = $this.find('.right')[0];
		var li = $this.find('li:visible')[0];
			
		if( li != null && li != undefined )
		{
			var current = getPageNum( li.className );
			var next = current+1;
			var prev = current-1;
			var $right = jQuery( right );
			var $left = jQuery( left );
			if( right != null )
			{
				if( hasPage( $this, next ) )
				{
					$right.removeClass('deactive');
				} else {
					$right.addClass('deactive');
				}
			}
			
			if( left != null )
			{
				if( hasPage( $this, prev ) )
				{
					$left.removeClass('deactive');
				} else {
					$left.addClass('deactive');
				}
			}
		}
	}
	else {
		var defaults = {};
		var opts = jQuery.extend(defaults, options);
	
		return this.each(function() {
			var $this = jQuery(this);
			$this.data( 'opts', opts );
			$this.find('li:not(.page0)').hide();
			$this.blaetterbox('aktuell');
			$this.blaetterbox('initButtons');
		});
	}
			
}

function getPageNum( className )
{
	var regexp = /page([0-9]*)/;
	if( className == undefined ) { return false; }
	var result = className.match( regexp );
	if( result )
	{
		return Number(result[1]);
	}
	return false;
}
	
function hasPage( obj, pageNum )
{
	if( pageNum < 0) { return false; }
	var lis = obj.find('li.page' + pageNum);
	return (lis.length > 0);
}

function chooseHighlight(event, data, formatted)
{
	if( data != null ) { 
	
		if( data.href != null && data.href != "")
		{
			location.href = data.href;
		}
	}
}

jQuery().ready(function() {		
	jQuery("#quicksearch").autocomplete(data, {
										 max: 50,
										 minChars: 1,
										 matchContains: true,
										 formatItem: function(row) {
											return '<span class="icon ' + row.target + '"></span>' + row.title;
										 },
										 formatResult: function(row) {
											return row.title;
										 },
										 formatMatch: function(row) {
											return row.title + ' ' + row.alttitle;
										 }
										}).result( chooseHighlight );;
});