var stock = Class.create();

//个股信息
stock.companyInfo = Class.create();
//初始化程序
stock.companyInfo.prototype.initialize = function(htmlId,sec){
	this.htmlId = htmlId;
	this.sec = sec*1000;

    var classInstance = this;
    classInstance.update();

}

stock.companyInfo.prototype.update = function(){
    var url='company.php?action=company';
	var p = "code=" + $(this.htmlId+'_Code').value;
    var classInstance = this;

    var tag= 1;
    function checkTag()
    {
        if(tag == 1)
        {
            classInstance.update();
        }
    }
    setTimeout(function () {checkTag();}, 30000);
    
    new Ajax.Request
    (
        url,
        {
            method: 'post',
            parameters: p,
            onComplete: function(transport){
                    var x = 'var responseValues = ' + transport.responseText;
                    eval(x);

                    if(responseValues.code < 0)
                    {
                        alert(responseValues.string);
                    }
                    else
                    {
                        if(responseValues.data.investor){
                            classInstance.maxBuying(responseValues.data.investor.buyable_shares);
                            classInstance.MaxSelling(responseValues.data.investor.sellable_shares);
                        }
                        classInstance.info(responseValues.data.info);
						classInstance.buyingDelegate(responseValues.data.buying_delegate?responseValues.data.buying_delegate:false);
						classInstance.sellingDelegate(responseValues.data.selling_delegate?responseValues.data.selling_delegate:false);
                        //classInstance.investor(responseValues.data.investor);
                        
                    }
                    // 请求完成后开始计时，5s后开始下一次请求
                    tag =2;
                    setTimeout(function() {classInstance.update();},classInstance.sec);
            }
        }
    );

}//update
stock.companyInfo.prototype.maxBuying = function(value){
    $('MaxBuying').innerHTML = value;
}//MaxBuying
stock.companyInfo.prototype.MaxSelling = function(value){
    $('MaxSelling').innerHTML = value;
}//MaxSelling
stock.companyInfo.prototype.info = function(info){
    $(this.htmlId+'_price').innerHTML=parseFloat(info.price).toFixed(2);//显示最新价格

}//info
stock.companyInfo.prototype.buyingDelegate = function(buyingDelegate){
    var price=this.htmlId + '_price_buying';
    var shares=this.htmlId + '_shares_buying';
    var i;
    for(i=1;i<6;i++)
    {
        if(buyingDelegate[i-1])
        {
            $(price + i).innerHTML = buyingDelegate[i-1].price.toFixed(2);
            $(shares + i).innerHTML = buyingDelegate[i-1].shares;
        }else{
            $(price + i).innerHTML = "";
            $(shares + i).innerHTML = ""
        };
    }

}//buyingDelgegate
stock.companyInfo.prototype.sellingDelegate = function(sellingDelegate){
    var price=this.htmlId + '_price_selling'; 
    var shares=this.htmlId + '_shares_selling';
    var i;
    for(i=1;i<6;i++)
    {
        if(sellingDelegate[i-1])
        {
            $(price + i).innerHTML = sellingDelegate[i-1].price.toFixed(2);
            $(shares + i).innerHTML = sellingDelegate[i-1].shares;
        }else{
            $(price + i).innerHTML = "";
            $(shares + i).innerHTML = "";
        }
    }
    
}//sellingDelegate
stock.companyInfo.prototype.investor = function(investor){
    if(investor.cash){
        $(this.htmlId+'_cash').innerHTML=(investor.cash).toFixed(2);//显示资金总数
        $(this.htmlId+'_available_cash').innerHTML=(investor.available_cash).toFixed(2);//显示可用资金
    }else{
        $(this.htmlId+'_cash').innerHTML=0.00;
        $(this.htmlId+'_available_cash').innerHTML=0.00;//
    }
    //$(this.htmlId+'_shares').innerHTML=(investor.shares);//显示持股数
    //$(this.htmlId+'_available_shares').innerHTML=(investor.available_shares);//显示可卖股数
}//investor


stock.dialogs = Class.create();

//消息提示框 Class
stock.dialogs.messageBox = Class.create(earth.controls.dialog, {
    initialize: function($super, htmlId, text, timeout, width, height, icon) {
        this.text = text;
        this.timeout = timeout;
        this.width = width;
        this.height = height;
        this.icon = icon;

        $super(htmlId);
    }
});

