jQuery(document).ready(function(){
	clearForm();
	getPopup();
	initValidate();

	jQuery('div.slideshow-holder').gallery({
		duration: 500,
		autoRotation: 4000,
		listOfSlides: 'li',
		effect: 'fade'
	});
});

function clearForm() {
	jQuery('input:text, input:password').each(function(){
		this.def = this.value;
		jQuery(this).focus(function(){
			if(this.value == this.def) this.value = '';
		}).blur(function(){
			if(this.value == '') this.value = this.def;
		});
	});
	jQuery('textarea').each(function(){
		this.def = this.value;
		jQuery(this).focus(function(){
			if(this.value == this.def) this.value = '';
		}).blur(function(){
			if(this.value == '') this.value = this.def;
		});
	});
};

function getPopup(){
	var _btn = jQuery('.get-popup-form');
	var _txt = _btn.prev().find('textarea').get(0);
	jQuery('.get-popup-form').simpleLightbox({
		lightboxContentBlock: '#popup',
		faderOpacity: 0.65,
		faderBackground: '#000000',
		closeLink:'a.btn-close, .close-popup',
		href:false
	});
	_btn.parents('form:eq(0)').submit(function(){
		if(_txt.def != _txt.value){
			this.popup.find('textarea').value(_txt.value);
			_btn.get(0).init();
		}else {
			alert('Please input message!');
		}
		return false;
	});
	_btn.click(function(){
		if(_txt.def != _txt.value) {
			this.popup.find('textarea').val(_txt.value);
			_btn.get(0).init();
		}else {
			alert('Please input message!');
		}
		return false;
	})
}

function initValidate(){
	var _form = jQuery('.validate');
	_form.each(function(){
		var _this = this;
		var _submit = jQuery('.submit-form', jQuery(this));
		var _required = jQuery('.required', jQuery(this));
		_required.each(function(){
			jQuery(this).attr('def', jQuery(this).attr('value'));
			jQuery(this).focus(function(){
				if(jQuery(this).attr('value')==jQuery(this).attr('def')) jQuery(this).attr('value','');
			}).blur(function(){
				if(jQuery(this).attr('value')=='') jQuery(this).attr('value',jQuery(this).attr('def'));
			})
		})
		var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		_submit.click(function(){
			var step = true;
			var eror = false;
			_required.each(function(){
				if (jQuery(this).val()==0 || jQuery(this).val()==jQuery(this).attr('def') && step) {
					step = false;
					eror = true;
					alert(jQuery(this).attr('title')+' is empty');
				} else {
				if (jQuery(this).hasClass('email') && step){
						if (!_emailReg.test(jQuery(this).val())){
							eror = true;
							step = false;
							alert(jQuery(this).attr('title')+ ' not correct');
						}
					};
				}
			});
			step=true;
			if (eror) return false;
			else {
				jQuery.ajax({
					url: _form.attr('action'),
					type: 'POST',
					data: jQuery(_this).serialize(),
					success: function(){
						jQuery(_this).parents('.form-holder').find('.message-form').animate({opacity: 0},500,function(){
							jQuery(_this).parents('.form-holder').find('.confirm-form p span').each(function(i){
								jQuery(this).text(jQuery(_this).parents('.form-holder').find('.message-form .field').eq(i).val());
							});
							jQuery(_this).parents('.form-holder').find('.confirm-form').css({opacity:0,display:'block'}).animate({opacity: 1},300, function(){
								setTimeout(function(){
									jQuery(_this).parents('.form-holder').find('.confirm-form .close-popup').trigger('click');
								},4000);
							});
						});
					},
					error: function(){
						alert('Incorect path to file or file not found');
					}
				});
			}
			jQuery(_this).submit(function(){
				return false;
			});
		})
	})
}

