jQuery.fn.simpleLightbox = function(_options){
	// defaults options
	var _options = jQuery.extend({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0.5,
		faderBackground: '#000000',
		closeLink:'a.close, a.close02',
		href:true,
		onClick: null
	},_options);

	var _popupCounter = 1;

	return this.each(function(i, _this){
		var _this = jQuery(_this), _ajax = true;
		if (!_options.href)
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		else _this.lightboxContentBlock = _this.attr('href');
		var _parentForm;
		if (_this.is('input[type="submit"]')) {
			_parentForm = _this.parents('form');
			_this.lightboxContentBlock = _parentForm.attr('action');
		}
		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = $(_this.lightboxContentBlock);
			if (_lightbox.length) _ajax = false;
			_lightbox.addClass('lightbox-el');
			if (!jQuery('div.lightbox-fader').length) {
				_fader = jQuery('<div class="lightbox-fader"></div>');
				jQuery('body').append(_fader);
				_fader.css({
					opacity:_this.faderOpacity,
					backgroundColor:_this.faderBackground,
					display:'none',
					position:'absolute',
					top:0,
					left:0,
					zIndex:998,
					textIndent: -9999
				}).text('&nbsp;');
			} else
				_fader = jQuery('div.lightbox-fader');
			
			var _rel = _this.attr('rel');
			_this.removeAttr('rel');
			
			var _valide = true;
			if (_this.is('input[type="submit"]')){
				_rel = _this.attr('value');
				_this.attr('value','');
				_parentForm.submit(function(){
					return false;	
				});
			}
			
			_lightbox.css({
				'zIndex':999
			});
			_lightbox.shownFlag = false;
			
			_this.click(function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}
				var _popupURL = _this.attr('href');
				var _data = '';
				if (_this.is('input[type="submit"]')) {
					_data = _parentForm.serialize();
					_popupURL = _parentForm.attr('action');
					_valide = validate(_parentForm);
					if (!_valide) return false;
				}
				if(jQuery('div[rel*="'+_popupURL+'"]').length == 0 && _ajax) {
					if (_fader.is(':hidden')) {
						_fader.show();
					}
					$('div[id*="ajaxpopup"]:visible').fadeTo(200,0.5);
					jQuery.ajax({
						url: _popupURL,
						type: "GET",
						data: _data,
						success: function(msg){
							$('div[id*="ajaxpopup"]:visible').remove();
							$('.lightbox-el').hide();
							// append loaded popup
							_lightbox = $(msg);
							_lightbox.attr('rel',_popupURL).css({
								zIndex:999,
								position:'absolute',
								display:'none',
								top: -9999,
								left: -9999
							});
							_lightbox.attr('id','ajaxpopup'+_popupCounter);
							jQuery('body').append(_lightbox);
							_fader.fadeIn(300, function(){
								_lightbox.fadeIn(300);
							});
							_popupCounter++;
							// attach close event
							jQuery(_this.closeLink, _lightbox).click(function(){
								_lightbox.fadeOut(400, function(){
									_fader.fadeOut(300);
									_lightbox.remove();
								});
								return false;
							});

							positionLightbox(_lightbox);
							
							if (typeof clearFormFields == 'function' && _lightbox.length)
								clearFormFields({
									clearInputs: true,
									clearTextareas: true,
									passwordFieldText: true,
									addClassFocus: "focus",
									filterClass: "default",
									object: _lightbox.get(0)
								});
							
							$('.ajaxlightbox', _lightbox).simpleLightbox();
						},
						error: function(msg){
							alert('ajax error');
							return false;
						}
					});
				} else {
					_fader.fadeIn(300, function(){
						_lightbox.fadeIn(300);
						positionLightbox(_lightbox);
					});
					positionLightbox(_lightbox);
				}
				return false;
			});
			jQuery(_this.closeLink, _lightbox).click(function(){
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
				});
				return false;
			});
			_fader.click(function(){
				_lightbox.fadeOut(400, function(){
					_fader.fadeOut(300);
					_lightbox.remove();
				});
				return false;
			});
			var _scroll = false;
			function positionLightbox (_lbox) {
				var _height = 0;
				var _width = 0;
				var _minWidth = $('body > div:eq(0)').outerWidth();
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();
				var _page = $('body');
				if (_lbox.length) {
					if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
					if (_height > _page.innerHeight()) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					if(_rel){
						if (_page.innerHeight() < ($(_rel).offset().top + _lbox.outerHeight())) _fader.css('height',($(_rel).offset().top + _lbox.outerHeight())); 
						_lbox.css({
							position:'absolute',
							top: '112px',
							left: $(_rel).offset().left
						});
					} else {
						if (_height > _thisHeight) {
							if ($.browser.msie && $.browser.version < 7) {
								_lbox.css({
									position:'absolute',
									top: '112px'
								});
							} else {
								_lbox.css({
									position:'fixed',
									top: '112px'
								});
							}
						}
						else {
							var _fh = parseInt(_fader.css('height'));
							if (!_scroll) {
							if (_fh - _thisHeight > parseInt($(document).scrollTop())) {
									_fh = parseInt($(document).scrollTop())
									_scroll = _fh;
								} else {
									_scroll = _fh - _thisHeight;
								}
							}
							_lbox.css({
								position:'absolute',
								top: '112px'
							});
						}
						if (_width > _lbox.outerWidth()) _lbox.css({left:((_width - _lbox.outerWidth()) / 2 + 10) + "px"});
						else _lbox.css({position:'absolute',left: 0});
					}
				}
			}
			jQuery(window).resize(function(){
				if (_lightbox.is(':visible'))
					positionLightbox(_lightbox);
			});
		}
	});
}
$(document).ready(function(){
	$('.ajaxlightbox').simpleLightbox();
});

function validate(_form){
	var _name = $('#name', _form),
		_email = $('#email', _form),
		_message = $('#message', _form),
		_item02 = $('p.item02'),
		_valide = true;
		
	var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
	if (_name.val() == '') {
		_valide = false;
		_name.parents('div.row01').addClass('error');
	}
	if (_message.val() == '') {
		_valide = false;
		_message.parents('div.row01').addClass('error');
	}
	if (!_emailReg.test(_email.val())) {
		_valide = false;
		_email.parents('div.row01').addClass('error');
	}

	
	$([_name,_email,_message]).each(function(){
		$(this).focus(function(){
			$(this).parents('div.row01').removeClass('error');
			if (!$('.error').length) _item02.hide(300);
		});
	});
	
	if (!_valide) _item02.show(300);
	
	return  _valide;
}