stock.dialogs.messageBox.prototype.onInitializing = function(){
    this.lockWindow = true;
    this.hideOnClose = false;
} // onInitializing

stock.dialogs.messageBox.prototype.onShown = function(){
	var classInstance = this;

    if(this.timeout) {
        setTimeout(function() {
                classInstance.close();
            }, this.timeout * 1000
        );
    }
} // onShown

stock.dialogs.messageBox.prototype.buildDialogElement = function(){
    this.iconUrl = "../images/ok.png";
    switch(this.icon) {
        case 1:
        default:
            break;
        case 2:
            this.iconUrl = "../images/stop.png";
            break;
    }

    var innerHtml = '';
    innerHtml = '<div class="BoxDivTitle" style="overflow:hidden;margin-bottom:10px;display:inline-block;width:' + this.width + 'px;">';
    innerHtml += '	<a href="javascript:void(0);" class="closeMessageBoxButton" id="'+ this.htmlId +'_CloseButton">关闭</a>';
    innerHtml += '</div>';
    innerHtml += '<div class="BoxDivContent">';
    innerHtml += '	<img src="' + this.iconUrl+ '" alt="成功" style="float:left;padding:0px 20px 25px 35px;" />';
    innerHtml += '	<p style="float:left;text-align:left;color:#666;line-height:22px;padding-top:5px;font-size:24px;">';
    innerHtml += '		<span>' + this.text + '</span>';
    if(this.timeout) {
        innerHtml += '		<br /><span style="font-size:14px;">(' + this.timeout + '秒后返回)</span>';
    }
    innerHtml += '	</p>';
    innerHtml += '</div>';

    var dialogElement = document.createElement('div');
    dialogElement.innerHTML = innerHtml;

	dialogElement.style.width = this.width + "px";
	dialogElement.style.height = this.height + "px";
    dialogElement.style.padding = "5px";
	dialogElement.className = "BoxDiv";
    return dialogElement;
} // buildDialogElement






//下单按钮
stock.addDelegateButton =  Class.create();
stock.addDelegateButton.prototype.initialize = function(htmlId,code,type){
    
    this.htmlId = htmlId;
    this.code = code;
    this.type = type;
    var classInstance = this;

    ButtonEvent = function(event){
         $(classInstance.htmlId).disabled=true;
        var price  = $(classInstance.htmlId + '_Price').value;
        var shares = $(classInstance.htmlId + '_Shares').value;

        var url = 'profile.php?action=' + classInstance.type;
        var pars = "code=" + classInstance.code + "&price=" + price + "&shares=" + shares;
        var ajaxObject = new Ajax.Request(
            url,
            {
                method:'post',
                parameters:pars,
                onComplete:function(transport){
                    var x = 'var responseValues = ' + transport.responseText;
                    eval(x);

                    if(responseValues.code < 0)
                    {
                        alert(responseValues.string);
                    }
                    else
                    {
                        var dlg = new stock.dialogs.messageBox('MessageBox' + classInstance.type, '下单成功',2,'250','100',1);
                        dlg.show();
                        $(classInstance.htmlId + '_Price').value="";
                        $(classInstance.htmlId + '_Shares').value="";
                    }
                }
            }
        );
        
        $(classInstance.htmlId).disabled=false;
        }
    EnterHandler = function(event){
        if(event.keyCode==13&&$(classInstance.htmlId + '_Shares').value!=""&&$(classInstance.htmlId + '_Price').value!="")
            {
            //ButtonEvent(event);
            $(classInstance.htmlId).disabled=true;
            var price  = $(classInstance.htmlId + '_Price').value;
            var shares = $(classInstance.htmlId + '_Shares').value;

            var url = 'profile.php?action=' + classInstance.type;
            var pars = "code=" + classInstance.code + "&price=" + price + "&shares=" + shares;
            var ajaxObject = new Ajax.Request(
                url,
                {
                    method:'post',
                    parameters:pars,
                    onComplete:function(transport){
                        var x = 'var responseValues = ' + transport.responseText;
                        eval(x);

                        if(responseValues.code < 0)
                        {
                            alert(responseValues.string);
                        }
                        else
                        {
                            var dlg = new stock.dialogs.messageBox('MessageBox' + classInstance.type, '下单成功',2,'250','100',1);
                            dlg.show();
                            $(classInstance.htmlId + '_Price').value="";
                            $(classInstance.htmlId + '_Shares').value="";
                        }
                    }
                }
            );
            
            $(classInstance.htmlId).disabled=false;
            }

    }

    Event.observe(classInstance.htmlId, 'click', ButtonEvent);
    Event.observe(classInstance.htmlId + '_Shares', 'keyup', EnterHandler);
    
    
    
}



