/*------------------------------------------------------------------------------
 * JavaScript dialog Library
 * Version 1.0.2
 * by Dowell company , http://www.linkserv.cn/
 * Copyright (c) 2009-2012 Dowell company. All Rights Reserved.
 *------------------------------------------------------------------------------
 */
var dhtmlwindow = {
	currentDialogCount : 0,

	dialogArray : new Array(),
		
	dialogNameArray : new Array(),
		
	dialogTypeArray : new Array(),
	
	dialogParamArray : new Array(),
	
	dialogBgLayer : null,		
	
	dialogMoveLayer:null,
	
	currentMoveContorl:false,
	
	currentPointPosition:{x:0,y:0},
	/** private function **/
	
	showMoveLayer:function(){
		if(this.dialogMoveLayer==null){
			this.dialogMoveLayer=document.createElement("div");
			document.body.appendChild(this.dialogMoveLayer);
			this.dialogMoveLayer.className = "dowell_dialog_movebg";
			this.dialogMoveLayer.style.left=0 ;
			this.dialogMoveLayer.style.top=0 ;
			$(this.dialogMoveLayer).mousemove(function(event){
				if(dhtmlwindow.currentMoveContorl){
					var moveObj = dhtmlwindow.dialogArray[dhtmlwindow.currentDialogCount-1];
					var orig_pos = dhtmlwindow.currentPointPosition;
					var orig_left = parseInt(moveObj.style.left.substring(0,moveObj.style.left.indexOf("px")));
					var orig_top = parseInt(moveObj.style.top.substring(0,moveObj.style.top.indexOf("px")));
					var s_x = event.screenX;
					var s_y = event.screenY;
					moveObj.style.left = (orig_left - (orig_pos.x-s_x))+"px";
					var target_top = orig_top - (orig_pos.y-s_y);
					if(target_top<-2){
						target_top = -2;
					}
					moveObj.style.top = target_top+"px";
					dhtmlwindow.currentPointPosition.x = s_x;
					dhtmlwindow.currentPointPosition.y = s_y;
				}
			});
			$(this.dialogMoveLayer).mouseup(function(event){
				dhtmlwindow.dialogMoveLayer.style.visibility="hidden" ;
				dhtmlwindow.dialogMoveLayer.style.display="none";
				dhtmlwindow.dialogMoveLayer.style.zIndex = 0;
				dhtmlwindow.currentMoveContorl = false;
				dhtmlwindow.showBgLayer();
			});
		}
		if(this.currentDialogCount>0){
			this.dialogMoveLayer.style.visibility="visible" ;
			this.dialogMoveLayer.style.display="block";
			this.dialogMoveLayer.style.zIndex = this.currentDialogCount*10+6;
			var div = dhtmlwindow.dialogArray[dhtmlwindow.currentDialogCount-1];
			if($.browser.msie){
				this.dialogMoveLayer.style.width = ($(document).width()-21)+"px" //set up veil over page
				if($(div).height()<$(window).height()){
					this.dialogMoveLayer.style.height = ($(document).height()-4)+"px" //set up veil over page
				}else{
					this.dialogMoveLayer.style.height = ($(document).height()+1)+"px" //set up veil over page
				}
			}else{
				this.dialogMoveLayer.style.width = ($(document).width()-10)+"px" //set up veil over page
				this.dialogMoveLayer.style.height = ($(document).height())+"px" //set up veil over page
			}
		}else{
			this.dialogMoveLayer.style.visibility="hidden" ;
			this.dialogMoveLayer.style.display="none";
			this.dialogMoveLayer.style.zIndex = 0;
		}
	},
	
	showBgLayer:function(){
		if(this.dialogBgLayer==null){
			this.dialogBgLayer=document.createElement("div");
			document.body.appendChild(this.dialogBgLayer);
			this.dialogBgLayer.className = "dowell_dialog_popbg";
			this.dialogBgLayer.style.left=0 ;
			this.dialogBgLayer.style.top=0 ;
		}
		
		if(this.currentDialogCount>0){
			this.dialogBgLayer.style.visibility="visible" ;
			this.dialogBgLayer.style.display="block";
			this.dialogBgLayer.style.zIndex = this.currentDialogCount*10-5;
			var div = dhtmlwindow.dialogArray[dhtmlwindow.currentDialogCount-1];
			
			if($.browser.msie){
				this.dialogBgLayer.style.width = ($(document).width()-21)+"px" //set up veil over page
				if($(div).height()<$(window).height()){
					this.dialogBgLayer.style.height = ($(document).height()-4)+"px" //set up veil over page
				}else{
					this.dialogBgLayer.style.height = ($(document).height()+1)+"px" //set up veil over page
				}
			}else{
				this.dialogBgLayer.style.width = ($(document).width()-10)+"px" //set up veil over page
				this.dialogBgLayer.style.height = ($(document).height())+"px" //set up veil over page
			}
		}else{
			this.dialogBgLayer.style.visibility="hidden" ;
			this.dialogBgLayer.style.display="none";
			this.dialogBgLayer.style.zIndex = 0;
		}
	
	},
	
	closeDialog:function(userData){
		if(this.currentDialogCount==0){
			return;
		}
		var dialog = this.dialogArray.pop();
		var dialogName = this.dialogNameArray.pop();
		var dialogType = this.dialogTypeArray.pop();
		var dialogParam = this.dialogParamArray.pop();
		$(dialog).empty();
		$(dialog).hide();
		document.body.removeChild(dialog);
		this.currentDialogCount = this.currentDialogCount - 1;
		this.showBgLayer();
		if(dialogParam.closeFunc){
			dialogParam.closeFunc(userData,dialogParam);
		}
	},
	/** private function **/
	initFrameDialog:function(name,url){
		var container = document.createElement("div");
		container.id = "dowell_dialog_"+name;
		container.className = "dowell_dialog";
		container.style.display="none";
		container.style.width="450px";
		container.style.height="300px";
		var html = "<div class=\"dowell_dialog_title\">";
		html = html + "<div class=\"dowell_dialog_title_left\">";
		//html = html + "<img src=\"../..//images/forward.gif\">";
		html = html + "<span id='dowell_dialog_title'></span>";
		html = html + "</div>";
		html = html + "<div class=\"dowell_dialog_title_right\">";
		html = html + "<img src=\"../images/window_close.gif\"></div>";
		html = html + "</div>";
		html = html + "<iframe id='dowell_dialog_frame' name='dowell_dialog_frame' src='"+ url+"'  style=\"width:100%;height:100%;\" frameborder=\"0\"></iframe>";
		container.innerHTML = html;
		document.body.appendChild(container);
		return container;
	},
    /** private function **/
	initConfirmDialog:function(name,param){
		var container = document.createElement("div");
		container.className = "dowell_confirm";
		container.style.display="none";
		var html = "<div class='dowell_confirm_close'><span class=title></span></div>";
		html = html +"<div class='dowell_confirm_info'>";
		html = html + "<div class='dowell_confirm_info_left'><img src="+param.img+" /></div><div class='dowell_confirm_info_right'><table><tr><td>"+param.promptTip+"</td></tr></table></div>";
		html = html +"</div>";
		html = html + "<div class='dowell_confirm_function'></div>";
		container.innerHTML = html;
		document.body.appendChild(container);
		return container;
	},
	
	/** private function **/
	putDialogToStack:function(name,type,dialog,param){
		this.dialogArray.push(dialog);
		this.dialogNameArray.push(name);
		this.dialogTypeArray.push(type);
		this.dialogParamArray.push(param);
		this.currentDialogCount = this.currentDialogCount + 1;
	},
	setDivPosition:function(div,param){
		if(param.left&&(param.left!=0)){
			div.style.left = (param.left +$(document).scrollLeft())+"px";
		}else{
			div.style.left = (($(window).width()-$(div).width())/2 + $(document).scrollLeft())+"px";
		}
		if(param.top&&(param.top!=0)){
			div.style.top = (param.top +  $(document).scrollTop())+"px";
		}else{
			div.style.top = (($(window).height()-$(div).height())/2 + $(document).scrollTop())+"px";
		}
	},

	/** private function **/
	openConfirmDialog:function(param){
		var nextIndex = this.currentDialogCount + 1;
		var dialogName = "dowell_" + nextIndex;
		var confirmDialog = this.initConfirmDialog(dialogName,param);
		this.putDialogToStack(dialogName,"confirmDialog",confirmDialog,param);
		$(confirmDialog).find(".dowell_confirm_close > .title").html(param.title?param.title:"");
		this.setDivPosition(confirmDialog,param);
		var closegif = typeof param.closegif == 'undefined' ? true : param.closegif;
		var closeImgTarget = $(confirmDialog).find(".dowell_confirm_close > .closeImg").find("img");
		if(!closegif){
			closeImgTarget.hide();
		}else{
			closeImgTarget.click(function(){
				dhtmlwindow.closeDialog("");
			});
		}
		var fillArea = $(confirmDialog).find(".dowell_confirm_function");
		if(param.buttons&&param.buttons.length>0){
			
			for(var i=0;i<param.buttons.length;i++){
				var name = param.buttons[i].value;
				var label =  param.buttons[i].label;				
				fillArea.append("<input type=\"button\" id=\"button_"+ name  +"\" value=\""+ label +"\" userData=\""+ name +"\" /> ");
				var button = fillArea.find("#button_"+name)
				button.click(function(event){
					dhtmlwindow.closeDialog($(event.target).attr("userData"));
				});
			}
		}
		if(closegif){
			fillArea.append("<input type=\"button\" id=\"button_cancel_close\" value=\"Cancel\" userData=\"\" /> ");
			var button = fillArea.find("#button_cancel_close");
			button.click(function(event){
				dhtmlwindow.closeDialog();
			});
		}
		var moveHandler = $(confirmDialog).find(".dowell_confirm_close > .title");
		
		moveHandler.mousedown(function(event){
			dhtmlwindow.currentMoveContorl = true;
			dhtmlwindow.currentPointPosition = {x:event.screenX,y:event.screenY};
			dhtmlwindow.showMoveLayer();
		});
		
		moveHandler.mousemove(function(event){
			
		});
		
		moveHandler.mouseup(function(event){
			
		});
		$(confirmDialog).show();
		$(confirmDialog).css("zIndex",nextIndex*10);
		this.showBgLayer();		
	},
	/** private function **/
	openFrameDialog:function(url,param){
		var nextIndex = this.currentDialogCount + 1;
		var dialogName = "dowell_" + nextIndex;
		var frameDialog = this.initFrameDialog(dialogName,param.url);
		this.putDialogToStack(dialogName,"frameDialog",frameDialog,param);
		$(frameDialog).find("#dowell_dialog_title").html(param.title?param.title:"");
		if(param.width){			
			$(frameDialog).width(param.width);
		}
		if(param.height){
			$(frameDialog).height(param.height);
		}
		this.setDivPosition(frameDialog,param);
		var closegif = typeof param.closegif == 'undefined' ? true : param.closegif;
		var closeImgTarget = $(frameDialog).find(".dowell_dialog_title_right").find("img");
		if(!closegif){
			closeImgTarget.hide();
		}else{
			closeImgTarget.click(function(){
				dhtmlwindow.closeDialog();
			});
		}
		var moveHandler = $(frameDialog).find(".dowell_dialog_title_left");
		
		moveHandler.mousedown(function(event){
			dhtmlwindow.currentMoveContorl = true;
			dhtmlwindow.currentPointPosition = {x:event.screenX,y:event.screenY};

			dhtmlwindow.showMoveLayer();
		});
		
		moveHandler.mousemove(function(event){
			
		});
		
		moveHandler.mouseup(function(event){
			
		});
		$(frameDialog).show();
		$(frameDialog).css("zIndex",nextIndex*10);
		this.showBgLayer();		
	},
	
	/** public  function **/
	openDialog:function(param){
		param.width = param.weight;
		if(param.url){
			this.openFrameDialog(param.url,param);
		}else{			
			this.openConfirmDialog(param);
		}
		
	},
	/** public  function **/
	setCenter:function(x_center,y_center){
		if(this.currentDialogCount==0){
			return;
		}
		var div = this.dialogArray.pop();
		this.dialogArray.push(div);
		var dialogType = this.dialogTypeArray.pop();
		this.dialogTypeArray.push(dialogType);
		if((dialogType=="confirmDialog")||(dialogType=="promptDialog")){
			return;
		}
		if(x_center){
			div.style.left = (($(window).width()-$(div).width())/2 + $(document).scrollLeft())+"px";
		}
		if(y_center){
			div.style.top = (($(window).height()-$(div).height())/2 + $(document).scrollTop())+"px";
		}		
	},
	/** public  function **/
	setSize:function(width,height){
		if(this.currentDialogCount==0){
			return;
		}
		
		var dialog = this.dialogArray.pop();
		this.dialogArray.push(dialog);
		var dialogType = this.dialogTypeArray.pop();
		this.dialogTypeArray.push(dialogType);
		if((dialogType=="confirmDialog")||(dialogType=="promptDialog")){
			return;
		}
		if(width!=0){
			$(dialog).width(width);
		}
		if(height!=0){
			$(dialog).height(height);
			$(dialog).find("#dowell_dialog_frame").css("height",height-30);
		}
		this.showBgLayer();
	},
	/** public  function **/
	getParamObj:function(){
		if(this.currentDialogCount == 0){
			return ;	
		}
		var dialogParam = this.dialogParamArray.pop();
		this.dialogParamArray.push(dialogParam);
		return dialogParam;
	},
	/** public  function **/
	setParamObj:function(paramObj){
		if(this.currentDialogCount == 0){
			return ;	
		}
		var dialogParam = this.dialogParamArray.pop();
		this.dialogParamArray.push(paramObj);
		return dialogParam;
	},
	/** public  function **/
	setTitle:function(title){
		if(this.currentDialogCount == 0){
			return ;	
		}
		var dialog = this.dialogArray.pop();
		this.dialogArray.push(dialog);
		$(dialog).find("#dowell_dialog_title").html(title?title:dialog.title);
	},
	/** public  function **/
	alert:function(title,promptTip,func){
		var paramObj = {
	        title: title?title:'',
	        center: true,
	        promptTip:promptTip?promptTip:'',
			img:'../images/ask.png',
			closegif:false,
			buttons:[{label:"&nbsp;Ok&nbsp;",value:"ok"}],
	        closeFunc:function(param){		
					if(param&&func){							
						if(param=="ok"){						
							func("ok");	
						}	
					}
			}
	    }
		this.openConfirmDialog(paramObj);
	},
	/** public  function **/
	succeed:function(title,promptTip,func){
		var paramObj = {
	        title: title?title:'',
	        center: true,
	        promptTip:promptTip?promptTip:'',
			img:getRootPath()+'/images/right.gif',
			closegif:false,
			buttons:[{label:"&nbsp;Ok&nbsp;",value:"ok"}],
	        closeFunc:function(param){		
					if(param&&func){							
						if(param=="ok"){						
							func("ok");	
						}	
					}
			}
	    }
		this.openConfirmDialog(paramObj);
	},
	/** public  function **/
	error:function(title,promptTip,func){
		var paramObj = {
	        title: title?title:'',
	        center: true,
	        promptTip:promptTip?promptTip:'',
			closegif:false,
			img:getRootPath()+'/images/wrong.gif',
			buttons:[{label:"&nbsp;Ok&nbsp;",value:"ok"}],
	        closeFunc:function(param){		
					if(param&&func){							
						if(param=="ok"){						
							func("ok");	
						}	
					}
			}
	    }
		this.openConfirmDialog(paramObj);
	},
	/** public  function **/
	confirm:function(title,promptTip,func){
		var paramObj = {
	        title: title?title:'',
	        center: true,
	        promptTip:promptTip?promptTip:'',
	        img: getRootPath() + '/images/ask.png',
			closegif:false,
			buttons:[{label:"&nbsp;Ok&nbsp;",value:"ok"},{label:"Cancel",value:"cancel"}],
	        closeFunc:function(param){		
					if(param&&(typeof func == "function")){							
						if(param=="ok"){						
							func("ok");	
						}else{
							func("cancel");		
						}	
					}
			}
	    }
		this.openConfirmDialog(paramObj);
		
	}
};


