var Notification = {
	el: null,
	zip: null,
	form: null,
	dialog: null,
    dialogW: 300,
    dialogH: 210,
    mailValidate: /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/,

    /** function init mail object
     *
     */
    init: function() {
		this.el = YAHOO.util.Dom.get('message_box');
		this.form = YAHOO.util.Dom.get('zip-notification-form');
		this.link = getEl('city-link');
		this.link.addListener('click', this.showCityPanel, this, true);
		this.cityPanel = YAHOO.util.Dom.get('rest-div');

		this.createDialog();
    },

    /** function display city-panel
     *
     *	@return void
     */
    showCityPanel: function() {

		cityText = '<br />';
    	for (var i in this.city) {
			cityText += '<p style="font-size:12px;">' +
								'<a href="user_order_step_2n.php?cmd=pickup&id='+i+'">'+
									this.city[i] +
								'</a>' +
							'</p>';
    	}

		cityText += '<br /><a class="text_14" id="RequestLink_wind" style="color:#000" href="javascript:RD.show(\'RequestLink_wind\', \'index.php?\');"> Request a Restaurant</a>';

		this.cityPanel.innerHTML = cityText;
		this.cityPanel.style.display = 'block';

		this.dialog.resizeTo(this.dialogW, this.dialogH+200);
    },

    /** function save xip-code and call function which send request to server
     *
     *	@return void
     */
	checkZip: function(el, zip) {
		if (!this.el) {
			this.el = el;
		}
		this.zip = zip;

		this.zipRequest(zip);
	},

	/** function decode from JSON-string to JS-object server response
	 *	@param {Object} res - server response
	 *
	 *	@return {Object} - server response
	 */
	getResponse: function(res) {
		if (res.responseText) {
			return YAHOO.ext.util.JSON.decode(res.responseText);
		} else {
			return null;
		}
	},

	/** function fire when server request successfully
	 * 	@param {Object} response - server response
	 *
	 *	@return void
	 */
	requestSuccess: function(response) {
		var res = this.getResponse(response);

		if (!res || res.is_error) {
//TODO if need show some error mnessage
		} else {
			var data = res.data;
			if (data.hasStores) {
				var oForm = YAHOO.util.Dom.get('view-restaurants-form');
				if (oForm) {
					oForm.submit();
				}
			} else {
				if (!this.dialog) {
					this.createDialog();
				}

				this.dialog.show(this.el);

				if (data.city) {
					this.city = data.city;
				}
			}
		}
	},

	/** function fire when server request failure
	 * 	@param {Object} response - server response
	 *
	 *	@return void
	 */
	requestFailure: function(response) {
		MD.show(this.el.id, ['Request failure. Please contact support.']);
	},

	/** function send server request for search stores in zip-area
	 * 	@param {String} zip - zip code
	 *
	 *	@return void
	 */
	zipRequest: function(zip) {
		if (typeof zip == 'string' && zip.length == 5) {
			var callback = {
						success:this.requestSuccess,
						failure:this.requestFailure,
						scope: this
			};

			var params = 'cmd=check_zip_store&zip=' + zip;
			var conn = YAHOO.util.Connect.asyncRequest('POST', 'ajax_zipNotification.php', callback, params);
		}
	},

	/** function create new dialog window
	 *
	 *	@return void
	 */
	createDialog: function() {
		var position = new Array;
		position = YAHOO.util.Dom.getXY(this.el);

		this.dialog = new YAHOO.ext.BasicDialog("zip-notification-dlg", {
										          	draggable: false,
								                    modal:true,
													closed:true,
								                    width: this.dialogW,
								                    height: this.dialogH,
								                    x: position[0]+150,
								                    y: position[1]+10,
								                    shadow:false,
								                    autoScroll: true,
								                    resizable: false,
								                   	shim: false
							});

  	 	this.dialog.toFront();

  	 	this.dialog.addListener('hide', this.hideCityPanel, this, true);

        postBtn = getEl('post-dialog-btn-notification');
        postBtn.on('click', this.addZipToNotification, this);
	},

	/** function reset form, and hide city-panel
	 *
	 *	@return void
	 */
	hideCityPanel: function() {
		this.form.reset();
		this.cityPanel.style.display = 'none';
		this.dialog.resizeTo(this.dialogW, this.dialogH);
	},

	/** function send server request for add email for notification
	 *
	 *	@return void
	 */
	addZipToNotification: function() {
		var email = this.form.email.value;
		if (!this.mailValidate.test(email)) {
			MD.show('post-dialog-btn-notification','E-mail must be in name@domain.com format.');
		} else {
			var callback = {
				success:this.addEmailSuccess,
				failure:this.requestFailure,
				scope: this
			};
			var params = 'cmd=add_email&email='+email+'&zip='+this.zip;
			var conn = YAHOO.util.Connect.asyncRequest('POST', 'ajax_zipNotification.php', callback, params);
		}
	},

	/** function get response from server
	 * 	@param {Object} response - server response
	 *
	 * 	@return void
	 */
	addEmailSuccess: function(response) {
		var res = this.getResponse(response);
		if (res && res.messages && res.messages.length) {
			MD.show('post-dialog-btn-notification', res.messages);
		}
	},

	/** function hide dialog
	 *
	 *	@return void
	 */
	hideDialog: function() {
		this.dialog.hide();
	}
};

YAHOO.ext.EventManager.onDocumentReady(Notification.init, Notification, true);
