/**
 * @fileoverview Collection Accordion
 * 
 */
var collectionAccordion = function(){

	// 
	var $expandLinks = null,
	
	//
	viewingAll = false;
	

	
	/**
	 * The options passed through to this function
	 *
	 * @var Object
	 * @private
	 */
	var options = {

		animationTime : 250

	};
	
	
	/**
	 * Initialise the functionality
	 * @param {Object} options The initialisation options
	 * @return void
	 * @public
	 */
	var init = function(initOptions) {
		
		// save any options sent through to the intialisation script, if set
		for (var option in options) {
			if (!!initOptions[option] || initOptions[option] === false) {
				options[option] = initOptions[option];
			}
			
			// error check, if no element is specified then stop
			if (!options[option] && options[option] !== false && options[option] !== 0) {
				throw('Required option not specified: ' + option);
				//return false;
			}
		}
	
	
		// find and initialise all expandable links
		$expandLinks = $("p.expand a, a.expand");
		
		$expandLinks.bind('click', function(e){
			e.preventDefault();
			this.blur();
			if ($(this).parent().parent().hasClass('open')) {
				collapseBlock();
			} else {
				openBlock(this);
			}
		});
	};
	
	
	
	/*
	 *
	 */
	var openBlock = function(el) {
		closeAllBlocks();
		
		var block = $(el).parent().parent();
		
		var imgHeight = $( block ).find('div.collection-image-container img').height();
		var expandHeight = $( block ).find('div.expandable').height();
		
		$( block ).addClass('open');
		$( block ).find('.collection-image-container').stop().animate({height:imgHeight+"px"}, options.animationTime);
		$( block ).find('.expandable-wrap').stop().animate({height:expandHeight+"px"}, options.animationTime);
		$( block ).find('p.expand a').text('Read less');
	};
	
	
	
	/*
	 *
	 */
	var collapseBlock = function() {
		closeAllBlocks();
	};


	
	/*
	 *
	 */
	var closeAllBlocks = function() {
		$('div.collection-detail').each(function(index){
			$( this ).removeClass('open');
			$( this ).find('.collection-image-container').stop().animate({height:"100px"}, options.animationTime);
			$( this ).find('.expandable-wrap').stop().animate({height:0}, options.animationTime);
			$( this ).find('p.expand a').text('Read more');
		});
	};
	
	
	/*
	 * 
	 */
	return {
		init: init
	};
	
}();


