jQuery(function($){
	initCustomForms();
	//initAttachFile();
	//initClearInputs();
	initMoreContent();
	initProfileHover();
	initLastChild();

});

function initLastChild(){
	jQuery('.homepage-holder .box-inner:last-child, .box-comments:last-child, .profdetail-holder .box-inner:last-child, \
	.profiles .box-inner:last-child, .box-jobs:last-child').css('border','none');
}

// more content init
function initMoreContent(){

	var duration = (jQuery.browser.msie && jQuery.browser.version < 9) ? 0 :600;
	jQuery('.more-comments-area').each(function(){
		var _this = jQuery(this);
		var linkHolder = jQuery('.link-more', _this);
		var link = jQuery('.link-more a', _this);
		var holder = jQuery('.more-comments-holder', _this);
		link.click(function(){
			linkHolder.slideUp(duration);
			holder.fadeIn(duration);
			return false;
		});

	});

		//more checkboxes

		jQuery('.more-checkboxes-area .link-more').click(function(){
			jQuery(this).hide(duration);
			jQuery(this).parent().find('.more-checkboxes-holder').fadeIn(duration);
			return false;
		});

		//end

}

//profile hover init
function initProfileHover(){
	jQuery('.box-profile').mouseover(function(){
		jQuery(this).addClass('active');
		jQuery(this).find('.profile-info').css({'position':'relative', 'z-index':'20'});
		jQuery(this).find('.img-click').css({'position':'relative', 'z-index':'20'});
		jQuery(this).find('.wrapper-profile').show();
	});

	jQuery('.box-profile').mouseleave(function(){
		jQuery(this).removeClass('active');
		jQuery(this).find('.wrapper-profile').hide();
		jQuery(this).find('.profile-info').css({'position':'static', 'z-index':'0'});
		jQuery(this).find('.img-click').css({'position':'static', 'z-index':'0'});
	});

}

// attach file init
function initAttachFile(){
	var classOuttaHere = 'outtaHere';
	var classActive = 'attach-active';
	jQuery('input:file').each(function(ind){
		var _this = jQuery(this);
		var text = _this.attr("title");
		var holder = jQuery('<span class="attach-holder"><a class="attach" href="#">' + text + '</a><a class="clear-attach">clear attach</a></span>');
		var link = jQuery('.attach', holder);
		var clear = jQuery('.clear-attach', holder);

		_this.addClass(classOuttaHere);
		_this.after(holder);
		link.click(function(){
			_this.click();
			return false;
		});

		_this.change(function(){
			if(jQuery(this).val().length){
				link.html(jQuery(this).val());
				holder.addClass(classActive);
			} else {
				link.html(text);
				holder.removeClass(classActive);
			}
		});

		clear.click(function(){
			_this.val('');
			link.html(text);
			holder.removeClass(classActive);
		});
	});
}

