if(typeof(earth) == 'undefined') {
	var earth = function() {};
}
if(typeof(earth.controls) == 'undefined') {
	earth.controls = function() {};
}


/* Control */
earth.controls.Control = Class.create(earth.Object);

/* Control - 初始化 */
earth.controls.Control.prototype.initialize = function(domId) {
    this.domId = domId;
};

/* Control - 设置父控件 */
earth.controls.Control.prototype.setParent = function(control) {
    this.parent = control;
}

/* Control - 获取DOM ID */
earth.controls.Control.prototype.getDomId = function() {
    return this.domId;
}





/* List */
earth.controls.List = Class.create(earth.controls.Control);


/* List - 初始化 */
earth.controls.List.prototype.initialize = function(domId) {
    this.domId = domId;
    this.items = new Array();
};


/* List - 追加 */
earth.controls.List.prototype.append = function(item, options) {
    this.items.push(item);
    item.setParent(this);

    /* 获取HTML元素 */
    var bodyElement = earth.util.getBodyElement();
    var itemElement = $(item.getDomId());
    var listElement = $(this.domId);

    /* 从body中移除 */
    bodyElement.removeChild(itemElement);

    /* 加入list */
    listElement.appendChild(itemElement);

    /* 设为可见 */
    this.setItemVisible(item, options);
};

earth.controls.List.prototype.setItemVisible = function(item, options) {
    var itemDomId = item.getDomId(); // DOM ID of item
    var me = this;

    if(typeof(options) != 'object' || typeof(options.effect) != 'string') {
        $(itemDomId).style.display = '';
        this.dispatchItemAddedEvent(item);
        return;
    }

    switch(options.effect) {
        case 'Slide&FadeIn':
            var itemElementLayout = Element.getLayout(itemDomId);

            if(typeof(options.direction) == 'string' && options.direction == 'y') {
                var percent = itemElementLayout.get('height'); // Real height of item
                //$(itemDomId).style.height = '1px';
                var scaleOptions = {scaleX:false, scaleY:true, scaleMode:{originalHeight:1}};
            } else {
                var percent = itemElementLayout.get('width');
                //$(itemDomId).style.width = '1px';
                var scaleOptions = {scaleX:true, scaleY:false, scaleMode:{originalWidth:1}};
            }

            // Launch
            new Effect.Appear(itemDomId, {duration: 0.1, from: 0.0, to: 0.2});
            new Effect.Scale(itemDomId, percent * 100, Object.extend({duration:0.6, scaleContent:false, afterFinish:function() {
                new Effect.Appear(itemDomId, {duration: 0.6, from: 0.2, afterFinish:function() {
                    if(typeof(options.direction) == 'string' && options.direction == 'y') {
                        $(itemDomId).style.height = '';
                    } else {
                        $(itemDomId).style.width = '';
                    }

                    me.dispatchItemAddedEvent(item);
                }});
            }}, scaleOptions));
            break;
    }
};

earth.controls.List.prototype.setItemInvisible = function(item, options) {
    var itemDomId = item.getDomId(); // DOM ID of item
    var me = this;

    if(typeof(options) != 'object' || typeof(options.effect) != 'string') {
        if(typeof(options.oncomplete) == 'function') {
            options.oncomplete();
        }
        return;
    }

    switch(options.effect) {
        case 'FadeOut':
            // Launch
            new Effect.Fade(itemDomId, {duration: 0.6, afterFinish:function() {
                if(typeof(options.oncomplete) == 'function') {
                    options.oncomplete();
                }
            }});
            break;
        case 'FadeOut&Fold':
            var itemElementLayout = Element.getLayout(itemDomId);

            if(typeof(options.direction) == 'string' && options.direction == 'y') {
                var percent = itemElementLayout.get('height'); // Real height of item
                var scaleOptions = {scaleX:false, scaleY:true};
            } else {
                var percent = itemElementLayout.get('width');
                var scaleOptions = {scaleX:true, scaleY:false};
            }

            // Launch
            new Effect.Fade(itemDomId, {duration: 0.6, to:0.2, afterFinish:function() {
                new Effect.Scale(itemDomId,  1/(percent * 100), Object.extend({duration:0.6, scaleContent:false, afterFinish:function() {
                    if(typeof(options.oncomplete) == 'function') {
                        options.oncomplete();
                    }
                }}, scaleOptions));
            }});
            break;
    }
};