/* 个人信息 */
stock.investorInfo = Class.create();

stock.investorInfo.prototype.initialize = function(htmlId, sec){
    this.htmlId = htmlId;
    this.sec = sec * 1000;
    var classInstance = this;

    classInstance.update();
    this.HL = new stock.holdingsList(classInstance.htmlId + '_Holdings', null, new stock.holdingsBuilder());

};

stock.investorInfo.prototype.update = function() {
    var url = 'profile.php?action=investor';
    var pars = '';
    var classInstance = this;

    var tag= 1;
    function checkTag()
    {
        if(tag == 1)
        {
            classInstance.update();
        }
    }
    setTimeout(function () {checkTag();}, 30000);

    var ajaxObject = new Ajax.Request(
        url,
        {
            method:'post',
            parameters:pars,
            onComplete:function(transport){
                eval("var responseValues = " + transport.responseText + ";");

                if(responseValues.code < 0)
                {
                    var dlg = stock.dialogs.messageBox('MB' ,responseValues.string ,2 ,'240','100',1);
                    dlg.show();
                }
                else
                {
                    classInstance.funds(responseValues.data.funds);
                    classInstance.HL.innerClear();
                    if(responseValues.data.holdings.length == 0)
                    {
                        classInstance.HL.load(classInstance.HL.builder.build());
                    }
                    else
                    {
                        for(var i=0;i<responseValues.data.holdings.length;i++)
                        {
                            classInstance.HL.load(classInstance.HL.builder.build(responseValues.data.holdings[i]));
                        }
                    }
                }

                // 请求完成后开始计时，5s后开始下一次请求
                tag = 2;
                setTimeout(function() {classInstance.update();},classInstance.sec);
            }
        }
    );
}; // update

stock.investorInfo.prototype.funds = function(funds) {

    var innerTd0 = funds.name;
    var innerTd1 = funds.total_assets.toFixed(2);
    var innerTd2 = funds.cash.toFixed(2);
    var innerTd3 = funds.available_cash.toFixed(2);
    var innerTd4 = funds.total_value.toFixed(2);

    var td0 = $(this.htmlId + '_Funds_name');
    td0.innerHTML = innerTd0;
    var td1 = $(this.htmlId + '_Funds_total_assets');
    td1.innerHTML = innerTd1;
    var td2 = $(this.htmlId + '_Funds_cash');
    td2.innerHTML = innerTd2;
    var td3 = $(this.htmlId + '_Funds_available_cash');
    td3.innerHTML = innerTd3;
    var td4 = $(this.htmlId + '_Funds_total_value');
    td4.innerHTML = innerTd4;

}; // funds

// 持股列表
// holdingsBuilder
stock.holdingsBuilder = Class.create(earth.controls.list.builder);

    stock.holdingsBuilder.prototype.build = function(values)
    {
        if(!values)
        {
            return new stock.emptyHoldingsItem(values);
        }
        else
        {
            return new stock.holdingsItem(values);
        }

    };

// holdingsList
stock.holdingsList = Class.create(earth.controls.list, {
    initialize: function($super, htmlId, loader, builder) {
        $super(htmlId, loader, builder);
    }
});

    
stock.holdingsList.prototype.load = function(holdings) 
{
    this.append(holdings);
};


