
	
	var Pleasenotify = Class.create();

	Pleasenotify.prototype = {
		initialize: function() {
			this.lastViewportWidth     = -1;
			this.originalViewportWidth = document.viewport.getWidth();
			this.showingStatusMessage  = false;
			
			this.isiPhone = (navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPod') != -1) ? true : false;
		},
		
		send: function(el) {
			$('sendButton').blur();
			if(Form.check('pleaseNotifyForm'))
			{
				this.toggleActionLoading();
				new Ajax.Request(ajaxUrl+'send', { postBody: $('pleaseNotifyForm').serialize(), onComplete: this._send_callback.bindAsEventListener(this)});
			}
		},
		
		_send_callback: function(request) {
			var response = request.responseText.evalJSON();	
			this.showingStatusMessage = true;
			$('preview').hide();
			this.resetPreview();
			
			if(!response.status)
				$('promoText').update('Sorry but there was an error setting your reminder. <a href="javascript:void(0);" onclick="pleasenotify.toggleActionLoading()">Try again</a>.');
			else
			{
				this.toggleActionLoading();
				$('pleaseNotifyForm').reset();
				$('promoText').update(response.resultsContent);
			}
			$('promoText').show();
		},
		
		markAsDone: function(el, accessHash) {
			showLoading(el);
			new Ajax.Request(ajaxUrl+'markAsDone', { postBody: 'accessHash='+accessHash, onComplete: this._markAsDone_callback.bindAsEventListener(this,el)});
		},
		
		_markAsDone_callback:function(request,el) {
			hideLoading(el);
			var response = request.responseText.evalJSON();	

			if(!response.status)
				alert('a!');
			else
			{
				var currentSpacer = $$('#resultsContainer .spacer')[0];
				$('resultsContainer').insert({'bottom':response.resultsContent});
				new Effect.Fade(currentSpacer,{duration:0.2});
				new Effect.Appear($$('#resultsContainer .spacer')[1],{duration:0.2});
			}
		},
		
		resetPreview: function() {
			if(this.isiPhone) return;
			
			$('previewDescription').update('');
			$('previewDueDate').update('');
			$('previewSender').update('');
		},
		
		toggleActionLoading: function() {
			$('actionsLoading').update('<img src="'+sysUrl+'template/images/spinner4.gif" alt="Loading" class="spinner" />');
			$('actionsLoading').toggle();
		},
		
		updatePreview: function(el) {
			if(this.isiPhone) return;
			
			if(!$('preview').visible())
			{
				$('promoText').hide();
				$('preview').show();
			}
			
			if(el.name == 'sender') $('previewSender').update(el.value);
			if(el.name == 'description') $('previewDescription').update(el.value);
			if(el.name == 'due_date') $('previewDueDate').update($('formattedDueDates').innerHTML.evalJSON()[el.value]);
		},
		
		globalPreviewUpdate:function() {
			if(this.isiPhone) return;
			
			$('previewSender').update($F('sender'));
			$('previewDescription').update($F('description'));
			$('previewDueDate').update($('formattedDueDates').innerHTML.evalJSON()[$F('due_date')]);
		},
		
		fixWindow: function() {
			if(this.isiPhone) return;

			var currentViewportWidth = document.viewport.getWidth();
			
			if($('infoContainer'))
			{
				if(currentViewportWidth < 735)
				{
					var offset = 735-currentViewportWidth;
					$('infoContainer').style.left = '-'+offset+'px';
					var spacer = $('infoContainer').down('.spacer');
				}
			}
			else
			{
				this.lastViewportWidth = 1000;
				if(this.lastViewportWidth != currentViewportWidth)
				{

					if(currentViewportWidth >= 1000)
					{
						$('content').style.width = '1000px';
						$('resultsContainer').style.width = '488px';
						$('resultsContainer').down('.spacer').style.width = '420px';
						$('actionsContainer').style.left = '-150px';
					}
					else
					{
						var correctionOffset = 1000-currentViewportWidth;
						if($('resultsContainer'))
						{
							var resultsOffset = correctionOffset/1.5;
							var resultsWidth  = $('resultsContainer').getStyle('width').split('px')[0]*1;
							$('resultsContainer').style.width = (resultsWidth-resultsOffset)+'px';
							$('resultsContainer').down('.spacer').style.width = (resultsWidth-resultsOffset-60)+'px';
						}

						if($('actionsContainer'))
						{
							var leftPosition = $('actionsContainer').getStyle('left').split('px')[0]*1;
							// console.log(leftPosition);
							if(correctionOffset <= 95)
								$('actionsContainer').style.left = (leftPosition-correctionOffset)+'px';
							else
								$('actionsContainer').style.left = (leftPosition-95)+'px';
						}

						$('content').style.width = currentViewportWidth + 'px';
					}
				}
				resultsOffset = null;
				resultsWidth = null;
				leftPosition = null;
				correctionOffset = null;
			}
		},
		
		
		checkIE: function() {
			if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version == '6')
				$('wrapper').insert({'top':'<div id="IEWarning">pleasenotify.me would look much better if you were using a modern browser; <a href="http://www.mozilla.com/firefox/">Firefox</a> and <a href="http://www.apple.com/safari/">Safari</a> are good options.</div>'});
		}
	};

	var pleasenotify = new Pleasenotify();