earth.controls.List.prototype.dispatchItemAddedEvent = function(item) {
    var e = {sender:this, 'item':item};
    this.dispatchEvent('itemAdded', e);
};


/* List - 插入指定位置 */
earth.controls.List.prototype.insert = function(index, item, options) {

    item.setParent(this);
    var me = this;

    var bodyElement = earth.util.getBodyElement();

    /* 获取HTML元素 */
    var itemElement = $(item.getDomId());
    //itemElement.style.display = '';

    /* 从body中移除 */
    bodyElement.removeChild(itemElement);

    /* 将Li元素加入列表元素中 */
    var listElement = $(this.domId);
    if(me.items[index]) {
        listElement.insertBefore(itemElement, $(me.items[index].domId));
    } else {
        listElement.appendChild(itemElement);
    }
    listElement.style.display = '';

    /* 将列表项加入列表 */
    this.items.splice(index, 0, item);

    /* 设为可见 */
    this.setItemVisible(item, options);
};


/* List - 删除 */
earth.controls.List.prototype.remove = function(item, options) {
    for(var i=0;i<this.items.length;i++) {
        if(this.items[i] == item) {
            break;
        }
    }

    var itemElement = $(this.items[i].getDomId());
    var listElement = $(this.domId);


    /* 从items中移除 */
    this.items.splice(i, 1);

    this.setItemInvisible(item, Object.extend({oncomplete:function() {
        /* 从HTML元素中移除 */
        listElement.removeChild(itemElement);
    }}, options));
};



/* List - 删除指定位置 */
earth.controls.List.prototype.removeAt = function(index, options) {
    var item = this.items[index];

    this.remove(item, options);
};

earth.controls.List.prototype.getItemCount = function() {
    return this.items.length;
};


earth.controls.List.prototype.removeAll = function(){
    var num = this.getItemCount();
    for (var i =0;i<num ;i++ ){
        this.removeAt(0);
    }
}


earth.controls.List.prototype.clear = function() {
    for(var i=0;i<this.items.length;) {
        this.removeAt(0);
    }
};

/* List - 替换 */
earth.controls.List.prototype.replace = function(oldItem, newItem) {
    newItem.setParent(this);

    /* 替换数组中的元素 */
    for(var i=0;i<this.items.length;i++) {
        if(this.items[i] == oldItem) {
            this.items[i] = newItem;
            break;
        }
    }

    /* 获取HTML元素 */
    var bodyElement = earth.util.getBodyElement();
    var oldItemElement = $(oldItem.getDomId());
    var newItemElement = $(newItem.getDomId());
    var listElement = $(this.domId);

    /* 从body中移除 */
    bodyElement.removeChild(newItemElement);

    /* 设为可见 */
    newItemElement.style.display = '';

    /* 加入list */
    listElement.replaceChild(newItemElement,oldItemElement);
};

earth.controls.List.prototype.onitemAdded = function(event){};




/* List.Item */
earth.controls.List.Item = Class.create(earth.controls.Control);


/* List.Item - 初始化 */
earth.controls.List.Item.prototype.initialize = function(values, builder) {
    this.values = values;
    this.domId = 'ListItem' + Math.round(Math.random() * 1000000);
    this.builder = builder;

    /* 获得HTML元素 */
    var bodyElement = earth.util.getBodyElement();
    var itemElement = this.builder.build(values, this.domId);
    itemElement.id = this.domId;

    /* 设为隐藏 */
    itemElement.style.display = 'none';

    /* 加入到body中 */
    bodyElement.appendChild(itemElement);
};