// Item
stock.holdingsItem = Class.create(earth.controls.list.item);

    stock.holdingsItem.prototype.buildElement = function()
    {
        // 创建HTML元素
        innerTd0 = this.values.code;
        innerTd1 = this.values.name;
        innerTd2 = this.values.shares;
        innerTd3 = this.values.available_shares;
        innerTd4 = this.values.price.toFixed(2);
        innerTd5 = this.values.total_value.toFixed(2);
        innerTd6 = '<a href="company.php?code=' + this.values.code + '">委托交易</a>';

        // 创建对象
        var holdingsItem = document.createElement('tr');
        var td0 = document.createElement('td');
        td0.innerHTML = innerTd0;
        var td1 = document.createElement('td');
        td1.innerHTML = innerTd1;
        var td2 = document.createElement('td');
        td2.innerHTML = innerTd2;
        var td3 = document.createElement('td');
        td3.innerHTML = innerTd3;
        var td4 = document.createElement('td');
        td4.innerHTML = innerTd4;
        var td5 = document.createElement('td');
        td5.innerHTML = innerTd5;
        var td6 = document.createElement('td');
        td6.innerHTML = innerTd6;

        holdingsItem.appendChild(td0);
        holdingsItem.appendChild(td1);
        holdingsItem.appendChild(td2);
        holdingsItem.appendChild(td3);
        holdingsItem.appendChild(td4);
        holdingsItem.appendChild(td5);
        holdingsItem.appendChild(td6);

        // 返回对象
        return holdingsItem;

    };

stock.emptyHoldingsItem = Class.create(earth.controls.list.item);
    
    stock.emptyHoldingsItem.prototype.buildElement = function()
    {
        var innerTd = '';
        innerTd += '暂无持股信息。';

        var element = document.createElement('tr');
        var td0 = document.createElement('td');
        td0.innerHTML = innerTd;
        td0.colSpan = "7";

        element.appendChild(td0);
        return element;
    };







// 公司列表
stock.companyList = Class.create();

// 公司列表 - 列表
stock.companyList.list = Class.create(earth.controls.list, {
    // 公司列表 - 列表 - 初始化
    initialize: function($super, htmlId, loader, builder) {
        $super(htmlId, loader, builder);
    }
});



// 公司列表 - 构造器
stock.companyList.builder = Class.create(earth.controls.list.builder);

// 公司列表 - 构造器 - 构造
stock.companyList.builder.prototype.build = function(values) {
    if(values) {
        return new stock.companyList.item(values);
    } else {
        return new stock.companyList.emptyItem(values);
    }
};


//
stock.fixNum = function(num) {
    var n = new Number(num);
    return n.toFixed(2);
}



// 公司列表 - 列表项
stock.companyList.item = Class.create(earth.controls.list.item);

// 公司列表 - 列表项 - 创建HTML元素
stock.companyList.item.prototype.buildElement = function() {

//    earth.util.setCookie('requestState','Finish',30);

    var tableRow = document.createElement('tr');

    var tableData1 = document.createElement('td');
    tableData1.innerHTML = this.values.code;
    tableRow.appendChild(tableData1);

    var tableData2 = document.createElement('td');
    tableData2.innerHTML = this.values.name;
    tableRow.appendChild(tableData2);

    var tableData3 = document.createElement('td');
    tableData3.innerHTML = stock.fixNum(this.values.closing_price);
    tableRow.appendChild(tableData3);

    var tableData4 = document.createElement('td');
    if(this.values.opening_price) {
        tableData4.innerHTML = stock.fixNum(this.values.opening_price);
    } else {
        tableData4.innerHTML = '';
    }
    tableRow.appendChild(tableData4);

    var tableData5 = document.createElement('td');
    if(this.values.top_price) {
        tableData5.innerHTML = stock.fixNum(this.values.top_price);
    } else {
        tableData5.innerHTML = '';
    }
    tableRow.appendChild(tableData5);

    var tableData6 = document.createElement('td');
    if(this.values.bottom_price) {
        tableData6.innerHTML = stock.fixNum(this.values.bottom_price);
    } else {
        tableData6.innerHTML = '';
    }
    tableRow.appendChild(tableData6);

    var tableData7 = document.createElement('td');
    if(this.values.price) {
        tableData7.innerHTML = stock.fixNum(this.values.price);
    } else {
        tableData7.innerHTML = '';
    }
    tableRow.appendChild(tableData7);

    // 买入价
    var tableData8 = document.createElement('td');
    if(this.values.buying_price) {
        tableData8.innerHTML = stock.fixNum(this.values.buying_price);
    } else {
        tableData8.innerHTML = '';
    }
    tableRow.appendChild(tableData8);

    // 卖出价
    var tableData9 = document.createElement('td');
    if(this.values.selling_price) {
        tableData9.innerHTML = stock.fixNum(this.values.selling_price);
    } else {
        tableData9.innerHTML = '';
    }
    tableRow.appendChild(tableData9);

    // 涨跌幅
    var tableData10 = document.createElement('td');
    if(this.values.range) {
        tableData10.innerHTML = (this.values.range*100).toFixed(2) + '%';
        //根据涨跌幅改变字体颜色
        var TdColor='';
        var TdName = 'tableData';
        if(this.values.range>0)
            TdColor = 'red';
        else if(this.values.range<0)
            TdColor = 'green';
        for(var i = 1;i<=10;i++)
        {
            eval('tableData' + i+'.style.color = TdColor');
        }
    } else {
        tableData10.innerHTML = '';
    }
    tableRow.appendChild(tableData10);

    var tableData11 = document.createElement('td');
    tableData11.innerHTML = '<a href="company.php?code='+ this.values.code +'">详情</a>';
    tableRow.appendChild(tableData11);

    return tableRow;
};


