	function addEvent( obj, type, fn )
	{
	   if (obj.addEventListener) {
		  obj.addEventListener( type, fn, false );
	   } else if (obj.attachEvent) {
		  obj["e"+type+fn] = fn;
		  obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		  obj.attachEvent( "on"+type, obj[type+fn] );
	   }
	}

	function removeEvent( obj, type, fn )
	{
	   if (obj.removeEventListener) {
		  obj.removeEventListener( type, fn, false );
	   } else if (obj.detachEvent) {
		  obj.detachEvent( "on"+type, obj[type+fn] );
		  obj[type+fn] = null;
		  obj["e"+type+fn] = null;
	   }
	}

	function rollOver() {
		this.src	= '/img/header/' + this.id.substr( 4 ) + '_.gif';
	}

	function rollOut() {
		this.src	= '/img/header/' + this.id.substr( 4 ) + '.gif';
	}

	function init() {
		var imgs = document.getElementsByTagName( 'img' );

		for ( var i=0; i<imgs.length; i++ ) {
			var img = imgs[ i ];
			if ( img.id.substr( 0, 4 ) == 'roi_' ) {
				addEvent( img, 'mouseover', rollOver, true );
				addEvent( img, 'mouseout', rollOut, true );
			}
		}

		var sidebarIndexContainer = document.getElementById( 'sidebar_index_container' );

		if ( sidebarIndexContainer )
			rebuildSidebarIndex( sidebarIndexContainer );

		var smartFilter = document.getElementById( "smartFilter" );
		if ( smartFilter ) {
			smartFilter.readOnly = false;
			smartFilter.disabled = false;
		}
	}

function openPhoto( id ) {
	var photoWindow = window.open( '/image.php?id=' + id, 'bandphoto', 'WIDTH=400,HEIGHT=400' );
}

function addDynTableRow( sourceRow ) {
	var ftr = document.getElementById( sourceRow );

	if ( ftr ) {
		var ntr = ftr.cleanSample.cloneNode( true );

		var pos = ftr.parentNode.childNodes.length;

		for ( var i=0; i<ntr.childNodes.length; i++ ) {
			for (var j=0; j<ntr.childNodes[i].childNodes.length; j++ ) {


				if ( ntr.childNodes[i].childNodes[j].getAttribute ) {
					var nname = ntr.childNodes[i].childNodes[j].getAttribute( 'name' );
					ntr.childNodes[i].childNodes[j].setAttribute( 'name', nname.replace( /\[0\]/g, '[' + pos + ']' ) );
				}
				else if ( ntr.childNodes[i].childNodes[j].nodeValue ) {
					var nodeVal = ntr.childNodes[i].childNodes[j].nodeValue;
					ntr.childNodes[i].childNodes[j].nodeValue = nodeVal.replace( /\[0\]/g, '[' + pos + ']' );
				}
			}

		}

		ftr.parentNode.appendChild( ntr );

		return ntr;
	}
	else
		return false;
}

function initFeedUsBand() {
	saveClearSample( 'bandMembers' );
	saveClearSample( 'bandContacts' );
	saveClearSample( 'bandRecords' );
}

function initFeedUsRecord() {
	saveClearSample( 'recordTracks' );
	saveClearSample( 'recordFunctions' );
	saveClearSample( 'recordContacts' );
}

function initFeedUsPerson() {
	saveClearSample( 'personBands' );
	saveClearSample( 'personAddress' );
	saveClearSample( 'personContacts' );
}

function initFeedUsCompany() {
	saveClearSample( 'companyAddress' );
	saveClearSample( 'companyContacts' );
	saveClearSample( 'companyPersons' );
}


function saveClearSample( sourceRow ) {
	var ftr = document.getElementById( sourceRow );
	
	if ( ftr ) {
		ftr.cleanSample = ftr.cloneNode( true );
		ftr.cleanSample.setAttribute( 'id', '' );
		return ftr.cleanSample;
	}
	else
		return false;
}

function formatFilter( filter ) {
	var f = filter.toLowerCase();
	f.replace( ',', ' ' );

	while ( f.indexOf( '  ' ) != -1 )
		f = f.replace( '  ', ' ' );

	return f.split( ' ' );
}

function smartFilterOutput( output, text ) {
	var o = document.getElementById( output );

	if ( o ) {
		o.innerHTML = text;
		o.enabled	= true;
	}
	else {
		alert( 'Der Index wurde noch nicht vollständig geladen. Bitte haben Sie noch etwas Geduld.' );
	}
}

function updateSmartFilter( list, filter, output, linkFix ) {
	if ( filter.length < 1 || filter == '' ) {
		smartFilterOutput( output, 'Der SmartFilter benötigt zumindest ein Zeichen.' );
		return false;
	}
	else
		filter = formatFilter( filter );

	var tmp = new Array();

	var tooMany = false;
	var count	= 0;

	for ( var i in list ) {
		if ( count >= 100 ) {
			tooMany = true;
			break;
		}

		var found = 0;
		for ( var j = 0; j < filter.length; j++ )
			if ( list[i].toLowerCase().indexOf( filter[j] ) != -1 )
				found += 1;
				

		if ( found == filter.length ) {
			count += 1;
			tmp.push( '<a href="/' + linkFix + '/' + i + '">' + list[i] + '</a>' );
		}
	}

	if ( !tooMany )
		smartFilterOutput( output, 'Results found for "' + filter + '": ' + tmp.length + '<br/>' + tmp.join( ', ' ) );
	else
		smartFilterOutput( output, 'Mehr als 100 Resultate gefunden: <br/>' + tmp.join( ', ' ) );


	return true;
}

