tone = function() {
	return {
		
		/***  Variables ***/
		tone	: null,
		links	: {
			emailLink : "/admin/api/tonefunctions.php?c=emailTone"
		},
		
		/***  Method  ***/
		init: function() {
			//Initialize Sending Functions
			$("#frmSend").submit(function(e) { 
				e.preventDefault();

				number = $("#number").val();
				carrier = $("#carrier option:selected").val();

				var response = tone.validateNumber(number);
				if (response !== '') {
					$("#sendmsg").html(response);
				} else if (!carrier) {
					$("#sendmsg").html("Please select a carrier.");
				} else {
					$("#sendmsg").html("<img src=/images/sending.gif></img>");

					var url = '/admin/api/tonefunctions.php?c=sendTone&id='+tone.tone.id+'&number='+number+'&carrier='+carrier;

					$.getJSON(url, function(data) {
						if (data) {
							if (data.result == 1) {
								$("#sendmsg").html("Tone sent.");
								//tone.incDownload();
							} else {
								$("#sendmsg").html("Error sending tone.");
							}
						}
					});
				}

			});
			
			
			$("#getIt").click(function(e) {
				tone.incDownload();
			});

			$("#getIt").bind('contextmenu', function(e) {
				tone.incDownload();
				return true;
			});
			
			$("#getIt").bind('contextmenu', function(e) {
				tone.incDownload();
				return true;
			});
			
			//Init Email Dialog Button
			$('#emailTone').click(function(e) {
		        e.preventDefault();

		        tone.initEmailDialog();
	      	});
	
			$("#moreInfo").click(function(e) {
				e.preventDefault();
				
				$('<div/>').attr('id', 'dialogEmail').html("Some phones still exist that do not support recieving MP3 ringtones via MMS (Notably the iPhone). You are free to download the ringtone as an MP3 file and do what you can to create a ringtone from it. If you have any suggestions on how to deliver tones in a different format we welcome your feedback.  If you are having difficulty and you believe your phone is compatible, please email info@deeplocal.com with your request or suggestion for technical help.").dialog({
					'modal' : true,
					'title' : 'Receiving tones via MMS',
					'dialogClass' : 'dialogEmail',
					'width' : '450px',
					'buttons' : {
						'Close' : function() {
							$(this).dialog("close");
							$('#dialogEmail').remove();
						}
					}
				});
			})
		},
		
		
		incDownload : function() {
			var url = "http://www.locallytoned.org/admin/api/tonefunctions.php?c=incDownloads&id="+tone.tone.id;
			
			$.getJSON(url, function(data) {
				//Increment Downloads
			});
		},
		
		initEmailDialog : function() {
	      $('<div/>').attr('id', 'dialogEmail').html('<form action="' + tone.links.emailLink + '" method="post"><input type="hidden" name="id" id="id" value="' + tone.tone.id + '"/><p><strong><label for="to">Their email address</label></strong></p><p><input type="text" name="to" value="" id="to"></p><p><strong><label for="from">Your email address</label></strong></p><p><input type="text" name="from" value="" id="from"></p></form>').dialog({
	        'modal' : true,
	        'title' : 'Share this ringtone with your friends',
	        'dialogClass' : 'dialogEmail',
	        'width' : '450px',
	        'buttons' : {
	          'Send email now' : function() {
	            $(this).dialog("close");
	            $('#dialogEmail form').submit();
	          },
	          'Cancel' : function() {
	            $(this).dialog("close");
	            $('#dialogEmail').remove();
	          }
	        }
	      });
	    },
		
		
		validateNumber: function(number) {
			var err = '';
			var stripped = number.replace(/[\(\)\.\-\ ]/g, '');

			if (stripped === '') {
				err = "Please enter a phone number.";
			} else if (isNaN(parseInt(stripped))) {
				err = "The phone number contains illegal characters.";
			} else if (stripped.length != 10) {
				err = "The phone number is the wrong length.  Make sure to include your area code.";
			}
			return err;
		}
	};
}();
