﻿var isIE;
if(isIE==undefined){
	isIE=navigator.userAgent.indexOf("MSIE")>0;	
}
function toPx(value)
{
	return value+"px";
}
var WSPDefaultIDs={
    ScreenDivID:"wsp_screen_divall",
    MsgID:"wsp_msg_div",
    ScreenIframeID:"wsp_screen_iframe"
}
WSP.Controls.DivDragMove=Class.create(function(){
    var _obj=null,_isDown=false,_parentDiv=null,body=null,b=WSP.Util.BAgent,ev=WSP.Util.Event;
    function createShadowDiv(){
        if(_parentDiv.ShadowDiv == undefined){
		    var shadowDiv=_parentDiv.ShadowDiv = document.createElement("div");
		    pZIndex=WSP.Util.Dom.getCurrentStyle(_parentDiv).zIndex;
		    if(pZIndex==undefined){
		        pZIndex=0;
		    }
		    shadowDiv.style.cssText = _parentDiv.style.cssText;
		    shadowDiv.className = _parentDiv.className;
		    shadowDiv.style.position = "absolute";
		    shadowDiv.style.backgroundColor = "#ffffff";
		    shadowDiv.style.border = "2px dashed #888";
		    shadowDiv.style.zIndex = pZIndex+10;
		    shadowDiv.style.cursor = "move";
		    if(b.ie){
			    shadowDiv.style.filter = "Alpha(opacity=20)"; 
			    _parentDiv.insertAdjacentElement("beforeBegin",shadowDiv);
			    var ifr=_parentDiv.ifr = document.createElement("iframe");
	            ifr.style.cssText=shadowDiv.style.cssText;
	            ifr.style.zIndex = pZIndex+9;
	            ifr.style.filter = 'alpha(opacity=0)';
	            ifr.className=shadowDiv.className;
                _parentDiv.insertAdjacentElement("beforeBegin",_parentDiv.ifr);
			}else{
			    shadowDiv.style.opacity="0.2";
			    _parentDiv.parentNode.insertBefore(shadowDiv, _parentDiv.nextSibling);
			}
	    }
	    else{
	        with(_parentDiv.ShadowDiv.style){
	            display="block";
	        }
	        if(b.ie){
	            with(_parentDiv.ifr.style){
	                display="block";
	            }
            }
	    }
    }
    function toInt(obj){
        var str=obj.toString();
        if(str.indexOf("px") >= 0){
            return parseInt(str.substring(0, obj.length-2));
        } 
        else{
            return parseInt(str);
        }
    }
    function start(e){
        e=ev.getEvent(e);
        var o = _obj = ev.getTarget(e);
        var parentDiv = o.parentNode;
        parentDiv.lastMouseX = e.clientX-toInt(parentDiv.style.left);
        parentDiv.lastMouseY = e.clientY-toInt(parentDiv.style.top);
        createShadowDiv();
        document.body.onmousemove = (function(){
            return function(e){
                drag(e);
            }    
        })();
        document.body.onmouseup =  (function(){
            return function(e){
                end(e);
            }    
        })();
        _isDown = true;
        e.returnValue = false;
		e.cancelBubble = true;
    }
    function drag(e){
        e=ev.getEvent(e);
        if(_isDown){
            var o = _obj = ev.getTarget(e);
            var nowleft = e.clientX - _parentDiv.lastMouseX;
            var nowtop = e.clientY - _parentDiv.lastMouseY;
            if(nowleft< 0){
                nowleft = 0;
            }
            if(nowtop<0){
                nowtop = 0;
            }
            if(nowtop + toInt(_parentDiv.ShadowDiv.offsetHeight)<=_body.clientHeight+_body.scrollTop){
                _parentDiv.ShadowDiv.style.left = nowleft + "px";
                _parentDiv.ShadowDiv.style.top = nowtop + "px";
                if(b.ie){
                    _parentDiv.ifr.style.left = nowleft + "px";
                    _parentDiv.ifr.style.top = nowtop + "px";    
                }
                e.returnValue = false;
		        e.cancelBubble = true;
		    }
		}
		else{
		    end();
		}
    }
    function end(e){
        document.body.onmousemove = document.body.onmouseup = null;
        _isDown = false;
        if(_parentDiv.ShadowDiv != null){                   
            _parentDiv.style.left = _parentDiv.ShadowDiv.style.left;
            _parentDiv.style.top = _parentDiv.ShadowDiv.style.top;
            with(_parentDiv.ShadowDiv.style){
	            display="none";
	        }
	        if(b.ie){
	            with(_parentDiv.ifr.style){
	                display="none";
	            }
	        }
        }
    }
    return {
        initialize:function(divTitle, divAll){
            divTitle.onmousedown = (function() {
                return function(e){
                    start(e);
                }
            })();
            _parentDiv = divAll;
            _body=getBodyObj();
        }
    }
}());
//整屏遮盖div
WSP.Controls.ScreenShadow={
    create:function(msg){
        if($(WSPDefaultIDs.ScreenDivID)){
            return;
        }
        var divAll = document.createElement("div");
        divAll.style.display = "";
        divAll.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=25,style=3,finishOpacity=50)";
        divAll.style.opacity = "0.6";
        divAll.style.zIndex = "1";
        divAll.style.position="absolute";
        divAll.style.top = "0px";
        divAll.style.left = "0px";
        divAll.style.textAlign="center";
        divAll.style.backgroundColor="#5b5b5b";
        var dom=WSP.Util.Dom;
        if(msg!=undefined){
            var top = toPx(dom.getViewportHeight()/2 - 100 + dom.getBody().scrollTop);
            var left = toPx(dom.getViewportWidth()/2 + dom.getBody().scrollLeft - 125);
            var msgdiv="<div style='position:absolute;top:"+top+";left:"+left+";background-color:#fff;height:32px;z-index:20;color:black;font-weight:bolder;line-height:32px;padding:5px 0 5px 0;border:1px solid black;'>";
            msgdiv+="<img src='"+WSPCore.siteUrl+"/images/wait.gif' alt='' border=0 align=middle />";
            msgdiv+=msg+"</div>";
            divAll.innerHTML=msgdiv;
        }
        divAll.id = WSPDefaultIDs.ScreenDivID;
        document.body.bgcolor = "transparent";
        var frame = document.createElement("iframe");
        frame.style.position = "absolute";
        frame.style.top = frame.style.left = "0px";
        frame.style.zIndex = "0";
        frame.style.filter = "Alpha(opacity=10)";
        frame.id = WSPDefaultIDs.ScreenIframeID;
        document.body.appendChild(frame);
        document.body.appendChild(divAll);
        var w,h;
        if(WSP.Util.BAgent.ff){
            w=toPx(dom.getDomWidth()-10);
        }
        else{
            w=toPx(dom.getDomWidth());
        }
	    divAll.style.width=w;
        divAll.style.height=toPx(dom.getDomHeight());
	    frame.style.width=divAll.style.width;
        frame.style.height=divAll.style.height;
    },
    remove:function(){
        if($(WSPDefaultIDs.ScreenDivID)){
            document.body.removeChild($(WSPDefaultIDs.ScreenDivID));
        }
        if($(WSPDefaultIDs.ScreenIframeID)){
            document.body.removeChild($(WSPDefaultIDs.ScreenIframeID));
        }
    },
    close:function(){
        this.remove();  
    },
    dispose:function(){
        this.close();
    }
}
/***********************************************************
Author: Hexj
Add Time: 2007-10-13
Descript: public msg js
2008-01-15 modified :can run in firefox.

1.confirmMsg(msg, fCallback, obj) 
==this can show cancel and confirm button
==param 1: msg
==param 2: when after you click confirm, continue to execute fCallback
==param 3: can use obj while fCallback is null and now only be used by <a> or <input> tag. == when <a> then click confirm button, can do eval(this.href)
when <input> then eval __dopostback
for example: confirmMsg("Do you sure?", function(){alert(1);Gooo();})
WSP.MessageBox.confirmMsg("Do you sure?", null, this);
2.WSP.MessageBox.errorMsg(msg, fCallback, obj)
3.WSP.MessageBox.alertMsg(msg, fCallback, obj)
4.WSP.MessageBox.tipMsg(msg, fCallback, obj)

For example:
WSP.MessageBox.confirmMsg("Do you sure?", function(){alert(1);})
WSP.MessageBox.errorMsg("error");
WSP.MessageBox.alertMsg("alert");
WSP.MessageBox.tipMsg("tip");
*************************************************************/
WSP.MessageBox={
  alertMsg:function(msg,fCallback,obj,objStyle){
    WSP.Controls.ScreenShadow.create();
    this.createShowDiv(msg, Tip_Text, this.buttonType.Alert, this.imageType.Alert, fCallback, obj, objStyle);
    return false;
  }, 
  confirmMsg:function(msg,fCallback,obj,objStyle,closeFCallBack){
    WSP.Controls.ScreenShadow.create();
    this.createShowDiv(msg, Tip_Text, this.buttonType.Confirm, this.imageType.Question, fCallback, obj, objStyle,closeFCallBack);
    return false;
  },
  errorMsg:function(msg,fCallback,obj,objStyle){
    WSP.Controls.ScreenShadow.create();
    this.createShowDiv(msg, Error_Text, this.buttonType.Alert, this.imageType.Error, fCallback, obj,objStyle);
    return false;
  },
  tipMsg:function(msg,fCallback,obj,objStyle){
    WSP.Controls.ScreenShadow.create();
    this.createShowDiv(msg, Tip_Text, this.buttonType.Alert, this.imageType.Tip, fCallback, obj,objStyle);
    return false;
  },
  buttonType:{Alert : "Alert",Confirm : "Confirm",Advanced : "Advanced"},
  imageType:{Error : "g-msg-errorimg",Question: "g-msg-questionimg",Tip : "g-msg-tipimg",Alert : "g-msg-alertimg"},
  createShowDiv:function(msg,titletext,buttonType,imageType,fCallback,obj,objStyle,closeFCallBack){
        this.objStyle={
	        width:"280px",
	        height:"160px",
	        contentHeight:"100px"
        }
        Object.extend(this.objStyle,objStyle||{});
        var divShow = document.createElement("div");
        divShow.className="g-msg-main";
        divShow.style.width = this.objStyle.width;
        divShow.style.height = this.objStyle.height
        divShow.id = WSPDefaultIDs.MsgID;
        var body=getBodyObj();
        divShow.style.top = toPx(body.clientHeight/2 - 100 + body.scrollTop);
        divShow.style.left = toPx(body.clientWidth/2 + body.scrollLeft - 125);
        //divTitle  begin 
        var divTitle = document.createElement("div");
        divTitle.className="g-msg-title";
        divTitle.style.width = this.objStyle.width
        divTitle.innerHTML=titletext;
        divShow.appendChild(divTitle);
        //end
        //divContent  begin
        var divContent = document.createElement("div");
        divContent.className="g-msg-content";
        divContent.innerHTML = "<div style='height:"+this.objStyle.contentHeight+";overflow:auto;text-align:center;'><table cellpadding=0 cellspacing=0 border=0 height=\""+this.objStyle.contentHeight+"\"><tr><td width='25%' align='center'><div class='"+imageType+"'></div></td><td valign=middle width='75%'>" + msg + "</td></tr></table></div>";
        divContent.style.width = this.objStyle.width;
        divContent.style.height = this.objStyle.contentHeight;
        divShow.appendChild(divContent);
        //end
        //divBtn begin
        var divBtn = document.createElement("div");
        divBtn.className="g-msg-bottom";
        divBtn.style.width = this.objStyle.width;
        divBtn.style.height=toPx(parseInt(this.objStyle.height.replace(/px/,""),10)-(24+parseInt(this.objStyle.contentHeight.replace(/px/,""),10)));
        var btnHTML = "";
        btnHTML = "<table><tr><td valign='middle' align='center'><input type='button' id='btn_show_confirm' class='g-msg-button' />";
        if(buttonType!=this.buttonType.Alert)
        {
            btnHTML += "&nbsp;<input type='button' id='btn_show_cancel' class='g-msg-button' />";
        }
        btnHTML += "</td></tr></table>";
        divBtn.innerHTML = btnHTML;
        divShow.appendChild(divBtn);

        //end
        document.body.appendChild(divShow);
        new WSP.Controls.DivDragMove(divTitle, divShow);

        var btnConfirm = document.getElementById('btn_show_confirm');
        btnConfirm.value = BUTTON_CONFIRM_TEXT;
        btnConfirm.focus();
        var self=this;
        btnConfirm.onclick = function(){
            WSP.Controls.ScreenShadow.remove();
            self.remove();
            if(fCallback){
                fCallback();
            }
            else if(obj){
                if(obj.tagName.toLowerCase()=="a"){    
                    eval(obj.href);
                }
                else if(obj.tagName.toLowerCase()=="input"&&obj.type=="submit"){
                    __doPostBack(obj.name, "");
                }
            }
        };
        if(buttonType != this.buttonType.Alert){
            var btnCancel = document.getElementById('btn_show_cancel');
            btnCancel.value = BUTTON_CANCLE_TEXT;
            btnCancel.onclick=function(){
                WSP.Controls.ScreenShadow.remove();
                self.remove();
                if(closeFCallBack){
                    closeFCallBack();
                }
            }
        }
    },
    remove:function(){
        if($(WSPDefaultIDs.MsgID)){
            document.body.removeChild($(WSPDefaultIDs.MsgID));
        } 
    },
    close:function(){
        this.remove();
    },
    dispose:function(){
        this.remove();
    }
}
//end
//以下功能为了兼容，请调用方法里的方法。
//var initDivMove = new WSP.Controls.DivDragMove();
function cancelevent(){
    WSP.Controls.ScreenShadow.remove();
    WSP.MessageBox.remove();
    return false;
}
function createAllDiv(msg){
    WSP.Controls.ScreenShadow.create(msg);
}
function alertMsg(msg, fCallback, obj, objStyle){
    return WSP.MessageBox.alertMsg(msg, fCallback, obj, objStyle);
}
function confirmMsg(msg, fCallback, obj, objStyle, closeFCallBack){
    return WSP.MessageBox.confirmMsg(msg, fCallback, obj, objStyle, closeFCallBack);
}
function errorMsg(msg, fCallback, obj,objStyle){
    return WSP.MessageBox.errorMsg(msg, fCallback, obj,objStyle);
}
function tipMsg(msg, fCallback, obj,objStyle){
    return WSP.MessageBox.tipMsg(msg, fCallback, obj,objStyle);
}
