var OrderForm = Class.create({
	initialize: function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});

		this.popup = null;
		this.popup_form_container = null;

		if($('order_link'))
		{
			$('order_link').observe('click', this.show_order.bind(this));
			this.create_popup();
		}
	},

	show_order: function (evt)
	{
		this.reposition_popup()

		MAG.Mask.show(45);

		new Effect.Appear(this.popup, {
			afterFinish: this.load_form.bind(this)
		});

		if($('fog')) {
			$('fog').observe('click', this.hide_popup.bind(this));
		}
	},

	load_form: function() {
		new Ajax.Updater(this.popup_form_container, this.options.url, {
			method: 'get',
			parameters: 'method=load_order_form&id=' + this.options.id,
			onComplete: this.prepare_form.bind(this)
		});
	},

	prepare_form: function() {
		new Effect.Appear(this.popup_form_container, {duration: 0.3});
		$('submit_form').observe('click', this.submit_form.bind(this));
		$('close_button').observe('click', this.hide_popup.bind(this));
	},

	submit_form: function()
	{
		new Ajax.Request(this.options.url, {
			method: 'get',
			parameters: 'method=submit_order_form&url=' + window.location + '&' + $('order_form').serialize(),
			onComplete: this.submit_form_complete.bind(this)
		});
	},

	submit_form_complete: function(req)
	{
		var response = req.responseText.evalJSON();

		if(!response['errors']) 
		{
			this.popup.setStyle({ width:'520px', height:'130px', top:'250px' });
			this.popup_form_container.setStyle({ display:'inline' });
			this.popup_form_container.update('<div class="success_message" style="margin-top:50px;text-align:center;font-size:16px;font-weight: bold;width:500px;"><p>' + response.success + '</p></div>');
			setTimeout(function()
			{
				this.hide_popup();
			}.bind(this), 2000);
			
		}
		else
		{
			$$('.order_form div.error').each(function(elem){ elem.remove();});

			$$('.order_form label.text_alert').each(function(elem){
				elem.removeClassName('text_alert');
			});

			for(key in response['errors']) {
				$(key).up().select('label').first().addClassName('text_alert');
				$(key).up().insert('<div class="error">' + response['errors'][key] + '</div>');
			}

		}
	},

	hide_popup: function ()
	{
		var self = this;
		new Effect.SwitchOff(self.popup_form_container,
		{
			afterFinish: function()
			{
				new Effect.Fade(self.popup, 
				{
					duration: 0.3,
					afterFinish: function(){MAG.Mask.hide();
						self.popup.setStyle({ width:'520px', height:'630px', top:'120px' });
						self.popup_form_container.setStyle({ display:'none' });
					}
				});

			}
		});
	},

	create_popup: function ()
	{
		this.popup = new Element('div', {id: 'popup_order'}).setStyle({
			left: '120px',
			top: '120px',
			display: 'none',
			position: 'absolute',
			width: '520px',
			height: '630px',
			backgroundColor: '#E7DECC',
			zIndex: 600
		});

		this.popup_form_container = new Element('div', {id: 'popup_form_container'}).setStyle({display: 'none'});

		document.body.appendChild(this.popup);
		$(this.popup).appendChild(this.popup_form_container);
	},

	reposition_popup: function()
	{
		var screen_dims = MAG.Mask.getDimensions();
		var offsets = document.viewport.getScrollOffsets();

		var top = screen_dims.height / 2 - this.popup.getHeight() / 2 + offsets[0] / 2 -40 ;
			top = top < 0 ? 0 : top;

		this.popup.setStyle({
			left: screen_dims.width / 2 - this.popup.getWidth() / 2  - offsets.left / 2 + 'px',
			top:  top + 'px'
		})
	}

});
