var OrderTickets = new Class({
	Implements: [Options, Events, Log],
	Extends: Page,
	
	// Variables
	stepIndex: 0,
	steps: [],
	shoppingCart: null,
	iframe: null,

	// Class instances


	initialize: function( name, element, options )
	{
		this.enableLog();
		//this.disableLog();
		this.log("OrderTickets::initialize(" + name + ", " + element + ", " + options + ")");

		// Invoke parent method
		this.parent( name, element, options );

		window.changeButton = this.changeButton.bind(this);
		window.setIFrameHeight = this.setIFrameHeight.bind(this);

		this.steps = this.element.getElements('.step');
		this.shoppingCart = this.element.getElement('.shopping-cart');
		if(this.shoppingCart)
		{
			this.shoppingCart.fx = new Fx.Reveal(this.shoppingCart, {
				duration: 500,
				link: 'cancel',
				transitionOpacity: false
			});
			this.shoppingCart.hide();

			this.shoppingCart.getElements('li').each(function(element) {
				var eventItem = new EventItem(element);
			}.bind(this));
		}

		// Iframe
		//this.initIFrame();
		//this.iframe = $('order-tickets-frame');
		this.iframe = this.element.getElement('.orderproces iframe');
		this.log("this.iframe: "+ this.iframe);
		this.iframe.fx = new Fx.Tween(this.iframe, {
			duration: 500,
			transition: Fx.Transitions.Quad.easeOut,
			link: 'cancel',
			property: 'height'
		});

//		this.iframe.addEvent('load', function () {
//			this.log('IFRAME load');
//			this.setIFrameHeight();
//		}.bind(this));

		// Show first step
		this.gotoStep( this.stepIndex );
	},

	/*
	initIFrame: function()
	{
		this.log("OrderTickets::initIFrame()");

		var page = $('ticketing').getElement('input[name="ticketingiframe"]').value;
		var iframe = new Element('iframe', {'src': page, 'name': 'order-tickets', 'id': 'order-tickets-frame', 'width': '100%', 'frameborder': '0', 'scrolling': 'auto'})

		this.element.getElement('.orderproces').adopt(iframe);
	},
	*/

	setIFrameHeight: function()
	{
		this.log("OrderTickets::setIFrameHeight()");

		/*
		this.log("this.iframe.contentWindow.document.body.scrollHeight: "+ this.iframe.contentWindow.document.body.scrollHeight);
		this.log("this.iframe.contentWindow.document.body.getScrollSize().y: "+ this.iframe.contentWindow.document.body.getScrollSize().y);
		this.log("this.iframe.contentWindow.document.body.getElementById('body'): "+ this.iframe.contentWindow.document.body.getElementById('body'));
		this.log("this.iframe.contentWindow.document.body.getElementById('body').getSize().y: "+ this.iframe.contentWindow.document.body.getElementById('body').getSize().y);

		this.log("this.iframe.contentWindow.document.body.get('id'): " + this.iframe.contentWindow.document.body.get('id'));
		this.log("this.iframe.contentWindow.document.body.get('class'): " + this.iframe.contentWindow.document.body.get('class'));
		*/


		this.log("this.iframe: "+ this.iframe);
		this.log("this.iframe.contentWindow.document.body: "+ this.iframe.contentWindow.document.body);
		this.log("this.iframe.contentWindow.document.body.scrollHeight: "+ this.iframe.contentWindow.document.body.scrollHeight);

		//this.log("this.iframe.contentWindow.document.body.getElementById('masterbody'): "+ this.iframe.contentWindow.document.body.getElementById('masterbody'));
		//this.log("this.iframe.contentWindow.document.body.getElementById('main'): "+ this.iframe.contentWindow.document.body.getElementById('main'));
		

//		var content = this.iframe.contentWindow.getElementById('content');

		if(this.iframe.contentWindow.document.body)
		{
//			for(var i in this.iframe.contentWindow.document.body.children)
//			{
//				this.log("this.iframe.contentWindow.document.body.children[" + i + "]: " + this.iframe.contentWindow.document.body.children[i]);
//			}
//			for(var i in this.iframe.contentWindow.document.body.parentElement)
//			{
//				this.log("this.iframe.contentWindow.document.body.parentElement[" + i + "]: " + this.iframe.contentWindow.document.body.parentElement[i]);
//			}

			//innerHTML

			var content = this.iframe.contentWindow.document.body.getElementById('content');
			this.log("content: " + content);
			if(content)
			{
				content.setStyle('clear', 'both');
				content.setStyle('overflow', 'hidden');

//			var height = this.iframe.contentWindow.document.body.scrollHeight;
//		var height = this.iframe.contentWindow.document.body.getScrollSize().y;
//			var height = this.iframe.contentWindow.document.body.getElementById('body').getSize().y;
			var height = this.iframe.contentWindow.document.body.getElementById('content').getSize().y;

			this.log("NEW height: " + height);

	//		this.iframe.setStyle('height', height);
			this.iframe.fx.start(height);
			}


		}
		
	},

	changeButton: function( button )
	{
		this.log("OrderTickets::changeButton(" + button + ")");

		this.log("OrderTickets::this.shoppingCart: " + this.shoppingCart);

		switch(button)
		{
			case 'step-1':
			case 'login':
				this.gotoStep(0);
			break;
			
			case 'step-2':
			case 'bestel':
				this.gotoStep(1);
			break;

			case 'step-3':
			case 'controle':
				this.gotoStep(2);
			break;

			case 'step-4':
			case 'betalen':
				this.gotoStep(3);
			break;
		}
	},

	previousStep: function()
	{
		this.log("OrderTickets::previousStep()");

		var newStepIndex = (this.stepIndex - 1 >= 1) ? this.stepIndex - 1 : 0;

		if( newStepIndex != this.stepIndex ) this.gotoStep( newStepIndex );
	},

	nextStep: function()
	{
		this.log("OrderTickets::nextStep()");

		this.log("this.stepIndex: "+ this.stepIndex);

		var newStepIndex = (this.stepIndex + 1 <= this.steps.length - 1) ? this.stepIndex + 1 : this.steps.length - 1;

		if( newStepIndex != this.stepIndex ) this.gotoStep( newStepIndex );
	},

	gotoStep: function( index )
	{
		this.log("OrderTickets::gotoStep(" + index + ")");

		this.stepIndex = index;

		if(this.shoppingCart)
		{
			if(index === 1)
			{
				this.shoppingCart.fx.reveal();
			}
			else
			{
				this.shoppingCart.fx.dissolve();
			}
		}

		// Iframe height
		//this.setIFrameHeight.delay(4000, this);

		// Steps
		this.steps.each(function(step, index) {

			// Button state
			step.removeClass('selected');
			step.removeClass('disabled');

			if(index == this.stepIndex)
			{
				step.addClass('selected');
			}
			else if(index > this.stepIndex)
			{
				step.addClass('disabled');
			}

			// Events
			step.removeEvents('click');

			if(index < this.stepIndex)
			{
				step.addEvent('click', function(event) {
					event.preventDefault();

					this.gotoStep( index );
				}.bind(this));
			}
			else if(index >= this.stepIndex)
			{
				step.addEvent('click', function(event) {
					event.preventDefault();
				}.bind(this));
			}

		}.bind(this));
	}

});