/* List.Item - 删除 */
earth.controls.List.Item.prototype.remove = function() {
    if(this.parent) {
        this.parent.remove(this);
    }
};



/* List.Item.Builder */
earth.controls.List.Item.Builder = Class.create(earth.Object);

/* List.Item.Builder - 构造HTML元素 */
earth.controls.List.Item.Builder.prototype.build = function(values, itemDomId) {
};






/* AutoLoadList */
earth.controls.AutoLoadList = Class.create(earth.controls.List);

/* AutoLoadList - 初始化 */
earth.controls.AutoLoadList.prototype.initialize = function(domId, loader, decider) {
    this.domId = domId;
    this.loader = loader;
    this.decider = decider;
    this.page = 1;
    this.items = new Array();

    var loader = this.loader;

    var me = this;

    Event.observe(loader, 'load', function(event) {

        var data = event.values;

        if(me.options.reverse) {
            data.reverse();
        }

        if(me.options.append) {
            if(me.options.before) {
                if(data.length > 0) {
                    for(var i=0; i<data.length; i++) {
                        me.insert(i, me.decider.decide(data[i]));
                    }
                } else if(me.items.length == 0) {
                    me.append(me.decider.decide());
                }
            } else {
                if(data.length > 0) {
                    for(var i=0; i<data.length; i++) {
                        me.append(me.decider.decide(data[i]));
                    }
                } else if(me.items.length == 0) {
                    me.append(me.decider.decide());
                }
            }
        } else {

            var list = $(me.domId);

            if(me.items.length < data.length) {
                for(var a=0; a<me.items.length; a++) {
                    var newItem = me.decider.decide(data[a]);
                    me.replace(me.items[a], newItem);
                }

                for(var b=me.items.length; b<data.length; b++) {
                    me.append(me.decider.decide(data[b]));
                }
            } else if (me.items.length == data.length) {
                if(data.length > 0){
                    for(var i=0; i<me.items.length; i++) {
                        var newItem = me.decider.decide(data[i]);
                        me.replace(me.items[i], newItem);
                    }
                }else{
                    me.append(me.decider.decide());
                }
            } else if (me.items.length > data.length) {
                for(var a=0; a<data.length; a++) {
                    var newItem = me.decider.decide(data[a]);
                    me.replace(me.items[a], newItem);
                }

                var oldItemsLength = me.items.length;
                var c = data.length;
                for(var b=c; b<oldItemsLength; b++) {
                    me.removeAt(c);
                }
            }

        }

    });

};

/* AutoLoadList - 加载 */
earth.controls.AutoLoadList.prototype.load = function(options) {

    this.options = options;
    var me = this;

    var loader = this.loader;

    loader.load(me.page);
};

/* AutoLoadList - 加载下一页 */
earth.controls.AutoLoadList.prototype.loadNextPage = function(options) {
    this.page++;
    this.load(options);
};

/* AutoLoadList - 加载上一页 */
earth.controls.AutoLoadList.prototype.loadPrevPage = function(options) {
    this.page--;
    this.load(options);
};





/* SubmitControl */
earth.controls.SubmitControl = Class.create(earth.controls.Control);