// 公司列表 - 空列表项
stock.companyList.emptyItem = Class.create(earth.controls.list.item);

stock.companyList.emptyItem.prototype.buildElement = function() {

    earth.util.setCookie('requestState','Finish',30);
    alert(earth.util.getCookie('requestState'));

    var tableRow = document.createElement('tr');

    var tableData = document.createElement('td');
    tableData.innerHTML = "暂无上市公司！";
    tableData.colSpan = "11";

    tableRow.appendChild(tableData);

    return tableRow;
};

// 公司列表 - 结束




// 公司管理
stock.manageCompany = Class.create();

stock.manageCompany.prototype.initialize = function(htmlId){
    this.htmlId = htmlId;

    var list = new stock.manageCompany.list(htmlId + '_List', null, new stock.manageCompany.listBuilder());
    this.addButton = new stock.manageCompany.addButton("ManageCompany", list);
};


// 公司管理 - 列表
stock.manageCompany.list = Class.create(earth.controls.list, {
    initialize:function($super, htmlId, loader, builder) {
        $super(htmlId, loader, builder);
    }
    , append:function($super, item) {
        $super(item);

        var classInstance = this;

        // 添加修改动作
        if(item.attachEditClickEvent) {
            var editBox = new stock.manageCompany.editBox(classInstance.htmlId);
            editBox.setValue(item);
            editBox.show();
        }

        // 添加移除动作
        if(item.attachRemoveClickEvent) {
            classInstance.remove(item);
        }
    }

});


// 公司管理 - 构造器
stock.manageCompany.listBuilder = Class.create(earth.controls.list.builder, {
    initialize:function($super, list){
        $super();
        this.list = list;
    }
});



// 公司管理 - 构造器 - 构造元素
stock.manageCompany.listBuilder.prototype.build = function(values) {
    if(values) {
        return new stock.manageCompany.listItem(values, this.list);
    } else {
        return new stock.manageCompany.emptyListItem(values, this.list);
    }
} // stock.manageCompany.listBuilder.prototype.build



// 公司管理 - 列表项
stock.manageCompany.listItem = Class.create(earth.controls.list.item, {
    initialize:function($super, values, list){
        $super(values);
        this.list = list;
    }
});

// 公司管理 - 列表项 - 构造HTML元素
stock.manageCompany.listItem.prototype.buildElement = function() {

    var innerHtmlTd1 = '<div id="'+ this.htmlId + '_CodeValue">' + this.values.code + '</div>';
    innerHtmlTd1 += '<input id="'+ this.htmlId +'_CodeInput" type="hidden" name="companies['+ this.htmlId +'][code]" value="'+ this.values.code +'" />';

    var innerHtmlTd2 = '<div id="'+ this.htmlId + '_NameValue">' + this.values.name + '</div>';
    innerHtmlTd2 += '<input id="'+ this.htmlId +'_NameInput" type="hidden" name="companies['+ this.htmlId +'][name]" value="'+ this.values.name +'" />';

    var innerHtmlTd3 = '<div id="'+ this.htmlId + '_PriceValue">' + this.values.closing_price + '</div>';
    innerHtmlTd3 += '<input id="'+ this.htmlId +'_PriceInput" type="hidden" name="companies['+ this.htmlId +'][closing_price]" value="'+ this.values.closing_price +'" />';

    var innerHtmlTd4 = '<a id="'+ this.htmlId +'_EditButton">修改</a> | <a id="'+ this.htmlId +'_RemoveButton">删除</a>';

    var tableRow = document.createElement('tr');

    var tableData1 = document.createElement('td');
    tableData1.innerHTML = innerHtmlTd1;

    var tableData2 = document.createElement('td');
    tableData2.innerHTML = innerHtmlTd2;

    var tableData3 = document.createElement('td');
    tableData3.innerHTML = innerHtmlTd3;

    var tableData4 = document.createElement('td');
    tableData4.innerHTML = innerHtmlTd4;

    tableRow.appendChild(tableData1);
    tableRow.appendChild(tableData2);
    tableRow.appendChild(tableData3);
    tableRow.appendChild(tableData4);

    return tableRow;
} // stock.manageCompany.listItem.prototype.buildElement


