$(function(){
	fadeGallery();
	initAjaxFormSend();
	initAjaxFormSend2();
	clearInputs();
})
function clearInputs(){
	$('input:text').each(function(){
		$(this).attr('def', $(this).attr('value'));
		$(this).focus(function(){
			if($(this).attr('value')==$(this).attr('def')) $(this).attr('value','')
		}).blur(function(){
			if($(this).attr('value')=='') $(this).attr('value',$(this).attr('def'));
		})
	});
	$('textarea').each(function(){
		$(this).attr('def', $(this).attr('value'));
		$(this).focus(function(){
			if($(this).attr('value')==$(this).attr('def')) $(this).attr('value','')
		}).blur(function(){
			if($(this).attr('value')=='') $(this).attr('value',$(this).attr('def'));
		})
	});
}

function initAjaxFormSend(){
	var _form = $('.validate');
	_form.each(function(){
		var _submit = $('.submit', $(this));
		var _required = $('.required', $(this));
		var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		_required.each(function(){
			$(this).attr('def', $(this).attr('value'));
			$(this).focus(function(){
				$(this).css({color: ''});
				if($(this).attr('value')==$(this).attr('def')) $(this).attr('value','')
			}).blur(function(){
				if($(this).attr('value')=='') $(this).attr('value',$(this).attr('def'));
			})
		})
		_submit.click(function(){
			var step = true;
			var eror = false;
			_required.each(function(){
			if ($(this).val()==0 || $(this).val()==$(this).attr('def') && step) {
				step = false;
				eror = true;
				$(this).css({color: '#f00'});
				alert($(this).attr('title'));
			} else {
				if ($(this).hasClass('email') && step) {
						if (!_emailReg.test($(this).val())) {
							eror = true;
							step = false;
							$(this).css({color: '#f00'});
							alert('Lütfen e-mail adresinizi kontrol ediniz.');
						}
					};
				}
			});
			step=true;
			if (eror) return false;
			else {
// ajax send form
				var _data = _form.serialize();
				var _url = _form.attr('action');
				$.ajax({
					url: _url,
					data: _data,
					type: "post",
					success: function(_text){
						// newsletter confirmation
						alert('Mesajınızı en kısa sürede yanıtlayacağız. Rezervasyon için 0212 263 51 99 nolu telefonumuzu arayabilirsiniz. Rezervasyonlar haftaiçi ve haftasonu saat 19:00 ve sonrası için yapılmaktadır.');
					}
				})
			}
			return false;
		});
	})
}

function initAjaxFormSend2(){
	var _form = $('.validate2');
	_form.each(function(){
		var _submit = $('.submit', $(this));
		var _required = $('.required', $(this));
		var _emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		_required.each(function(){
			$(this).attr('def', $(this).attr('value'));
			$(this).focus(function(){
				$(this).css({color: ''});
				if($(this).attr('value')==$(this).attr('def')) $(this).attr('value','')
			}).blur(function(){
				if($(this).attr('value')=='') $(this).attr('value',$(this).attr('def'));
			})
		})
		_submit.click(function(){
			var step = true;
			var eror = false;
			_required.each(function(){
			if ($(this).val()==0 || $(this).val()==$(this).attr('def') && step) {
				step = false;
				eror = true;
				$(this).css({color: '#f00'});
				alert($(this).attr('title'));
			} else {
				if ($(this).hasClass('email') && step) {
						if (!_emailReg.test($(this).val())) {
							eror = true;
							step = false;
							$(this).css({color: '#f00'});
							alert('Lütfen e-mail adresinizi kontrol ediniz.');
						}
					};
				}
			});
			step=true;
			if (eror) return false;
			else {
// ajax send form
				var _data = _form.serialize();
				var _url = _form.attr('action');
				$.ajax({
					url: _url,
					data: _data,
					type: "post",
					success: function(_text){
						// contact form confirmation
						alert('Mangerie bültene üye olduğunuz için teşekkürler. Adresiniz başkalarıyla paylaşılmayacaktır.');
					}
				})
			}
			return false;
		});
	})
}
function fadeGallery(){
	var _swichTime = 10000;
	var _d = 400;
	$('.gallery-box').each(function(){
		var _this = $(this);
		var _list = $('.s-items > li', _this);
		var _Links = $('.tabs li');
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if($.browser.msie) _d = 0;
		_swichTime +=_d;
		if(_a == -1) _a = 0;
		_Links.removeClass('active').eq(_a).addClass('active');
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		var _t;
		if (_swichTime){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, _swichTime);
		}
		_Links.click(function(){
			var _ind = _Links.index(this);
			changeEl(_ind);
			return false;
		})
		_Links.mouseenter(function(){
			$(this).find('span.hover-state').fadeIn(_d);
		}).mouseleave(function(){
			if(!$(this).hasClass('active')) $(this).find('span.hover-state').fadeOut(_d);
		})
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_Links.eq(_a).removeClass('active').find('span.hover-state').css({display: 'block'}).fadeOut(_d, function(){
				});
				_Links.eq(_ind).find('span.hover-state').fadeIn(_d,function(){
					_Links.eq(_ind).addClass('active');
				});
				_list.eq(_a).removeClass('active').css({display:'block'}).animate({opacity: 0}, {queue:false, duration:_d});
				_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:_d});
				_a = _ind;
				
			}
			if (_swichTime){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, _swichTime);
			}
		}
	});
}