/* SubmitControl - 添加控件 */
earth.controls.SubmitControl.prototype.addControl = function(control) {
    if(!this.controls) {
        this.controls = new Array();
    }

    if(!this.controlsQueue) {
        this.controlsQueue = new Array();
    }

    var me = this;

    Event.observe(control, 'complete', function(event){
        /* 如果有下一个控件，则提交下一个控件 */
        if(me.controlsQueue.length > 0) {
            var nextControls = me.controlsQueue.splice(0,1);
            var nextControl = nextControls[0];

            var descriptionContainer = $(me.domId + '_ProgressText');
            if(descriptionContainer) {
                descriptionContainer.innerHTML = '正在提交' + nextControl.getDescription() + '...';
            }

            nextControl.submit();
        } else {
            /* 没有其他控件时，隐藏读取图片，回复禁用按钮，尝试转跳 */
            var loadingImage = $(me.domId + '_LoadingImage');
            if(loadingImage) {
                loadingImage.style.display = 'none';
            }

            var submitButton = $(me.domId + '_Button');
            if(submitButton) {
                submitButton.disabled = false;
            }

            // 隐藏文字层
            var descriptionContainer = $(me.domId + '_ProgressText');
            if(descriptionContainer) {
                descriptionContainer.innerHTML = '';
            }

            if(me.redirect) {
                location.href = me.redirect;
                return;
            }else{
                var e = {sender:me};
                me.dispatchEvent('complete', e);
            }
        }
    });

    Event.observe(control, 'fail', function(event){
        /* 没有其他控件时，隐藏读取图片，回复禁用按钮，尝试转跳 */
        var descriptionContainer = $(me.domId + '_ProgressText');
        if(descriptionContainer) {
                descriptionContainer.innerHTML = '';
        }

        var loadingImage = $(me.domId + '_LoadingImage');
        if(loadingImage) {
            loadingImage.style.display = 'none';
        }

        var submitButton = $(me.domId + '_Button');
        if(submitButton) {
            submitButton.disabled = false;
        }

        me.controlsQueue = new Array();

        if(event.string) {
            var errorDescription = event.string;
        } else {
            var errorDescription = '未知错误';
        }
        alert(errorDescription);
    });

    this.controls.push(control);
};

/* SubmitControl - 设定转跳地址 */
earth.controls.SubmitControl.prototype.setRedirect = function(redirect) {
    this.redirect = redirect;
};

/* SubmitControl -  */
earth.controls.SubmitControl.prototype.submit = function() {
    var me = this;

    var submitButton = $(me.domId+ '_Button');
    if(submitButton) {
        submitButton.disabled = true;
    }

    var loadingImage = $(me.domId + '_LoadingImage');
    if(loadingImage) {
        loadingImage.style.display = '';
    }

    for(var i=0; i<this.controls.length; i++) {
        this.controlsQueue.push(this.controls[i]);
    }

    if(this.controlsQueue.length > 0) {
        // 有控件时，显示第一个控件的提示文字，并提交第一个控件
        var firstControls = this.controlsQueue.splice(0,1);
        var firstControl = firstControls[0];

        var descriptionContainer = $(me.domId + '_ProgressText');
        if(descriptionContainer) {
            descriptionContainer.innerHTML = '正在提交' + firstControl.getDescription() + '...';
        }

        firstControl.submit();
    } else {
        /* 没有控件时，隐藏读取图片，回复禁用按钮，尝试转跳 */
        var loadingImage = $(me.domId + '_LoadingImage');
        if(loadingImage) {
            loadingImage.style.display = 'none';
        }

        var submitButton = $(me.domId + '_Button');
        if(submitButton) {
            submitButton.disabled = false;
        }

        if(this.redirect) {
            location.href = this.redirect;
        } else {
            var e = {sender:this};
            this.dispatchEvent('complete', e);
        }
    }

};

earth.controls.SubmitControl.prototype.oncomplete = function() {};



/** 
 * 对话框基类
 *
 * @author Timandes
 */
earth.controls.Dialog = Class.create(earth.Object);
/**
 * 初始化函数
 *
 * @param string domId 对话框的DOM元素ID
 * @param earth.controls.Dialog.Builder builder 对话框构造者
 * @param object options 选项（支持：hideOnClose, lockWindow）
 */
earth.controls.Dialog.prototype.initialize = function(domId, builder, options) {
    this.domId = domId;
	this.builder = builder;
	this.options = (typeof(options) == 'undefined')?{}:options;

    this.create();

    var me = this;

    // close button
    this.closeButtonElement = $(this.domId + '_CloseButton');
    if(this.closeButtonElement) {
		Event.observe(this.closeButtonElement, 'click', function(event) {
            if(me.options.hideOnClose) {
                me.hide();
            } else {
                me.close();
            }
        });
    }
}

/**
 * 创建DOM元素
 */
earth.controls.Dialog.prototype.create = function() {
    if(this.options.lockWindow) {
        this.createMask();
    }

    this.createDialog();
}