// 公司管理 - 列表项 - 初始化前相关处理
stock.manageCompany.listItem.prototype.onInitializing = function() {
    this.eidtClickEventQueue = new Array();
    this.removeClickEventQueue = new Array();
    return false;
};


// 公司管理 - 列表项 - 初始化后相关处理
stock.manageCompany.listItem.prototype.onInitialized = function() {
    var editButtonId = this.htmlId + '_EditButton';
    var removeButtonId = this.htmlId + '_RemoveButton';
    var classInstance = this;

    // 如果修改按钮已创建，挂接动作
    var editButton = $(editButtonId);
    if(editButton) {
        editButton.observe("click", editButtonClickEvent);
    }

    // 如果删除按钮以创建，挂接动作
    var removeButton = $(removeButtonId);
    if(removeButton) {
        removeButton.observe("click", removeButtonClickEvent);
    }

    // 修改事件
    function editButtonClickEvent(event) {

        var listId = classInstance.list.htmlId;
        var htmlId = listId.replace(/_List/, '');

        var editBox = new stock.manageCompany.editBox(htmlId, classInstance.list, classInstance.htmlId);
        editBox.show();

        /*
        for(var i=0; i<classInstance.editClickEventQueue.length; i++) {
            classInstance.eidtClickEventQueue[i](classInstance);
        }
        */
    }

    // 移除事件
    function removeButtonClickEvent(event) {
        classInstance.list.remove(classInstance);
        /*
        for(var i=0; i<classInstance.removeClickEventQueue.length; i++) {
            classInstance.removeClickEventQueue[i](classInstance);
        }
        */
    }


}; // stock.manageCompany.listItem.prototype.onInitialized



/*
// 公司管理 - 列表项 - 追加修改动作
stock.manageCompany.listItem.prototype.attachEditClickEvent = function(eventHandler) {
    this.editClickEventQueue.push(eventHandler);
};

// 公司管理 - 列表项 - 追加移除动作
stock.manageCompany.listItem.prototype.attachRemoveClickEvent = function(eventHandler) {
    this.removeClickEventQueue.push(eventHandler);
};
*/


// 公司管理 - 空列表项
stock.manageCompany.emptyListItem = Class.create(earth.controls.list.item);

// 公司管理 - 空列表项 - 构造HTML元素
stock.manageCompany.emptyListItem.prototype.buildElement = function() {

    var tableRow = document.createElement('tr');

    var tableData = document.createElement('td');
    tableData.innerHTML = "暂无上市公司，请添加！";
    tableData.colSpan = "4";

    tableRow.appendChild(tableData);

    return tableRow;
}; // stock.manageCompany.listItem.prototype.buildElement





// 公司管理 - 添加公司按钮
stock.manageCompany.addButton = Class.create();

// 公司管理 - 添加公司按钮 - 初始化
stock.manageCompany.addButton.prototype.initialize = function(htmlId, list) {
    this.htmlId = htmlId + '_AddButton';
    this.list = list;
    var classInstance = this;

    // 挂接按钮的onclick事件
    Event.observe($(this.htmlId), "click", addButtonClickEvent);

    // 按钮的onclick事件
    function addButtonClickEvent(event) {
        var editBox = new stock.manageCompany.editBox(htmlId, classInstance.list);
        editBox.show();
    }
}





// 公司管理 - 添加公司对话框
stock.manageCompany.editBox = Class.create(earth.controls.dialog, {
    initialize:function($super, htmlid, list, itemId) {
        this.list = list;
        this.itemId = itemId;
        $super(htmlid);
    }
});

