﻿var ajax = new Object();
ajax.$x = function(url,method,onload,onerror,postStr,relate,stateArray){
	this.url = url;
	this.req = null;
	this.method = method;
	this.onload = onload;
	this.postStr = postStr;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.relate = relate;
	this.stateNum = (stateArray) ? stateArray : false;
	this.loadXMLDoc();
}
ajax.$x.prototype = {
	loadXMLDoc:function(){
		if(window.XMLHttpRequest){
			this.req = new XMLHttpRequest();
			if(this.req.overrideMimeType){
					this.req.overrideMimeType('text/xml');
			}
		}else if(window.ActiveXObject){
		    var MSXML = ["Msxml3.XMLHTTP","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
		    for(var i=0, j=MSXML.length; i<j; i++){
		        try{
		            this.req = new ActiveXObject(MSXML[i]);
		            break;
		        }catch(e){}
		    }
		}
		if(this.req){
			try{
				var loader = this;
				this.req.onreadystatechange = function(){
					loader.onReadyState.call(loader)
				}
				this.req.open(this.method,this.url,true);
				this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
				this.req.send(this.postStr);
			}catch(err){
				this.onerror.call(this);
			}
		}
	},
	onReadyState:function(){
		var req = this.req;
		var ready = req.readyState;
		if(this.stateNum && ready >= 1 && ready <= 3){
			this.stateNum[ready-1].call(this);
		}else if(ready == 4){
			var httpStatus = req.status;
			if(httpStatus == 200 || httpStatus == 0){
				this.onload.call(this);
			}else{
				this.onerror.call(this);
			}
		}
	},
	defaultError:function(){
		alert("数据连接错误!"
			+ "\n\nreadyState: " + this.req.readyState
			+ "\nstatus: " + this.req.status
			+ "\nheafers: " + this.req.getAllResponseHeaders()
			)
	}
}
function initSelecter(e){
    this.style.left = e.clientX + document.documentElement.scrollLeft +  "px";
    this.style.top = e.clientY + document.documentElement.scrollTop + "px";
    if(!this.close){
        this.close = function(){this.style.display = "none";this.display = false;};
        this.open = function(){this.style.display = "block";this.display = true;};
    }
}
function DrogTitle(e){
    if(!e)e = window.event;
    var oObj = e.target?e.target:e.srcElement;
    if(this == oObj){
    	var intObjX = e.clientX - parseInt(this.parentNode.style.left);
    	var intObjY = e.clientY - parseInt(this.parentNode.style.top);
    	if(this.setCapture){
			this.setCapture();
		}else if(window.captureEvents){
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		}
		oObj.parentNode.style.border = "2px solid #333";
		oObj.parentNode.style.zIndex = "99";
        document.onmousemove = function(e){
            if(!e)e = window.event;
            BatchDiffStyle.call(oObj.parentNode,["left",e.clientX - intObjX + "px"]
                    ,["top",e.clientY - intObjY + "px"]);
        }
        document.onmouseup = function(e){
            if(this.releaseCapture){
	            this.releaseCapture();
            }else if(window.captureEvents){
	            window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
            }
            oObj.parentNode.style.border = "2px solid #999";
			oObj.parentNode.style.zIndex = "1";
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
}
function setDefaultValue(defaultValue){
    if(this.type == "checkbox"){
        defaultValue = (typeof(defaultValue) == "string")?eval(defaultValue.toLowerCase()):defaultValue;
        this.defaultChecked = defaultValue;
        this.checked = defaultValue;
    }else if(this.type == "select-one"){
        this.defaultValue = defaultValue;
        this.value = defaultValue;
        this.options[this.selectedIndex].defaultSelected = true;
    }else{
        defaultValue = (this.format)?this.format(defaultValue):defaultValue;
        this.defaultValue = defaultValue;
        this.value = defaultValue;
    }
}
function AreaSelectChange(){
    if(this.value > 0){
        var URL = "/member/validate.xhtml";
        var postStr = "action=Location&Layer="+this.form["Layer"].value+"&ParentID="+this.value;
        var Total = new ajax.$x(URL,"POST",BindReturnValueToLocation,false,postStr,this);
        SetResultBG.call(this, "loading","Loding locations, wait please...");
    }else{
        SetSelectDefault.call(this.Sibling);
        this.form["Location_Second"].style.display = "none";
        this.form["Layer"].value = 0;
    }
}
function BindReturnValueToLocation(){
    var res = this.req.responseText;
    if(res != ""){
        var ResString = ParseXml(res,"string")[0];
        var result = eval(ResString);
        BindListToSelect.call(this.relate.Sibling, result);
    }
}
function LocationSelectChange(){
    this.form["Layer"].value = 0;
    if(this.value > 0){
        AreaSelectChange.call(this);
    }else{
        this.Sibling.style.display = "none";
        SetSelectDefault.call(this.Sibling);
    }
}
function LocationSubSelectChange(){
    if(this.value == 0){
        SetResultBG.call(this.Parent,"error","Please select your location");
    }else{
        this.IsCorrect = true;
        SetResultBG.call(this.Parent,"success","Ok!");
    }
}
function BindListToSelect(result){
    if(this.Parent && result.length == 0){
        this.IsCorrect = true;
        this.style.display = "none";
        SetResultBG.call(this.Parent,"success","Ok!");
    }else{
        if(this.Parent){
            SetResultBG.call(this.Parent,"","");
            this.style.display = "";
        }
        SetSelectDefault.call(this);
        for(var i=0,j=result.length;i<j;i++){
            this.appendChild(BulidOptionTag(result[i][0],result[i][1], false));
        }
    }
}
function SetSelectDefault(){
    this.innerHTML = "";
    this.appendChild(BulidOptionTag(0,"請選擇區域", true));
    this.form["Layer"].value = 0;
    setDefaultValue.call(this, 0);
    SetResultBG.call(this, "","");
    if(this.Sibling)
        SetSelectDefault.call(this.Sibling);
}
function BulidOptionTag(iValue, sTxt, isSelected){
    var oDom = document.createElement("option");
    oDom.value = iValue;
    oDom.innerHTML = sTxt;
    if(isSelected)
        oDom.setAttribute("selected", "selected");
    return oDom;
}
function BindSearchFrom(){
    var obj = $fm("SearchForm");
    if(typeof(obj) != "undefined"){
        obj.onsubmit = function(){
            if(obj["keyword"].value == obj["keyword"].defaultValue || obj["keyword"].value.trim() == ""){
                return false;
            }else{
                return true;
            }
        }
        SetTextAreaEvent.call(obj["keyword"]);
    }
}
function SetTextAreaEvent(){
    this.onfocus = function(){
        if(this.value == this.defaultValue){
            this.value = "";
        }
    }
    this.onblur = function(){
        if(this.value == ""){
            this.value = this.defaultValue;
        }
    }
}
function BindLangAndArea(){
    var obj = $fm("LangAreaForm");
    if(typeof(obj) != "undefined"){
        var Effect = new EffectWindow($("ChooseHandle","ChooseWrap"),obj);
        document.onclick = function(e){
		    if(!e)e = window.event;
		    var EventObj = e.target?e.target:e.srcElement;
		    if(!FindObj.call(EventObj) && Effect.Status){
			    Effect.Close();
		    }
        }
    }
}
function FindObj(){
    var oObj = this;
    try{
        do{
            if(oObj == $("ChooseWrap")){
                return true;
                break;
            }
            oObj = oObj.parentNode;
        }while(oObj.tagName != "div" || oObj != $("ChooseWrap"))
    }catch(e){}
}
function EffectWindow(oObjects,oForm){
    this.Handler = oObjects[0];
    this.Handler.Parent = this;
    this.Contain = oObjects[1];
    this.Contain.Parent = this;
    this.form = oForm;
    this.MaxHeight = GetMaxHeight.call(this.Contain);
    this.MaxWidth = 209;
    this.Speed = 0.15;
    this.Fix = 20;
    this.Delay = 10;
    this.Status = false;
    this.Init();
}
EffectWindow.prototype = {
    Init:function(){
        BatchDiffEvent.call(this.Handler,["onmouseover",this.HandlerOver],["onmouseout",this.HandlerLeaved],["onclick",this.HandlerClick]);
        BatchDiffEvent.call(this.Contain,["onmouseover",this.ContainOver]);//,["onclick",this.ContainClick]
    },
    HandlerOver:function(){
        if(this.Parent.DelayHidden)clearTimeout(this.Parent.DelayHidden);
        if(!this.Parent.Status){
            var obj = this.Parent;
            obj.DelayOpen = setTimeout(function(){
	            obj.Open();
	        },200);
        }
    },
    HandlerLeaved:function(){
        if(this.Parent.DelayOpen)clearTimeout(this.Parent.DelayOpen);
        if(this.Parent.Status){
            var obj = this.Parent;
            obj.DelayHidden = setTimeout(function(){
	            obj.Close();
	        },500);
        }
    },
    HandlerClick:function(){
        this.Parent.ClearSetTime();
        this.Parent.SetStatus();
    },
    ContainOver:function(e){
        var obj = this;
        document.onmouseover = function(e){
	        if(!e)e = window.event;
	        var EventObj = e.target?e.target:e.srcElement;
	        if(obj.Parent.GetParent.call(EventObj,obj)){
	            if(obj.Parent.DelayHidden)clearTimeout(obj.Parent.DelayHidden);
	        }else{
	            obj.Parent.HandlerLeaved.call(obj);
	            document.onmouseover = null;
	        }
        };
    },
    SetStatus:function(){
        if(this.Status) this.Close();
        else this.Open();
    },
    Open:function(){
        this.ClearSetTime();
        var obj = this.Contain;
	    obj.style.background = "none";
        obj.style.display = "block";
        this.ContainOpen = setInterval(function(){
            obj.Parent.SetStyle.call(obj,
                obj.offsetHeight + (obj.Parent.MaxHeight * obj.Parent.Speed-obj.Parent.Fix),
		        obj.offsetWidth + (obj.Parent.MaxWidth * obj.Parent.Speed-obj.Parent.Fix),
		        (obj.Parent.MaxWidth - obj.offsetWidth)/2
		    )
            if(obj.offsetWidth >= obj.Parent.MaxWidth || obj.offsetHeight >= obj.Parent.MaxHeight){
                obj.Parent.SetStyle.call(obj,obj.Parent.MaxHeight,obj.Parent.MaxWidth,0);
			    obj.style.backgroundColor = "#FFF";
                obj.Parent.Status = true;
                clearInterval(obj.Parent.ContainOpen);
            }
        },this.Delay);
    },
    Close:function(){
        this.ClearSetTime();
        var obj = this.Contain;
	    obj.style.background = "none";
        this.ContainClose = setInterval(function(){
            obj.Parent.SetStyle.call(obj,
                obj.offsetHeight - (obj.Parent.MaxHeight * obj.Parent.Speed+obj.Parent.Fix),
                obj.offsetWidth - (obj.Parent.MaxWidth * obj.Parent.Speed+obj.Parent.Fix),
                (obj.Parent.MaxWidth - obj.offsetWidth)/2
            );
            if(obj.offsetWidth <= 42 || obj.offsetHeight <= 30){
                obj.Parent.SetStyle.call(obj,0,0,obj.Parent.MaxWidth / 2)
                obj.style.display = "none";
                obj.Parent.Status = false;
                clearInterval(obj.Parent.ContainClose);
            }
        },this.Delay);
    },
    SetStyle:function(height,width,marginLeft){
        this.style.height = height + "px";
	    this.style.width = width + "px";
	    this.style.marginLeft = marginLeft + "px";
    },
    GetParent:function(Contain){
	    var oParent = this;
	    while(oParent.tagName != "HTML"){
		    if(oParent == Contain){
			    return true;
		    }
		    oParent = oParent.parentNode;
	    }
	    return false;
    },
    ClearSetTime:function(){
        if(this.ContainOpen)clearInterval(this.ContainOpen);
        if(this.ContainClose)clearInterval(this.ContainClose);
	    if(this.DelayHidden)clearTimeout(this.DelayHidden);
	    if(this.DelayOpen)clearTimeout(this.DelayOpen);
    }
}
function SetEnvironment(id, types){
    this.form["action"].value = types;
    this.form["ID"].value = id;
}
function GetMaxHeight(){
    var Count = 0;
    var DLs = $as.call(this,"dl");
    var A = $as.call(DLs[0],"dd");
    var B = $as.call(DLs[1],"dd");
    if(A.length > B.length)
        Count = A.length;
    else
        Count = B.length;
    return (Count+1)*21;
}
function AlphaEffect(MaxAlpha,DisappearFun,ShowFun){
    this.Alpha = 0;
    this.MaxAlpha = MaxAlpha;
    this.Speed = 3;
    this.Delay = 10;
    this.Status = false;
    this.DisappearFun = DisappearFun;
    this.ShowFun = ShowFun;
    this.Clear = function(){
        if(this.TotalBoxStart)clearInterval(this.TotalBoxStart);
        if(this.TotalBoxEnd)clearInterval(this.TotalBoxEnd);
    }
    this.Show = function(){
        var oEvent = this;
        this.style.display = "";
        this.TotalBoxStart = setInterval(function(){
            oEvent.Alpha += oEvent.Speed;
            if(oEvent.Alpha >= oEvent.MaxAlpha){
                oEvent.Alpha = oEvent.MaxAlpha;
                SetAlpha(oEvent,oEvent.Alpha);
                oEvent.Clear();
                oEvent.Status = true;
                if(oEvent.ShowFun)
                    oEvent.ShowFun();
            }else{
               SetAlpha(oEvent,oEvent.Alpha);
            }
        },this.Delay);
    }
    this.Disappear = function(){
        this.Status = false;
        var oEvent = this;
        this.TotalBoxEnd = setInterval(function(){
            oEvent.Alpha -= oEvent.Speed;
            if(oEvent.Alpha <= 0){
                oEvent.Clear();
                oEvent.Alpha = 0;
                oEvent.style.display = "none";
                if(oEvent.DisappearFun)
                    oEvent.DisappearFun();
            }else{
                SetAlpha(oEvent,oEvent.Alpha);
            }
        },this.Delay);
    }
}
DomLoaded.load(Stadholder);
function Stadholder(){
    ExternalLinks();
    BindSearchFrom();
    BindLangAndArea();
}