﻿/* --------------------------------------------------------------------------
 * Xuanhe JavaScript framework, version 1.0.0.0
 * (c) 2006-2008 Xuanhe
 * Content:Web Os base framework
 * Requires:Prototype.lite.js
 * Author:Henry Fang
 * Last modified : 2008/3/27
/*--------------------------------------------------------------------------*/
/*
                   Contents
                   
Global Variables:
    --IE,FF
Namespace:
    --Display
        --Move
        --Windows
        --Effect
        --StatusInfo
    --System
        --Ajax
*/

/********************************************************
Function library
********************************************************/
//get the type of user browser 
var IE = document.all;   
var FF = document.getElementById&&!document.all;
//rewrite setTimeout method
var _st_method = window.setTimeout;
window.setTimeout = function(fRef, mDelay)
{
    if(typeof fRef == 'function')
    {
        var argu = Array.prototype.slice.call(arguments,2);
        var f = (function(){ fRef.apply(null, argu); });
        return _st_method(f, mDelay);
    }
    return _st_method(fRef,mDelay);
}
/********************************************************
View
********************************************************/
Display = {};
//get the page size
Display.getPageSize = function()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
//windows moving component
Display.Move = new function()
{
    this.zindex = 1000;
    this.X = 0;
    this.Y = 0;
    this.nTX = 0;
    this.nTY = 0;
    this.windowDiv = null;
    this.windowShadow = null;
    this.isDrag = false;
    //mouse move
    this.mouseMove = function(e)
    {
        if(this.isDrag)
        {
            var currentTop = FF ? this.nTY + e.clientY - this.Y : this.nTY + event.clientY - this.Y;
            var currentLeft = FF ? this.nTX + e.clientX - this.X : this.nTX + event.clientX - this.X;
            this.windowDiv.style.top = currentTop + "px";
            this.windowShadow.style.top =  parseInt(currentTop + 5) + "px";
            this.windowDiv.style.left = currentLeft + "px";
            this.windowShadow.style.left =  parseInt(currentLeft + 5) + "px";
        }
    }
    //prepare object
    this.beginToMove = function(e)
    {
        var titleDiv = FF ? e.target:event.srcElement;
        var oDiv = titleDiv.parentNode.parentNode;
        this.isDrag = true;
        this.windowDiv = oDiv;
        var shadowDivName = oDiv.id + "_shadow";
        this.windowShadow = $(shadowDivName); 
        this.nTX = parseInt(this.windowDiv.style.left + 0);
        this.nTY = parseInt(this.windowDiv.style.top + 0);
        this.X = FF ? e.clientX : event.clientX;
        this.Y = FF ? e.clientY : event.clientY;
        document.onmousemove = this.mouseMove;
    }
}
//windows component
Display.Window = function()
{
    this.Div = null;
    this.Name = "未命名";
    this.Title = "未定义";
    this.Width = 100;
    this.Height = 100;
    this.createWindow = function()
    {
        if(this.Div != null)
        {
            return;
        }
        var oBody = document.getElementsByTagName("body")[0];
        
        var oDiv = document.createElement("div");
        oDiv.className = "window";
        oDiv.style.top = "100px";
        oDiv.style.left = "100px";
        oDiv.style.width = this.Width + "px";
        oDiv.style.height = this.Height + "px";
        oDiv.style.zIndex = Display.Move.zindex + 1;
        oDiv.id = this.Name;
        //oDiv.style.display = "none";
        var iDiv = '<div class="windowTitle" style="width:' + this.Width + ';background-color:#666666;"> ';
        iDiv += '<div style="width:' + parseInt(this.Width - 18 * 2 - 10) + 'px;padding:3px;cursor:move;float:left;" onmousedown="Display.Move.windowMove(event);" onmouseup="Display.Move.isDrag=false;">' + this.Title + '</div>';
        iDiv += '<img src="images/min.gif" /><img src="images/close.gif" />';
        iDiv += '</div>';
        oDiv.innerHTML = iDiv;
        
        var sDiv = document.createElement("div");
        sDiv.className = "windowShadow";
        sDiv.style.top = "105px";
        sDiv.style.left = "105px";
        sDiv.style.width = this.Width + "px";
        sDiv.style.height = this.Height + "px";
        sDiv.style.zIndex = Display.Move.zindex;
        sDiv.id = this.Name + "_shadow";
        
        oBody.appendChild(oDiv);
        oBody.appendChild(sDiv);
        this.Div = $(this.Name)
        Display.Move.zindex += 2;
    }
}
//Opacity effects
Display.Effect = {};
Display.Effect.Opacity = function(target,start,end,once,time,isClose)
{
    this.target = target;
    this.start = start;
    this.end = end;
    this.once = once;
    this.time = time;
    this.isClose = isClose;
    this.method = null;
    if(start < end)
    {
        this.method = 1;
    }
    else if(start > end)
    {
        this.method = 0;
    }
    
    this.Begin = function()
    {
        if(this.method == 1 && this.start < this.end)
        {
            if(this.target.style.display == "none")
            {
                this.target.style.display == "";
            }
            if(FF)
            {
                this.target.style.opacity = (this.target.style.opacity * 100 + this.once)/100;
                this.start += this.once;
                setTimeout(this.Begin,this.time,this);
            }
            else
            {
                this.target.style.filter = "Alpha(Opacity=" + parseInt(this.start * 1  + this.once) + ")";
                this.start += this.once;
                setTimeout(this.Begin,this.time,this);
            }
        }
        else if(this.method == 0 && this.start > this.end)
        {
            if(FF)
            {
                this.target.style.opacity = (this.target.style.opacity * 100 - this.once)/100;
                this.start -= this.once
                setTimeout(this.Begin,this.time,this);
            }
            else
            {
                this.target.style.filter = "Alpha(Opacity=" + parseInt(this.start * 1 - this.once) + ")";
                this.start -= this.once;
                setTimeout(this.Begin,this.time,this);
            }
        }
        else if(this.method == 0 && this.start == this.end)
        {
            if(this.isClose)//remove element
            {
                this.target.parentNode.removeChild(this.target);
            }
            else//hidden element
            {
                this.target.style.display = "none";
            }
        }
        else if(this.method == null)
        {
            return;
        }
    }
}

