/**
 * NVS_STAF
 * 
 * @author		Sebastiaan Smid <sebastiaan@netvlies.nl>
 * 
 * Optional parameters:
 * @category		jQuery Plugin
 * @package			Netvlies Album
 * @version			1.0
 * @requires		popup.js, overlay.js, jquery-easing.js
 *
 */
 
 (function($) {
	
	/**
	* Shows a send to a friend form on an overlay
	* Mixed object: 
	*		title: 					(string) 'Send to a friend',
	*		intro:					(string) 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In consequat leo vitae dui.<br/><br /> Velden met een * zijn verplicht.',
	*		sending:				(string) 'Uw e-mail wordt verstuurd',
	*		error:					(string) 'Er is iets mis gegaan tijdens het versturen',
	*		fromNameText:			(string) 'Your name',
	*		fromEmailText:			(string) 'Your e-mailaddress',
	*		toNameText:				(string) 'Friend name',
	*		toEmailText:			(string) 'Friend e-mailaddress',
	*		phpFormHandler:			(string) URL,
	*		language:				(string) 'nl' OR 'en',	
	*
	*		popUpBackground:		(string) css,
	*		popUpWidth:				(int),
	*		popUpHeight:			(int),
	*		popUpPadding:			(string) css,
	*		
	*		overlayBackground: 		(string) css,
	*		overlayTransparancy: 	(decimal) between 0 and 1,
	*		overlaySpeed: 			(int time) milliseconds
	*
	* @param 	Object	Mixed
	* 
	*/
		

	
	$.fn.lpxStaf = function(options) {
		var defaults = { 
			title: 					'Send to a friend',
			intro:					'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In consequat leo vitae dui.<br/><br /> Velden met een * zijn verplicht.',
			sending:				'Bezig met controleren van de velden...',
			error:					'Er kon geen verbinding worden gemaakt met de server probeer het nog eens.',
			fromNameText:			'Uw naam *',
			fromEmailText:			'Uw e-mail adres *',
			toNameText:				'Naam relatie *',
			toEmailText:			'E-mail adres relatie *',
			phpFormHandler:			'/ajax/staf.ajax.php',
			language:				'nl',
			btnSubmit:				'/img/frm_staf_btn_submit.png',
			btnClose:				'/img/frm_staf_btn_close.png',

			popUpBackground:		'url(/img/frm_staf_bg.png) no-repeat',
			popUpWidth:				179,
			popUpHeight:			382,
			popUpPadding:			'22px 17px 0 17px',
			
			overlayBackground: 		'#000',
			overlayTransparancy: 	0.5,
			overlaySpeed: 			250
		}
		
		
		var options = $.extend(defaults, options);
		var jQueryMatchedObj = this;
		
		function _initialize() {
			// remove focus from clicked element
			this.blur();
			_start();
			// stop following the anchor
			return false; 
		}
		
		function _start() { 
			// show the nvsOverlay
			$.nvsOverlay.show({ 
				background: options.overlayBackground, 
				transparancy: options.overlayTransparancy, 
				speed: options.overlaySpeed  
			});
	
			// show the nvsPopup
			$.nvsPopup.show({ 
				background: options.popUpBackground,
				width: options.popUpWidth,
				height: options.popUpHeight,
				padding: options.popUpPadding,
				fade: false,
				callback: function() { 
					_draw_nvsStaf(); 
				}
			}); 
		}
		
		function _draw_nvsStaf() {
			$('<form>').attr({ id: 'staf_frm' }).css({ 
					position: 'relative',
					textAlign: 'left'
				}).appendTo('#nvs_popup');
			
			// Titel
			$('<h2>').text(options.title).attr({ id: 'staf_frm_header' }).css({}).appendTo('#staf_frm');
			
			//NOT STANDARD
			if(_typeface_js) {
				_typeface_js.replaceText(document.getElementById('staf_frm_header'));
				$('#staf_frm_header').css({ visibility: 'visible' });
			}

			// introduction or explaination
			$('<div>').html(options.intro).attr({ id: 'staf_intro' }).appendTo('#staf_frm');
			
			$('<fieldset>').attr({ id: 'staf_fieldset' }).appendTo('#staf_frm');
			
			// Hidden input url
			$('<input>').attr({ type: 'hidden', id: 'staf_url', name: 'staf_url', value: document.location }).appendTo('#staf_fieldset');
			
			// Hidden input language
			$('<input>').attr({ type: 'hidden', id: 'staf_lang', name: 'staf_lang', value: options.language }).appendTo('#staf_fieldset');
				
			// Input From Name
			$('<label>').text(options.fromNameText).attr({ id: 'staf_label_from_name' }).appendTo('#staf_fieldset');
			$('<div>').attr('id','staf_div_frm_name').appendTo('#staf_fieldset');
			$('<input>').attr({ type: 'text', id: 'staf_from_name', name: 'staf_from_name' }).appendTo('#staf_div_frm_name');
			
			// Input From Email
			$('<label>').text(options.fromEmailText).attr({ id: 'staf_label_from_email' }).appendTo('#staf_fieldset');
			$('<div>').attr('id','staf_div_from_email').appendTo('#staf_fieldset');
			$('<input>').attr({ type: 'text', id: 'staf_from_email', name: 'staf_from_email' }).appendTo('#staf_div_from_email');
		
			// Input To Name
			$('<label>').text(options.toNameText).attr({ id: 'staf_label_to_name' }).appendTo('#staf_fieldset');
			$('<div>').attr('id','staf_div_to_name').appendTo('#staf_fieldset');
			$('<input>').attr({ type: 'text', id: 'staf_to_name', name: 'staf_to_name' }).appendTo('#staf_div_to_name');
			
			// Input To Email
			$('<label>').text(options.toEmailText).attr({ id: 'staf_label_to_email' }).appendTo('#staf_fieldset');
			$('<div>').attr('id','staf_div_to_email').appendTo('#staf_fieldset');
			$('<input>').attr({ type: 'text', id: 'staf_to_email', name: 'staf_to_email' }).appendTo('#staf_div_to_email');
			
			// Buttons submit and close
			$('<input>').attr({ type: 'image', src: options.btnSubmit, id: 'staf_send', name: 'staf_send' }).appendTo('#staf_frm');
			$('<input>').attr({ type: 'image', src: options.btnClose, id: 'staf_close', name: 'staf_close' }).appendTo('#staf_frm');
			
			_add_events();
		}
		
		function _submit_form() {
			// remove focus it looks nicer
			$('#staf_send').blur();
			
			// fadeout the form and show a nice message
			$('#staf_intro, #staf_fieldset, #staf_send').fadeOut('fast', function() {
					$('#staf_intro').text(options.sending).fadeIn('fast', function() { 
						window.setTimeout(function() { _submit_ajax(); }, 1500);	
				   })
			});
		}
		
		function _submit_ajax() {
			$.ajax({
				type: "POST",
				cache: false,
				url: options.phpFormHandler,
				data: $('#staf_frm').serialize(),
				dataType: "json",
				success: function(data){	

				/*
				*	The Returned JSON looks like this 
				*	msg : is the message that will be displayed when an error occurs or the mail has been send
				*	field: is an object with the form field and an error boolean 
				*
				*	({
				*			"msg" : "Een van de velden is niet of incorrect ingevuld.",
				*			"fields": [
				*				{ "id" : "staf_label_from_name", 	"error" : "false" },
				*				{ "id" : "staf_label_from_email", 	"error" : "true" },
				*				{ "id" : "staf_label_to_name",	 	"error" : "false" },
				*				{ "id" : "staf_label_to_email", 	"error" : "false" }
				*			]
				*	})
				*/

					hasError = false;
					 $.each(data.fields, function(i,field){
						if(field.error == "true" ) {
							$("#"+ field.id).css({ color: '#ff0000' });
							hasError = true;
						}
          			});
					if(hasError) {  
						$('#staf_intro').text(data.msg);
						$('#staf_intro, #staf_fieldset, #staf_send').fadeIn('fast');				
					} else { 
						$('#staf_intro').text(data.msg);
						$('#staf_intro').fadeIn('fast', function () { window.setTimeout(function() { _finish() }, 2500);  });
					}
				},
				error: function() {
					// when even the ajax call fails display an error
					$('#staf_intro').text(options.error);
					$('#staf_intro, #staf_fieldset, #staf_send').fadeIn('fast');
				}
			});
		}
		
		function _add_events() {
			// add some keyboard events
			$(document).bind('keydown', _keyboard_action);
			
			$('#staf_send').click( function() { _submit_form(); });
			
			// close send to a friend when nvs_overlay, nvs_popup_container or staf_close is clicked
			$('#nvs_overlay, #nvs_popup_container, #staf_close').click( function () { _finish(); });
			
			// do nothing when clicked on the nvs_popup
			$('#nvs_popup').click( function() { return false; });
		}
		
		function _keyboard_action(objEvent) {
			switch(objEvent.keyCode) {
			// key ESC
				case 27: 	_finish(); 
							break;
			// key X
				case 88:	_finish();
							break;
				default: 	break;
			}
		}
		
		function _finish() {
			// okay we're done now lets get rid of it
			$(document).unbind('keydown', _keyboard_action);
			$('#nvs_popup_container').remove() 
			$.nvsOverlay.hide(); 
		}
		
		return this.unbind('click').click(_initialize);
	}
	
})(jQuery);