/**
 * 创建遮罩DOM元素
 */
earth.controls.Dialog.prototype.createMask = function() {
    var bodyElement = earth.util.getBodyElement();

    var maskId = this.domId + '_Mask';
    var maskElement = document.createElement('div');
    maskElement.id = maskId;
    maskElement.style.display = 'none';
    maskElement.style.backgroundColor = '#333333';
    maskElement.style.position = 'absolute';
    maskElement.style.top = '0';
    maskElement.style.left = '0';
    maskElement.style.zIndex = 1000;
    bodyElement.appendChild(maskElement);
};

/**
 * 创建对话框DOM元素
 */
earth.controls.Dialog.prototype.createDialog = function() {
    var bodyElement = earth.util.getBodyElement();

    var dialogElement = this.builder.build(this.domId);
    dialogElement.id = this.domId;
    dialogElement.style.display = 'none';
    dialogElement.style.backgroundColor = '#ffffff';
    dialogElement.style.position = 'absolute';
    dialogElement.style.zIndex = 1001;
    bodyElement.appendChild(dialogElement);
};

/**
 * 显示对话框
 */
earth.controls.Dialog.prototype.show = function() {
    var me = this;


    me.dispatchEvent('showing', {'sender':me});


    var maskId = this.domId + '_Mask';
    var dialogId = this.domId;

    // calculate size of dialog
    var arrayPageSize = earth.util.getPageSize();
    var arrayPageScroll = earth.util.getPageScroll();
    var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 5);
    var iDialogLeft = (arrayPageSize[0] - parseInt($(dialogId).style.width)) / 2;
    Element.setLeft(dialogId, iDialogLeft);
    Element.setTop(dialogId, lightboxTop);


    if(this.options.lockWindow) {
        // display mask
        var arrayPageSize = earth.util.getPageSize();
        var maskWidth = arrayPageSize[0];
        if(screen.availWidth == arrayPageSize[0]) {
        if(document.documentElement) {// non-IE
            if(document.documentElement.scrollHeight > document.documentElement.clientHeight) {// has vertical scroll
    			maskWidth = arrayPageSize[0] - 17;
            }
        }
        }
        var maskHeight = arrayPageSize[1];
        if(document.all) { // IE
            maskHeight = arrayPageSize[1] + 30;
        }
        Element.setWidth(maskId, maskWidth);
        Element.setHeight(maskId, maskHeight);
        new Effect.Appear(maskId, { duration: 0.2, from: 0.0, to: 0.8, afterFinish: function() {
            // display dialog
            new Effect.Appear(dialogId, { duration: 0.5, queue: 'end', afterFinish: function() {
				me.dispatchEvent('show', {'sender':me});
            }
            });
        }
        });
    } else {
        // display dialog
        new Effect.Appear(dialogId, { duration: 0.5, queue: 'end', afterFinish: function() {
			me.dispatchEvent('show', {'sender':me});
        }
        });
    }
};

/**
 * 隐藏对话框
 */
earth.controls.Dialog.prototype.hide = function() {
    var maskId = this.domId + '_Mask';
    var dialogId = this.domId;

    var me = this;

    new Effect.Fade(dialogId, { duration: 0.2, afterFinish: function(){
        if(me.options.lockWindow) {
            new Effect.Fade(maskId, { duration: 0.2});
        }
    }
    });
};


/**
 * 关闭对话框
 */
earth.controls.Dialog.prototype.close = function() {
    var maskId = this.domId + '_Mask';
    var dialogId = this.domId;

    var me = this;

    var dialogElement = $(this.domId);
    if(!dialogElement) {
        return false;
    }

    var bodyElement = earth.util.getBodyElement();

    new Effect.Fade(dialogId, { duration: 0.2, afterFinish: function(){
        if(me.options.lockWindow) {
            new Effect.Fade(maskId, { duration: 0.2, afterFinish: function(){
                bodyElement.removeChild(dialogElement);
				me.dispatchEvent('close', {'sender':me});
            }
            });
        } else {
            bodyElement.removeChild(dialogElement);
            me.dispatchEvent('close', {'sender':me});
        }
    }
    });
};


