// JS-LINT ran: 07.16.2008
/*
 * Modal Types:	1 - Non Modal Custom Dialog
 * 				2 - Modal Custom Dialog
 * 				3 - Prompt
 * 				4 - Confirm
 * 				5 - Alert
 * 				6 - Error
 * 
 */
TlcIndigo.Modal = {};
//Setup the modal area
TlcIndigo.Modal.init = function(){
	TlcIndigo.Modal.Container = $j('<div></div>').attr('id','modalContainer2');
	TlcIndigo.Modal.Shade = $j('<div></div>').attr('id','modalShade');
	TlcIndigo.Modal.Container.append(TlcIndigo.Modal.Shade);
	$j(document.body).append(TlcIndigo.Modal.Container);
	TlcIndigo.Modal.objects = [];
	//TODO: Find a better way to set listener
	TlcIndigo.Modal.intervalID = setInterval(function(){
		if(TlcIndigo.laaMainWindow){
			clearInterval(TlcIndigo.Modal.intervalID);
			TlcIndigo.laaMainWindow.onShowBookRiver.subscribe(TlcIndigo.Modal.CloseAll);
		}
	},100);
};
//Close ALL Modals
TlcIndigo.Modal.CloseAll = function(){
	for(var i=0; i<TlcIndigo.Modal.objects.length; i++){
		if (TlcIndigo.Modal.objects[i]){
			TlcIndigo.Modal.objects[i].Close();
		}
	}
	TlcIndigo.Modal.Shade.css('z-index',1);
	TlcIndigo.Modal.Container.hide();
	TlcIndigo.Modal.objects = [];
	
	$j('#laaMainWindow').unbind('click',TlcIndigo.Modal.CloseAll);
};
//Open model
TlcIndigo.Modal.Open = function(){
	if(this.onBeforeOpen){
		this.onBeforeOpen();
	}
	//Get Highest order of Modal
	var shadeType = this.type;
	
	for(var i=0; i<TlcIndigo.Modal.objects.length; i++){
		if(TlcIndigo.Modal.objects[i] !== undefined && TlcIndigo.Modal.objects[i] !== null){
			shadeType = (TlcIndigo.Modal.objects[i].type > shadeType) ? TlcIndigo.Modal.objects[i].type : shadeType;
		}
	}
	TlcIndigo.Modal.Shade.css('z-index',shadeType);
	
	TlcIndigo.Modal.Container.removeClass();
	
	if(shadeType>1){
		TlcIndigo.Modal.Container.addClass('modalOn');
	}else{
		TlcIndigo.Modal.Container.addClass('modalOff');
		$j('#laaMainWindow').bind('click',TlcIndigo.Modal.CloseAll);
	}
	
	if(!this.modal){this.winRef.addClass('modalNo');}
	TlcIndigo.Modal.Container.show();
	TlcIndigo.Modal.Container.append(this.winRef);
	var margin = '-' + this.winRef.height()/2 + 'px 0px 0px -' + this.winRef.width()/2 + 'px';
	if(this.modal){
		this.winRef.css('margin',margin);
	}else{
		TlcIndigo.Modal.Container.css('margin',margin);
		this.winRef.css('margin',margin);
	}
	
//	if(jQuery.browser.mozilla && parseInt(jQuery.browser.version,10)<3){//This hack will allow the cursor to be visible in Firefox ver 2
	if(jQuery.browser.mozilla && parseFloat(jQuery.browser.version) < 1.9){
//		this.winRef.css('overflow','auto');
		this.winRef.find('input.textInput').parent().css('overflow','auto');
	}
	
	this.winRef.css('visibility','visible');
	if(this.onOpen){
		this.onOpen();
	}
	if(shadeType == this.type){
		this.onFocus();
	}
};
//Close modal
TlcIndigo.Modal.Close = function(){
	if(this.onBeforeClose){
		this.onBeforeClose();
	}
	if(this.winRef){
		this.winRef.remove();
	}
	TlcIndigo.Modal.Container.removeClass();
	
	//Get Highest order of Modal
	var shadeType = 1;
	for(var i=0; i<TlcIndigo.Modal.objects.length; i++){
		if(TlcIndigo.Modal.objects[i] !== undefined && TlcIndigo.Modal.objects[i] !== null && TlcIndigo.Modal.objects[i] != this){
			shadeType = (TlcIndigo.Modal.objects[i].type > shadeType) ? TlcIndigo.Modal.objects[i].type : shadeType;
		}
	}
	TlcIndigo.Modal.Shade.css('z-index',shadeType);
	
	if(TlcIndigo.Modal.objects.length == 1){
		TlcIndigo.Modal.Container.hide();
		$j('#laaMainWindow').unbind('click',TlcIndigo.Modal.CloseAll);
	}else{
		if(shadeType>1){
			TlcIndigo.Modal.Container.addClass('modalOn');
		}else{
			TlcIndigo.Modal.Container.addClass('modalOff');
		}
	}
	if(this.onClose){
		this.onClose();
	}
	
	for (var i = 0; i < TlcIndigo.Modal.objects.length; i++){
		if (TlcIndigo.Modal.objects[i] && TlcIndigo.Modal.objects[i].type == this.type){
			delete TlcIndigo.Modal.objects[i];
		}
	}
	
	for (var i = TlcIndigo.Modal.objects.length - 1; i >= 0; i--){
		if (TlcIndigo.Modal.objects[i] !== undefined){
			TlcIndigo.Modal.objects[i].onFocus();
			break;
		}
	}
//	if(TlcIndigo.Modal.objects.length > 0){
//		TlcIndigo.Modal.objects[TlcIndigo.Modal.objects.length - 1].onFocus();
//	}
};
//Focus Element
TlcIndigo.Modal.OnFocus = function(){
	var lastFocused = this.winRef.find('input.lastfocused').attr('lastfocused');
//	lastFocused=lastFocused;
	if(lastFocused !== undefined){
		this.winRef.find('input[@tabindex=' + lastFocused + ']')[0].focus();
	}else{
		this.winRef.find('input')[0].focus();
	}
};
//Display modal processing
TlcIndigo.Modal.Process = function(){
	this.winRef.find('.processing').show();
};
//Hide modal processing
TlcIndigo.Modal.EndProcess = function(){
	this.winRef.find('.processing').hide();
};
//Display ErrorMessage
TlcIndigo.Modal.OnError = function(msg){
	TlcIndigo.Modal.Alert(msg);
};
//Create Button Holder
TlcIndigo.Modal.ButtonGroup = function(){
	return $j('<div></div>')
				.addClass('buttonGroup justifyC');
};
//Create a Button
TlcIndigo.Modal.Button = function(type,caption,func,objRef){
	var btn;
	if(type=='submit'){
		btn = $j('<input type="submit"/>');
	}else{
		btn = $j('<input type="button"/>').bind('click',this.ButtonClick);
		btn[0].func = func;
		btn[0].obj = objRef;
	}

	return btn
			.addClass('button')
			.attr('title',caption)
			.val(caption);
};
//Button Function
TlcIndigo.Modal.ButtonClick = function(){
	this.func(this.obj);
};

