
oRcsf.RegisterObject('Module_Guestbook_Guestbook');
var RCSF_Project_Module_Guestbook_Guestbook =
{
	msExtends : 'RCSF_Base_Form'
	
	,OnDomLoaded : function()
	{
		var o_contact_form = $('btn_submit');
		o_contact_form.observe('click', this.SubmitForm.bind(this, $('guestbookform')));
		
		$('btn_guestbook_up').observe('click',this.ShowMoreGuestbook.bind(this,'up'));
		$('btn_guestbook_down').observe('click',this.ShowMoreGuestbook.bind(this,'down'));
		
		this.ShowGuestbook('gastenboek_entries');
	}
	
	,ShowMoreGuestbook : function(UpDown)
	{
		switch(UpDown)
		{
			case 'up':
				if(parseInt($('entries_wrapper').getStyle('top')) < 0)
					new Effect.Move('entries_wrapper', { x: 0, y: 34,duration : 0.4});
				break;
			case 'down':
				
				new Effect.Move('entries_wrapper', { x: 0, y: -34, duration : 0.4});
				break;
		}
	}
	
	/**
	 * Submits the form
	 */
	,SubmitForm : function(oForm,event)
	{
		event.stop();
		this.HideFieldErrors(oForm);
		
		this.moModule.moCore.RequestServer(
			this.GetFormData(oForm)
			,{
				s_mode			: 'data'
				,s_module		: 'guestbook'
				,s_controller	: 'guestbook'
				,s_action		: 'sign'
				,o_scope		: this
				,s_callback		: 'OnGuestbookSigned'
			}
		);
		
		return false;
	}
	
	/**
	 * Handles form submit result
	 */
	,OnGuestbookSigned : function(oData)
	{
		if (oData.s_status == 'done')
		{
			this.ShowGuestbook('gastenboek_success');
			pageTracker._trackEvent('Formulier','Gastenboek ingevuld');
		}
		else
			this.ShowFieldErrors(oData);
	}
	
	/**
	 * Shows error messages
	 * 
	 * @param	Object	oData	return object with errors
	 */
	,ShowFieldErrors : 	function(oData)
	{
		this.HideFieldErrors(oData);
		//var o_error_msg = $$('#' + oData.s_form_id + ' .msg').first();
		//if(o_error_msg) o_error_msg.update(oData.s_message);		
		for (var s in oData.a_errors)
		{	
			$(s).addClassName('error');;
			$('label-' + s).addClassName('error');;
		}
	}
	
	/**
	 * Hides all error messages
	 * 
	 * @param	Object	oData	[id] 
	 * 							[s_form_id]
	 * 					
	 */
	,HideFieldErrors : function(oData)
	{
		var s_id = (!Object.isUndefined(oData.id)) ? oData.id : oData.s_form_id;
		$$('#' + s_id + ' .error').invoke('removeClassName','error')
	}
	
	,ShowGuestbook : function(sPage)
	{
		var b_skip = false;
		$$('.guestbook_element').each(function(oEl){
			if(oEl.getHeight() > 30 && oEl.style.display != 'none')
			{
				if(oEl.id == sPage)
					b_skip = true;
				// else
				this.HideElement(oEl);
			}
		}.bind(this))
		
		if (!b_skip) {
			if ($(sPage).style.display != 'block') {
				this.ShowElement($(sPage));
			}
		}
	}
	
	,ShowElement : function(oEl)
	{
		switch($(oEl).id) 
		{
			case 'gastenboek_entries' :
				$('guestbook_title').update('Heb je leuke berichten die je met ons wilt delen?');
				$('btn_showguestbookform').update('post ze hier!');
				Cufon.refresh();
				$('btn_showguestbookform').onclick = function(){
					oRcsf.moModules.moGuestbook.moGuestbook.ShowGuestbook('gastenboek_entry')
				}
			break;
			case 'gastenboek_entry' :
				$('s_entry').clear();
				$('guestbook_title').update('Laat hieronder jouw bericht achter en je naam.');
				$('btn_showguestbookform').update('klik hier voor alle post');
				Cufon.refresh();
				$('btn_showguestbookform').onclick = function(){
					oRcsf.moModules.moGuestbook.moGuestbook.ShowGuestbook('gastenboek_entries')
				}
				break;
			case 'gastenboek_success' :
		}
		
		/*
		$(oEl).morph('height:'+oEl.getHeight() + 'px',{
			 duration 		: 2
		});
		*/
		Effect.BlindDown(oEl.id,{queue : 'end',duration : 0.5})
	}
	
	,HideElement : function(oEl)
	{
		Effect.BlindUp(oEl.id,{queue : 'end',duration : 0.5})
	}
}