/**
 * show事件
 */
earth.controls.Dialog.prototype.onshow = function(event) {};
earth.controls.Dialog.prototype.onshowing = function(event) {};

earth.controls.Dialog.prototype.onclose = function(event) {};


earth.controls.Dialog.Builder = Class.create(earth.Object);
earth.controls.Dialog.Builder.prototype.build = function(domId) {};



/**
 * Tab页
 */
earth.controls.tabPage = Class.create();
earth.controls.tabPage.prototype.initialize = function(htmlId, onLoad, activeCssClass, inactiveCssClass, hoverCssClass) {
    this.htmlId = htmlId;
    this.onLoad = onLoad;
    this.activeCssClass = (activeCssClass?activeCssClass:"");
    this.inactiveCssClass = (inactiveCssClass?inactiveCssClass:"");
    this.hoverCssClass = (hoverCssClass?hoverCssClass:"");
    this.loaded = false;
    this.tabControl = null;
    this.enabled = true;

    if(!$(this.htmlId) && !$(this.htmlId + '_Tab')) {
        return false;
    }

    var classInstance = this;

    $(this.htmlId + '_Page').style.display = 'none';
    $(this.htmlId + '_Tab').className = this.inactiveCssClass;

    if($(this.htmlId)) {
        Event.observe($(this.htmlId), 'click', function(event) {
            if(classInstance.enabled) {
                classInstance.tabControl.activateTabPage(classInstance);
            }
        });
    } else {
        Event.observe($(this.htmlId + '_Tab'), 'click', function(event) {
            if(classInstance.enabled) {
                classInstance.tabControl.activateTabPage(classInstance);
            }
        });
        $(this.htmlId + '_Tab').style.cursor = 'pointer';
        Event.observe($(this.htmlId + '_Tab'), 'mouseover', function(event) {
            if($(classInstance.htmlId + '_Page').style.display != '' && classInstance.enabled) {
                $(classInstance.htmlId + '_Tab').className = classInstance.hoverCssClass;
            }
        });
        Event.observe($(this.htmlId + '_Tab'), 'mouseout', function(event) {
            if($(classInstance.htmlId + '_Page').style.display != '' && classInstance.enabled) {
                $(classInstance.htmlId + '_Tab').className = classInstance.inactiveCssClass;
            }
        });
    }
};
earth.controls.tabPage.prototype.setParent = function(tabControl) {
    this.tabControl = tabControl;
};
earth.controls.tabPage.prototype.activate = function() {
    $(this.htmlId + '_Page').style.display = '';
    $(this.htmlId + '_Tab').className = this.activeCssClass;

    if(!this.loaded && this.onLoad) {
        this.tabControl.setEnabled(false);
        this.onLoad();
        this.loaded = true;
        this.tabControl.setEnabled(true);
    }
};
earth.controls.tabPage.prototype.inactivate = function() {
    $(this.htmlId + '_Page').style.display = 'none';
    $(this.htmlId + '_Tab').className = this.inactiveCssClass;
};
earth.controls.tabPage.prototype.setEnabled = function(enabled) {
    this.enabled = enabled;
};



/**
 * Tab控件
 */
earth.controls.tabControl = Class.create();
earth.controls.tabControl.prototype.initialize = function() {
    this.activeTabPage = null;
    this.tabPages = new Array();
};
earth.controls.tabControl.prototype.addTabPage = function(tabPage) {
    tabPage.setParent(this);
    this.tabPages.push(tabPage);

    if(!this.activeTabPage) {
        this.activeTabPage = tabPage;
        tabPage.activate();
    }
};
earth.controls.tabControl.prototype.activateTabPage = function(tabPage) {
    if(this.activeTabPage == tabPage) {
        return;
    }

    tabPage.activate();

    if(this.activeTabPage) {
        this.activeTabPage.inactivate();
    }

    this.activeTabPage = tabPage;
};
earth.controls.tabControl.prototype.setEnabled = function(enabled) {
    for(var i=0;i<this.tabPages.length;i++) {
        this.tabPages[i].setEnabled(enabled);
    }
};



