﻿var CoursesLocationTrigger = new Class({
	initialize: function(trigger, parent) {
		this.Element	= trigger;
		this.Parent		= parent;
		this.Content	= trigger.getNext();
		this.Container	= new Element('div');
		this.Effect		= new Fx.Styles(this.Container, {duration:320, transition:Fx.Transitions.circOut});
		this.IsOpen		= true;
		
		
		
		this.Content.replaceWith(this.Container).appendChild(this.Content);
		this.MaxHeight	= this.Content.getSize().scrollSize.y;
		if(this.MaxHeight > 201)
		{
			this.Container.setStyles({
			'overflow-x':			'hidden',
			'overflow-y':			'scroll',
			'padding-bottom':	'1px',
			'width':			'335px'
			});
			
		}
		else
		{
			this.Container.setStyles({
			'overflow-x':			'hidden',
			'overflow-y':			'hidden',
			'padding-bottom':	'1px',
			'width':			'335px'
			});
		}		
		this.MaxHeight	=	'202';
					
		this.Element.addEvent('click', this.Toggle.bind(this, [false]));
		this.Element.setStyle('cursor', 'pointer');
	},
			
	Close: function(quick) {
		this.IsOpen = false;
		this.Effect.stop();
		if(this.Element.hasClass('ExpandedOrangeLocationHeading'))
		{
			this.Element.removeClass('ExpandedOrangeLocationHeading');
			this.Element.addClass('CollapsedOrangeLocationHeading');
		}
		else
		{
			this.Element.addClass('CollapsedPinkLocationHeading');
			this.Element.removeClass('ExpandedPinkLocationHeading');
		}
		
		
		if (quick) {
			this.Effect.set({
				height:		[0],
				opacity:	[0]
			});
		}
		else {
			this.Effect.start({
				height:		[0],
				opacity:	[0]
			});
		}
	},
	
	Open: function(quick) {
		this.IsOpen = true;
		this.Effect.stop();
		if(this.Element.hasClass('CollapsedOrangeLocationHeading'))
		{
			this.Element.removeClass('CollapsedOrangeLocationHeading');
			this.Element.addClass('ExpandedOrangeLocationHeading');
		}
		else
		{
			this.Element.removeClass('CollapsedPinkLocationHeading');
			this.Element.addClass('ExpandedPinkLocationHeading');
		}
			
		
		if (quick) {
			this.Effect.set({
				height:		[this.MaxHeight],
				opacity:	[1]
			});
		}
		else {
			this.Effect.start({
				height:		[this.MaxHeight],
				opacity:	[1]
			});
		}
		
		this.Parent.CloseAll(false, this);
	},
	
	Toggle: function(quick) {
		if (this.IsOpen)
			this.Close(quick);
		else
			this.Open(quick);
	}
});

var CoursesLocationAccordion = new Class({
	initialize: function(triggers) {
		this.Triggers = new Array();
		
		$each(triggers, function(trigger) { this.AddTrigger($(trigger)); }, this);
		
		this.CloseAll(true);
		this.Triggers[0].Open(true);
	},
	
	AddTrigger: function(trigger) {
		this.Triggers[this.Triggers.length] = new CoursesLocationTrigger(trigger, this);
	},
	
	CloseAll: function(quick, exception) {
		$each(
			this.Triggers,
			function(trigger) {
				if (trigger !== exception)
					trigger.Close(quick);
			}
		);
	}
});