// 公司管理 - 添加公司对话框 - 初始化前相关处理
stock.manageCompany.editBox.prototype.onInitializing = function(){
    this.lockWindow = true;
    this.hideOnClose = false;

    this.submittedEventQueue = new Array();
}; // stock.manageCompany.editBox.prototype.onInitializing


// 公司管理 - 添加公司对话框 - 追加提交后事件
stock.manageCompany.editBox.prototype.attachSubmittedEvent = function(eventHandler) {
    this.submittedEventQueue.push(eventHandler);
};


// 公司管理 - 添加公司对话框 - 构建HTML元素
stock.manageCompany.editBox.prototype.buildDialogElement = function(){

    if($(this.itemId + '_CodeValue')) {
        var codeValue = $(this.itemId + '_CodeValue').innerHTML;
    } else {
        var codeValue = '';
    }

    if($(this.itemId + '_NameValue')) {
        var nameValue = $(this.itemId + '_NameValue').innerHTML;
    } else {
        var nameValue = '';
    }

    if($(this.itemId + '_PriceValue')) {
        var priceValue = $(this.itemId + '_PriceValue').innerHTML;
    } else {
        var priceValue = '';
    }

    var s = '';

    s += '	<div class="BoxDivTitle" style="overflow:hidden;margin-bottom:0px;display:inline-block;width:520px;">';
    s += '		<a href="javascript:void(0);" class="closeDivA" id="' + this.htmlId + '_CloseButton">关闭</a>';
    s += '	</div>';
    s += '	<div class="BoxDivContent">';
    s += '		<p style="color:#555;"></p>';
    s += '		<div style="width:500px;height:120px;overflow-y:auto;border:1px solid #DDD;padding:6px;margin-top:10px;color:#555;">';

    s += '			<table style="border-collapse:collapse;">';
    s += '				<tr>';
    s += '					<td style="padding:5px;">公司代码：</td>';
    s += '					<td style="padding:5px;"><input id="'+ this.htmlId +'_CodeInput" value="'+ codeValue +'" /></td>';
    s += '				</tr>';
    s += '				<tr>';
    s += '					<td style="padding:5px;">公司名称：</td>';
    s += '					<td style="padding:5px;"><input id="'+ this.htmlId +'_NameInput" value="'+ nameValue +'" /></td>';
    s += '				</tr>';
    s += '				<tr>';
    s += '					<td style="padding:5px;">昨收盘价</td>';
    s += '					<td style="padding:5px;"><input id="'+ this.htmlId +'_ClosingPriceInput" value="'+ priceValue +'"/></td>';
    s += '				</tr>';
    s += '				<tr>';
    s += '					<td style="padding:5px;"></td>';
    s += '					<td style="padding:5px;"><button id="'+ this.htmlId +'_SubmitButton" class="Button60">确认</button> <button id="'+ this.htmlId +'_CancelButton" class="Button60" style="margin-left:10px;">取消</button></td>';
    s += '				</tr>';
    s += '			<table>';

    s += '		</div>';
    s += '	</div>';

    var dialogElement = document.createElement('div');
    dialogElement.innerHTML = s;
    dialogElement.style.cssText = 'width:520px;height:265px;-webkit-box-shadow:2px 2px 3px #999;padding:5px 10px;';
    dialogElement.cssClass = 'BoxDiv';
    return dialogElement; 
}; // stock.manageCompany.editBox.prototype.buildDialogElement