/**
 * 自动伸缩高度的Tab页
 */
earth.controls.autoScaleTabPage = Class.create();
earth.controls.autoScaleTabPage.prototype.initialize = function(htmlId, maxHeight) {
    this.htmlId = htmlId;          // 当前块ID
    this.maxHeight = maxHeight;    // 当前块可以展开的最大高度
    this.speed = 30;               // 动作速度
    this.beginScaleEventQueue = new Array();
    this.action = 0;                // 1-collapsing; 2-expanding

    var classInstance = this;

    if(!$(this.htmlId)) {
        return false;
    }

    var tabLayer = $(this.htmlId);
    this.titleLayer = tabLayer.getElementsByTagName('dt')[0];
    this.contentLayer = tabLayer.getElementsByTagName('dd')[0];

    Event.observe(this.titleLayer, 'click', function(event) {
        classInstance.onBeginScale();
    });
/*
    Event.observe(this.titleLayer, 'mouseover', function(event) {
        classInstance.onBeginScale();
    });*/
};
earth.controls.autoScaleTabPage.prototype.onBeginScale = function() {
    for(i=0;i<this.beginScaleEventQueue.length;i++) {
        this.beginScaleEventQueue[i](this);
    }
};
earth.controls.autoScaleTabPage.prototype.attachBeginScaleEvent = function(eventHandler) {
    this.beginScaleEventQueue.push(eventHandler);
};
earth.controls.autoScaleTabPage.prototype.expand = function() {
	var maxHeight = this.maxHeight;
	var moveBy = this.speed;
	var mainInstance = this;
	var tabLayer = $(this.htmlId);
/*
    switch(this.action) {
        default:
        case 0:
            this.action = 2;
            break;
        case 1:
            this.action = 2;
            clearInterval(this.timer);
            break;
        case 2:
            break;
    }*/

    var fun = function() {
		var curHeight = tabLayer.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < maxHeight) {
			tabLayer.style.height = newHeight + "px";
            setTimeout(fun, 30);
        } else {
            mainInstance.action = 0;
			tabLayer.style.height = maxHeight + "px";
		}
	};
	setTimeout(fun, 30);
};
earth.controls.autoScaleTabPage.prototype.collapse = function() {
	var minHeight = this.titleLayer.offsetHeight;
	var moveBy = this.speed;
	var mainInstance = this;
	var tabLayer = $(this.htmlId);
/*
    switch(this.action) {
        default:
        case 0:
            this.action = 1;
            break;
        case 1:
            break;
        case 2:
            this.action = 1;
            clearInterval(this.timer);
            break;
    }*/

    var fun = function() {
		var curHeight = tabLayer.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight) {
			tabLayer.style.height = newHeight + "px";
            setTimeout(fun, 30);
        } else {
            mainInstance.action = 0;
			tabLayer.style.height = minHeight + "px";
		}
	};
	setTimeout(fun, 30);
};
earth.controls.autoScaleTabPage.prototype.getHtmlId = function() {
    return this.htmlId;
};


/**
 * 自动伸缩高度的Tab页
 */
earth.controls.autoSwitchTabControl = Class.create();
earth.controls.autoSwitchTabControl.prototype.initialize = function() {
    this.tabPages = new Array();
};
earth.controls.autoSwitchTabControl.prototype.add = function(tabPage) {
    var classInstance = this;

    tabPage.attachBeginScaleEvent(function(senderTabPage) {
        senderTabPage.expand();
        for(i=0;i<classInstance.tabPages.length;i++) {
            var tp = classInstance.tabPages[i];
            if(tp.getHtmlId() != senderTabPage.getHtmlId()) {
                tp.collapse();
            }
        }
    });
    this.tabPages.push(tabPage);
};


