﻿var prevUrlHistory = '';
var hostLinkData = 'http://' + location.host + '/LiveChart/';
var prevPath = '';

AGBB.ThongTinDoanhNghiep = $.extend
({
    //Mã CT
    idThongTinDoanhNghiep: '116',
    idCongTyNiemYet: '101',
    idCongTyChuaNiemYet: '102',
    idCongTyUpcom: '382',
    idTraiPhieu: '103',
    PageTongQuan: 'TongQuan',
    PageThongKeCoBan: 'ThongKeCoBan',
    PageHoSoDoanhNghiep: 'HoSoDoanhNghiep',
    PageTQuyenSoHuu: 'QuyenSoHuu',
    PageBaoCaoTaiChinh: 'BaoCaoTaiChinh',
    PageTruyVanGia: 'TruyVanGia',
    statusMarket: '',


    symbol: ($.cookie('symbol') == null) ? '-1' : $.cookie('symbol'),
    setSymbol: function(Symbol) {
        Symbol = Symbol.toUpperCase();
        AGBB.ThongTinDoanhNghiep.symbol = Symbol;
        $.cookie('symbol', AGBB.ThongTinDoanhNghiep.symbol);
        AGBB.ThongTinDoanhNghiep.setPage(AGBB.ThongTinDoanhNghiep.Template + '_Default');
    },
    //Template
    Template: ($.cookie('Template') == null) ? '-1' : $.cookie('Template'),
    setTemplate: function(template) {
        AGBB.ThongTinDoanhNghiep.Template = template;
        $.cookie('Template', AGBB.ThongTinDoanhNghiep.Template);
    },
    //Trang
    page: ($.cookie('page') == null) ? '-1' : $.cookie('page'),
    setPage: function(Page) {
        AGBB.ThongTinDoanhNghiep.page = Page;
        $.cookie('page', AGBB.ThongTinDoanhNghiep.page);
        location.hash = '#' + AGBB.ThongTinDoanhNghiep.symbol + ',' + Page;
        if (prevUrlHistory == '') prevUrlHistory = '#';
    },

    reloadPage: function(Page) {
        AGBB.ThongTinDoanhNghiep.page = Page;
        $.cookie('page', AGBB.ThongTinDoanhNghiep.page);
        var tabid = AGBB.request('tabid');
        strUrl = location.host;
        strUrl = 'http://' + strUrl + '/tabid/' + tabid + '/Symbol/' + AGBB.ThongTinDoanhNghiep.symbol + '/Page/' + AGBB.ThongTinDoanhNghiep.page + '/default.aspx';
        document.location.href = strUrl;

    },

    run: function() {
        $.get('/DesktopModules/AG.StockThongKe/Backend/AG_Flag_ALL_Floor.ashx', function(result) {
            var arrStatus = new Array(3);
            arrStatus = result.split(',');
            AGBB.ThongTinDoanhNghiep.statusMarketHO = arrStatus[0];
            AGBB.ThongTinDoanhNghiep.statusMarketHA = arrStatus[1];
            AGBB.ThongTinDoanhNghiep.statusMarketUPCOM = arrStatus[2];
        })

        var hashPage = location.hash;
        var strId = AGBB.request('tabid');
        if (strId == AGBB.ThongTinDoanhNghiep.idCongTyNiemYet || strId == AGBB.ThongTinDoanhNghiep.idThongTinDoanhNghiep)
            AGBB.ThongTinDoanhNghiep.setTemplate('NiemYet');
        if (strId == AGBB.ThongTinDoanhNghiep.idCongTyChuaNiemYet)
            AGBB.ThongTinDoanhNghiep.setTemplate('ChuaNiemYet');
        if (strId == AGBB.ThongTinDoanhNghiep.idTraiPhieu)
            AGBB.ThongTinDoanhNghiep.setTemplate('TraiPhieu');
        if (strId == AGBB.ThongTinDoanhNghiep.idCongTyUpcom)
            AGBB.ThongTinDoanhNghiep.setTemplate('Upcom');
        if (hashPage == '' || hashPage == '#') {
            var strSymbol = AGBB.request('Symbol');
            var strPage = AGBB.request('Page');
            if (strPage.indexOf(AGBB.ThongTinDoanhNghiep.Template) == -1 || strPage.indexOf('_') == -1) {
                strPage = '';
            }
            if (strSymbol == '') {

                AGBB.ThongTinDoanhNghiep.setPage('CongTy' + AGBB.ThongTinDoanhNghiep.Template);
            }
            else {
                if (strPage == '' || strPage == 'null')
                    AGBB.ThongTinDoanhNghiep.setSymbol(strSymbol);
                else {
                    AGBB.ThongTinDoanhNghiep.symbol = strSymbol;
                    $.cookie('symbol', AGBB.ThongTinDoanhNghiep.symbol);
                    AGBB.ThongTinDoanhNghiep.setPage(strPage);
                }
            }
        }
        else {
            hashPage = hashPage.replace('#', '');
            var arrTTDN = new Array();
            arrTTDN = hashPage.split(',');
            AGBB.ThongTinDoanhNghiep.symbol = arrTTDN[0];
            $.cookie('symbol', AGBB.ThongTinDoanhNghiep.symbol);
            AGBB.ThongTinDoanhNghiep.setPage(arrTTDN[1]);
            $.cookie('page', AGBB.ThongTinDoanhNghiep.page);
        }
    },
    loadPage: function() {
    $.extend(AGBB.ThongTinDoanhNghiep, AGBB.xml('/DesktopModules/AG.ThongTinDoanhNghiep/Page/' + AGBB.ThongTinDoanhNghiep.page + '.xml', 'page'));
        var data = AGBB.xml;
        AGBB.loadData('CongTyNiemYetND', '/DesktopModules/AG.ThongTinDoanhNghiep/layout/' + AGBB.ThongTinDoanhNghiep.page + '.htm');
        $.each(AGBB.ThongTinDoanhNghiep.regions, function(i, item) {
            if (item.name != null && item.name != '')
                AGBB.loadData(item.id, '/DesktopModules/AG.ThongTinDoanhNghiep/region/' + item.path + '/' + item.name + '.htm');
        });
        $.each(AGBB.ThongTinDoanhNghiep.controls, function(i, item) {
            if (item.jsControl != null && item.jsControl != '') {
                if (item.jsData != null && item.jsData != '') {
                    AGBB.loadjs('includeJs', '/DesktopModules/AG.ThongTinDoanhNghiep/region/' + item.path + '/' + item.jsData + '.js', function() { AGBB.loadjs('includeJs', '/DesktopModules/AG.ThongTinDoanhNghiep/region/' + item.path + '/' + item.jsControl + '.js', function() { }); });
                }
                else {
                    AGBB.loadjs('includeJs', '/DesktopModules/AG.ThongTinDoanhNghiep/region/' + item.path + '/' + item.jsControl + '.js', function() { });
                }
            }
        });
    },
    /*************************************************************************************************/
    SubString: function(current_str, position) {
        var cL = current_str.length;

        var tem;
        //neu nho hon
        if (position < cL) {
            tem = current_str.substring(0, position);
            var pos = tem.lastIndexOf(' ');

            var kaka = tem.substring(0, pos);
            return kaka;

        }
        else {
            return current_str;
        }
    },
    UrlFilePathDetail: ($.cookie('UrlFilePathDetail') == null) ? '-1' : $.cookie('UrlFilePathDetail'),
    setUrlDetail: function(UrlFilePathDetail) {
        AGBB.ThongTinDoanhNghiep.UrlFilePathDetail = UrlFilePathDetail;
        $.cookie("UrlFilePathDetail", AGBB.ThongTinDoanhNghiep.UrlFilePathDetail);
    },
    setPageDetail: function(Page, Url) {
        AGBB.ThongTinDoanhNghiep.setPage(Page);
        AGBB.ThongTinDoanhNghiep.setUrlDetail(Url);
        AGBB.ThongTinDoanhNghiep.loadPage();
        AGBB.TinTuc_DoanhNghiep.LoadTinTucDoanhNghiepHander(1);


    },
    setDetail: function(UrlFilePath) {
        prevPath = UrlFilePath;
        AGBB.ThongTinDoanhNghiep.setUrlDetail(UrlFilePath);

        AGBB.ThongTinDoanhNghiep.UrlFilePathDetail = UrlFilePath;
        $.cookie("UrlFilePathDetail", AGBB.ThongTinDoanhNghiep.UrlFilePathDetail);
        AGBB.TinTuc_DoanhNghiep.LoadToTinTucDoanhNghiepDetail();

    },
    back: function() {
        AGBB.ThongTinDoanhNghiep.setDetail(prevPath);
    },
    setCurrentPage: function(CurrentPage) {
        AGBB.Paging.currentPage = CurrentPage;
        $.cookie('currentPage', AGBB.Paging.currentPage);
        AGBB.Paging.UpdateCSS(CurrentPage);
        if (AGBB.ThongTinDoanhNghiep.page == "NiemYet_Default") {
            AGBB.TinTuc_TongQuan.LoadTinTucTongQuan(CurrentPage);
        }
        else {
            AGBB.TinTuc_DoanhNghiep.LoadTinTucDoanhNghiepHander(CurrentPage);
            try {
                AGBB.ThongTinDoanhNghiep.setUrlDetail(AGBB.ThongTinDoanhNghiep.UrlFilePathDetail);
                AGBB.TinTuc_DoanhNghiep.LoadToTinTucDoanhNghiepDetail();
            }
            catch (Error) {
            }
        }


        AGBB.Paging.SetTextIndex(CurrentPage);
        AGBB.Paging.SetIndex(CurrentPage);
        // location.reload();
    },

    LoadTTDNById: function(Id) {
        var Url = '/DesktopModules/AG.ThongTinDoanhNghiep/region/DoanhNghiep/GetPath.ashx?Id=' + Id;
        $.get(Url, function(Result) {
            urlFilePath = $(Result).find('Data').text();
            AGBB.ThongTinDoanhNghiep.setPageDetail('NiemYet_TinTucDoanhNghiep', urlFilePath);
        })
    }
});

