if ( typeof(ID) === 'undefined' ) { throw "The ID class is required for ModalDialog.js"; }

ID.ModalDialog = Class.create(ID, {
	toString: function() { return 'ID.ModalDialog'; },
	
	initialize: function() { },
	
	show: function()
	{
		this._log('show dialog: ' + this.id);
		if ( !$(this.id) )
		{ 
			this.dialog = this.getDialogElement();
		}
		
		this.setup();
		document.fire('overlaywindow:show', { element: this.dialog });
		document.fire('custom:systemfontreplace');
	},
	
	hide: function()
	{
		document.fire('overlaywindow:show');
	},
	
	getDialogElement: function()
	{
		this._error('getDialogElement must be overloaded');
		throw "ID.ModalDialog::getDialogElement must be overloaded";
	},
	
	setup: function()
	{
		this._error('setup must be overloaded');
		throw "ID.ModalDialog::setup must be overloaded";
	},
	
	teardown: function()
	{
		this._error('teardown must be overloaded');
		throw "ID.ModalDialog::teardown must be overloaded";
	}
});

ID.DeliveryDialog = Class.create(ID.ModalDialog, {
	toString: function() { return 'ID.DeliveryDialog'; },
	
	initialize: function()
	{
		this.id = 'dlgDeliveryMethod';
		document.observe('dialog:showdeliverydialog', this.show.bind(this));
	},
	
	getDialogElement: function()
	{
		var markup = 
			'<div class="dialog">'+
				'<div class="dlgHeader"><!----></div>'+
				'<div class="dlgContent">'+
					'<div class="dialogcontent">'+
						'<div id="dlgdeliverytop" class="dialogtop dlgtopinactive">'+
							'<div id="dlgdeliveryinactivetop">'+
								'<h1 class="bliss cufon-20px">Levering eller afhentning?</h1>'+
								'<span class="action action_selectdelivery coloredlink" id="closedialog"><a>&raquo; Luk vindue</a></span>'+
								'<div class="clear"><!----></div>'+
							'</div>'+
							'<div id="dlgdeliveryactivetop">'+
								'<div style="width: 260px; float: left; line-height: 16px; padding-top: 12px"><span>Hvis du ikke foretager noget valg, antager vi, at du vil have dine varer leveret.</span></div>'+
								'<span class="action action_selectdelivery coloredlink" id="closedialog"><a>&raquo; Luk vindue</a></span>'+
								'<div class="clear"><!----></div>'+
							'</div>'+
						'</div>'+
						'<div class="dialogmain">'+
							'<div class="dialogcolumn leftdialogcolumn">'+
								'<h4 class="cendia cufon-12px">Jeg vil gerne have mine varer leveret</h4>'+
								'<p>Leveringsprisen vil fremgå af din indkøbskurv.</p>'+
							'</div>'+
							'<div class="dialogcolumn rightdialogcolumn">'+
								'<h4 class="cendia cufon-12px">Jeg vil gerne afhente mine varer</h4>'+
								'<p>Når du selv afhenter dine varer, er det selvfølgelig gratis</p>'+
							'</div>'+
							'<div class="clear"><!----></div>'+
							'<div class="dialogcolumn leftdialogcolumn">'+
								'<img class="action_selectdelivery action actionbutton" src="/files/system/gfx/leveringsdialog/lev_delbut.png" alt="Vælg levering" />'+
							'</div>'+
							'<div class="dialogcolumn rightdialogcolumn">'+
								'<img class="action_selectpickup action actionbutton" style="margin-top: -1px" src="/files/system/gfx/leveringsdialog/lev_pickbut.png" alt="Vælg afhentning" />'+
							'</div>'+
							'<div class="dialogfooter">'+
								'<p>Du kan altid ændre dit valg, når du går til kassen.</p>'+
							'</div>'+
						'</div>'+
					'</div>'+
				'</div>'+
				'<div class="dlgFooter"><!----></div>'+
			'</div>';
		
		return new Element('div', { 
			'id': this.id, 
			'className': 'overlaywindow'
		}).insert(markup);
	},

	_selectDelivery: function(e)
	{
		this._log('Select delivery');
		dk.ide.webservice.DeliveryService.SetDeliveryMethod(this.basket.basketId, false, Prototype.emptyFunction, Prototype.emptyFunction);
		this.hide();
	},
	
	_selectPickup: function(e)
	{
		this._log('Select pickup');
		dk.ide.webservice.DeliveryService.SetDeliveryMethod(this.basket.basketId, true, Prototype.emptyFunction, Prototype.emptyFunction);
		this.hide();
	},

	show: function($super, e)
	{
		this.basket = e.memo;
		$super();
	},

	setup: function()
	{
		this._log('setup');
		
		document.observe('action:selectdelivery', this._selecthandler = this._selectDelivery.bind(this));
		document.observe('action:selectpickup', this._pickuphandler = this._selectPickup.bind(this));
		document.observe('overlaywindow:hidden', this._teardownhandler = this.teardown.bind(this));
		
		(function() {
			$('dlgdeliverytop').observe('mouseenter', function(){ $('dlgdeliverytop').removeClassName('dlgtopinactive').addClassName('dlgtopactive'); });
			$('dlgdeliverytop').observe('mouseleave', function(){ $('dlgdeliverytop').removeClassName('dlgtopactive').addClassName('dlgtopinactive') });
		}).defer();
	},
	
	teardown: function()
	{
		this._log('teardown');
		document.stopObserving('action:selectdelivery', this._selecthandler);
		document.stopObserving('action:selectpickup', this._pickuphandler);
		document.stopObserving('overlaywindow:hidden', this._teardownhandler);
		
		$('dlgdeliverytop').stopObserving('mouseenter');
		$('dlgdeliverytop').stopObserving('mouseleave');
		
		// update shopping cart
		(function() { getShoppingCart(); }).defer();
	}
});

