jQuery.noConflict();
jQuery(function(){
	initClearForm();
	initGallery();
});
/*--- initGallery ---*/
function initGallery(){
	jQuery('div.intro-block-holder').galleryCircle({
		btnPrev: 'a.up',
		btnNext: 'a.down',
		slidesHolder: '.intro-block-frame',
		slider: '.scroll-gal',
		easing: 'swing',
		autoSlide: true,
		switchTime: 10000,
		duration : 700
	});
}

jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btnPrev: 'a.link-prev',
		btnNext: 'a.link-next',
		holderList: 'div',
		slidesHolder :'.frame',
		slider:'ul',
		hideNav:false,
		autoSlide: false,
		switcher:false,
		switchTime: 6000,
		easing:'linear',
		duration : 700
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _btnPrev = jQuery(_options.btnPrev, _holder);
		var _btnNext = jQuery(_options.btnNext, _holder);
		var _slidesHolder = jQuery(_options.slidesHolder, _holder);
		var _slider = jQuery(_options.slider, _slidesHolder);
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideHeight = _slides.eq(0).outerHeight(true);
		var _curIndex = 0;
		var _prevIndex = 0;
		var _animating = false;
		var _timer;
		var _top = _slidesHolder.outerHeight();
		var _waitAnimation = true;
		var _autoSlide = _options.autoSlide;
		var _easing = _options.easing;
		var _switchTime = _options.switchTime;
		var _speed = _options.duration;
		var direction = true;
		var _animating = false;
		_slides = _slider.children();
		_holder.css({position:'relative'});
		_slides.not(_slides.eq(0)).css({top:-_top});
		var _hideNav = _options.hideNav;
		if(_hideNav){
			_btnNext.hide();
			_btnPrev.hide();
		}
		//gallery control
		_btnPrev.click(function(){
			prevSlide();
			return false;
		});
		_btnNext.click(function(){
			nextSlide();
			return false;
		});
		// gallery animation
		function prevSlide() {
			if(_animating) return;
			if(_curIndex == 0){
				_prevIndex = _curIndex;
				_curIndex = _slides.length-1;
				direction = false;
			}
			else{
				_prevIndex = _curIndex;
				_curIndex--;
				direction = false;
			}
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			if(_curIndex < _slides.length-1){
				_prevIndex = _curIndex;
				_curIndex++;
				direction = true;
			}
			else{
				_prevIndex = _curIndex;
				_curIndex = 0;
				direction = true;
			}
			switchSlide();
		}

		// gallery animation
		function switchSlide(){
			_animating = true;
			if(direction){
				_slides.eq(_curIndex).css({top:_top}).animate({top:0},{duration: _speed, queue: false,complete:function(){
					_animating = false;
				}});
				_slides.eq(_prevIndex).animate({top:-_top},{duration: _speed, queue: false});
			}
			else{
				_slides.eq(_curIndex).css({top:-_top}).animate({top:0},{duration: _speed, queue: false,complete:function(){
					_animating = false;
				}});
				_slides.eq(_prevIndex).animate({top:_top},{duration: _speed, queue: false});
			}
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		autoSlide();
	});
}


/* initClearForm */
function initClearForm()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