AGBB.ThongTinDoanhNghiep.run();

setInterval('checkUrlHistory()', 1000);

function checkUrlHistory() {
    var temp = location.hash.replace('#', '');
    if (temp == '' && prevUrlHistory != '') {
        history.go(-1);
        return false;
    }
    if (prevUrlHistory == '') prevUrlHistory = temp;
    else if (prevUrlHistory != temp) {
        prevUrlHistory = temp;
        var arrTTDN = new Array();
        arrTTDN = temp.split(',');
        AGBB.ThongTinDoanhNghiep.symbol = arrTTDN[0];
        AGBB.ThongTinDoanhNghiep.page = arrTTDN[1];
        $.cookie('symbol', AGBB.ThongTinDoanhNghiep.symbol);
        $.cookie('page', AGBB.ThongTinDoanhNghiep.page);
        AGBB.ThongTinDoanhNghiep.loadPage(AGBB.ThongTinDoanhNghiep.page);
    }
};
//hàm hiển thị biểu đồ theo thời gian
var timeIntervalChart = null;
function displayChartByTime(month) {
    var date = new Date();
    if (document.getElementById('img_NiemYet_BieuDoTongQuan') != null) {
        var strLinkChart = '/LiveChart/Default.aspx?Width=465&Height=300&pColor=FE2C2D&vColor=0000FF&bColor=000000&t=' + month + '&Symbol=' + AGBB.ThongTinDoanhNghiep.symbol;
        document.getElementById('img_NiemYet_BieuDoTongQuan').src = 'http://' + location.host + strLinkChart + '&_=' + date;
    }
}; 

