// accordion.js v3.0
//
// Copyright (c) 2007 stickmanlabs
// Author: Kevin P Miller | http://www.stickmanlabs.com
// 
// Accordion is freely distributable under the terms of an MIT-style license.
//
// I don't care what you think about the file size...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

// Riadattato PESANTEMENTE da DiGiness 22/12/2008

/*-----------------------------------------------------------------------------------------------*/

if (typeof Element == 'undefined')
	throw("accordion.js requires the Prototype library!");

var Accordion = Class.create();
Accordion.prototype = {

	steps: null,
	currentStep: null,

	initialize: function(container, steps, options) {
		if (!$(container)) {
			throw(container+" doesn't exist!");
			return false;
		}
		
		if (steps.size == 0) {
			throw("Deve essere specificato almeno uno step!")
			return false;
		}
		else {
			this.steps = steps.clone();
		}
		
		this.options = Object.extend({
			classNames : {
				header : '#steps .header',
				toggleActive : 'header_active',
				content : '#steps .content',
				nextButtons: '#steps .buttons .next',
				visited: "visited"
			}
		}, options || {});

		this.steps.keys().each(function(k) {
			if (this.steps.get(k).button) Event.observe(this.steps.get(k).button, 'click', this.steps.get(k).handler, false);
			if (this.steps.get(k).header) Event.observe(this.steps.get(k).header, 'click', Step.onlyBack.bind(this, this.steps.get(k).header), false);
		}.bind(this));
		if (Clickables.headers[3]) Event.observe(Clickables.headers[3], 'click', Step.onlyBack.bind(this, Clickables.headers[3]), false);
			
		this.currentStep = this.currentStep || 'billing'

		var step = document.location.hash.substring(1);

		$$('#steps .buttons').each(function(button){
			button.show();
		});

		stateOrDistrict();

		// XXX #order_form non deve essere un LiveValidationForm!!
		CheckoutValidations.email.formObj.destroy(true);

		if (!(step == '')) {
			this.activate(step);
			Element.hide('intro');
		}
		else {
			Event.observe($('to_billing'), 'click', function() {
				this.activate('billing');
				Element.hide('intro');
			}.bind(this));
		}
	},

	destroy: function() {
		Clickables.buttons.each(function(button){
			Event.stopObserving(button, 'click');
		});
        
		Clickables.headers.each(function(header){
			Event.stopObserving(header, 'click');
			header.removeClassName(this.options.classNames.visited);
		}.bind(this));
	},

	//
	//  Activate an accordion
	//
	activate : function(id) {
		if (this.animating) {
			return false;
		}

		var next_step = $(id);
		var prev_step = $(this.currentStep);
		
		$(this.currentStep).removeClassName(this.options.classNames.toggleActive);

		if (next_step.tagName == "H1") {
			start = this.steps.get(this.currentStep).count
			end = this.steps.get(id).count
			for (i=start;i >= end;i = i-1) {
				Clickables.headers[i].removeClassName("visited");
			}
		}

		this.currentStep = id;

		next_step.addClassName(this.options.classNames.toggleActive);
		prev_step.addClassName(this.options.classNames.visited);
		
		if (prev_step) prev_step.next().hide();
		
		window.location.hash = "#" + id;

		next_step.next().show();
		pageTracker._trackEvent('Checkout', id, 'action:new_order');
	}
}
