﻿msgBox = new function(){
	var xOkButton, xCancelButton, xCloseButton;
	this.iconSuccess = globalImgServer+'/images/eng/style/css_images/successB.gif';
	this.iconError = globalImgServer+'/images/eng/style/css_images/errorB.gif';
	this.iconAlert = globalImgServer+'/images/eng/style/css_images/alertB.gif';
	this.iconInfo = globalImgServer+'/images/eng/style/css_images/infoC.gif';
	this.iconNotice = globalImgServer+'/images/eng/style/css_images/noticeC.gif';
	this.icon = '';
	this.showBGAnim = false;
	this.showMsgBoxAnim = false;
	
	this.playAnimation = function(BGDiv,MsgBoxDiv,onComplete){
		var oBG = get(BGDiv);
		var oMsgBox = get(MsgBoxDiv);
		var h = get('msgBoxContent').offsetHeight;
		oMsgBox.style.height = h + 'px';
		if(this.showBGAnim){
			var animBG = new YAHOO.util.Anim(oBG,{opacity:{from:0,to:0.5}}, 0.2);
			if(this.showMsgBoxAnim){
				animBG.onComplete.subscribe(function(){
					var animMsgBox = new YAHOO.util.Anim(oMsgBox,{opacity:{from:0,to:1}}, 0.3);
					animMsgBox.onComplete.subscribe(function(){
						oMsgBox.style.filter = '';
						onComplete();
					});
					animMsgBox.animate();
				});
			}
			else{
				animBG.onComplete.subscribe(function(){
					oMsgBox.style.filter = '';
					oMsgBox.style.opacity = 1;
					oMsgBox.style.MozOpacity = 1;
					onComplete();
				});
			}
			animBG.animate();
		}
		else if(this.showMsgBoxAnim){
			var animMsgBox = new YAHOO.util.Anim(oMsgBox,{opacity:{from:0,to:1}}, 0.3);
			animMsgBox.onComplete.subscribe(function(){
				oMsgBox.style.filter = '';
				onComplete();
			});
			animMsgBox.animate();
		}
		else{
			onComplete();
		}
	}
	
	this.getWindowWH = function (){
		var w = document.body.clientWidth;
		var h = document.body.scrollHeight;
		return [w,h];
	}
	
	this.xConfirm = function(sContent,aWH,oCallBackInstance,sBoxTitle,aButtonText,defaultButton,showAnim,boxType){
		//if(get('xMsgBox')){
		//msgBox.closeMsgBox();
		//}
		this.showBGAnim = (showAnim && showAnim[0]);
		this.showMsgBoxAnim = (showAnim && showAnim[1]);
		this.icon = this.getBoxType(boxType);
		if(!aButtonText){
			var aButtonText=[['Ok',IL.ok],['Cancel',IL.cancel]];
		}
		else if(!YL.isArray(aButtonText[0]) && !YL.isArray(aButtonText[1])){
			aButtonText = [['Ok',aButtonText[0]],['Cancel',aButtonText[1]]];
		}
		if(!defaultButton){
			var defaultButton='Ok';
		}
		var objBackgroundDiv = this.createMaskArea([0,0],this.getWindowWH(),'backgroundDiv',999998);
		var w=YUD.getViewportWidth();
		w/=2;
		w-=aWH[0]/2;
		var h=(YUD.getViewportHeight()/2) - aWH[1]/2;
		var objXConfirmBox = this.createMsgBox([w,h],aWH,'xMsgBox',999999,sContent,sBoxTitle,false,20,true);
		
		var xContent  = YUD.getElementsByClassName('PopUpContent','div',objXConfirmBox)[0];
		
		xContent.innerHTML+='<div align="center" id="msgBoxContainer"  style="margin-top:10px;"><input type="button" value="'+aButtonText[0][1]+'" name="msgBox'+aButtonText[0][0]+'Button" class="'+this.getButtonStyle(aButtonText[0][0])+'" /> <input type="button" value="'+aButtonText[1][1]+'" name="msgBox'+aButtonText[1][0]+'Button" class="'+this.getButtonStyle(aButtonText[1][0])+'" /></div>';
		xOkButton = YUD.getElementsBy(function(el){return el.getAttribute('name') == 'msgBoxOkButton';}, 'input', objXConfirmBox)[0];
		xCancelButton = YUD.getElementsBy(function(el){return el.getAttribute('name') == 'msgBoxCancelButton';}, 'input', objXConfirmBox)[0];
		xCloseButton = YUD.getElementsByClassName('msgBoxCloseButtonImg','img',objXConfirmBox)[0]; 
		YUE.addListener(xOkButton, 'click',this.closeMsgBox);
		YUE.addListener(xCancelButton, 'click',this.closeMsgBox);
		YUE.addListener(xOkButton, 'click',oCallBackInstance.callBackConfirm,[true,oCallBackInstance]);
		YUE.addListener(xCancelButton, 'click',oCallBackInstance.callBackConfirm,[false,oCallBackInstance]);
		YUE.addListener(xCloseButton, 'click',oCallBackInstance.callBackConfirm,[false,oCallBackInstance]);
		var xDefaultButton;
		if(defaultButton == 'Ok') xDefaultButton = xOkButton;
		else xDefaultButton = xCancelButton;
		//get("msgBoxOkButton").focus();
		if(!aWH[1] && YUD.getViewportHeight()<=objXConfirmBox.offsetHeight){
			h=document.documentElement.scrollTop;
		}else if(!aWH[1] && YUD.getViewportHeight()>objXConfirmBox.offsetHeight){
			h-=get('msgBoxContent').offsetHeight/2;
		}
		if(!aWH[0]){
			w=w-get('msgBoxContent').offsetWidth/2;
		}
		YUD.setXY(objXConfirmBox,[w,h]);
		if (!AE.bom.isIE6) objXConfirmBox.style.position = 'fixed';
		else{
			objXConfirmBox.style.top = h + YUD.getDocumentScrollTop() + 'px';
			YUE.on(window, 'scroll', function(){setTimeout(function(){objXConfirmBox.style.top = h + YUD.getDocumentScrollTop() + 'px';}, 300);});
		}
		YUE.on(document, 'keypress', function(e){
			var code = e.keyCode || e.which;
			if(code == 27) xCancelButton.click();
			if(code == 13) xOkButton.click();
		});
		this.playAnimation('backgroundDiv','xMsgBox',function(){xDefaultButton.focus();});
	}
	
	this.xAlert = function (sContent,aWH,sBoxTitle,showAnim,aButtonText,boxType){
		//if(get('xMsgBox')){
		//msgBox.closeMsgBox();
		//}
		this.showBGAnim = (showAnim && showAnim[0]);
		this.showMsgBoxAnim = (showAnim && showAnim[1]);
		this.icon = this.getBoxType(boxType);
		var objBackgroundDiv = this.createMaskArea([0,0],this.getWindowWH(),'backgroundDiv',999998);
		var w=YUD.getViewportWidth();
		w/=2;
		w-=aWH[0]/2;
		var h=(YUD.getViewportHeight()/2) - aWH[1]/2;
		var objXAlertBox = this.createMsgBox([w,h],aWH,'xMsgBox',999999,sContent,sBoxTitle,false,20,true);
		var xContent  = YUD.getElementsByClassName('PopUpContent','div',objXAlertBox)[0];
		if(!aButtonText){
			aButtonText=IL.ok;
		}
		
		xContent.innerHTML+='<div align="center" id="msgBoxContainer" style="margin-top:10px;"><input type="button" value=" '+aButtonText+' " class="msgBoxCloseButton" name="msgBoxCloseButton" /></div>';
		xCloseButton = YUD.getElementsByClassName('msgBoxCloseButton','input',xContent)[0];
		YUE.addListener(xCloseButton, 'click',this.closeMsgBox);
		//xCloseButton.focus();
		if(!aWH[1] && YUD.getViewportHeight()<=objXAlertBox.offsetHeight){
			h=document.documentElement.scrollTop;
		}else if(!aWH[1] && YUD.getViewportHeight()>objXAlertBox.offsetHeight){
			h-=get('msgBoxContent').offsetHeight/2;
		}
		if(!aWH[0]){
			w=w-get('msgBoxContent').offsetWidth/2;
		}
		if(h<0){h=0;}
		YUD.setXY(objXAlertBox,[w,h]);
		if (!AE.bom.isIE6) objXAlertBox.style.position = 'fixed';
		else{
			objXAlertBox.style.top = h + YUD.getDocumentScrollTop() + 'px';
			YUE.on(window, 'scroll', function(){setTimeout(function(){objXAlertBox.style.top = h + YUD.getDocumentScrollTop() + 'px';}, 300);});
		}
		this.playAnimation('backgroundDiv','xMsgBox',function(){xCloseButton.focus();});
	}
	
	this.xLock = function (sContent,aWH,sBoxTitle,showAnim,boxType){
		this.showBGAnim = (showAnim && showAnim[0]);
		this.showMsgBoxAnim = (showAnim && showAnim[1]);
		this.icon = this.getBoxType(boxType);
		if(get('xMsgBox')){
			msgBox.closeMsgBox();
		}
		var objBackgroundDiv = this.createMaskArea([0,0],this.getWindowWH(),'backgroundDiv',999998);
		var w=YUD.getViewportWidth();
		w/=2;
		w-=aWH[0]/2;
		var h=(YUD.getViewportHeight()/2) - aWH[1]/2;
		var objXAlertBox = this.createMsgBox([w,h],aWH,'xMsgBox',999999,sContent,sBoxTitle,false,20,false);
		//window.focus();
		if(!aWH[1] && YUD.getViewportHeight()<=objXAlertBox.offsetHeight){
			h=document.documentElement.scrollTop;
		}else if(!aWH[1] && YUD.getViewportHeight()>objXAlertBox.offsetHeight){
			h-=get('msgBoxContent').offsetHeight/2;
		}
		if(!aWH[0]){
			w=w-get('msgBoxContent').offsetWidth/2;
		}
		if(h<0){h=0;}
		YUD.setXY(objXAlertBox,[w,h]);
		if (!AE.bom.isIE6) objXAlertBox.style.position = 'fixed';
		else{
			objXAlertBox.style.top = h + YUD.getDocumentScrollTop() + 'px';
			YUE.on(window, 'scroll', function(){setTimeout(function(){objXAlertBox.style.top = h + YUD.getDocumentScrollTop() + 'px';}, 300);});
		}
		this.playAnimation('backgroundDiv','xMsgBox',function(){window.focus();});
	}

	this.xWindow = function (sContent,aWH,sBoxTitle,bIsIframe,showAnim,boxType){
		this.showBGAnim = (showAnim && showAnim[0]);
		this.showMsgBoxAnim = (showAnim && showAnim[1]);
		this.icon = this.getBoxType(boxType);
		if(get('xMsgBox')){
			msgBox.closeMsgBox();
		}
		var objBackgroundDiv = this.createMaskArea([0,0],this.getWindowWH(),'backgroundDiv',999998);
		var w=YUD.getViewportWidth();
		w/=2;
		w-=aWH[0]/2;
		var h=(YUD.getViewportHeight()/2) - aWH[1]/2;
		var objXWindowBox = this.createMsgBox([w,h],aWH,'xMsgBox',999999,sContent,sBoxTitle,bIsIframe,0,true);
		if(!aWH[1] && YUD.getViewportHeight()<=objXWindowBox.offsetHeight){
			h=document.documentElement.scrollTop;
		}else if(!aWH[1] && YUD.getViewportHeight()>objXWindowBox.offsetHeight){
			h-=get('msgBoxContent').offsetHeight/2;
		}
		if(!aWH[0]){
			w=w-get('msgBoxContent').offsetWidth/2;
		}
		if(h<0)h=0;
		YUD.setXY(objXWindowBox,[w,h]);
		if (!AE.bom.isIE6) objXWindowBox.style.position = 'fixed';
		else{
			objXWindowBox.style.top = h + YUD.getDocumentScrollTop() + 'px';
			YUE.on(window, 'scroll', function(){setTimeout(function(){objXWindowBox.style.top = h + YUD.getDocumentScrollTop() + 'px';}, 300);});
		}
		this.playAnimation('backgroundDiv','xMsgBox',function(){try{get('msgBoxCloseButtonImg').focus();}catch(e){}});
	}

	this.closeMsgBox =function(){
		try{
			get('xMsgBox').innerHTML='';
			document.body.removeChild(get('xMsgBox'));
		}catch(e){}
			
		try{
			document.body.removeChild(get('backgroundIframe'));
		}catch(e){}
			
		try{
			get('backgroundDiv').innerHTML='';
			document.body.removeChild(get('backgroundDiv'));
		}catch(e){}
			
		window.focus()
		
		try{afterClose();}catch(e){};
		
		try{
			YUE.removeListener(window, 'scroll');
			YUE.removeListener(document, 'keypress');
		}catch(e){}
	}
	
	
	this.getCloseButton=function(e){
		return get('msgBoxCloseButtonImg');
	}
	
	this.getButtonStyle=function(b){
		if(b=='Ok') return 'buttonSkinA S';
		if(b=='Cancel') return 'buttonSkinB S';
		return '';
	}
	
	this.getBoxType=function(t){
		if(t=='success') return this.iconSuccess;
		if(t=='error') return this.iconError;
		if(t=='alert') return this.iconAlert;
		if(t=='notice') return this.iconNotice;
		if(t=='info') return this.iconInfo;
		return null;
	}
	
	this.createMsgBox=function(pos,aWH,id,z_index,sContent,sBoxTitle,bIsIframe,bottomPx,needCloseIcon){
		var dv = document.createElement('div');
		dv.id = id;
		dv.style.position='absolute';
		dv.style.zIndex=z_index;
		if(this.showBGAnim || this.showMsgBoxAnim){
			dv.style.filter = 'alpha(opacity=0)';
			dv.style.opacity = 0;
			dv.style.MozOpacity = 0;
		}
		
		if(aWH[0]){
			dv.style.width = aWH[0] + 'px';
		}
		if(aWH[1]){
			dv.style.height = aWH[1] + 'px';
		}
		
		if(needCloseIcon){
			dv.innerHTML='<a href="javascript:void(0)" id="msgBoxCloseButtonImg" class="msgBoxCloseButtonImg" onclick="msgBox.closeMsgBox()" style="z-index:'+(z_index+1)+';"></a>';
		}
		if(sBoxTitle){
			dv.innerHTML+='<h3 id="msgBoxTitle">'+sBoxTitle+'</h3>';
		}
		if(bIsIframe){
			var s = '<div id="msgBoxContent" class="PopUpContent" style="position:relative;top:4px;left:2px;';
			s+='width:'+(aWH[0]-16)+'px;background:#FFFFFF;padding:21px 8px 9px 8px;';
			s+='"><iframe src="'+sContent+'" width="'+(aWH[0]-16)+'" height="'+(aWH[1]-40)+'" frameborder="0" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe></div>';
			dv.innerHTML+=s;
		}
		else{
			var s = '<div id="msgBoxContent" class="PopUpContent" style="line-height:16px;position:relative;';
			if(this.icon) s+='width:'+(aWH[0]-52)+'px;background:#FFFFFF url('+this.icon+') no-repeat 14px 24px;padding:21px 18px 9px 34px;';
			else s+='width:'+(aWH[0]-36)+'px;background:#FFFFFF;padding:24px 18px 16px 18px;';
			s+='">'+sContent+'</div>';
			dv.innerHTML+=s;
		}
		
		dv.className='PopUpFrame';
		document.body.appendChild(dv);
		YUD.setXY(dv,pos);
		return dv;
	}


	this.createMaskArea=function(pos,aWH,id,z_index){
		var dv;
		dv = document.createElement('div');
		dv.id = id;
		dv.className='PopUpMask';
		dv.style.position='absolute';
		dv.style.zIndex=z_index;
		//dv.style.width = aWH[0] + 'px';
		dv.style.width = '100%';
		dv.style.height = aWH[1] + 'px';
		if(this.showBGAnim){
			dv.style.filter = 'alpha(opacity=0)';
			dv.style.opacity = 0;
			dv.style.MozOpacity = 0;
		}
		else{
			dv.style.filter = 'alpha(opacity=50)';
			dv.style.opacity = 0.5;
			dv.style.MozOpacity = 0.5;
		}
		document.body.appendChild(dv);
		
		if (!AE.bom.isIE6) dv.style.position = 'fixed';
		//else{
		//	dv.style.height = YUD.getViewportHeight() + 80;
		//	dv.style.top = YUD.getDocumentScrollTop() - 40 + 'px';
		//	YUE.on(window, 'scroll', function(){dv.style.top = YUD.getDocumentScrollTop() - 40 + 'px';});
		//}
		
		this.iframe=fixSelectView('backgroundIframe',document.body,aWH,z_index-1);
		YUD.setXY(dv,pos);
		YUD.setXY(this.iframe,pos);
		return dv;
	}

}

function fixSelectView(id,node,aWH,z_index){
	var iframe = document.createElement('iframe');
	iframe.id = id;
	iframe.style.position='absolute';
	iframe.style.zIndex=z_index-1;
	iframe.style.width = aWH[0] + 'px';
	iframe.style.height = aWH[1] + 'px';
	iframe.style.background = '#fff';
	iframe.style.filter = 'alpha(opacity=0)';
	iframe.style.opacity = 0;
	iframe.style.MozOpacity = 0;
	iframe.style.border='0px';
	iframe.style.padding='0px';
	iframe.style.margin='0px';
	node.appendChild(iframe);
	return iframe;
}