﻿(function ($) {

	function Enroll(options) {
		this._init(options || {});
	}

	Enroll.prototype = {

		options: {},

		_init: function (options) {
			this.options = $.extend({}, $.defaults, options);
			var $this = this;
			$this._initButtons();
		},
		_initButtons: function () {
			var $this = this;
			$this._initSubmit();
			$this._initTryAgain();
		},
		_initSubmit: function () {
			var $this = this;
			$("button.submit").button({
				icons: {
					primary: "ui-icon-none",
					secondary: "ui-icon-triangle-1-e"
				}
			}).click(function () {
				$this._showDialog();
			});
		},
		_initTryAgain: function () {
			var $this = this;
			$($this.options.tryAgainSelector).button({
				icons: {
					primary: "ui-icon-triangle-1-w"
				}
			}).click(function () {
				// BUG!!
				// when in modal mode, the button clicked to 
				// open the modal gets stuck in its active state, 
				// so it needs to be reinitialized.
				$this._initSubmit();
				$($this.options.disagreeDialogSelector).dialog('close');
			});
		},
		_showDialog: function () {
			var $this = this;
			if ($($this.options.agreeRadioSelector + ":checked").length > 0) {
				$($this.options.agreeDialogSelector).dialog({
					modal: true,
					draggable: false,
					width: 600, height: 250,
					close: function () {
						// stupid ie6 hack... 
						$('select').css({ 'visibility': 'visible' });
					},
					open: function () {
						// stupid ie6 hack... 
						$('select').css({ 'visibility': 'hidden' });
					}
				});

			} else if ($($this.options.disagreeRadioSelector + ":checked").length > 0) {
				$($this.options.disagreeDialogSelector).dialog({
					modal: true,
					draggable: false,
					width: 600, height: 250,
					close: function () {
						// stupid ie6 hack... 
						$('select').css({ 'visibility': 'visible' });
					},
					open: function () {
						// stupid ie6 hack... 
						$('select').css({ 'visibility': 'hidden' });
					}
				});
			}
		}
	};

	$.fn.enroll = function (options) {
		return new Enroll(options);
	};

	$.defaults = {
		tryAgainSelector: '#tryAgain',
		disagreeDialogSelector: '#disagreeDialog',
		agreeDialogSelector: '#agreeDialog',
		agreeRadioSelector: '#rdoAgree',
		disagreeRadioSelector: '#rdoDisagree'
	};

})(jQuery);



