//init page
$(function(){
	initClearInputs();
	initSlideBlock();
	initTabsArea();
});

//init tabs area
function initTabsArea(){
	$('.content-box').each(function(){
		var hold = $(this);
		var startBox = hold.find('.start-box');
		var tabsBox = hold.find('.tabs-box');
		var link = startBox.find('a.more');
		link.click(function(){
			startBox.hide();
			tabsBox.show();
			return false;
		});
	});
	$('.tabs-box').each(function(){
		var hold = $(this);
		var slides = hold.find('.slide');
		var linkPrev = hold.find('.prev').hide();
		var linkNext = hold.find('.next');
		var linkHome = hold.find('a.home').show();
		var linkSign = hold.find('a.sign-up').hide();
		var pagingLinks = hold.find('.steps a');
		var curSlide = 0, prevSlide;
		var step = hold.find('.step_num');
		var activeClass = 'active';
		slides.hide().eq(curSlide).show();
		
		function changeSlide(){
			slides.eq(prevSlide).hide();
			slides.eq(curSlide).show();
			step.empty().html(curSlide+1);
			pagingLinks.parent().removeClass(activeClass).eq(curSlide).addClass(activeClass);
			if(curSlide == 0) {
				linkPrev.hide();
				linkHome.show();
			}else{
				linkPrev.show();
				linkHome.hide();
			}
			if(curSlide == slides.length - 1) {
				linkNext.hide();
				linkSign.show();
			}else{
				linkNext.show();
				linkSign.hide();
			}
		}
		
		linkPrev.click(function(){
			prevSlide = curSlide;
			if(curSlide == 0) curSlide = slides.length - 1;
			else curSlide--;
			changeSlide();
			return false;
		});
		linkNext.click(function(){
			prevSlide = curSlide;
			if(curSlide == slides.length - 1) curSlide = 0;
			else curSlide++;
			changeSlide();
			return false;
		});
		pagingLinks.each(function(ind){
			$(this).click(function(){
				prevSlide = curSlide;
				curSlide = ind;
				changeSlide();
				return false;
			});
		});
	});
	jQuery('.busines-holder').each(function(){
		var hold = jQuery(this);
		var linkOpen = hold.find('a.link-see');
		var box = hold.find('.block');
		linkOpen.click(function(){
			jQuery(this).parent().hide();
			box.show();
			return  false;
		});
	});
}

//init slide block
function initSlideBlock(){
	var hold = jQuery('#nav');
	var links = hold.find('a.tab');
	var speed = 500;
	links.each(function(){
		var link = jQuery(this);
		var box = jQuery(link.attr('href'));
		var boxH = box.height();
		var linkToggle = box.find('a.tab');
		link.click(function(){
			if(links.filter('.active').length && links.filter('.active').get(0) !== link.get(0)){
				links.filter('.active').each(function(){
					jQuery(jQuery(this).removeClass('active').attr('href')).stop().animate({height:0},{
						duration: speed,
						complete: function(){
							jQuery(this).hide().removeAttr('style');
							link.addClass('active');
							box.show().css({height: 0}).stop().animate({height: boxH}, {duration: speed});
						}
					});
				});
			}else if(link.hasClass('active')){
				link.removeClass('active');
				box.stop().animate({height:0},{
					duration: speed,
					complete: function(){
						box.hide().removeAttr('style');
					}
				});
			}else{
				link.addClass('active');
				
				box.show().css({height: 0}).stop().animate({height: boxH}, {duration: speed});
			}
			return false;
		});
		linkToggle.click(function(){
			jQuery(links.filter('.active').removeClass('active').attr('href')).hide().removeAttr('style');
			jQuery(jQuery(this).attr('href')).show();
			links.filter('[href='+ jQuery(this).attr('href') +']').addClass('active');
			return false;
		});
	});
}

//init clear inputs
function initClearInputs(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}

//clear form fields module
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";
	}
}
