/**
 * @author Rytis Alekna
 */
( function ($) {
	
	var opts = {
		
		lightboxHolder	: 'raLightboxHolder',
		
		lightboxBackground : 'raLightboxBackground',
		
		lightbox	: 'lightbox',
		
		closeButton	: 'closeButton'
	}
	
	var currentLightbox = null;
	
	$.raLightbox = function ( target, options ) {
		
		opts = $.extend( opts, options );
		
		$( '.'+opts.lightboxHolder ).removeClass( 'closed' );
		
		$( '.'+opts.lightboxHolder ).append('<div class=\'' + opts.lightboxBackground + '\'></div>');
		
		$( '.'+opts.lightboxBackground ).css( 'opacity', 0 );
		
		$( '.'+opts.lightboxBackground ).fadeTo( 'fast', 0.33 )
		
		var clonedLightbox = $( target ).clone(true).removeClass('closed');
		clonedLightbox.click(
			function (e) {
				e.stopPropagation();
			}
		)
		
		$( '.'+opts.lightboxHolder ).append( clonedLightbox );
		$( '.'+opts.lightboxHolder + ', ' + '.'+opts.lightboxBackground ).height( $(document).height() );
		$( '.'+opts.lightboxHolder+ ', ' + '.'+opts.lightboxBackground ).width( $(document).width() );
		
		clonedLightbox.css( 'left', $(document).width() / 2 - clonedLightbox.width() / 2  );
		clonedLightbox.css( 'top', $(window).height() / 2 - clonedLightbox.height() / 2  );
		clonedLightbox.css( 'position', 'fixed' );
		currentLightbox = clonedLightbox;
		
	}
	
	$.initRaLightbox = function () {
		
		$('body').append('<div class=\'raLightboxHolder closed\'></div>');
		
		$( '.'+opts.lightboxHolder ).click(
			function(e) {
				e.stopPropagation();
				var that = this;
				$('.'+opts.lightboxBackground).fadeOut( 'fast',
					function () {
						$(that).addClass('closed');
						$(that).empty();
						currentLightbox = null;
					}
				)
			}
		)
		
		$( '.'+opts.lightbox + ' .' + opts.closeButton + ', ' + '.'+opts.lightbox + ' .close' ).click(
			function () {
				$(this).parents('.' + opts.lightboxHolder ).click();
			}
		)
		
	}
	
	/**
	 * 
	 * @param {Sring} name
	 */
	$.getFormElementByName = function ( name ) {
		
		if ( currentLightbox != null ) {
			return $( '[name=' + name + ']', currentLightbox )
		} else {
			return null;
		}
		
	}

	/**
	 * 
	 * @param {Sring} id
	 */
	$.getFormElementById = function ( id ) {
		
		if ( currentLightbox != null ) {
			return $( '#' + id, currentLightbox )
		} else {
			return null;
		}
		
	}
	
	/**
	 * Get all form elements
	 * @return {jQuery} a set of form elements
	 */
	$.getAllFormElements = function () {
		
		if ( currentLightbox != null ) {
			return $( 'input, textarea, select', currentLightbox );
		} else {
			return $();
		}
		
	}
	
	/**
	 * Set error to lightbox form 
	 * @param {Object} text
	 */
	$.setErrorText = function ( text ) {
		if ( currentLightbox != null ) {
			return $( '.formError', currentLightbox ).text( text );
		}
	} 
	
	/**
	 * Validate all form elements of lightbox
	 * @return {Boolean} true if form is valid and false if not
	 */
	$.validateLightboxForm = function () {
		
		var retVal = true;
		
		var elementValidationFlag = true;
		
		if ( currentLightbox != null ) {
			
			var allFormElements = $.getAllFormElements();
			
			allFormElements.each(
			
				function() {
					
					if ($(this).hasClass('number')) {
						
						elementValidationFlag = isFloat($(this).val());
						if (!elementValidationFlag) {
							$.setErrorText( formSetErrorMessage('number') );
							$(this).focus();
							return ( retVal = false );
						}
						
					}
					
					if ($(this).hasClass('digits')) {
						
						elementValidationFlag = isInteger($(this).val());
						if (!elementValidationFlag) {
							$.setErrorText( formSetErrorMessage('digits') );
							$(this).focus();
							return ( retVal = false );
						}
					}
					
					if ($(this).hasClass('email')) {
						
						elementValidationFlag = isEmail($(this).val());
						if (!elementValidationFlag) {
							$.setErrorText( formSetErrorMessage('email') );
							$(this).focus();
							return ( retVal = false );
						}
					}
					
					if ($(this).hasClass('required')) {
						
						elementValidationFlag = ( ($(this).val() != '' ) && ($(this).val() != $(this).attr('title') ) );
						if (!elementValidationFlag) {
							$.setErrorText( formSetErrorMessage('required') );
							$(this).focus();
							return ( retVal = false );
						}
					}
				}				
			)
			
		}
		if ( retVal ) $.setErrorText('');
		return retVal;
		
		/*
		if (currentLightbox != null) {
			
			retVal = $( "form", currentLightbox ).validate({
						onfocusout: false,
						onkeyup: false,
						errorContainer: ".formError",
						errorLabelContainer: ".formError",
						wrapper: "li", // debug:true,
						messages: {
							email: function() {
								formSetErrorMessage('email')
							},
							required: function() {
								formSetErrorMessage('required')
							},
							digits: function() {
								formSetErrorMessage('digits')
							},
							number: function() {
								formSetErrorMessage('number')
							}
						}
					}
					
			).form();			
			
			if ( retVal == true ) {
				
				$( "form input:filled", currentLightbox ).each( 
					function () {
						if ( ( $(this).val() == $(this).attr('title') ) ) {
							alert( $(this).val() )
							if (!$(this).hasClass('required')) {
								$(this).val('');
							} else {
								// $( ".formError", currentLightbox ).text(  )
								
								return false;
								
							}
						}
						
					}
				)
				
			}
			
		} */
	}
	
	$.closeCurrentLightbox = function () {
		
		if ( currentLightbox != null ) {
			
			$(currentLightbox).fadeOut( 'fast',
				function () {
					$(currentLightbox).parent().addClass('closed');
					$(currentLightbox).parent().empty();
					currentLightbox = null;
				}
			)			
			
		}
	}
	
	$.displayThankYouLightbox = function () {
		if (currentLightbox) {
			$('.' + opts.lightboxHolder).empty();
			$.raLightbox('.thankYouLightbox');
		}
	}
	
	
	$.displayThankYouSendOnLightbox = function () {
		if (currentLightbox) {
			$('.' + opts.lightboxHolder).empty();
			$.raLightbox('.thankYouSendOnLightbox');
		}
	}	
	
	/**
	 * Validate if is integer
	 * @param {Stirng} string to validate
	 * @return {Boolean}
	 */
	function isInteger( s ) {
		var reg_exp = /^\s*(\+|-)?\d+\s*$/;
		s.split(' ').join('');
		return String(s).search(reg_exp) != -1;
	}
	
	/**
	 * Validate if is float
	 * @param {Stirng} string to validate
	 * @return {Boolean}
	 */
	function isFloat( s ) {
		var reg_exp = new RegExp(/^[-]?[0-9]+[\.]?[0-9]+$/);
		return reg_exp.test(s);
	}
	
	function isEmail ( s ) {
		var reg_exp = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
		return reg_exp.test( s );
	}
	
	
} )(jQuery)


function closeCurrentLightbox () {
	$.closeCurrentLightbox();
}