// clear inputs init
function initClearInputs(){
	jQuery ('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
}

// custom forms init
function initCustomForms(){
	jQuery('select:not(".multiple")').customSelect();
	jQuery('input:radio').customRadio();
	jQuery('input:checkbox').customCheckbox();
}
// custom forms plugin
(function(jQuery){
	// custom checkboxes module
	jQuery.fn.customCheckbox = function(_options){
		var _options = jQuery.extend({
			checkboxStructure: '<div></div>',
			checkboxDisabled: 'disabled',
			checkboxDefault: 'checkboxArea',
			checkboxChecked: 'checkboxAreaChecked'
		}, _options);
		return this.each(function(){
			var checkbox = jQuery(this);
			if(!checkbox.hasClass('outtaHere') && checkbox.is(':checkbox')){
				var replaced = jQuery(_options.checkboxStructure);
				this._replaced = replaced;
				if(checkbox.is(':disabled')) replaced.addClass(_options.checkboxDisabled);
				else if(checkbox.is(':checked')) replaced.addClass(_options.checkboxChecked);
				else replaced.addClass(_options.checkboxDefault);

				replaced.click(function(){
					if(checkbox.is(':checked')) checkbox.removeAttr('checked');
					else checkbox.attr('checked', 'checked');
					changeCheckbox(checkbox);
				});
				checkbox.click(function(){
					changeCheckbox(checkbox);
				});
				replaced.insertBefore(checkbox);
				checkbox.addClass('outtaHere');
			}
		});
		function changeCheckbox(_this){
			_this.change();
			if(_this.is(':checked')) _this.get(0)._replaced.removeClass().addClass(_options.checkboxChecked);
			else _this.get(0)._replaced.removeClass().addClass(_options.checkboxDefault);
		}
	}

	// custom radios module
	jQuery.fn.customRadio = function(_options){
		var _options = jQuery.extend({
			radioStructure: '<div></div>',
			radioDisabled: 'disabled',
			radioDefault: 'radioArea',
			radioChecked: 'radioAreaChecked'
		}, _options);
		return this.each(function(){
			var radio = jQuery(this);
			if(!radio.hasClass('outtaHere') && radio.is(':radio')){
				var replaced = jQuery(_options.radioStructure);
				this._replaced = replaced;
				if(radio.is(':disabled')) replaced.addClass(_options.radioDisabled);
				else if(radio.is(':checked')) replaced.addClass(_options.radioChecked);
				else replaced.addClass(_options.radioDefault);
				replaced.click(function(){
					if(jQuery(this).hasClass(_options.radioDefault)){
						radio.attr('checked', 'checked');
						changeRadio(radio.get(0));
					}
				});
				radio.click(function(){
					changeRadio(this);
				});
				replaced.insertBefore(radio);
				radio.addClass('outtaHere');
			}
		});
		function changeRadio(_this){
			jQuery(_this).change();
			jQuery('input:radio[name='+jQuery(_this).attr("name")+']').not(_this).each(function(){
				if(this._replaced && !jQuery(this).is(':disabled')) this._replaced.removeClass().addClass(_options.radioDefault);
			});
			_this._replaced.removeClass().addClass(_options.radioChecked);
		}
	}

	// custom selects module
	jQuery.fn.customSelect = function(_options){
		var _options = jQuery.extend({
			selectStructure: '<div class="selectArea"><span class="left"></span><span class="center"></span><a href="#" class="selectButton"></a><div class="disabled"></div></div>',
			hideOnMouseOut: false,
			copyClass: true,
			selectText: '.center',
			selectBtn: '.selectButton',
			selectDisabled: '.disabled',
			optStructure: '<div class="optionsDivVisible"><div class="select-top"></div><div class="select-center"><ul></ul><div class="select-bottom"></div></div>',
			optList: 'ul'
		}, _options);
		return this.each(function() {
			var select = jQuery(this);
			if(!select.hasClass('outtaHere')){
				if(select.is(':visible')){
					var hideOnMouseOut = _options.hideOnMouseOut;
					var copyClass = _options.copyClass;
					var replaced = jQuery(_options.selectStructure);
					var selectText = replaced.find(_options.selectText);
					var selectBtn = replaced.find(_options.selectBtn);
					var selectDisabled = replaced.find(_options.selectDisabled).hide();
					var optHolder = jQuery(_options.optStructure);
					var optList = optHolder.find(_options.optList);
					if(copyClass) optHolder.addClass('drop-'+select.attr('class'));

					if(select.attr('disabled') || select.attr('readonly')) {
                        selectBtn.hide();
                        selectDisabled.show();
                    }
					select.find('option').each(function(){
						var selOpt = jQuery(this);
						var _opt = jQuery('<li><a href="#">' + selOpt.html() + '</a></li>');
						if(selOpt.attr('selected')) {
							selectText.html(selOpt.html());
							_opt.addClass('selected');
						}
						_opt.children('a').click(function(){
							optList.find('li').removeClass('selected');
							select.find('option').removeAttr('selected');
							jQuery(this).parent().addClass('selected');
							selOpt.attr('selected', 'selected');
							selectText.html(selOpt.html());
							select.val(selOpt.val());
							select.change();
							optHolder.hide();
							return false;
						});
						optList.append(_opt);
					});
					replaced.width(select.outerWidth());
					replaced.insertBefore(select);
					optHolder.css({
						width: select.outerWidth(),
						display: 'none',
						position: 'absolute'
					});
					jQuery(document.body).append(optHolder);

					var optTimer;
					replaced.hover(function() {
						if(optTimer) clearTimeout(optTimer);
					}, function() {
						if(hideOnMouseOut){
							optTimer = setTimeout(function(){
								optHolder.hide();
							}, 200);
						}
					});
					optHolder.hover(function(){
						if(optTimer) clearTimeout(optTimer);
					}, function() {
						if(hideOnMouseOut){
							optTimer = setTimeout(function(){
								optHolder.hide();
							}, 200);
						}
					});
					selectBtn.click(function(){
						if(optHolder.is(':visible')){
							optHolder.hide();
						}
						else{
							if(_activeDrop) _activeDrop.hide();
							optHolder.children('ul').css({height:'auto', overflow:'hidden'});
							optHolder.css({
								top: replaced.offset().top + replaced.outerHeight(),
								left: replaced.offset().left,
								display: 'block'
							});
							if(optHolder.children('ul').height() > 200) optHolder.children('ul').css({height:200, overflow:'auto'});
							_activeDrop = optHolder;
						}
						return false;
					});
					replaced.addClass(select.attr('class'));
					select.addClass('outtaHere');

					select.data('refresh', function() {
						selectBtn.remove();
						optHolder.remove();
						replaced.remove();
						select.show();
						select.removeClass('outtaHere');
						select.customSelect();
					});
				}
			}
		});
	}

	// event handler on DOM ready
	var _activeDrop;
	jQuery(function(){
		jQuery('body').click(hideOptionsClick)
		jQuery(window).resize(hideOptions)
	});
	function hideOptions() {
		if(_activeDrop && _activeDrop.length) {
			_activeDrop.hide();
			_activeDrop = null;
		}
	}
	function hideOptionsClick(e) {
		if(_activeDrop && _activeDrop.length) {
			var f = false;
			jQuery(e.target).parents().each(function(){
				if(this == _activeDrop) f=true;
			});
			if(!f) {
				_activeDrop.hide();
				_activeDrop = null;
			}
		}
	}
})(jQuery);

