﻿var NewsTrigger = new Class({
	initialize: function(trigger, parent) {
		this.Element	= trigger;		
		this.Parent		= parent;		
		//this.Content	= trigger.getNext();
		this.Content	= $('ExpandedSection');		
		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;
	   
	
	    this.Container.setStyles({
			'overflow':			'hidden',
			'padding-bottom':	'1px'
		});

					
		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('ExpandedGreyHeading'))
//		{
//			this.Element.removeClass('ExpandedGreyHeading');
//			this.Element.addClass('CollapsedGreyHeading');
//		}
//		else
//		{
			this.Element.addClass('CollapsedSearchHeading');
			this.Element.removeClass('ExpandedSearchHeading');
//		}
		
		
		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('CollapsedGreyHeading'))
//		{
//			this.Element.removeClass('CollapsedGreyHeading');
//			this.Element.addClass('ExpandedGreyHeading');
//		}
//		else
//		{
			this.Element.removeClass('CollapsedSearchHeading');
			this.Element.addClass('ExpandedSearchHeading');
//		}
			
		
		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 SearchAccordion = new Class({ 
	initialize: function(triggers) {
		this.Triggers = new Array();
			
		var pathname =  location.pathname; 
		
	
		$each(triggers, function(trigger) { this.AddTrigger($(trigger)); }, this);
		
		this.CloseAll(true);
		
		if(pathname == "/SearchList.aspx")
		{
		    this.Triggers[0].Open(false);
		}
	},
	
	AddTrigger: function(trigger) {
	
		this.Triggers[this.Triggers.length] = new NewsTrigger(trigger, this);
	},
	
	CloseAll: function(quick, exception) {
		$each(
			this.Triggers,
			function(trigger) {
				if (trigger !== exception)
					trigger.Close(quick);
			}
		);
	}
});

