$.sm = {

	// webroot
	"webroot" : 'http://schedulemeet.com/',
	
	// app xml
	"xml" : false,

	// prepare user interface
	"prepare_ui" : function( xml ) {
	
		$.sm.xml = xml;
		
		// configure ui controls
		$.xo.ui_controls.configure( xml );
		
		$(function() {
			if ( $("input.date-picker").length)
				$( "input.date-picker" ).datepicker( {  changeMonth: true , changeYear: true } );
		});
		
	},

	"init" : function( context ) {

		// when document is ready...
		$( document ).ready( function() {
		
			// don't cache ajax
			$.ajaxSetup ( {
			
				cache: false
				
			});
		
			// bind jquery
			$.xo.init( "schedmeet" , $.sm.prepare_ui, context );
			
		});
		
	},
	
	// fair module
	"meeting" : {
	
		// archive callback
		"archive_cb" : function( result ) {


			if ( /success/.test( result ) ) {
	
				var matches = /archive ([0-9]+)/.exec( result);
				var id = matches[1];
		
				// remove meeting from view
				$( 'div#meeting-' + id ).fadeOut("med").remove();
		
				// add a notice
				$( '.notice').html('successfully archived');
				
			} else {
	
				$('.notice').addClass('error').html('an error occurred');
		
			}

		},

	
		// archive a meeting
		// 
		"archive" :  function( e, elem ) {

			// get meeting id
			var matches = /archive-([0-9]+)/.exec( $( elem ).attr('class'));
			var id = matches[1];

	
			// archive it using the API
				$.xo.api.call (
		
					'business/meeting/' + id + '/archive',
					$.sm.meeting.archive_cb
		
				);
	
		},

	
		// view appts for a meeting
		"appts" : function( e, elem ) {

			// get meeting id
			var matches = /appts-([0-9]+)/.exec( $( elem ).attr('class'));
			var id = matches[1];
	
			// redirect
			window.location.href = $.sm.webroot + 'appts/' + id;
	
		},

		// delete callback
		"delete_cb" : function( result ) {


			if ( /recycle ([0-9]+)/.test( result ) ) {
	
				var matches = /recycle ([0-9]+)/.exec( result);
				var id = matches[1];
		
				// remove confirm
				$( 'button.delete-' + id ).removeClass('confirmed');
		
				// add a notice
				$( '.notice').html('successfully deleted!');
		
				// remove
				$( 'div#meeting-' + id).fadeOut("fast").remove();
		
			} else {
	
				$('.notice').addClass('error').html('an error occurred');
		
			}

		},

		// delete a meeting
		"delete" :  function( e, elem ) {

			// get meeting id
			var matches = /delete-([0-9]+)/.exec( $( elem ).attr('class'));
			var id = matches[1];

			// request confirmation
			if ( ! $(elem).hasClass('confirmed') ) {
	
				$( elem ).addClass('confirmed');
		
				// add a notice
				$( '.notice').html('click button again to confirm delete');
		
			} else {
	
				// delete it using the API
				$.xo.api.call (
		
					'business/meeting/' + id + '/cascade_recycle',
					$.sm.meeting.delete_cb
		
				);
	
			}
	
		},

	
		// mouseout event to clear a delete confirmation
		"mouseleave" : function( e, elem ) {

			// get meeting id
			var matches = /meeting-([0-9]+)/.exec( $( elem ).attr('id'));
			var id = 0;
			if ( matches != null )
			id = matches[1];

			$('button.delete-' + id ).removeClass('confirmed');
	
		},

		// edit one
		"edit" : function( e, elem ) {
		
			var matches = /edit-([0-9]+)/.exec( $( elem ).attr('class'));
			var id = matches[1];
			
			window.location.href = $.sm.webroot + 'edit/' + id;
		
		},
	
		// add one
		"add" : function( e, elem ) {

			window.location.href = $.sm.webroot + 'add';
		
		}
	
	},
	
	// appointment module
	"appointment" : {
	
		// approve callback
		"approve_cb" : function( result ) {
		
			if ( /success/.test( result ) ){

				// get meeting id
				var matches = /approve ([0-9]+)/.exec( result);
				var id = matches[1];
			
			
				$('a.approve-' + id ).replaceWith( '<span>confirmed</span>');
				
				$('.notice').html('appointment confirmed');
				
				
			
			} else $( '.notice').addClass('error').html('an error occurred');
		
		},
	
		// approve an appt 
		"approve" : function(e,elem){
		
			// get meeting id
			var matches = /approve-([0-9]+)/.exec( $( elem ).attr('class'));
			var id = matches[1];

			// approve it using the API
				$.xo.api.call (
		
					'business/appointment/' + id + '/approve',
					$.sm.appointment.approve_cb
		
				);
	
		}

	
	}
};
