/*  
	MultiSlide, version 1.0
	(c) 2009 Intereactive, LLC - Ryan Hargrave
	http://blueprint.intereactive.net/multislide-for-mootools
	
	Compatible with Mootools v1.2 - http://mootools.net
	Extends the capability of the Fx.Slide Class  - http://mootools.net/docs/Plugins/Fx.Slide
	
	This code is freely distributable under the terms of an MIT-style license.
*/

var multislide = new Class({
	
	Implements: [Events, Options],

	options: {
	
		slide_duration:			300,
		slide_transition: 		'sine:in:out',
		class_expand_all: 		'expand_all',
		class_collapse_all: 	'collapse_all',
		class_toggler: 			'ms_toggler',
		class_expander: 		'ms_expander',
		class_ecbutton:			'ecButton',
		build_ecbuttons:		false,
		jumpToHash:				true,
		
		//these take functions
		addTransition:   		$empty,
		onExpand: 				$empty,
		onCollapse: 			$empty, 
		onExpand_all: 			$empty, 
		onCollapse_all: 		$empty
	},
	
	//sets up the animations for the team layout
	initialize: function(container_class, options) {
		this.setOptions(options);
		
		//set empty array to hold all the sliders
		this.person_slide = [];
		this.expanders = []; // PCC - Added by DT
		this.togglers = []; // PCC - Added by DT

		//create the sliders and set up event listeners
		$$('.'+container_class).each(function(container, i){
			//Added expand/collaspe all links - SAS
			if(i == 0 && this.options.build_ecbuttons) { //add only before first toggle element
				var buttonWrap = new Element('p');
				var eAll = new Element('a', {
					'href': '#', 
					'class': this.options.class_expand_all+' '+this.options.class_ecbutton,
					'html': 'Expand All'
				});
				var cAll = new Element('a', {
					'href': '#', 
					'class': this.options.class_collapse_all+' '+this.options.class_ecbutton,
					'html': 'Collapse All'
				});
				
				buttonWrap.inject(container, 'before');
				buttonWrap.grab(eAll);
				buttonWrap.grab(cAll);
			}
			
			this.addEvent('transitioning', function(start) { // PCC - added by SAS for SideBarFixer
					if(!this.options.addTransition) return;
					if(start) {
						this.transitionEffects = $clear(this.transitionEffects);
						this.transitionEffects = (function(){(this.options.addTransition).attempt()}).periodical(10, this);
					}
					else {
						this.transitionEffects = $clear(this.transitionEffects);
					}
				}.bind(this)
			);

			this.togglers[i] = container.getElement('.'+this.options.class_toggler); // PCC - modded by DT
			
			//set up the slide instance for each element and hide all the sliders
			this.expanders[i] = container.getElement('.'+this.options.class_expander) // PCC - modded by DT							  
			this.person_slide[i] = new Fx.Slide(this.expanders[i], { // PCC - modded by DT
					duration: this.options.slide_duration, 
					transition: this.options.slide_transition, 
					link : 'ignore',
					onStart : function(){
						if(this.person_slide[i].open){
							this.fireEvent('collapse', this.togglers[i]); // PCC - modded by DT
						} else {
							this.fireEvent('expand', this.togglers[i]); // PCC - modded by DT
						}
						this.fireEvent('transitioning', 1); // PCC - added by SAS
					}.bind(this),
					onComplete: function(){
						this.fireEvent('transitioning'); // PCC - added by SAS
					}.bind(this)
			}).hide();
			
			//add the toggle slide event		
			this.togglers[i].addEvent('click', function(e){ // PCC - modded by DT
					e.stop();
					this.person_slide[i].toggle();
			}.bind(this));
		}.bind(this));
		
		$$('.'+this.options.class_expand_all).addEvent('click', function(e){
			e.stop();
			this.expand_all();
		}.bind(this));
		
		
		$$('.'+this.options.class_collapse_all).addEvent('click', function(e){
			e.stop();
			this.collapse_all();
		}.bind(this));
		
		if(this.options.jumpToHash) {
			//make sure empty anchors have height to scroll to properly - add by SAS
			$$('a[id]').each(function(item){
				if(item.getSize().y == 0 && item.getProperty('html') == '') {
					item.setStyles({
						'height' : '1em',
						'width' : 0,
						'display' : 'inline-block'
					});
				}
			});
			
			//read url hash, strip off the leading #, get element if exists, and go straight to it
			this.expand_ifcontains($(window.location.hash.substr(1)), 1);
			
			//add click events for local page links to hashes in multislide
			$(document.body).getElements('a[href*=#]').each(function(item, i){
				var id = /#[^\?]*/.exec(item.getProperty('href'))[0].substr(1);
				if(id!='') {
					item.addEvent('click', function(id){
							this.expand_ifcontains($(id));
						}.pass(id, this)
					);
				}
			}, this);
		}

	},
	
	expand_all: function() {
		//run the effect on the entire array
		this.person_slide.each(function(sl){
			if(!sl.open){
				sl.slideIn();
			}
		});
		this.fireEvent('expand_all');
	},
	
	collapse_all: function() {
		//run the effect on the entire array
		this.person_slide.each(function(sl){
			if(sl.open){
				sl.slideOut();
			}
		});
		this.fireEvent('collapse_all');
	},

	// PCC - Added by DT; modified by SAS to accomodate immediate display if in url hash
	expand_ifcontains: function(el, set) {
		this.person_slide.some(function(sl, i){ //returns true if element is in a slider
			if (this.expanders[i].hasChild(el) | this.togglers[i].hasChild(el)) {
				if(!sl.open){
					if(set) {//go straight to it
						sl.show();
						var myFx = new Fx.Scroll(window).set(el.getCoordinates().left, el.getCoordinates().top);
					}
					else {//go to by scrolling
						sl.addEvent('complete', function(){
							var myFx = new Fx.Scroll(window).toElement(el);
						});
						sl.slideIn();
					}
				}
			}
		},this);
	}
});
