// 3 visual voicemail thinger
// &copy; Andrew Harrison / http://andrew.harrison.org / andrew@harrison.org 2009
// Bits and pieces (iPhone user agent validation etc) adapted from Neven Mrgan's fantastic Glyphboard

$(document).ready(function(){
	//in the olden days, andrew.harrison.org stopped working for a while, and whilst
	//developing this app, it was hosted at the temporary server on lynxas. before
	//we do anything, let's go to the real domain.
	//var href = window.location.host;
	//if (href = 'andrewharrisontest.lynxas.com') {
		//window.location.href = 'http://andrew.harrison.org/3mail';
	//	iphonecheck();	
	//} else {
		iphonecheck();
	//}
	//loginfunctions();		

});


function iphonecheck() {
//	if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i))) { // it's an iPhone! Check if it's installed a springboard app
	//if (!window.navigator.standalone) { //It's not installed as an app. Tell them so!
			//Hide the content and show the "install as an app" message
	//		$('#notanapp').show();
	//		$('#content').hide();
	//	} else { //it's an iPhone AND it's installed as an app. Let's Go!
			loginfunctions();		
	//	}
//	} else { //It's not an iPhone! Sneaky Sneaky! 
		//Hide the content and show the not an iPhone message
//		$('#notaniphone').show();
//		$('#content').hide();
//	}
}

function loginfunctions() {
	//if the username cookie exists, fill in the username field with it.
	if ($.cookie('usernamecookie') != ''){
		$('#username').val($.cookie('usernamecookie'));	
	}
	//if the password cookie exists, fill in the password field with it.	
	if($.cookie('pwdcookie') != '') {
		$('#pwd').val($.cookie('pwdcookie'));	
	}
	//if the login in the past has been good, log them in and go.
	if($.cookie('goodlogincookie') === '1') {
		getmessages();
	} else {
		$('#content').show();
	}
}

$(document).ready(function(){
	$('#gogetmessages').click(function() { //when Onward! is clicked
		getmessages();
		return false;
	});
});

function getmessages() {
		$('form#getmessages').slideUp(100); //hide the form
		$('#loading').slideDown(100); // show the loading message
		$.ajax({ //serialise the data in the inputs and send it to getmessages.php
			type : 'POST',
			url : 'getmessages.php',
			data: {
				server : $('#server').val(),
				username : $('#username').val(),
				pwd : $('#pwd').val()
			},
			success : function(msg){ //on success
				if (msg === 'error') { //error logging in
					$('#loading').hide(10); // hide the loading message
					$('form#getmessages').show();
					$('#content').show();
					$('#loginerror').show();						
				} else if  (msg === 'noemails') { //can't find any messages
					$('#loading').hide(10); // hide the loading message
					$('form#getmessages').show();
					$('#content').show();
					$('#noemails').show();						
				} else {
					$('#loading').hide(10); // hide the loading message
					$('form#getmessages').hide();
					$('#content').show();
					$('#messagelist').html(msg).show(10); //show the php response [list of messages]
					$('.logout').show();	
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {//on error
				$('#loading').show(10); // hide the loading message
				$('#getmessages').hide(10); //show the form
				$('#error').html(msg).show(10); //show the error
			}
		});
		$.cookie('usernamecookie', $('#username').val(), {expires: 30});
		$.cookie('pwdcookie', $('#pwd').val(), {expires: 30});
}


$(document).ready(function(){
	$('#messagelist a').live("click", (function() { //used live() because its loaded after DOM
		//$(this).html('Downloading Message');
		$(this).css("background","url(images/bgselect.png)");
		$(this).children('span.date').hide();
		$(this).children('span.spinner').show();
		//$("#loadattachment").slideDown(100);
		$.cookie('goodlogincookie', '1', {expires: 30});		
		$.ajax({ //serialise the data in the inputs and send it to getattachment.php
			type : 'GET',
			url : 'getattachment.php',
			data: {
				server : $('#server').val(),
				username : $('#username').val(),
				pwd : $('#pwd').val(),
				msgid : $(this).attr('id')			
				},
			success : function(msg){ //on success
				//$(this).text("poo");
				var msg = jQuery.parseJSON(msg);
				//alert("Message: " + results.filename + " ID: " + results.msgid);
				//$('a#'+msg.msgid).html('<audio src="'+msg.filename+'" autoplay="true" controls="true">Your browser does not support the audio element.</audio>');
				//$('#messagelist ul').append('<li>Playing Message... <audio src="'+msg.filename+'" autoplay="true" controls="true">Your browser does not support the audio element.</audio></li>');
				//$(this).replaceWith(msg);
				//$("#file").html(msg);
				//$("#loadattachment").slideUp(100);
				//$("#fileno").html($(this).attr("msgid"));
				//$('#logout h3 a').html(msg);
				//if((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i))) { 
					window.location.href = msg.filename; //redirect to the wav it just downloaded
				//}
			}//,
			//error : function(XMLHttpRequest, textStatus, errorThrown) {//on error
			//	$(this).html("Something Went Wrong!");
			//}
		});
		return false;
	}));
});

$(document).ready(function(){
	$('#logout').click(function() { //when Logout is clicked
		// forget the cookies
		//$.cookie('usernamecookie', null);
		$.cookie('pwdcookie', null);
		$.cookie('goodlogincookie', null);
		// show the logging out message
		$('#loggingout').show();
		//hide the content
		$('#content').hide();
		//refresh the page to start over at login screen
		location.reload(true);
	});
});

