/**
 * copyright (c) 2005-2006 - lx barjon <lx@iassa.com>
 * class collapsableBlocks
 * version : 0.1.1 (18-04-2006)
 * licence : GNU GPL
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************
 *
 * @author lx barjon <lx@iassa.com>
 * @access public
 * @version 0.1.1
 */

var collapsableBlocks = Class.create();
collapsableBlocks.prototype = {
	initialize: function( options ) {
		this.version = '0.1.1';

		if ( !document.getElementById ) return;

		// options
		Object.extend( this.options = {
			parent: 'none',
			pathToImgs: '',
			imgExpanded: 'minus.png',
			imgCollapsed: 'plus.png',
			imgInsert: 'before',                  // before OU after - positionnement des images
			// anim
			useAnim: true,
			expandAnim: Effect.SlideDown,
			collapseAnim: Effect.SlideUp,
			duration: 0.5,
			// ----
			expandOneByOne: true,
			expand: []                            // 'all' OU les numéros des block à ouvrir (ex: [1,3])
		}, options || {} );

		// currently expanded (used if this.options.expandOneByOne = true)
		if ( this.options.expandOneByOne ) this.currentlyExpanded = 'none';

		// get all collapsable blocks using class name
		if ( this.options.parent != 'none' ) this.blocks = document.getElementsByClassName( 'collapsable', this.options.parent );
		else this.blocks = document.getElementsByClassName( 'collapsable' );
		if ( this.blocks.length < 1 ) return; // do nothing if no block found
		// get corresponding top (blockID + '_handle') and hook events
		for( var cpt = 0; cpt < this.blocks.length; cpt++ ) {
			this.blocks[cpt].handle = $(this.blocks[cpt].id + '_handle' );
			if ( this.blocks[cpt].handle ) {
				var imgCode = '<img src="' + this.options.pathToImgs + this.options.imgCollapsed + '" id="' + this.blocks[cpt].id + '_imgStatus" class="imgStatus" />';
				if ( this.options.imgInsert == 'before' ) {
					new Insertion.Top( this.blocks[cpt].handle, imgCode );
				} else {
					new Insertion.Bottom( this.blocks[cpt].handle, imgCode );
				}
				this.blocks[cpt].handle.imgStatus = $(this.blocks[cpt].id + '_imgStatus');
				this.blocks[cpt].handle.style.cursor = 'pointer';
				Event.observe( this.blocks[cpt].handle, 'click', this.expandOrCollapse.bindAsEventListener(this) );
				Event.observe( this.blocks[cpt].handle.imgStatus, 'click', this.expandOrCollapse.bindAsEventListener(this) );
				if ( cpt == 0 && this.options.expandFirst ) {
					this.blocks[cpt].handle.imgStatus.src = this.options.pathToImgs + this.options.imgExpanded;
					Element.addClassName( this.blocks[cpt].handle, 'blockOn' );
					if ( this.options.expandOneByOne ) this.currentlyExpanded = this.blocks[cpt];
				}
				if ( this.options.expand.length > 0 && (this.options.expand[0] == 'all' || inArray((cpt+1), this.options.expand)) ) {
					this.blocks[cpt].handle.imgStatus.src = this.options.pathToImgs + this.options.imgExpanded;
					this.blocks[cpt].style.display = '';
					Element.addClassName( this.blocks[cpt].handle, 'blockOn' );
				} else {
					this.blocks[cpt].style.display = 'none';
					Element.addClassName( this.blocks[cpt].handle, 'blockOff' );
				}
			}
		}
	},

	expandOrCollapse: function( event ) {
		// get id by removing '_handle' from the end of the element id that triggered the event
		var block = $(Event.element(event).id.substring(0, Event.element(event).id.length - 7));
		// if no block -> imgStatus was clicked (remove '_imgStatus')
		if ( !block ) {
			block = $( Event.element(event).parentNode.id.substring(0, $(Event.element(event)).id.length - 10) );
		}
		if ( block.style.display == 'none' ) {
			if ( this.options.expandOneByOne && this.currentlyExpanded != 'none' ) {
				Element.removeClassName( this.currentlyExpanded.handle, 'blockOn' );
				Element.addClassName( this.currentlyExpanded.handle, 'blockOff' );
				if ( this.options.useAnim ) new this.options.collapseAnim( this.currentlyExpanded, {duration: this.options.duration} );
				else Element.hide( this.currentlyExpanded );
				this.currentlyExpanded.handle.imgStatus.src = this.options.pathToImgs + this.options.imgCollapsed;
			}
			Element.removeClassName( block.handle, 'blockOff' );
			Element.addClassName( block.handle, 'blockOn' );
			if ( this.options.useAnim ) new this.options.expandAnim( block, {duration: this.options.duration} );
			else Element.show( block );
			if ( this.options.expandOneByOne ) this.currentlyExpanded = block;
			block.handle.imgStatus.src = this.options.pathToImgs + this.options.imgExpanded;
		} else {
			if ( this.options.expandOneByOne ) this.currentlyExpanded = 'none';
			Element.removeClassName( block.handle, 'blockOn' );
			Element.addClassName( block.handle, 'blockOff' );
			if ( this.options.useAnim ) new this.options.collapseAnim( block, {duration: this.options.duration} );
			else Element.hide( block );
			block.handle.imgStatus.src = this.options.pathToImgs + this.options.imgCollapsed;
		}
		// prevent propagation (in case imgStatus is clicked)
		if ( event && event.stopPropagation && event.preventDefault ) {
			event.stopPropagation();
			event.preventDefault();
		}
		if ( window.event ) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return false;
		}
	}
}