if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {
	showAccordion : null,
	currentAccordion : null,
	duration : null,
	effects : [],
	animating : false,

	slid1: false,
	slid2: false,
	slid3: false,
	slid4: false,
	slid5: false,
	slid6: false,
	slid7: false,
	slid8: false,	
	
	initialize: function(container, options) {
	  if (!$(container)) {
	    throw(container+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'accordion_toggle',
				toggleActive : 'accordion_toggle_active',
				content : 'accordion_content'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
		accordions.each(function(accordion) {
			Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
			if (this.options.onEvent == 'click') {
			  accordion.onclick = function() {return false;};
			}
			
			if (this.options.direction == 'horizontal') {
				var options = {width: '0px', display:'none'};
			} else {
				var options = {height: '0px', display:'none'};
			}
//			options.merge({display: 'none'});			
			
			this.currentAccordion = $(accordion.next(0)).setStyle(options);
		}.bind(this));
	},
	
	activate : function(accordion) {
		
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));
		
		// We are trying to clos an item. Instead of deactivating the current item, we simply want to activate the one to the left of it.
		if (this.currentAccordion == this.showAccordion) {
			this.currentAccordion = $(accordion.previous(0));
		}else{
			// We are opening an item. If we are opening anything other than services, we need to edit the styles for services.
			if (accordion.id=="tab_services"){
				$("tab_services").addClassName("services");
				$("tab_services").removeClassName("farleft");
			}else{
				$("tab_services").addClassName("farleft");
				$("tab_services").removeClassName("services");
			}


//alert(accordion.id);

			// Kick off the auto-scroll for a given section.


			if (accordion.id=="tab_services" && (this.slid1==false)) {
				this.slid1 = true;
				hCarousel1.intervalHandle = self.setInterval("hCarousel1.autoanimate('hCarousel1')", 500);
			}

			if (accordion.id=="tab_patients" && (this.slid2==false)){
				this.slid2 = true
				hCarousel2.intervalHandle = self.setInterval("hCarousel2.autoanimate('hCarousel2')", 500);
			}

			if (accordion.id=="tab_inspired" && (this.slid3==false)){
				this.slid3 = true
				hCarousel3.intervalHandle = self.setInterval("hCarousel3.autoanimate('hCarousel3')", 500);
			}

			if (accordion.id=="tab_find_physician" && (this.slid4==false)){
				this.slid4 = true;
				hCarousel4.intervalHandle = self.setInterval("hCarousel4.autoanimate('hCarousel4')", 500);
			}

			if (accordion.id=="tab_resources"  && (this.slid5==false)){
				this.slid5 = true;
				hCarousel5.intervalHandle = self.setInterval("hCarousel5.autoanimate('hCarousel5')", 500);
			}

			if (accordion.id=="tab_for_physicians"   && (this.slid6==false)){
				this.slid6 = true;				
				hCarousel6.intervalHandle = self.setInterval("hCarousel6.autoanimate('hCarousel6')", 500);
			}

			if (accordion.id=="tab_get_involved" && (this.slid7==false)){
				this.slid7 = true;
				hCarousel7.intervalHandle = self.setInterval("hCarousel7.autoanimate('hCarousel7')", 500);
			}

			if (accordion.id=="tab_about_us" && (this.slid8==false)){
				this.slid8 = true;
				hCarousel8.intervalHandle = self.setInterval("hCarousel8.autoanimate('hCarousel8')", 500);
			}



		}

		this.currentAccordion.setStyle({
			display: 'block'
		});		
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
		
		// Instead of deactivating the current item, we simply want to activate the one to the left of it.
		this._handleAccordion();
		
	},



	
	activateOnly : function(accordion) {
		
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));
		
		
		// Instead of deactivating the current item, we simply want to activate the one to the left of it.
		if (this.currentAccordion == this.showAccordion) {
			return;
			//this.currentAccordion = $(accordion.previous(0));
		}else{
			// We are opening an item. If we are opening anything other than services, we need to edit the styles for services.
			if (accordion.id=="tab_services" || accordion.id=="tab_home"){
				$("tab_services").addClassName("services");
				$("tab_services").removeClassName("farleft");
			}else{
				$("tab_services").addClassName("farleft");
				$("tab_services").removeClassName("services");
			}
		}

		this.currentAccordion.setStyle({
			display: 'block'
		});		
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
		
		// Instead of deactivating the current item, we simply want to activate the one to the left of it.
		this._handleAccordion();
		
	},


	activateInstant : function(accordion) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));

		// ROB - Instead of deactivating the current item, we simply want to activate the one to the left of it.
		if (this.currentAccordion == this.showAccordion) {
			this.currentAccordion = $(accordion.previous(0));
		}else{
				if (accordion.id=="tab_home"){
				$("tab_services").addClassName("services");
				$("tab_services").removeClassName("farleft");
			}else{
				$("tab_services").addClassName("farleft");
				$("tab_services").removeClassName("services");
			}
		}

		this.currentAccordion.setStyle({
			display: 'block'
		});		
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
		
		//  Instead of deactivating the current item, we simply want to activate the one to the left of it.
		this._handleAccordionInstant();
	
	},
	// 
	// Deactivate an active accordion
	//
	deactivate : function() {

		var options = $H({
		  duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			},
			afterFinish: function() {
				this.showAccordion.setStyle({
          height: 'auto',
					display: 'none'
				});				
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});    
//    options.merge(this.scaling);

    this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
    
		new Effect.Scale(this.showAccordion, 0, options.update(this.scaling).toObject());
	},

  //
  // Handle the open/close actions of the accordion
  //
	_handleAccordion : function() {
		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			}
		});
		options.merge(this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options.update(this.scaling).toObject())
		);

		if (this.showAccordion) {
		
			//alert(this.showAccordion.id);			
				
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(this.scaling);
			
			this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options.update(this.scaling).toObject())
			);	

		}
		
    new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showAccordion) {

					if (this.showAccordion.id == "homepage"){
						// Change the home page background image when it is not 
						swapHomePageBackground();
					}

					this.showAccordion.setStyle({
						display: 'none'
					});				
				}
				$(this.currentAccordion).setStyle({
				  height: 'auto'
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;




			}.bind(this)
		});
	},

	_handleAccordionInstant : function() {
		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			}
		});
		options.merge(this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options.update(this.scaling).toObject())
		);

		new Effect.Parallel(this.effects, {
			duration: 0, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showAccordion) {
					this.showAccordion.setStyle({
						display: 'none'
					});				
				}
				$(this.currentAccordion).setStyle({
				  height: 'auto'
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}

}

