/*  Vom Feenhaus Prototype & Scriptaculous Handler
 *  (c) 2006 Markus Kniebes <mk@kniebes.net>
 *
/*--------------------------------------------------------------------------*/

vfh={
	
	openNav:"",
	after:"eyes",
	insertion:"anav",
	url:'/textpattern/ajaxhandler.php',
	debugon:false,
	openlinkpanel:"",
	openlinkpanelh:"",
	
	AxInsert:function( what ) {
		done = false; 
		pars = 'fn='+what;
		if( vfh.openNav != "" ) {
			Effect.BlindUp( vfh.insertion, {queue: 'end'} );
			Element.remove( vfh.insertion );
			if( vfh.openNav == what ) { 
				vfh.openNav = ""; 
				done = true;
			}
		}			
		if( what != vfh.openNav && done == false ) {
			vfh.openNav = what; 	
			vfh.debug( "ajaxrequest: "+vfh.url );
			$('activityindicator').setAttribute('class', 'active');
			new Ajax.Request(
				vfh.url, 
				{method: 'get', parameters: pars, onComplete: vfh.AxFill}
			);			
			vfh.debug( "ajaxrequest fertig " );
		} else {	
//			Effect.BlindDown('eyes');
		}
	},
	
	AxFill:function( originalRequest ) {
		cnt = '<div id="' + vfh.insertion + '" style="display:none";>'
			+ originalRequest.responseText
			+ '</div>';
		new Insertion.After( vfh.after, cnt );
		Effect.BlindDown( vfh.insertion, {queue:'end'} );
		if( vfh.openNav == 'search' ) {
			document.getElementById('q').setAttribute("autocomplete","off");
			document.getElementById('q').focus();
		}		
		$('activityindicator').setAttribute('class', 'inactive');
//		Effect.BlindUp('eyes');
	},
	
	AxError:function() {
		alert( 'AxInsert failed' );
	},
	
	CpInsert:function( what ) {
		done = false; 		
		if( vfh.openNav == what ) {
			Element.remove( vfh.insertion );
			done = true; 
			vfh.openNav = '';
		}
		if( vfh.openNav != "" && what != vfh.openNav ) {
			Element.remove( vfh.insertion );
		}
		if( what != vfh.openNav && done == false ) {
			cnt = '<div id="' + vfh.insertion + '" style="display:none";>'
				+ $(what).innerHTML
				+ '</div>';
			new Insertion.After( after, cnt );
			Effect.BlindDown( vfh.insertion, {duration:0.2} );
			vfh.openNav = what;
		}
	}, 

	Email:function()	{
		if( $F('realname') != "" && $F('ebody') != "" && $F('emailf') != "" ) {
			var url  = 'http://vom-feenhaus.de/textpattern/kontaktantw.php';
			var f = document.forms['mailform'];	
			$('activityindicator').setAttribute('class', 'active');
			var myAjax = new Ajax.Request(
				url, 
				{method: 'post', postBody:Form.serialize( f ), onComplete: vfh.contactResponse}
				);
			
		} else {
			window.alert( "Name, E-Mailadresse und ein Nachricht sollten schon sein..." ); 
		}
	},

	contactResponse:function(originalRequest) {
		if( originalRequest.responseText != "" ) {
			$('mailresponsefield').innerHTML = originalRequest.responseText;
		}
		$('ebody').value = '';
		$('subjectf').value = '';
		$('ebody').value = '';
		$('activityindicator').setAttribute('class', 'inactive');
	},

	doSearch:function() {
		val = $F('q'); 
		pars = 'fn=livesearch&q='+escape( val );
		$('activityindicator').setAttribute('class', 'active');
		var myAjax = new Ajax.Request( vfh.url, {method: 'get', parameters: pars, onComplete: vfh.showSearchresults} );	
	},

	showSearchresults:function( originalRequest ) {
		$('searchresults').innerHTML = originalRequest.responseText;
		$('activityindicator').setAttribute('class', 'inactive');
	},

	pencode:function( input ) {
		return escape( input.replace( /\+/, "{pluszeichen}") );
	},
	
	togglelinkpanel:function( id, hid ) {
		if( vfh.openlinkpanel != id && vfh.openlinkpanel != "" ) {
			Effect.BlindUp( vfh.openlinkpanel, {queue: 'end'} );
			$(vfh.openlinkpanelh).setAttribute('class','closedlinkpanel');
			vfh.openlinkpanel = "";
			vfh.openlinkpanelh = "";
		}
		if( vfh.openlinkpanel == "" ) {
			Effect.BlindDown( id, {queue: 'end'} );
			$(hid).setAttribute('class','openlinkpanel');
			vfh.openlinkpanel = id;
			vfh.openlinkpanelh = hid;
		}
	},			
	
	debug:function( msg ) {
		if( vfh.debugon == true ) {
			if(window.console) {
				window.console.log( msg );
			} 
			else {
				window.alert( msg );
			}
		}
		return true;
	}
}