//the information layer
Display.StatusInfo = new function()
{
    //status information layer in the documnet
    this.infoDiv = null;
    //init the layer
    this.init = function()
    {
        if (this.statusDiv != null)
        {
            return;
        }
        var oBody = document.getElementsByTagName("body")[0];
        var oDiv = document.createElement("div");
        oDiv.style.position = "absolute";
        oDiv.style.top = "50%";
        oDiv.style.left = "50%";
        oDiv.style.width = "280px";
        oDiv.style.margin = "-50px 0 0 -100px";        
        oDiv.style.padding = "15px";
        oDiv.style.backgroundColor = "#353555";
        oDiv.style.border = "1px solid #CFCFFF";
        oDiv.style.color = "#CFCFFF";
        oDiv.style.fontSize = "12px";
        oDiv.style.textAlign = "center";
        oDiv.id = "StatusInfo";
        oBody.appendChild(oDiv);
        oDiv.style.display = "none";
        this.infoDiv = document.getElementById("StatusInfo");
    }
    //show the information or not
    //_show:boolean，true[show]，false[hidden]
    this.setStatusShow = function(_show)
    {      
        if (this.infoDiv == null)
        {
            this.init();
        } 
        if (_show)
        {
            this.infoDiv.style.display = "";
        }
        else
        {
            this.infoDiv.innerHTML = "";
            this.infoDiv.style.display = "none";
        }
    }
    //display the information
    this.showInfo = function(_message)
    {      
        if (this.infoDiv == null)
        {
            this.init();
        }  
        this.setStatusShow(true);
        this.infoDiv.innerHTML = _message;        
    }
}
/********************************************************
Controller
********************************************************/
System = {};
//Ajax engine
System.Ajax = new function()
{
    this.showStatus = true;
    //XHR objects cache
    this.xmlHttpRequestCache = new Array();
    //create a new XHR object
    this.createXmlHttpRequest = function()
    {
        return (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    }
    //get a free XHR object from the cache pool
    this.getXmlHttpRequest = function()
    {
        var XHR = null;
        var getOne = false;
        if(this.xmlHttpRequestCache.length > 0)
        {
            for(var i = 0; i < this.xmlHttpRequestCache.length; i++)
            {
                if(this.xmlHttpRequestCache[i].readyState == 0 || this.xmlHttpRequestCache[i].readyState == 4)
                {
                    XHR = this.xmlHttpRequestCache[i];
                    getOne = true;
                    break;
                }
            }
        }
        if(!getOne)
        {
            XHR = this.createXmlHttpRequest();
            this.xmlHttpRequestCache.push = XHR;
        }
        return XHR;
    }
    //Send the request to server form client
    this.send = function(url,soap,callbackMethod,asynchronous)
    {
        if(url.length == 0)
        {
            Display.StatusInfo.showInfo("目的为空,请求失败.")
            window.setTimeout("Display.StatusInfo.setStatusShow(false)",3000);
            return
        }
        if(this.showStatus)
        {
            Display.StatusInfo.showInfo("对象处理中,请稍后......");
        }
        if(asynchronous == undefined)
        {
           asynchronous = true;
        }
        var XHR = this.getXmlHttpRequest()
        if(XHR == null)
        {
            Display.StatusInfo.showInfo("浏览器不提供XmlHttpRequest对象的支持.");
            window.setTimeout("Display.StatusInfo.setStatusShow(false)",3000);
            return;
        }
        if(asynchronous == true && typeof(callbackMethod) == "function")
        {
            XHR.onreadystatechange = function()
            {
                if(XHR.readyState == 4 && XHR.status == 200)
                {
                    callbackMethod(XHR);
                    window.setTimeout("Display.StatusInfo.setStatusShow(false)",3000);
                }
            }
        }
        //Get and Post model
        if(soap.length == 0)
        {
            XHR.open("GET",url,asynchronous);
            XHR.send(null);
        }
        else//[SOAP -- WebService]
        {
            XHR.open("POST",url,asynchronous);
            XHR.setRequestHeader("Content-Type","text/xml");
            XHR.setRequestHeader("charset","utf-8");
            XHR.setRequestHeader("Content-Length",soap.length);
            XHR.setRequestHeader("HOST","www.xuanhepro.com");
            XHR.setRequestHeader('SOAPAction','http://tempuri.org/CalculateTmp');
            XHR.send(soap);
        }
    }
}
/********************************************************
Test
********************************************************/