/*global jQuery */
var Squish;

/**
 * An accordion widget.
 * Author: Thomas Peri
 * Modified: 2010-08-31
 */
Squish = function (settings) {
	var $ = jQuery,
		setup, // private method below
		items = [], // the array of items 
		current = -1, // the index of the currently-open item
	
		// SETTINGS
		
		// optional open and close transitions
		transitions = settings.transitions || {},
		
		// selector for the element that fully encloses an item, usually an LI.
		item = settings.item,
		
		// selector for the element that gets collapsed and expanded.
		content = settings.content,
		
		// selector for the element that is shown always, 
		// and serves as the trigger for opening and closing.
		heading = settings.heading,
		
		// the css class for open items.
		openClass = settings.openClass;

	/*
	 * How to set up each squish item.
	 */
	setup = function (itemObj) {
	
		var curr; // shortcut for items[current], the current item
	
		// make the heading *look* clickable...
		$(itemObj.heading).css({
			cursor: 'pointer'
			
		// and then make it *actually* clickable.
		}).click(function () {
		
			// collaspe the current one
			if (current >= 0) {
				curr = items[current];
				$(curr.item).removeClass(openClass);
				$(curr.content).animate({
					height: 0
				});
				if (transitions.close) {
					transitions.close.apply(curr.item);
				}
			} 
			
			if (itemObj.index === current) {
				// if the clicked one was the current one, 
				// then it's already collapsing,
				// nothing needs to expand,
				// and there will be no new current one.
				current = -1;
			} else {
				// otherwise (when the clicked one was NOT the current one),
				// make this be the current one, and expand it.
				current = itemObj.index;
				$(itemObj.item).addClass(openClass);
				$(itemObj.content).animate({
					height: itemObj.height
				});
				if (transitions.open) {
					transitions.open.apply(itemObj.item);
				}
			}
		});
	};

	// Hide the content elements until the document is ready.
	$(content).css({display: 'none'});
	
	$(function () {
		var j; // loop control
		
		// Show the content elements before operating on them.
		$(content).css({display: 'block'});
		
		// create each item object, keeping track of each "item" element
		// and its index in the array.
		$(item).each(function (i) {
			items.push({
				item: this,
				index: i
			});
		});
		
		// add to each item object: its content element and the open height thereof,
		// then set the height to zero to close it.
		$(content).each(function (i) {
			items[i].content = this;
			items[i].height = $(this).height();
			$(this).css({
				height: 0,
				overflow: 'hidden'
			});
		});
		
		// add to each item object its heading element
		$(heading).each(function (i) {
			items[i].heading = this;
		});
		
		// set up each item object further...
		for (j = 0; j < items.length; j += 1) {
			setup(items[j]);
		}
	});
};