//Window Creator
TlcIndigo.Modal.Window = function(params){
	for(var i=0; i < TlcIndigo.Modal.objects.length; i++){
		if(TlcIndigo.Modal.objects[i] && TlcIndigo.Modal.objects[i].type == params.type){
			return;
		}
	}
	
	if(!params.onSubmit){
		params.onSubmit = function(objRef){objRef.Close();return false;};
	}
	if(!params.onError){
		params.onError = TlcIndigo.Modal.OnError;
	}
	if(!params.onFocus){
		params.onFocus = TlcIndigo.Modal.OnFocus;
	}
	
	this.onOpen = params.onOpen;
	this.onClose = params.onClose;
	this.onBeforeOpen = params.onBeforeOpen;
	this.onBeforeClose = params.onBeforeClose;
	this.onSubmit = params.onSubmit;
	this.onError = params.onError;
	this.onFocus = params.onFocus;
	
	this.modal = (params.modal !== undefined) ? params.modal : true;
	this.width = params.width;
	this.height = params.height;
	this.type = params.type;
	this.callback = params.callback;
	this.title = params.title;
	this.buttons = params.buttons;
	this.html = params.html;
	
	var constant = {
		cssClass: {
			modal:			'modal',
			modalbg:		'modalbg',
			title:			'titlebar',
			message:		'message',
			justifyC:		'justifyC',
			justifyL:		'justifyL',
			justifyR:		'justifyR',
			modalPane:		'modalPane',
			processing:		'processing'
		},
		src: {
			processing:		'scripts/TlcIndigo/util/Modal/30-1.gif'
		}
	};
	
	if(typeof this.html === undefined){
		this.html = $j('<form></form>');
	}else if(typeof this.html == 'string'){
		this.html = $j(this.html);
	}
	
	this.winRef = $j('<div></div>')
						.addClass(constant.cssClass.modal)
						.attr('type',this.type)
						.append($j('<div></div>')
							.addClass(constant.cssClass.modalbg)
						);
	
	if(this.width){
		this.winRef.width(this.width);
	}
	if(this.height){
		this.winRef.width(this.height);
	}
	if(this.title){
		this.winRef.append($j('<div></div>')
										.addClass(constant.cssClass.title)
										.html(this.title)
									);
	}
	if(this.buttons){
		var buttonGroup = TlcIndigo.Modal.ButtonGroup();
		for(var i=0; i<this.buttons.length; i++){
			buttonGroup.append(this.Button(this.buttons[i].type,this.buttons[i].text,this.buttons[i].func,this));
		}
		this.html.append(buttonGroup);
	}
	
	this.html.append($j('<input type="hidden"/>').addClass('lastfocused').attr('lastfocused',''));
	
	this.winRef.append(this.html);
	
	this.winRef.append($j('<div></div>')
						.addClass(constant.cssClass.processing)
						.append($j('<div></div>'))
						.append($j('<img/>')
							.attr('src',constant.src.processing)
						)
				);
	var tabindex = 100 * this.type;
	var _this = this;
	$j.each(this.winRef.find('input'), function(index, input){
		$j(input).attr('tabindex',tabindex++).focus(function(){
//			_this.winRef.find('input[@lastFocused]').attr('lastFocused',this.tabindex)
//			$j(this).parents('.' + constant.cssClass.modal).find('input.lastfocused').attr('lastfocused',this.tabindex);
			var p = $j(this).parents('.' + constant.cssClass.modal);
			p = p.find('input.lastfocused');
			p.attr('lastfocused',$j(this).attr('tabindex'));
		});
	});
	
	this.form = this.winRef.find('form');
	if(this.form.length > 0){
		_this = this.form[0];
		_this.obj = this;
		_this.func = this.onSubmit;
		_this.method = 'post';
		_this.onsubmit = function(){
			try{
				_this.func(_this.obj);
				return false;
			}catch(e){
				alert('An error has occurred!');
				return false;
			}
		};
	}
	
	this.Open();
};
TlcIndigo.Modal.Window.prototype.Button = TlcIndigo.Modal.Button;
TlcIndigo.Modal.Window.prototype.ButtonClick = TlcIndigo.Modal.ButtonClick;
TlcIndigo.Modal.Window.prototype.Open = TlcIndigo.Modal.Open;
TlcIndigo.Modal.Window.prototype.Close = TlcIndigo.Modal.Close;
TlcIndigo.Modal.Window.prototype.Process = TlcIndigo.Modal.Process;
TlcIndigo.Modal.Window.prototype.EndProcess = TlcIndigo.Modal.EndProcess;

