$(document).ready(function(){
	$('.gallery-holder').fadeGallery({
		gallery : '.img-holder',
		prev : false,
		next : false,
		play : false,
		pause : false,
		autoplay : true,
		autoheight:false,
		fixedheight:true,
		IE:true,
		duration : 3500,
		fade_duration : 600
	});
	
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		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.filter) o.filter = "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)) {
				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)) {
				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";
	}
}

jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		gallery : '.gallery',
		prev : '.prev',
		next : '.next',
		play : '.play',
		pause : '.pause',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoplay : true,
		autoheight:true,
		fixedheight:false,
		duration : 2000,
		fade_duration : 200,
		IE:false
	}, options);
	
	return this.each(function(){
		var holder = $(this),
			gallery = $(options.gallery, holder),
			prev = $(options.prev, holder),
			next = $(options.next, holder),
			play = $(options.play, holder),
			pause = $(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = $(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('a'),
			autoheight = options.autoheight,
			fixedheight = options.fixedheight,
			IE = options.IE,
			current,
			timer;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (!autoheight && fixedheight) gallery.css({height: slides.eq(current).height()});
		if (fixedheight) gallery.css({
			height: slides.eq(current).height(),
			overflow:'hidden'
		});
		gallery.css({position:'relative'});
		slides.css({
			position:'absolute',
			zIndex:0,
			top:'0',
			left:'0',
			opacity:0,
			display:'none'
		});
		slides.eq(current).css({
			position:'relative',
			zIndex:1,
			top:'0',
			left:'0',
			opacity:1,
			display:'block'
		});
		if (IE && $.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			var tmp = current;
			if (IE && $.browser.msie) {
				slides.eq(tmp).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
				if (autoheight) {
					gallery.stop().css({
						height: slides.eq(tmp).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.fade_duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			} else {
				slides.eq(tmp).stop().removeClass('active').animate({
					opacity:0
				},options.fade_duration, function(){
					$(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.fade_duration).css({
					position:'relative',
					zIndex:1
				});
				if (autoheight) {
					gallery.stop().css({
						height: slides.eq(tmp).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.fade_duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(tmp).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			if (autoplay) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, options.duration);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!autoplay) {
				autoplay = true;
				clearTimeout(timer);
				timer = setTimeout(function(){
					rotate(nextEl());
				}, options.duration);
			} 
			return false;
		});
		gallery.mouseenter(function(){
			clearTimeout(timer);
		})
		gallery.mouseleave(function(){
			timer = setTimeout(function(){
				rotate(nextEl());
			},options.duration);
		})
		pause.click(function(){
			if (autoplay) {
				clearTimeout(timer);
				autoplay = false;
			}
			return false;
		});
		
		// thumbnails
		thumbs.each(function(i){
			$(this).click(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, options.duration);
					}
				}
				return false;
			})
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},options.duration);
		}
	});
}
