$(document).ready(function() {
	mdl.init()

})


var mdl = (function(){

	/***************************
	*
	* Private members	
	*  
	***************************/
	
	//Settings
	var confirmAnswer;
	var current;
	
	function setCurrent(w) {
		if(current) {
			current.jqmHide();
		}
		current = w;
	}
	function clearCurrent() {
		current = null;
	}

	function bindTriggers(container) {
		$(".mdlAjaxTBYB").jqmAddTrigger($(".mdlAjaxTBYBTrigger", container));
		$(".mdlAjaxCPage").jqmAddTrigger($(".mdlAjaxCPageTrigger", container));
		$(".mdlHtml").jqmAddTrigger($(".mdlHtmlTrigger", container));
	}

	/***************************
	*
	* Public members	
	*  
	***************************/
	return {
		init: function(){
			//Ajax modal window setup
			$('.mdlAjaxTBYB').jqm({
				ajax: '@href',
				trigger: '.mdlAjaxTBYBTrigger',
				target: '.mdlTarget',
				onShow: function(hash){
					setCurrent(hash.w);
					hash.w.show().find('.mdlLoading').show();
					hash.o.show(); 
				},
				onHide: function(hash){
					hash.w.hide().find('.mdlTarget').html(''); 
					hash.o.remove();
					clearCurrent(); 
				},
				onLoad: function(hash){
					hash.w.find('.mdlLoading').hide();
					bindTriggers(hash.w);
				}
			});
			
			$('.mdlAjaxCPage').jqm({
				ajax: '@href',
				trigger: '.mdlAjaxCPageTrigger',
				target: '.mdlTarget',
				onShow: function(hash){
					setCurrent(hash.w);
					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(); 
					clearCurrent();
				},
				onLoad: function(hash){
					hash.w.find('.mdlLoading').hide();
					bindTriggers(hash.w);
				}
			});
			
			$('.mdlHtml').jqm({
				trigger: $('.mdlHtmlTrigger'),
				onShow: function(hash){
					setCurrent(hash.w);
					var mdlSource = $($(hash.t).attr('mdlSource'));
					var mdlTitle = $(hash.t).attr('mdlTitle');
					var clone = mdlSource.clone(true);
					// NOTE: Trigger functionality is not copied with cloning even, event
					// handlers do get copied, however, jqModal adds expando properties 
					// directly to element that aren't copied across during clone
					bindTriggers(clone);
					hash.w.show();
					hash.w.find('.mdlTarget').show().append(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} )
					}
					clearCurrent();
				}
			})
			
			$('#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
			
		}
	}
})()
	
