var ProductList = new Class({
	initialize : function() {
		this.accordion = new Accordion('li.atStart', 'ul.atStart', {
			opacity: false}, $('accordion'));
		var section = Cookie.get('section');
		//alert(section);
		// pull section from url if it exists
		var sectionArray = document.location.href.split("&section=");
		if ( sectionArray.length == 2 ) {
			//section = section.split("&");
			//if ( section.length > 0 ) {
				this.accordion.display(parseInt(sectionArray[1]));
			//} else {
			//	this.accordion.display(-1);
			//}
		} else {
			if ( section ) {
				this.accordion.display(section);
			} else {
				this.accordion.display(-1);
			}
		}
		// hook up an event to store the cookie
		var i = 0;
		var togglers = $('accordion').getElements('li[class*=toggler]').each( function(item) {
			item.accordionIndex = i++;
			item.getFirst().addEvent('click',function(e) {
				new Event(e).stop();
				this.accordion.display(-1);
				Cookie.remove('section');
				Cookie.set('section',item.accordionIndex,{path : '/'});
				var el = item.getFirst();
				var url = el.getProperty('href');
				
				(function(){
				    this.onClickSection(url)
				}.bind(this)).delay(1000);
			}.bind(this));
		},this)
	},
	onClickSection : function(url) {
		//alert("here");
		document.location = url;
	}
});

window.addEvent('domready', function(){
	var productList = new ProductList();
});
