//page init
$(function(){
	$('#edit-submitted-name').val('name');
	$('#edit-submitted-e-mail-address').val('e-mail address');
	// initGallery();  // Don't init gallery until the proverbium is loaded.
	initClearInputs();
	$('a.open-popup').simplebox();
});

// init gallery
function initGallery(){
	$('.gallery-holder').each(function(){
		new GalleryConstuctor({
			holder: this,
			autorotation: false,
			animSpeed: 500,
			duration: 2000
		});
	});
}

// init clear inputs
function initClearInputs(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
//lightbox
(function($) {
	$.fn.simplebox = function(options) { 
		return new Simplebox(this, options); 
	};
	
	function Simplebox(context, options) { this.init(context, options); };
	
	Simplebox.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 300,
				linkClose: 'a.close, a.btn-close',
				divFader: 'fader',
				faderColor: 'black',
				opacity: 0.7,
				wrapper: '.page',
				linkPopap: '.link-submit'
			}, options || {});
			this.btn = $(context);
			this.select = $(this.options.wrapper).find('select');
			this.initFader();
			this.btnEvent(this, this.btn);
		},
		btnEvent: function($this, el){
			el.click(function(){
				if ($(this).attr('href')) $this.toPrepare($(this).attr('href'));
				else $this.toPrepare($(this).attr('title'));
				return false;
			});
		},
		calcWinWidth: function(){
			this.winWidth = $('body').width();
			if ($(this.options.wrapper).width() > this.winWidth) this.winWidth = $(this.options.wrapper).width();
		},
		toPrepare: function(obj){
			this.popup = $(obj);
			this.btnClose = this.popup.find(this.options.linkClose);
			this.submitBtn = this.popup.find(this.options.linkPopap);
			
			if ($.browser.msie) this.select.css({visibility: 'hidden'});
			this.calcWinWidth();
			this.winHeight = $(window).height();
			this.winScroll = $(window).scrollTop();
			
			this.popupTop =  this.popup.outerHeight(true)>this.winHeight?this.winScroll:this.winScroll + (this.winHeight/2) - this.popup.outerHeight(true)/2;
			if (this.popupTop < 0) this.popupTop = 0;
			
			this.popup.css({
				zIndex: 1000,
				top: this.popupTop,
				left: this.winWidth/2 - this.popup.outerWidth(true)/2
			}).hide();
			
			this.initAnimate(this);
			this.initCloseEvent(this, this.btnClose, true);
			this.initCloseEvent(this, this.submitBtn, false);
			this.initCloseEvent(this, this.fader, true);
		},
		initCloseEvent: function($this, el, flag){
			el.click(function(){
				//jwplayer("container").stop();							
				if (player) { player.stopVideo(); }
											
				$('body > div.outtaHere').removeClass('optionsDivVisible').addClass('optionsDivInvisible')
				$this.popup.fadeOut($this.options.duration, function(){
					$this.popup.css({left: '-9999px',top: '-9999px'}).show();
					if ($.browser.msie) $this.select.css({visibility: 'visible'});
					$this.submitBtn.unbind('click');
					$this.fader.unbind('click');
					$this.btnClose.unbind('click');
					$(window).unbind('resize');
					if (flag) $this.fader.fadeOut($this.options.duration);
					else {
						if ($this.submitBtn.attr('href')) $this.toPrepare($this.submitBtn.attr('href'));
						else $this.toPrepare($this.submitBtn.attr('title'));
					}
				});
				return false;
			});
		},
		initAnimate:function ($this){
			$this.fader.fadeIn($this.options.duration, function(){
				$this.popup.fadeIn($this.options.duration);
			});
			$(window).resize(function(){
				$this.calcWinWidth();
				$this.popup.animate({
					left: $this.winWidth/2 - $this.popup.outerWidth(true)/2
				}, {queue:false, duration: $this.options.duration});
				$this.fader.css({width: $this.winWidth});
			});
		},
		initFader: function(){
			if ($(this.options.divFader).length > 0) this.fader = $(this.options.divFader);
			else{
				this.fader = $('<div class="'+this.options.divFader+'"></div>');
				$('body').append(this.fader);
				this.fader.css({
					position: 'fixed',
					width:'100%',
					height:'100%',
					zIndex: 999,
					left:0,
					top:0,
					background: this.options.faderColor,
					opacity: this.options.opacity
				}).hide();
			}
		}
	}
}(jQuery));
// gallery constructor
function GalleryConstuctor(options){
	this.opt = {
		holder: null,
		prevBtn: '.btn-prev',
		nextBtn: '.btn-next',
		innerHolder: '.gallery-box',
		sliderElement: 'ul',
		slides: 'li',
		switcherElements: '.switcher li',
		autorotation: false,
		animSpeed: 1000,
		duration: 2000
	}
	$.extend(this.opt, options);
	
	if (this.opt.holder) {
		this.getGalleryElements();
		this.costructStructure();
		this.handlers();
		this.autorotation();
	}
}