function intervalDisplayChart(month) {
	var date=new Date();
    if (typeof (bRunOff) != 'undefined') { //biến cho biết mã đang trong giờ giao dịch hay ko true:co false:ko
        if (bRunOff) {
            clearTimeout(timeIntervalChart);
            timeIntervalChart = setTimeout("intervalDisplayChart('0')", 20000);
        }
        else {
            clearTimeout(timeIntervalChart);
            timeIntervalChart = setTimeout("intervalDisplayChart('-3')", 300000);
        }
       var imgChart = document.getElementById('img_NiemYet_BieuDoTongQuan');
        if (imgChart != null) {
            var strLinkChart = '/LiveChart/Default.aspx?Width=465&Height=300&pColor=FE2C2D&vColor=0000FF&bColor=000000&t=' + month + '&Symbol=' + AGBB.ThongTinDoanhNghiep.symbol;
            document.getElementById('img_NiemYet_BieuDoTongQuan').src = 'http://' + location.host + strLinkChart+'&_='+date;
            //img.src = strLinkChart;
        }
        else
            clearInterval(timeIntervalChart);
    }
};

function GetLinkChart(month) {
    var urlLink = '/DesktopModules/AG.ThongTinDoanhNghiep/region/Chart/GetLinkChart.ashx?';
    $.ajax({
        type: "GET",
        url: urlLink + "SymbolId=" + AGBB.ThongTinDoanhNghiep.symbol + "&Time=" + month,
        async: false,
        success: function(result) {
            urlLink = result;
        }
    })
}      