function iHaveNotesForThisPage() {
	var pageTitle	= document.title;
	var pageURL		= document.URL;
	var wAnnotate	= window.open( '/annotate.php?title=' + encodeURI( pageTitle ) + '&url=' + encodeURI( pageURL ), '', 'WIDTH=400,HEIGHT=500,SCROLL=NONE' );
}

var itemsPerPage = 20;

function rebuildSidebarIndex( sidebarIndexContainer ) {
	var sidebarIndex	= document.getElementById( 'sidebar_index' );
	var childNodes		= sidebarIndex.getElementsByTagName( 'li' );
	var entries			= new Array();
	var h1s				= document.getElementsByTagName( 'h1' );

	if ( h1s[0] && h1s[0].id ) {
		// 0 = type (band/person/...), 1 = id
		var currentIndex = h1s[0].id.split( '_' );
	}
	else
		var currentIndex = new Array( 0, 0 );

	for ( var i=0; i<childNodes.length; i++ ) {
		var childNode = childNodes[i];
		var a = childNode.firstChild;
		var link = a.href;
		var text = a.innerHTML;

		var parts	= link.split( '/' );
		var type	= parts[ parts.length-2 ];
		var id		= parts[ parts.length-1 ];

		if ( type == currentIndex[0] && id == currentIndex[1] ) {
			sidebarIndexContainer.index = Math.floor( i / itemsPerPage ) * itemsPerPage;
			sidebarIndexContainer.markID = currentIndex[1];
		}

		var entry = new Object();
		entry.link	= link;
		entry.text	= text;
		entry.id	= id;

		entries.push( entry );
	}

	sidebarIndexContainer.entries	= entries;
	
	if ( !sidebarIndexContainer.index )
		sidebarIndexContainer.index		= 0;

	redrawSidebarIndex();
}

function redrawSidebarIndex() {
	var container = document.getElementById( 'sidebar_index_container' );
	var entries = container.entries;
	var index	= container.index;
	var toIndex	= Math.min( entries.length, index+itemsPerPage );
	var out		= '';

	// prepare navigation
	if ( index > 0 )
		var prevIndex = '<a href="javascript:sidebarFirst();">first</a> | <a href="javascript:sidebarPrev();">previous</a> ';
	if ( index + itemsPerPage < entries.length )
		var nextIndex = '<a href="javascript:sidebarNext();">next</a> | <a href="javascript:sidebarLast();">last</a>';

	if ( prevIndex || nextIndex ) {
		var navigation = '<table style="width:100%;"><tr>'

		if ( prevIndex )
			navigation += '<td>' + prevIndex + '</td>';

		if ( prevIndex || nextIndex ) {
			navigation += '<td style="text-align:center;"><a href="javascript:sidebarAll();">all</a></td>';
		}

		if ( nextIndex ) 
			navigation += '<td style="text-align:right;">' + nextIndex + '</td>';
	
		navigation += '</tr></table>';
	}
	else
		var navigation = '';


	out += '<div style="margin-bottom:8px;">displaying results ' + (index+1) + '-' + (toIndex) + '</div>';

	out += navigation;

	out += '<table style="margin:8px 0px 8px 0px;">';

	for ( var i=index; i < toIndex; i++) {
		var entry = entries[i];
		if ( entry.id == container.markID )
			out += '<tr><td style="text-align:right;font-weight:bold;">' + (i+1) + '</td><td style="font-weight:bold;"><a href="' + entry.link + '">' + entry.text + '</a></td></tr>';
		else
			out += '<tr><td style="text-align:right;">' + (i+1) + '</td><td><a href="' + entry.link + '">' + entry.text + '</a></td></tr>';
	}

	out += '</table>';
	
	out += navigation;

	container.innerHTML = out;
}

function sidebarNext() {
	var container = document.getElementById( 'sidebar_index_container' );
	var i = container.index;
	i = Math.min( i+itemsPerPage, container.entries.length-1 );

	container.index = i;

	redrawSidebarIndex();
}

function sidebarPrev() {
	var container = document.getElementById( 'sidebar_index_container' );
	var i = container.index;
	i = Math.max( i-itemsPerPage, 0 );

	container.index = i;

	redrawSidebarIndex();
}

function sidebarFirst() {
	var container = document.getElementById( 'sidebar_index_container' );
	container.index = 0;
	redrawSidebarIndex();
}

function sidebarLast() {
	var container = document.getElementById( 'sidebar_index_container' );
	var i = container.index;
	i = container.entries.length - itemsPerPage;

	container.index = i;

	redrawSidebarIndex();
}

function sidebarAll() {
	var container = document.getElementById( 'sidebar_index_container' );
	container.index = 0;
	itemsPerPage = container.entries.length;

	redrawSidebarIndex();
}
