$(document).ready(function() {
	mdl.init()

})


var mdl = (function(){

	/***************************
	*
	* Private members	
	*  
	***************************/
	
	//Settings
	var confirmAnswer;
	

	/***************************
	*
	* Public members	
	*  
	***************************/
	return {
		init: function(){
			//Ajax modal window setup
			$('.mdlAjaxTBYB').jqm({
				ajax: '@href',
				trigger: '.mdlAjaxTBYBTrigger',
				target: '.mdlTarget',
				onShow: function(hash){
					hash.w.show().find('.mdlLoading').show();
					hash.o.show(); 
				},
				onHide: function(hash){
					hash.w.hide().find('.mdlTarget').html(''); 
					hash.o.remove(); 
				},
				onLoad: function(hash){
					hash.w.find('.mdlLoading').hide();
				}
			});
			
			$('.mdlAjaxCPage').jqm({
				ajax: '@href',
				trigger: '.mdlAjaxCPageTrigger',
				target: '.mdlTarget',
				onShow: function(hash){
					var mdlTitle = $(hash.t).attr('mdlTitle');
					hash.w.show().find('.mdlLoading').show();
					hash.w.find('.mdlControls .e_title').html(mdlTitle);
					hash.o.show();
					hash.w.css
				},
				onHide: function(hash){
					hash.w.hide().find('.mdlTarget').html(''); 
					hash.o.remove(); 
				},
				onLoad: function(hash){
					hash.w.find('.mdlLoading').hide();
				}
			});
			
			$('.mdlHtml').jqm({
				trigger: $('.mdlHtmlTrigger'),
				onShow: function(hash){
					var mdlSource = $( $(hash.t).attr('mdlSource') );
					var mdlTitle = $(hash.t).attr('mdlTitle');
					hash.w.show();
					hash.w.find('.mdlTarget').show().append( mdlSource.clone() );
					hash.w.find('.mdlTarget').find('.mdlHtmlContent').show()
					hash.w.find('.mdlControls .e_title').html(mdlTitle);
					
					//Call a MyDocuments event
					if( typeof myd != 'undefined' ){
						myd.actionHandler( 'modalHtmlOnShow', {hash: hash} );
					}
					
				},
				onHide: function(hash){
					hash.w.hide()
					hash.w.find('.mdlTarget').empty(); 
					hash.w.find('.mdlControls .e_title').empty(); 
					hash.o.remove();
					
					//Call a MyDocuments event
					if( typeof myd != 'undefined' ){
						myd.actionHandler( 'modalHtmlOnHide', {hash: hash} )
					}
				}
			})
			
			$('#mdlPrompt').jqm({
				modal: true, trigger: false
			})
			
			$('#mdlConfirm').jqm({
				modal: true, trigger: false
			})
			
		},
		prompt: function(args, callback){
			var mdlPrompt = $("#mdlPrompt")
			mdlPrompt.jqmShow()
			
			//Set the message and default value of the input
			mdlPrompt.find('.e_message').html(args.message)
			
			//defaultVal is optional
			if( args.defaultVal != undefined ){
				mdlPrompt.find('.e_input INPUT').val(args.defaultVal)
			}
			
			//Bind CLICK listeners to the OK and CANCEL button 
			mdlPrompt.find('.e_buttons .e_cancel INPUT').click(function(){
				mdlPrompt.jqmHide();
				mdlPrompReset();
			})
			mdlPrompt.find('.e_buttons .e_ok INPUT').click(function(){
				mdlPrompt.jqmHide();
				callback( mdlPrompt.find('.e_input INPUT').val() );	
				mdlPrompReset();
			})
			
			//Prompt reset function
			function mdlPrompReset(){
				mdlPrompt.find('.e_message').html('');
				mdlPrompt.find('.e_input INPUT').val('');
				mdlPrompt.find('.e_buttons *').unbind()
				
			}
			
		},
		confirm: function(message, callback){
			var mdlConfirm = $('#mdlConfirm');
			mdlConfirm.jqmShow()
			
			//Set the message and default value of the input
			mdlConfirm.find('.e_message').html(message)
			
			
			//Bind CLICK listeners to the OK and CANCEL button 
			mdlConfirm.find('.e_buttons .e_cancel INPUT').click(function(){
				mdlConfirm.jqmHide();
				mdlConfirmReset();
			})
			mdlConfirm.find('.e_buttons .e_ok INPUT').click(function(){
				mdlConfirm.jqmHide();
				callback();	
				mdlConfirmReset();
			})
			
			
			//Prompt reset function
			function mdlConfirmReset(){
				mdlConfirm.find('.e_message').html('');
				mdlConfirm.find('.e_input INPUT').val('');
				mdlConfirm.find('.e_buttons *').unbind()
				
			}
			return false
			
		}
	}
})()
	