TlcIndigo.Modal.Exists = function(type){
	return TlcIndigo.Modal.objects[type] !== undefined;
};

//Custom Dialog
TlcIndigo.Modal.Dialog = function(params){
	var type = (params.modal === undefined || params.modal === true) ? 2 : 1;
	if(!this.Exists(type)){
		params.type = type;
		var objRef = new TlcIndigo.Modal.Window(params);
		objRef.winRef.css('z-index',type);
		TlcIndigo.Modal.objects[type] = objRef;
	}else{
		return false;
	}
};
TlcIndigo.Modal.Dialog.prototype.Exists = TlcIndigo.Modal.Exists;
//Prompt
TlcIndigo.Modal.Prompt = function(msg,ans,cb){
	var type = 3;
	if(!this.Exists(type)){
		var objRef = new TlcIndigo.Modal.Window({
			type:type,
			width:'400px',
			modal:true,
			html:$j('<form></form>')
					.append($j('<div></div>')
						.addClass('modalPane prompt')
						.append($j('<div></div>')
							.html(msg)
						)
						.append($j('<input type="text"/>')
							.attr('id','promptAnswer')
							.addClass('textInput large')
							.val(ans)
						)	
					),
			callback:cb,
			buttons:[{
				text:'OK',
				func:function(objRef){
					var cb = objRef.callback;
					var ans = $j('#promptAnswer').val();
					objRef.Close();
					cb(ans);
				}
			},
			{
				text:'CANCEL',
				func:function(objRef){
					var cb = objRef.callback;
					objRef.Close();
					cb();
				}
			}],
			onOpen:function(){$j('#promptAnswer').focus();}
		});
		objRef.winRef.css('z-index',type);
		TlcIndigo.Modal.objects[type] = objRef;
	}else{
		return false;
	}
};
TlcIndigo.Modal.Prompt.prototype.Exists = TlcIndigo.Modal.Exists;
//Confirm
TlcIndigo.Modal.Confirm = function(msg,cb){
	var type = 4;
	if(!this.Exists(type)){
		var objRef = new TlcIndigo.Modal.Window({
			type: type,
			width:'350px',
			modal:true,
			html:$j('<form></form>').append($j('<div></div>').addClass('modalPane confirm').html(msg)),
			callback:cb,
			buttons:[{text:'OK',func:function(objRef){
					var cb = objRef.callback;
					objRef.Close();
					cb(1);
				}
			},{text:'CANCEL',func:function(objRef){
					var cb = objRef.callback;
					objRef.Close();
					cb(-1);
				}
			}],
			onOpen:function(){$j('.confirm').parent().find('.buttonGroup .button')[0].focus();}
		});
		objRef.winRef.css('z-index',type);
		TlcIndigo.Modal.objects[type] = objRef;
	}else{
		return false;
	}
};
TlcIndigo.Modal.Confirm.prototype.Exists = TlcIndigo.Modal.Exists;
//Alert
TlcIndigo.Modal.Alert = function(msg){
	var type = 5;
	if(!this.Exists(type)){
		var _this=this;
		var funcOk = function(objRef){
			objRef.Close();
			objRef = null;
			if(_this.Waiting.length>0){
				TlcIndigo.Modal.Alert(_this.Waiting.shift());
			}
		};
		var objRef = new TlcIndigo.Modal.Window({
			type:type,
			width:'350px',
			modal:true,
			html:$j('<form></form>').append($j('<div></div>').addClass('modalPane alert').html(msg)),
			buttons:[{text:'OK',func:funcOk}],
			onOpen:function(){$j('.alert').parent().find('.buttonGroup .button')[0].focus();}
		});
		objRef.winRef.css('z-index',type);
		TlcIndigo.Modal.objects[type] = objRef;
		if(!this.Waiting){
			this.Waiting = [];
		}
	}else{
		this.Waiting.push(msg);
		return false;
	}
};
TlcIndigo.Modal.Alert.prototype.Exists = TlcIndigo.Modal.Exists;
//Error
TlcIndigo.Modal.Error = function(error,details){
	var type = 6;
	var objRef;
	if(!this.Exists(type)){
		var _this = this;
		var detailsFunc = function(){
			var dPane = $j('#modalErrorDetailsPane');
			if(dPane.css('display') == 'none'){
				dPane.show();
				TlcIndigo.Modal.objects[type].winRef.find('input[@title=Show Details]').attr('title','Hide Details').val('Hide Details');
			}else{
				dPane.hide();
				TlcIndigo.Modal.objects[type].winRef.find('input[@title=Hide Details]').attr('title','Show Details').val('Show Details');
			}
		};
		var prevFunc = function(){
			_this.ErrorNum--;
			TlcIndigo.Modal.objects[type].winRef.find('input[@title=Next]').removeClass('disabled').removeAttr('disabled');
			if(_this.ErrorNum === 0){//Disable this button
				TlcIndigo.Modal.objects[type].winRef.find('input[@title=Previous]').addClass('disabled').attr('disabled',true);
			}
			//Show Error
			$j('#modalErrorErrorPane > div > div+div').empty().append(_this.Errors[_this.ErrorNum].error);
			$j('#modalErrorErrorPane > div+div > div+div').empty().append(_this.Errors[_this.ErrorNum].details);
		};
		var nextFunc = function(){
			_this.ErrorNum++;
			TlcIndigo.Modal.objects[type].winRef.find('input[@title=Previous]').removeClass('disabled').removeAttr('disabled');
			if(_this.ErrorNum == _this.Errors.length-1){//Disable this button
				TlcIndigo.Modal.objects[type].winRef.find('input[@title=Next]').addClass('disabled').attr('disabled',true);
			}
			//Show Error
			$j('#modalErrorErrorPane > div >div+div').empty().append(_this.Errors[_this.ErrorNum].error);
			$j('#modalErrorErrorPane > div+div >div+div').empty().append(_this.Errors[_this.ErrorNum].details);
		};
		objRef = new TlcIndigo.Modal.Window({
			type: type,
			width:'350px',
			modal:true,
			html:$j('<form></form>')
						.append($j('<div></div>')
							.addClass('modalPane error')
							.append($j('<div></div>')
								.html('One or more errors has occurred.  For more information, click the \'Show Details\' button.')
							)
							.append($j('<div></div>')
								.attr('id','modalErrorDetailsPane')
								.append($j('<div></div>')
									.attr('id','modalErrorErrorPane')
									.append($j('<div></div>')
										.append($j('<div></div>')
											.html('Error: ')
										)
										.append($j('<div></div>')
											.html(error)
										)
									)
									.append($j('<div></div>')
										.append($j('<div></div>')
											.html('Details: ')
										)
										.append($j('<div></div>')
											.html(details)
										)
									)
								)
								.append($j('<div></div>')
									.append($j('<input type="button"/>')
										.addClass('button disabled')
										.attr('disabled',true)
										.attr('title','Previous')
										.val('Previous')
										.click(prevFunc)
									)
									.append($j('<input type="button"/>')
										.addClass('button disabled')
										.attr('disabled',true)
										.attr('title','Next')
										.val('Next')
										.click(nextFunc)
									)
								)
							)
						),
			buttons:[{text:'Ok',func:function(objRef){objRef.Errors=[];objRef.Close();}},
				{text:'Show Details',func:detailsFunc}
			],
			onOpen:function(){$j('.error').parent().find('.buttonGroup .button')[0].focus();}
		});
		objRef.winRef.css('z-index',type);
		TlcIndigo.Modal.objects[type] = objRef;
		this.Errors = [];
		this.ErrorNum = 0;
		this.Errors.push({error:error,details:details});
	}else{
		this.Errors.push({error:error,details:details});
		objRef = TlcIndigo.Modal.objects[type];
		objRef.winRef.find('input[@title=Next]').removeClass('disabled').removeAttr('disabled');
		
	}
};
TlcIndigo.Modal.Error.prototype.Exists = TlcIndigo.Modal.Exists;

$j(document).ready(function(){
	TlcIndigo.Modal.init();
});







TlcIndigo.Modal.Message = function(msg){
	var modalMessage = $j('<div></div>')
			.addClass('modalMessage')
			.css({opacity:1})
			.html(msg);
	$j(document.body).append(modalMessage);
	var margin = '0px 0px 0px ' + (modalMessage.width()/2) + 'px';
	modalMessage.css('margin',margin).css('visibility','visible');
	var _this=this;
	setTimeout(function(){TlcIndigo.Modal.Message.Remove(modalMessage);},3000);
};
TlcIndigo.Modal.Message.Remove = function(modal){
	modal.animate({opacity:0}, 'slow', function(){
		modal.remove();
	});
};
