jQuery(document).ready(function(){
	var message = "Enter your email address";
	initValidation(message);
	jQuery('div.coda-slider-wrapper').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
});

jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

return this.each(function(){
		
		jQuery.extend( jQuery.easing, // from the jquery.easing plugin
	{
		easeInOutExpo: function (x, t, b, c, d) {
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		}
	});
		
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('div.panel-container');
		var _el = _hold.find('div.panel-container > div.panel');
		var _next = _hold.find('div.arrow-next');
		var _prev = _hold.find('div.arrow-prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		_active = 0;
		
		var _btn = jQuery('<ul class="switcher"></ul>');
		jQuery('div.coda-nav > div').append(_btn);
		_el.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		_btn = _btn.find('a');
		_btn.parent('li').removeClass('active');
		_btn.eq(_active).parent('li').addClass('active');
		
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed, easing: "easeInOutExpo"});
			_btn.parent('li').removeClass('active');
			_btn.eq(_active).parent('li').addClass('active');
			if(_active == _count){
				_next.fadeOut();
				_prev.fadeIn();
			}
			else{
				_prev.fadeOut(100);
				_next.fadeIn();
			}
		}
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active = 0;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
		_btn.click(function(){
			_active = _btn.index(jQuery(this));
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
	});
}
function initValidation(message){
	var _errorClass = 'error';
	var _mailData;
	var _regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var _regEmails = /^([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4})+([, ]+[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4})*$/;
	var _regPhone = /^[0-9\-\ \()]+$/;
	var _regNum = /^[0-9]+$/;
	var _regDay = /^(([0]{1}[1-9]{1})|([1-9]{1})|([1-2]{1}[0-9]{1})|([3]{1}[0-1]{1}))$/;
	var _regMon = /^(([0]{1}[1-9]{1})|([1-9]{1})|([1]{1}[0-2]{1}))$/;
	var _regYear = /^[19]{2}[0-9]{2}$/;
	
	jQuery('form.subscribe').each(function(){
		var _form = jQuery(this);
		function checkFields() {
			var _flag = false;
			_form.find('.'+_errorClass).removeClass(_errorClass);

			// fields validation
			_form.find('input.required-email').each(function(){
				if(!_regEmail.test(jQuery(this).val())) addError(jQuery(this));
			});

			// error class adding
			function addError(_obj) {
				_obj.parent('div.text').addClass(_errorClass);
				alert(message);
				_flag=true;
			}
			return _flag;
		}

		// catch form submit event

		_form.submit(function(){
			if(checkFields()) {
				return false;
			}else{
				var _path = _form.attr('action');
				jQuery.ajax({
					type: "POST",
					global: false,
					//dataType: "html",
                    data:_form.serialize(),
					url: _path,
					success: function(msg){
						_form.fadeOut();
						_active = 1;
						$('div.panel-container').animate({
							marginLeft: -(725) + "px"
						}, {queue:false, duration: 700, easing: "easeInOutExpo"});
						_btn = $('ul.switcher').find('a');
						_btn.parent('li').removeClass('active');
						_btn.eq(_active).parent('li').addClass('active');
						
						
						jQuery('ul.switcher > li').fadeIn();
						jQuery('div.arrow-next').css('zIndex', 15);
					},
					error: function(msg){
						alert('Email send error!');
					}
				});
				return false;
			}
		});
	});
}