GalleryConstuctor.prototype = {
	// get elements and set def props
	getGalleryElements : function(){
		// gallery holder
		this.gelleryBlock = $(this.opt.holder);
		// inner gallery box
		this.innerHolder = this.gelleryBlock.find(this.opt.innerHolder);
		// control buttons
		this.prevBtn = this.gelleryBlock.find(this.opt.prevBtn);
		this.nextBtn = this.gelleryBlock.find(this.opt.nextBtn);
		// ul || slides container
		this.slider = this.innerHolder.find(this.opt.sliderElement);
		// (li || slides) & their length and width of one elem
		this.slideElems = this.slider.find(this.opt.slides);
		this.slideElemsLength = this.slideElems.length;
		this.sliderWidth = this.slideElems.height();
		// switchers
		this.switchersEl = this.gelleryBlock.find(this.opt.switcherElements);
		this.currentIndex = this.slideElemsLength;
		this.autorotationTrigger = this.opt.autorotation;
		this.animSpeed = this.opt.animSpeed;
		this.duration = this.opt.duration;
		this.timer;
		this.animate;
	},
	// cloning
	costructStructure: function(){
		var leftCopy = this.slideElems.clone();
		var rightCopy = this.slideElems.clone();
		leftCopy.insertBefore(this.slideElems.eq(0));
		rightCopy.appendTo(this.slider);
		this.slider.css({top:-this.slideElemsLength*this.sliderWidth});
	},
	// switcher method
	switcher: function(){
		if(this.currentIndex === this.slideElemsLength*2 || this.currentIndex === 0){
			if(!this.animate){
				this.animate = true;
				this.slider.animate({top: -this.sliderWidth * this.currentIndex}, {
					duration: this.animSpeed,
					queue: false,
					complete:$.proxy(function(){
						this.animate = false;
						this.currentIndex = this.slideElemsLength;
						this.slider.css({top: -this.slideElemsLength*this.sliderWidth});
					},this)
				});
			}
		}else{
			this.animate = true;
			this.slider.animate({top:-this.sliderWidth*this.currentIndex},{
				duration: this.animSpeed,
				queue:false,
				complete:$.proxy(function(){
					this.animate = false},this)
				});
		}
		this.autorotation();
	},
	handlers: function(){
		this.nextBtn.click($.proxy(function(){
			if(!this.animate){
				this.currentIndex++;
				this.switcher();
				this.refresh();
			}
			return false;
		},this));
		this.prevBtn.click($.proxy(function(){
			if(!this.animate){
				this.currentIndex--;
				this.switcher();
				this.refresh();
			}
			return false;
		},this));
		var _this = this;
		this.switchersEl.click(function(){
			if(!this.animate){
				var current = $(this);
				var index = _this.switchersEl.index(current) + _this.slideElemsLength;
				_this.currentIndex = index;
				_this.switcher();
				_this.refresh();
			}
			return false
		});
	},
	refresh: function(){
		var _ind = this.currentIndex - this.slideElemsLength;
		if(_ind === this.slideElemsLength) _ind = 0;
		this.switchersEl.removeClass('active').eq(_ind).addClass('active');
	},
	autorotation: function(){
		if(this.autorotationTrigger){
			if(this.timer) clearTimeout(this.timer);
			this.timer = setTimeout($.proxy(function(){
				this.nextBtn.trigger('click');
			},this),this.duration);
		}
	}
}

// clear inputs
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";
	}
}
