
SC.Download = {
	_dialog: null,

	// {{{ init()
	init: function(subscription_type, cartoon, download) {
		this.subscription_type = subscription_type;

		YAHOO.util.Event.on('download-button', 'click', this._handleDownloadClick, this, true);

		if (!download) {
			return;
		}
		

		var form = YAHOO.util.Dom.get('download-form');

		if(form) {
			this._createDialog();
		}
		else {
			// Not logged in.
			window.location = 'client/login.php?next='
				+encodeURIComponent('../?cartoon='+cartoon+'&download=1');
		}	
	},

	// }}}

	// {{{ _createDialog()
	_createDialog: function() {
		if (!this._dialog) {
			this._dialog = new YAHOO.widget.Dialog('download-dialog', {
				width: '400px',
				fixedcenter: true,
				visible: false,
				constraintoviewport: true,
				modal: true,
				buttons: [
					{
						text: 'Ok',
						handler: function() {
							this.hide();
							var form = YAHOO.util.Dom.get('download-form');
							form.submit();
						}, 
						isDefault: true 
					},
					{
						text: 'Cancel',
						handler: function() {
							this.hide();
						}
					}
				]	
			});

			
			var message = SC.Download.MSG_SUBSCRIPTION;
			if (subscription_type == 'per_cartoon') {
				message += '<br/><br/>' + SC.Download.MSG_PER_CARTOON;
			}

			YAHOO.util.Dom.get('download-message').innerHTML = message;
			//this._dialog.setHeader('Terms and Conditions');
			//this._dialog.setBody(message);

			YAHOO.util.Dom.get('download-dialog').style.display = '';
			this._dialog.render(document.body);
		}

		this._dialog.show();
	},
	// }}}
	// {{{ _handleDialogCancel()
	_handleDialogCancel: function() {
		console.log('cancel');
		this._dialog.hide();
	},
	// }}}
	// {{{ _handleDialogOk()
	_handleDialogOk: function() {
		console.log('ok');
		
	},
	// }}}
	// {{{ _handleDownloadClick()
	_handleDownloadClick: function(e) {
		this._createDialog();
		YAHOO.util.Event.preventDefault(e);
	}
	// }}}
	
}

SC.Download.MSG_SUBSCRIPTION = 'By clicking "Ok" below, you consent to agree '
+'with the terms stated below.<br/><br/>'
+'Purchase of this cartoon allows you non-exclusive rights for a '
+'single printing in your newspaper. Your purchase also allows you to '
+'post an electronic version of the cartoon on your newspaper\'s web site.';

SC.Download.MSG_PER_CARTOON = 'By clicking "Ok" you agree to pay '
+'the fee to Jesse Springer within 30 days upon receipt of the invoice. '
+'If you have trouble with a file and need to re-download, you may do '
+'so without additional cost. '
+'If you have to cancel the printing of a cartoon, click the "Cancel" '
+'option on your invoice within 48 hours of the initial download.';

