/*! Front Page */
$(function() {
	var i;i = 0;
	Messenger.options = {extraClasses: "messenger-fixed messenger-on-bottom messenger-on-left",theme: "flat"};
	var msg; msg = Messenger().post({
		message: 'I am currently online', type: 'success',
		actions: {retry: {label: 'Connect', delay: 60,
				action: function(){
					txt_dave();
					var msg_return; msg_return = Messenger().run({
					  	errorMessage: 'Please wait a moment while I am texted a message to get connected with you.', 
						successMessage: 'Connecting Now!',
					  	action: function(opts) {
						  msg.hide();		  
							if (++i < 2) {
								return opts.error({status: 500,readyState: 0,responseText: 0});	
							} 
							else {
								window.location.href = 'contact/chat';		
								return msg.update({message: 'Redirect to Chat', hideAfter: 20, type: 'success', actions: false});
								
							}
						}
					});
				}
			},
			cancel: {
				label: 'No Thanks',
				action: function() {
					return msg.update({message: 'Maybe Next Time', hideAfter: 1, type: 'error', actions: false});
				}
			}
		}
	});
});
function txt_dave() {
	$.ajax({
		url: 'https://davidawindham.com/wha/phony/sms',
		dataType: 'json',
		success: function(data){},
	});
}

/*! Chat Page */

Messenger.options = {extraClasses: "messenger-fixed messenger-on-bottom messenger-on-left",theme: "flat"};
var i;i = 0;
$(function(){
	var msg_return; msg_return = Messenger().run({
		id: 'one',
		errorMessage: 'Looking for David...', 
		action: function(opts) {
			if (++i < 2) {
				get_dave();
				return opts.error({status: 500,readyState: 0,responseText: 0});	
			} 
		}
	});
});

function get_dave() {
	$(function poll() {
		var x = 0;
		var countTimer = setInterval(function () {
			if(x > 5){clearInterval(countTimer)}
			else if(x == 5){dave_not_available()}
			else{
				var URLchatAPI = "http://macs.local:8080/status";
				var request = $.ajax({
				    url: URLchatAPI,
				    dataType: 'json',
					cache: false, 
					success: function (data) {
						online = data.online;
						if (online=='yes') {
							$('.chat').modal('show');
							x = x+5;
						};
						if (online=='no') {
							dave_connecting();
						};
					},
					error: function ( xhr, tStatus, err ) {
						dave_error();
						x = x+5;
					}
				});
				
				x++;
			}
		}, 5000);

	});
}

function dave_connecting(){
	var y;y = 0;
	var msg_waiting; msg_waiting = Messenger().run({
		id: 'one',
		hideAfter: 4,
		errorMessage: 'Waiting on David...',
		action: function(opts) {
			if (++y < 2) {
				get_dave();
				return opts.error({status: 500,readyState: 0,responseText: 0});	
			} 
		}
	});
}		

function dave_not_available(){
	var msg_error; msg_error = Messenger().post({
		message: 'Sorry, but I am busy.',
		type: 'error',
		id: 'one',
		showCloseButton: true,
		actions:{
			cancel: {
				label: 'Leave me a message',
				action: function(){
					window.location.href = '../';	
				}
			}	
		}	
	});
}

function dave_error(){
	var msg_error; msg_error = Messenger().post({
		message: 'Oops. Something has gone wrong.',
		type: 'error',
		id: 'one',
		showCloseButton: true,
		actions:{
			cancel: {
				label: 'Leave me a message',
				action: function(){
					window.location.href = '../';	
				}
			}	
		}
	});
}
$(function() {
    if (window.location.hash.indexOf("chat") !== -1) {
        $('.chat').modal('show');
    }
});