jQuery.fn.simpleLightbox = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		lightboxContentBlock: '.nonelight',
		faderOpacity: 0.65,
		faderBackground: '#000000',
		closeLink:'a.btn-close, .close',
		href:true,
		onShow: null,
		onClick: null
	},_options);

	return this.each(function(i, _this){
		var _this = jQuery(_this);
		var _d = jQuery.browser.msie ? 0 : 300;
		var _posTimer = null , _posTimer2 = null;
		if (!_options.href)
			_this.lightboxContentBlock = _options.lightboxContentBlock;
		else _this.lightboxContentBlock = _this.attr('href');
		if (_this.lightboxContentBlock != '' && _this.lightboxContentBlock.length > 1) {
			_this.faderOpacity = _options.faderOpacity;
			_this.faderBackground = _options.faderBackground;
			_this.closeLink = _options.closeLink;
			var _fader;
			var _lightbox = jQuery(_this.lightboxContentBlock);
			if (!jQuery('div.lightbox-fader').length)
				_fader = jQuery('body').append('<div class="lightbox-fader"></div>');
			
			_fader = jQuery('div.lightbox-fader');
			_lightbox.css('zIndex',999);
			_fader.css({
				opacity:_this.faderOpacity,
				backgroundColor:_this.faderBackground,
				display:'none',
				position:'absolute',
				top:0,
				left:0,
				zIndex:998,
				textIndent: -9999
			}).text('jQuerynbsp');
			if(typeof(document.body.style.maxHeight) == 'undefined'){
				var _frame = jQuery('<iframe />');
				_fader.html(_frame);
				_frame.css({
					opacity: 0,
					clear: 'both',
					position: 'absolute',
					left: 0,
					top: 0,
					width: "100%",
					height: "100%",
					zIndex: 0
				});
				var _newdiv = jQuery('<div />');
				_newdiv.css({
					background: '#f00',
					opacity:0,
					clear: 'both',
					position: 'absolute',
					left: 0,
					top: 0,
					width: "100%",
					height: "100%",
					zIndex: '90'
				});
				_fader.append(_newdiv);
			}
			_lightbox.shownFlag = false;

			_this.get(0).popup = _lightbox;
			_this.get(0).init = function(){
				if (jQuery.isFunction(_options.onClick)) {
					_options.onClick.apply(_this);
				}
				if (_fader.is(":visible")) {
					jQuery(".lightbox").each(function(){
						jQuery(this).css("display","none");
					});
					_lightbox.shownFlag = true;
					_lightbox.fadeIn(0);
					jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				} else {
					_fader.fadeIn(10, function(){
						_lightbox.shownFlag = true;
						_fader.shownFlag = true; 
						_lightbox.fadeIn(20, function(){
							if (jQuery.isFunction(_options.onShow)) {
								_options.onShow.apply(_lightbox);
							}
						});
						jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
					});
				}
			}
/* 			_this.click(function(){
				_this.get(0).init();
				return false;
			}); */
			_lightbox.bind('lbclose',function(){
				_lightbox.shownFlag = false;
				_lightbox.fadeOut(0, function(){
					_fader.fadeOut(0);
					jQuery(this).css('top','');
				});
			});
			jQuery(_this.closeLink, _lightbox).click(function(){
				_lightbox.trigger('lbclose');
				return false;
			});
			_fader.click(function(){
				_lightbox.trigger('lbclose');
				return false;
			});

			jQuery.fn.simpleLightbox.positionLightbox = function (_lbox) {
				if(!_lbox.shownFlag) return false;
				var _height = 0;
				var _width = 0;
				var _minWidth = jQuery('body > div:eq(0)').outerWidth();
				_minWidth = 1000;
				if (window.innerHeight) {
					_height = window.innerHeight;
					_width = window.innerWidth;
				} else {
					_height = document.documentElement.clientHeight;
					_width = document.documentElement.clientWidth;
				}
				var _thisHeight = _lbox.outerHeight();
				var _page = jQuery('body > div:eq(0)');
				if (_lbox.length) {
					if (_height > _page.outerHeight(true)) _fader.css('height',_height); else _fader.css('height',_page.innerHeight());
					if (_width < _minWidth) {_fader.css('width',_minWidth);} else {_fader.css('width','100%');}
					if (_height > _thisHeight){
						_lbox.css({position:'absolute'});
						var _top = document.documentElement.scrollTop + document.body.scrollTop;
						if(_posTimer) clearTimeout(_posTimer);
						_posTimer = setTimeout(function(){
							_lbox.animate({top: (_top + (_height - _thisHeight) / 2)}, 200);
						},300);
					}
					else {
						_lbox.css({
							position:'absolute',
							top: 0
						});
					}
					if(_posTimer2) clearTimeout(_posTimer2);
					_posTimer2 = setTimeout(function(){
						if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + document.documentElement.scrollLeft + document.body.scrollLeft+ "px"});
						else _lbox.css({position:'absolute',left: 0});
					},300);
				}
			}
			
			jQuery(window).resize(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			jQuery(window).scroll(function(){
				jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
			});
			
			jQuery.fn.simpleLightbox.positionLightbox(_lightbox);
				jQuery(document).keydown(function (e) {
				if (!e) evt = window.event;
				if (e.keyCode == 27) {
					_lightbox.trigger('lbclose');
				}
			});
		}
	});
}
