///////////////////////////////////////////////////////////
// ACTIE.js
///////////////////////////////////////////////////////////


/////////////////////////////////////////////////
// show fireworks
/////////////////////////////////////////////////

function showFireworks(visitor){

	// show the actie div
	$('actie').addClassName('active');
	
	// create the 
	var fireworksFO = { movie:"/static/actie/swf/fireworks.swf", id:"fireworksSwf", width:"100%", height:"100%", wmode:"transparent", majorversion:"9", allowScriptAccess: "always", build:"0", xi:"true", ximovie:"/static/swf/actie/ufo.swf", flashvars:"visitorName="+visitor };
	UFO.create(fireworksFO, "actie");
	
	// put the simpleHTMLUpdater on hold
	window.actieWindowActive = true;
	
}


/////////////////////////////////////////////////
// hide fireworks
/////////////////////////////////////////////////

function hideFireworks(){
	
	// hide it
	$('actie').removeClassName('active');
	
	// put the simpleHTMLUpdater on hold
	window.actieWindowActive = false;
	
	// setup the feedback form
	setupFeedbackForm.delay(.1);

}

/////////////////////////////////////////////////
// setup feedback form
/////////////////////////////////////////////////

function setupFeedbackForm(){
	
	// empty the div
	$('actie').update('');
	
	// initiate homepage stuff
	initHomepage();
	
	// show the infomessage
	$('infoMessage').addClassName('active');
	
	$$('#feedbackFormWrapper, #feedbackThanks').invoke('hide');
	
	// step 1
	feedbackStep(1, true);
	
	// watch the click
	$('feedbackLink').observe('click', onFeedbackLinkClick);
	
	// watch the send of the form
	Event.observe('feedbackForm', 'submit', sendForm);
}


/////////////////////////////////////////////////
// steps in the process
/////////////////////////////////////////////////


function feedbackStep(n, isLandingPage){

	if(!$('closeLink')){
		$('infoMessage').insert('<a href="'+ window.logoutURL +'" title="Deze balk weghalen?" id="closeLink">X</a>', { position: 'bottom' })
		$('closeLink').observe('click', logoutVisitor);
	}

	switch(n){
		
		/////////////////////////////////////////////////
		case 1:
		
		// scroll to top
		new Effect.ScrollTo('infoMessage');
		
		
		if(isLandingPage){
			
			$$('body')[0].removeClassName('playingFireworks');
			
			// hide intro message
			$('infoMessage').hide();
			
			// show intro
			new Effect.BlindDown('infoMessage', { duration: 1.0 });
			
		}else{
			$('infoMessage').show();
		}
		
		
		// hide others
		$$('#feedbackFormWrapper, #feedbackThanks').invoke('hide');
		break;
		
		
		/////////////////////////////////////////////////
		case 2:

		// show feedbackForm
		new Effect.Appear('feedbackFormWrapper', { duration: 1.0 });
		
		// hide others
		$$('#feedbackIntro, #feedbackThanks').invoke('hide');
		break;
		
		
		/////////////////////////////////////////////////
		case 3:
		
		// show feedbackThanks
		new Effect.Appear('feedbackThanks', { duration: 1.0 });
		
		// hide others
		$$('#feedbackIntro, #feedbackFormWrapper').invoke('hide');
		break;
		
	}
	
}


/////////////////////////////////////////////////
// onFeedbackLinkClick
/////////////////////////////////////////////////

function onFeedbackLinkClick(e){
	Event.stop(e);
	feedbackStep(2);
}


/////////////////////////////////////////////////
// logoutVisitor
/////////////////////////////////////////////////

function logoutVisitor(e){

	Event.stop(e);
	
	new Ajax.Request(Event.element(e).readAttribute('href'),{
		onComplete: function(transport) {
			if(transport.responseText == 'ok'){
				new Effect.BlindUp('infoMessage', { duration: 1.0 });
			}
		}
	});
}


/////////////////////////////////////////////////
// sendForm
/////////////////////////////////////////////////

function sendForm(e){
	Event.stop(e);
	
	new Ajax.Request(window.postURL,{
			parameters: Event.element(e).serialize(),
			onComplete: function(transport) {
				$('feedbackThanks').update(transport.responseText);
				feedbackStep(3);
				
				// find the try-again link
				if($('again')){
					$('again').observe('click', onFeedbackLinkClick)
				}
		}
	});
}

///////////////////////////////////////////////////////////
// GENERIC FUNCTIONS
///////////////////////////////////////////////////////////
function onFieldFocus(evt){
	var e = Event.element(evt);
	if(!e.hasClassName('focus')){
		e.addClassName('focus');
	}
	if($F(e) == e.defaultValue){
		e.value = '';
	}
}

function onFieldBlur(evt){
	var e = Event.element(evt);
	if(e.hasClassName('focus')){
		e.removeClassName('focus');
	}
	if($F(e) == ''){
		e.value = e.defaultValue;
	}
}

/////////////////////////////////////////////////
// initIntroMessage
/////////////////////////////////////////////////

function initIntroMessage(){
	
	if($('infoMessage') || !$$('.playingFireworks')){
		
		// put the simpleHTMLUpdater on hold
		window.actieWindowActive = false;

		// show the infomessage
		$('infoMessage').addClassName('active');

		$$('#feedbackFormWrapper, #feedbackThanks').invoke('hide');

		// step 1
		feedbackStep(1, false);

		// watch the click
		$('feedbackLink').observe('click', onFeedbackLinkClick);
		
		$$('input, textarea').invoke('observe', 'focus', onFieldFocus);
		$$('input, textarea').invoke('observe', 'blur', onFieldBlur);

		// watch the send of the form
		Event.observe('feedbackForm', 'submit', sendForm);

	}

}


/////////////////////////////////////////////////
// on load
/////////////////////////////////////////////////

document.observe('dom:loaded', initIntroMessage);