// 公司管理 - 添加公司对话框 - 初始化后相关处理
stock.manageCompany.editBox.prototype.onInitialized = function(){
    var classInstance = this;


    $(this.htmlId + '_SubmitButton').onclick = function(){
        submitEvent();
    }// _SubmitButton onclick

    $(this.htmlId + '_CancelButton').onclick = function(){
        classInstance.close();
    }// _DisagreeButton onclick

    var submitEvent = function(){

        //
        var codeInput = $(classInstance.htmlId + '_CodeInput');
        var nameInput = $(classInstance.htmlId + '_NameInput');
        var closingPriceInput = $(classInstance.htmlId + '_ClosingPriceInput');


        /*禁用按钮*/
        $(classInstance.htmlId + '_SubmitButton').disabled = true;

        var code = codeInput.value;
        var name = nameInput.value;
        var closingPrice = closingPriceInput.value;

        var error = '';

        // 检查错误
        if(!code) {
            error += '请填写公司代码！';
        }
        if(!name) {
            error += '请填写公司名称！';
        }
        if(!closingPrice) {
            error += '请填写昨收盘价！';
        }
        if(parseFloat(closingPrice) < 0) {
            error += '昨收盘价不能为负！';
        }

        if(!error) {
            // 如果是修改
            if(classInstance.itemId) {
                var duplicate = false;

                for(var a=0; a<classInstance.list.items.length; a++) {
                    if(classInstance.list.items[a].htmlId != classInstance.itemId) {
                        if($(classInstance.list.items[a].htmlId + '_CodeInput').value == code) {
                            duplicate = true;
                        }
                    }
                }

                if(!duplicate) {
                    $(classInstance.itemId + '_CodeValue').innerHTML = code;
                    $(classInstance.itemId + '_CodeInput').value = code;

                    $(classInstance.itemId + '_NameValue').innerHTML = name;
                    $(classInstance.itemId + '_NameInput').value = name;

                    $(classInstance.itemId + '_PriceValue').innerHTML = closingPrice;
                    $(classInstance.itemId + '_PriceInput').value = closingPrice;

                    classInstance.close();
                } else {
                    alert('该代码已添加！');
                    $(classInstance.htmlId + '_SubmitButton').disabled = false;
                }

                
            } else {

                var duplicate = false;

                for(var a=0; a<classInstance.list.items.length; a++) {
                    if(classInstance.list.items[a].htmlId != classInstance.itemId) {
                        if($(classInstance.list.items[a].htmlId + '_CodeInput').value == code) {
                            duplicate = true;
                        }
                    }
                }

                if(!duplicate) {
                    var v = {
                        "code":code,
                        "name":name,
                        "closing_price":closingPrice
                    };

                    var builder = new stock.manageCompany.listBuilder(classInstance.list);
                    classInstance.list.append(builder.build(v));

                    classInstance.close();
                } else {
                    alert('该代码已添加！');
                    $(classInstance.htmlId + '_SubmitButton').disabled = false;
                }
            }
        } else {
            var dlg = new stock.dialogs.messageBox('MB', error, '3', '240', '100', 1);
            dlg.show();
            $(classInstance.htmlId + '_SubmitButton').disabled = false;
        }
    }

} // addBlogDialog onInitialized


//


//公告实时刷新
stock.notice = Class.create();
stock.notice.prototype.initialize = function(htmlId) {
    this.htmlId = htmlId;
    var classInstance = this;

    setInterval(function(){classInstance.updata();},10000)
};

stock.notice.prototype.updata = function(){
    var url = 'announcement.php?action=getnewest';
    var pars = '';
    var classInstance = this;

    var ajaxObject = new Ajax.Request(
        url,
        {
            method:'post',
            parameters:pars,
            onComplete:function(transport){
                eval("var responseValues = " + transport.responseText + ";");

                if(responseValues.code < 0)
                {
                    var dlg = stock.dialogs.messageBox('MB' ,responseValues.string ,2 ,'240','100',1);
                    dlg.show();
                }
                else
                {
                    if (responseValues.data.title)
                    {
                        var noticeLink = 'announcement.php?action=view&announcement_id=' + responseValues.data.id;
                        var noticeTitle = responseValues.data.title;
                    }
                    else
                    {
                        var noticeLink = 'javascript:void(0);';
                        var noticeTitle = '暂无公告';
                    }
                    $(classInstance.htmlId).innerHTML = '<strong>最新公告：</strong><a href="' + noticeLink + '" target="_blank">' + noticeTitle + '</a>'
                }

            }
        }
    );
}

/* 实时刷新分时图 */
stock.timeLine = function(domId,gameId,processId,companyId){

    setInterval(function(){
        if (processId && processId != 0){
            $(domId).innerHTML = '<img src="./fst/g' + gameId + '/p' + processId + '/s' + companyId + '.png?t=' + Math.round(Math.random() * 1000000) + Math.round(Math.random() * 1000000) + '" alt="" />';
        }else{
            $(domId).innerHTML = '<img src="./images/fst.png" alt="" />';
        }

    },5000);
}
