// page init
$(function(){
	initFocus();
	initInputs();
	initScrollGallery();
	initFormValidation();
});

// clear inputs onfocus
function initInputs() {
	var _inputs = $('input[type="text"],input[type="password"],textarea');
	_inputs.each(function(){
		var _input = $(this);
		var _val = _input.val();
		if(_val.length) {
			_input.focus(function(){
				if(_input.val() == _val) _input.val('');
			});
			_input.blur(function(){
				if(!_input.val().length) _input.val(_val);
			});
		}
	});
}

// scroll gallery init
function initScrollGallery() {
	$('#carusel').scrollGallery({
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		switchTime:3000,
		duration:450,
		step:1
	});
}

// form validation
function initFormValidation() {
	var _errorClass = 'error';
	var _regEmail = /^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/;
	function isNumeric(val){return (val - 0) == val && val.length > 0;}

	$('form.validate-form').each(function(){
		var _form = $(this);

		function checkFields() {
			var _flag = false;
			_form.find('.'+_errorClass).removeClass(_errorClass);

			// fields validation
			_form.find('input.required-email').each(function(){
				if(!_regEmail.test($(this).val())) addError($(this));
			});
			_form.find('input.required-phone').each(function(){
				if(!isNumeric($(this).val())) addError($(this));
			});
			_form.find('input.required, textarea.required').each(function(){
				if(!$(this).val().length || $(this).val() == $(this).attr('alt')) addError($(this));
			});

			// error class adding
			function addError(_obj) {
				_obj.parent().addClass(_errorClass);
				_flag=true;
			}
			return _flag;
		}

		// catch form submit event
		_form.submit(function(){
			if(checkFields()) {
				return false;
			}
		});
	});
}

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
	var _options = jQuery.extend({
		sliderHolder: '>div',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		btnPrev:'a.link-prev',
		btnNext:'a.link-next',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		switchTime:5000,
		duration:650,
		easing:'swing',
		event:'click',
		step:false
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _sliderHolder = jQuery(_options.sliderHolder, _this);
		var _slider = jQuery(_options.slider, _sliderHolder);
		var _slides = jQuery(_options.slides, _slider);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _easing = _options.easing;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _step = _options.step;

		// gallery init
		if(!_slides.length) return;
		var _currentStep = 0;
		var _sumWidth = 0;
		var _hover = false;
		var _stepWidth;
		var _stepCount;
		var _offset;
		var _timer;
		_slides.each(function(){_sumWidth+=$(this).outerWidth(true)});

		// calculate gallery offset
		function recalcOffsets() {
			if(_step) {
				_stepWidth = _slides.eq(_currentStep).outerWidth(true);
				_stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth);
				_offset = -_stepWidth*_currentStep;
			} else {
				_stepWidth = _sliderHolder.width();
				_stepCount = _slides.length;
				_offset = -_stepWidth*_currentStep;
			}
		}

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentStep != _ind) {
						_currentStep = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// gallery animation
		function prevSlide() {
			recalcOffsets();
			if(_currentStep > 0) _currentStep--;
			else _currentStep = _stepCount-1;
			switchSlide();
		}
		function nextSlide() {
			recalcOffsets();
			if(_currentStep < _stepCount-1) _currentStep++;
			else _currentStep = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
		}
		function switchSlide() {
			recalcOffsets();
			_slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}

// radio focus function
function initFocus()
{
	var inputs = document.getElementsByTagName("input");
	var labels = document.getElementsByTagName("label");
	for (var i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == "radio" || inputs[i].type == "checkbox")
		{
			inputs[i].onclick = function ()
			{
				for (var j=0; j<labels.length; j++)
				{
					if(this.id == labels[j].htmlFor)
					{
						if(labels[j].className.indexOf("focus") == -1)
						{
							labels[j].className += " focus";
						}
						else if(this.type != "radio")
						{
							labels[j].className = labels[j].className.replace("focus", "");
						}
					}
					else if(this.type == "radio" && this.name == document.getElementById(labels[j].htmlFor).name)
					{
						labels[j].className = labels[j].className.replace("focus", "");
					}
				}
			}
			if(inputs[i].checked == true)
			{
				for (var j=0; j<labels.length; j++)
				{
					if(inputs[i].id == labels[j].htmlFor)
						labels[j].className += " focus";
				}
			}
		}
	}
}
