$(document).ready(function(){
	initOpenClose();
})
// Open-Close init
function initOpenClose() {
	$('.post').OpenClose({
		activeClass:'active',
		opener:'a.more',
		opener2:'h2 a',
		slider:'div.full-text',
		slider2:'div.intro-text',
		slideSpeed: 400
	});
}


// Open-Close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'short',
		opener:'.opener',
		opener2:'h2 a',
		slider:'.slide',
		slider2:'.slide2',
		slideSpeed: 400,
		animStart:false,
		animEnd:false,
		event:'click'
	},_options);
	
	hash = window.location.hash;

	return this.each(function(){
		// options
		
			var _holder = jQuery(this);
			var _slideSpeed = _options.slideSpeed;
			var _activeClass = _options.activeClass;
			var _opener = jQuery('>'+_options.opener, _holder);
			var _opener2 = jQuery('>'+_options.opener2, _holder);
			var _slider = jQuery('>'+_options.slider, _holder);
			var _slider2 = jQuery('>'+_options.slider, _holder);
			var _animStart = _options.animStart;
			var _animEnd = _options.animEnd;
			var _event = _options.event;
			if(_slider.length) {
			
				if(_holder.hasClass(_activeClass)) {_slider.show();_opener.html('Less')}
				else {_slider.hide();_opener.html('More')}
				
				// If it's in the hash in the URL, activate it
				if (hash && _holder.is(hash)) { _holder.addClass(_activeClass); _slider.show(); _opener.html('Less') }
				
				_opener2.bind(_event,function(){
					if(!_slider.is(':animated')) {
						if(typeof _animStart === 'function') _animStart();
						if(_holder.hasClass(_activeClass)){
							_opener.html('More');
							// _holder.removeClass(_activeClass).find(_options.slider).hide();
							
							_holder.removeClass(_activeClass).find(_options.slider).slideUp(_slideSpeed,function(){
								if(typeof _animEnd === 'function') _animEnd();
							});
							
						}	
						else{
							_opener.html('Less');
							// _holder.addClass(_activeClass).find(_options.slider).show();
							
							_holder.addClass(_activeClass).find(_options.slider).slideDown(_slideSpeed,function(){
								if(typeof _animEnd === 'function') _animEnd();
							});
							
						}
					}
					return false;
				});
			
				_opener.bind(_event,function(){
					if(!_slider.is(':animated')) {
						if(typeof _animStart === 'function') _animStart();
						if(_holder.hasClass(_activeClass)){
							$(this).html('More');
							// _holder.removeClass(_activeClass).find(_options.slider).hide();
							
							_holder.removeClass(_activeClass).find(_options.slider).slideUp(_slideSpeed,function(){
								if(typeof _animEnd === 'function') _animEnd();
							});
							
						}	
						else{
							$(this).html('Less');
							// _holder.addClass(_activeClass).find(_options.slider).show();
							
							_holder.addClass(_activeClass).find(_options.slider).slideDown(_slideSpeed,function(){
								if(typeof _animEnd === 'function') _animEnd();
							});
							
						}
					}
					return false;
				});
				
			}
	});
}


