jQuery(function(){
	initPlugins();
	initScrollable();
});

function initPlugins(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addParentFocus: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	/*jQuery('.aside').movingBlock({
		holder:'#twocolumns',
		topDif:0,
		bottomDif:0,
		toEl:'div.stop',
		cssTop:'marginTop',
		duration:150
	});*/
}

function clearFormFields(o){
	if(o.clearInputs == null){o.clearInputs = true;} // true/false
	if(o.clearTextareas == null){o.clearTextareas = true;} // true/false
	if(o.passwordFieldText == null){o.passwordFieldText = false;} // true/false
	if(o.addClassFocus == null){o.addClassFocus = false;} // focus class
	if(o.addParentFocus == null){o.addParentFocus = false;} // true/false or parent selector
	if(!o.filter){o.filter = "default";}
	if(o.clearInputs || o.clearTextareas){
		var inputs = jQuery(eval("o.clearInputs ? 'input' : ''") + eval("o.clearTextareas ? ', textarea' : ''"));
		inputs.each(function(){
			var _this = jQuery(this);
			var _type = _this.attr('type') || 'textarea';
			var _class = _this.attr('class');
			var _valHtml = _this.val();
			if(o.passwordFieldText && _type == 'password'){
				var fakeInput = jQuery('<input>');
				fakeInput.attr('type', 'text');
				fakeInput.attr('value', _valHtml);
				fakeInput.attr('class', _class);
				fakeInput.fake = true;
				fakeInput.focus(function(){
					_this.trigger('focus');
				});
				_this.parent().prepend(fakeInput);
				_this.fake = fakeInput;
				inputsSwap(_this, null);
			}
			if((_type == 'textarea' || _type == 'text' || _type == 'password') || _class == o.filterClass){
				_this.focus(function(){
					if(_valHtml == _this.val()){_this.val('');}
					if(_this.fake){
						inputsSwap(_this.fake, _this);
					}
					if(o.addClassFocus){
						_this.addClass(o.addClassFocus);
						if(_this.fake){_this.fake.addClass(o.addClassFocus);}
						if(o.addParentFocus == true){
							_this.parent().addClass('parent-'+o.addClassFocus);
						}else if(!o.addParentFocus == false){
							_this.parents(o.addParentFocus).addClass('parent-'+o.addClassFocus);
						}
					}
				});
				_this.blur(function(){
					if(_this.val() == ''){
						_this.val(_valHtml);
						if(_this.fake){inputsSwap(_this, _this.fake);}
					}
					if(o.addClassFocus){
						_this.removeClass(o.addClassFocus);
						if(_this.fake){_this.fake.removeClass(o.addClassFocus);}
						if(o.addParentFocus == true){
							_this.parent().removeClass('parent-'+o.addClassFocus);
						}else if(!o.addParentFocus == false){
							_this.parents(o.addParentFocus).removeClass('parent-'+o.addClassFocus);
						}
					}
				});
			}
		});
	}
	function inputsSwap(el, el2){
		if(el) el.hide();
		if(el2) el2.show();
	}
}

// init scrollable sidebar function
function initScrollable(){
	var holder = jQuery('div.aside').css({height: jQuery('.main-holder').height()});
	var scrollable = jQuery('div.ad', holder);
	var scrollableY = scrollable.offset().top;
	var blockOffset = 12;
	var duration = 200;
	jQuery(window).scroll(function(){
		var scrollY = jQuery(window).scrollTop();
		var sumHeight = scrollable.outerHeight();
		var holderHeight = holder.outerHeight() + 70;
		var topPos = scrollY - scrollableY + blockOffset;
		if(scrollY > scrollableY - blockOffset && topPos + sumHeight < holderHeight){
			if(jQuery(window).scrollTop() > scrollableY){
				scrollable.stop().animate({top: $(window).scrollTop() - scrollableY + blockOffset}, duration);
			}
		}else if(topPos + sumHeight > holderHeight){
			scrollable.stop().animate({top: holderHeight - sumHeight}, duration);
		}else if(jQuery(window).scrollTop() < scrollableY){
			scrollable.stop().animate({top: 0}, duration);
		}
	});
}

// movingBlock v1.0.0

jQuery.fn.movingBlock = function(_options){    
	// defaults options	    
	var _options = jQuery.extend({
		holder:'div.wrapper',
		topDif:0,
		bottomDif:0,
		toEl:false,
		cssTop:'marginTop',
		duration:350
	},_options);
	
	return this.each(function(){
		var _obj = jQuery(this);
		var _holder = _obj.parents(_options.holder);
		var _offset = _obj.offset();
		var _thisHeight = _obj.outerHeight(true);
		var _prop = _options.cssTop.toString();
		var _duration = _options.duration;
		var _defTop = parseInt(_obj.css(_prop));
		var _move = true, _height;
		if (!_defTop) _defTop = 0;
		var _toEl = (_options.toEl) ? jQuery(_options.toEl) : false;
		var _toElTop = (_options.toEl) ? _toEl.offset().top : 0;
		var _tD = parseInt(_options.topDif)
		var _top = _offset.top - _tD + _defTop;
		var _bottom = _holder.innerHeight() - _options.bottomDif - _thisHeight - _defTop;
		
		if (_options.toEl) {
			_offset.bottom = jQuery(_options.toEl).offset().top;
		}
		
		var _win = jQuery(window);
		var _cmd;
		
		_obj.bind('refresh', function(){
			_cmd = true;
			winResize();
			animateBlock();
		});
		
		_win.scroll(function(){
			winResize();
			animateBlock();
		});
		_win.resize(function(){
			winResize();
			animateBlock();
		});
		animateBlock();
		winResize();
		function winResize(){
			_thisHeight = _obj.outerHeight(true);
			if (window.innerHeight) _height = window.innerHeight;
			else _height = document.documentElement.clientHeight;
			if (_thisHeight >= _height) _move = false;
			else _move = true;
		}
		function animateBlock(){
			var _posTop = parseInt(_obj.position().top);
			var _scrollTop = parseInt(_win.scrollTop());
			var _dif = _scrollTop - _top;
			
			if (_toEl && _scrollTop - _toElTop + _tD > 0) _dif = _toElTop - _top -_tD;
			if(_move){
				if (_dif >= _bottom) _dif = _bottom;
				if (_dif > 0) {
					eval('_obj.animate({'+_prop+':_dif+_defTop}, {duration:_duration, queue:false})');
				} else {
					eval('_obj.animate({'+_prop+':_defTop}, {duration:_duration, queue:false})');
				}
			}else{
				if(_toElTop && _scrollTop <= _toElTop){
					if(_scrollTop < _posTop){
						eval('_obj.animate({'+_prop+':_scrollTop}, {duration:_duration, queue:false})');
					}else if (_scrollTop > _posTop + (_thisHeight - _height) || _cmd){
						if(_scrollTop - (_thisHeight - _height) > 0){
							eval('_obj.animate({'+_prop+':_scrollTop - (_thisHeight - _height)}, {duration:_duration, queue:false})');
						}
						_cmd = false;
					}
				}else{
					eval('_obj.animate({'+_prop+':_dif}, {duration:_duration, queue:false})');
				}
			}
		}
	});
}