/**
 * Motion Tab Page
 */
earth.controls.motionTabPage = Class.create();
earth.controls.motionTabPage.prototype.initialize = function(rank, htmlId) {
    this.htmlId = htmlId;
    this.rank = rank;
    this.speed = 30;
    this.beginMoveingEventQueue = new Array();

    var classInstance = this;

    if(!$(this.htmlId)) {
        return false;
    }

    var tabLayer = $(this.htmlId);
    this.titleLayer = tabLayer.getElementsByTagName('dt')[0];
    this.contentLayer = tabLayer.getElementsByTagName('dd')[0];

    Event.observe(this.titleLayer, 'click', function(event) {
        classInstance.onBeginMoving();
    });

    Event.observe(this.titleLayer, 'mouseover', function(event) {
        classInstance.onBeginMoving();
    });
};
earth.controls.motionTabPage.prototype.onBeginMoving = function() {
    for(i=0;i<this.beginMoveingEventQueue.length;i++) {
        this.beginMoveingEventQueue[i](this);
    }
};
earth.controls.motionTabPage.prototype.attachBeginMovingEvent = function(eventHandler) {
    this.beginMoveingEventQueue.push(eventHandler);
};
earth.controls.motionTabPage.prototype.moveUp = function() {
    var minTop = this.rank * this.titleLayer.offsetHeight;
	var moveBy = this.speed;
	var classInstance = this;
	var tabLayer = $(this.htmlId);

    var fun = function() {
        var newTop = tabLayer.offsetTop - moveBy;
        if(minTop < newTop) {
            tabLayer.style.top = newTop + "px";
            setTimeout(fun, 30);
        } else {
            tabLayer.style.top = minTop + "px";
        }
	};
	setTimeout(fun, 30);
};
earth.controls.motionTabPage.prototype.moveDown = function() {
    var maxTop = (this.rank - 1) * this.titleLayer.offsetHeight + $(this.htmlId).offsetHeight;
	var moveBy = this.speed;
	var classInstance = this;
	var tabLayer = $(this.htmlId);

    var fun = function() {
        var newTop = tabLayer.offsetTop + moveBy;
        if(maxTop > newTop) {
            tabLayer.style.top = newTop + "px";
            setTimeout(fun, 30);
        } else {
            tabLayer.style.top = maxTop + "px";
        }
	};
	setTimeout(fun, 30);
};
earth.controls.motionTabPage.prototype.getHtmlId = function() {
    return this.htmlId;
};


/**
 * Motion Tab Control
 */
earth.controls.motionTabControl = Class.create();
earth.controls.motionTabControl.prototype.initialize = function() {
    this.tabPages = new Array();
    this.selectedTabPage = null;
};
earth.controls.motionTabControl.prototype.add = function(tabPage) {
    var classInstance = this;

    tabPage.attachBeginMovingEvent(function(senderTabPage) {
        var senderIndex = classInstance.getIndex(senderTabPage);
        var selectedIndex = classInstance.getIndex(classInstance.selectedTabPage);

        if(senderIndex < 0) {
            return;
        }

        if(selectedIndex < senderIndex) {
            for(i=0;i<=senderIndex;i++) {
                classInstance.tabPages[i].moveUp();
            }
        } else {
            for(i=senderIndex+1;i<classInstance.tabPages.length;i++) {
                classInstance.tabPages[i].moveDown();
            }
        }

        classInstance.selectedTabPage = senderTabPage;
    });
    this.tabPages.push(tabPage);

    if(!this.selectedTabPage) {
        this.selectedTabPage = tabPage;
    }
};
earth.controls.motionTabControl.prototype.getIndex = function(tabPage) {
    if(!tabPage) {
        return -1;
    }

    var retval = -1;

    for(i=0;i<this.tabPages.length;i++) {
        var tp = this.tabPages[i];
        if(tp.getHtmlId() == tabPage.getHtmlId()) {
            retval = i;
            break;
        }
    }
    
    return retval;
}
// -----------------------------------------------------------------------------------

