﻿var datetimeNow = new Date();
var _Month = datetimeNow.getMonth() + 1;
var _Year = datetimeNow.getFullYear();

var now = new Date();
var year = now.getYear();
var month = now.getMonth() + 1;
var date = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var time = year.toString() + month.toString() + date.toString() + hours.toString() + minutes.toString() + seconds.toString();
var AriticleID = ($.cookie('articleID') == null) ? '-1' : $.cookie('articleID');
//var UrlDetail =($.cookie('urlTinTucDetail') == null) ? '-1' : $.cookie('urlTinTucDetail'),
AGBB.App = $.extend({
    AriticleID: ($.cookie('articleID') == null) ? '-1' : $.cookie('articleID'),
    UrlDetail: ($.cookie('urlTinTucDetail') == null) ? '-1' : $.cookie('urlTinTucDetail'),
    Domain: 'Files',
    layout: 'TSS',
    path: '/DesktopModules/AG.TinTuc',
    skin: 'TSS',
    version: '1.0',
    modules: {},
    currentPage: false,
    maxModuleId: 0,
    currentModule: null,
    configPath: '.xml',
    option: {},
    tabid: '67',
    time: function() {
        var now = new Date();
        var year = now.getYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();
        var time = year.toString() + month.toString() + date.toString() + hours.toString() + minutes.toString() + seconds.toString();
        return time;
    },
    init: function() {
        if (typeof (AGBB.allLinks) == 'undefined') {
            AGBB.allLinks = {};
            var loader = jQuery('<div id="loader"><img src="' + '/Portals/0/Images/Icon/loading.gif" alt="loading..." /></div>')
				        .css({ position: "absolute", top: "1em", left: "1em" })
				        .appendTo("#ContentAGBB")
				        .hide();
            jQuery().ajaxStart(function() {
                loader.show();
            }).ajaxStop(function() {
                loader.hide();
            }).ajaxError(function(a, b, e) {
                //throw e;
            });
        };

        AGBB.App.setcookie();

    },
    loadPage: function() {
        //   alert("2");
        try {
            AGBB.App.configPath = AGBB.App.page + '.xml';

            $.extend(AGBB.App, AGBB.xml(AGBB.App.path + '/AG.TinTuc/App/Page/' + AGBB.App.page + '.xml', 'page'));
           // AGBB.App.regions = null;
            AGBB.loadData('ContentAGBB', AGBB.App.path + '/AG.TinTuc/App/layout/' + this.layout + '.htm');
            //    alert("page" + AGBB.App.page);
            $.each(AGBB.App.regions, function(i, item) {
                if (item.name != null && item.name != '')
                  //  alert("Page" + AGBB.App.page + "Path:" + AGBB.App.path + '/App/' + item.path + '/' + item.name + '.htm');
                    AGBB.loadData(item.id, AGBB.App.path + '/AG.TinTuc/App/' + item.path + '/' + item.name + '.htm');

            });

            $.each(AGBB.App.controls, function(i, item) {
                if (item.jsControl != null && item.jsControl != '') {
                    if (item.jsData != null && item.jsData != '') {
                        AGBB.loadjs('includeJs', AGBB.App.path + '/AG.TinTuc/App/region/' + item.path + '/' + item.jsData + '.js', function() { AGBB.loadjs('includeJs', '/AG.TinTuc/App/region/' + item.path + '/' + item.jsControl + '.js', function() { }); });
                    }
                    else {
                        AGBB.loadjs('includeJs', AGBB.App.path + '/AG.TinTuc/App/region/' + item.path + '/' + item.jsControl + '.js', function() { });
                    }


                }
            });

        }
        catch (err) {
        }

    },

    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;
        }
    },
    SubStringEvent: 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 Event = tem.substring(0, pos);
            return Event + '';

        }
        else {
            return current_str;
        }
    },
    DeleteCookie: function(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = GetCookie(name);
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    },
    CheckOdd: function(number) {
        if ((number % 2) == 0) {
            return true;
        }
        else
            return false;
    },
    GetURLParamValue: function(param, d) {
        var _url = document.location.href.toLowerCase();
        var i = _url.indexOf('/' + param + '/');
        //if URL have URLrewrite style
        if (i >= 0) {
            _url = _url.substring(i + param.length + 2, _url.length);
            i = _url.indexOf('/');
            if (i >= 0) { _url = _url.substring(0, i); return _url; }
        } else {
            //If URL have ? style
            i = _url.indexOf(param + '=');
            if (i >= 0) {
                _url = _url.substring(i + param.length + 1, _url.length);
                i = _url.indexOf('&');
                if (i >= 0) _url = _url.substring(0, i);
                return _url;
            }
        }
        return d;
    },
    // Trang
    page: ($.cookie('page') == null) ? '-1' : $.cookie('page'),
    setcookiepage: function(Page) {
        AGBB.App.page = Page;
        $.cookie('page', AGBB.App.page);
        var url = document.location.href.toLowerCase();
        var i = url.indexOf('path');
        if (i == -1) {
            location.hash = '#' + AGBB.App.page + ',,' + AGBB.App.AriticleID + ',,' + AGBB.App.UrlDetail;
        }

    },

    setPage: function(Page) {
        // alert(Page);
        AGBB.App.page = Page;
        $.cookie('page', AGBB.App.page);
        location.hash = '#' + AGBB.App.page + ',,' + AGBB.App.AriticleID + ',,' + AGBB.App.UrlDetail;

    },
    setcookie: function() {
        var temp = location.hash.replace('#', '');
        var arrTTDN = new Array();
        var _tabidd = AGBB.request("tabid");
        if (temp == '') {
            // alert("1");
            switch (_tabidd) {

                case '205':

                    arrTTDN = temp.split(',');
                    //AGBB.App.page = arrTTDN[0];
                    if (arrTTDN[0] == '') {
                        AGBB.App.setcookiepage("TinTuc_ThiTruong_Page");
                    }
                    else {
                        AGBB.App.setcookiepage(AGBB.App.page);
                    }
                    break;
                case '400': AGBB.App.setcookiepage("TinTuc_TuyenDung_Page");
                    break;
                case '158': AGBB.App.setcookiepage("TinTuc_OTC_Page");
                    break;
                case '88': AGBB.App.setcookiepage("TinTuc_CongBoThongTin_Page");
                    break;
                case '91': AGBB.App.setcookiepage("TinTuc_DauGia_Page");
                    break;
                case '139': AGBB.App.setcookiepage("TinTuc_CSC_Page");
                    break;
                case '222': AGBB.App.setcookiepage("TinTuc_FullText");
                    break;
                case '223': AGBB.App.setcookiepage("TinTuc_TimKiemNangCao");
                    break;
                case '129':
                    AGBB.App.setcookiepage("TinTuc_CoPhieuToiQuanTam_Page");
                    break;
                case '138': AGBB.App.setcookiepage("TinTuc_LichSuKien_Page");
                    break;
                case '132': AGBB.App.setcookiepage("TinTuc_KienThucCoBan");
                    break;
                case '162': AGBB.App.setcookiepage("TinTuc_KienThucCoBan");
                    break;
                case '161': AGBB.App.setcookiepage("TinTuc_KienThucCoBan");
                    break;
                case '160': AGBB.App.setcookiepage("TinTuc_KienThucCoBan");
                    break;
                case '81': AGBB.App.setcookiepage("TinTuc_KienThucCoBan");
                    break;
                case '224': AGBB.App.setcookiepage("TinTuc_BangGiaOTC_Page");
                    break;
                case '144': AGBB.App.setcookiepage("GioiThieu_ThuVienAnh");
                    break;
                case '371': AGBB.App.setcookiepage("TinTuc_ThiTruong_Page");
                    break;
                case '372': AGBB.App.setcookiepage("TinTuc_ChungKhoanTheGioi_Page");
                    break;
                case '373': AGBB.App.setcookiepage("TinTuc_KinhTe_Page");
                    break;
                case '374': AGBB.App.setcookiepage("TinTuc_TaiChinhTienTe_Page");
                    break;
                case '375': AGBB.App.setcookiepage("TinTuc_BatDongSan_Page");
                    break;
                case '376': AGBB.App.setcookiepage("TinTuc_DocNhieuNhat_Page");
                    break;
                case '377': AGBB.App.setcookiepage("TinTuc_CSC_ThongBao_Page");
                    break;
                case '378': AGBB.App.setcookiepage("TinTuc_CSC_CBTT_Page");
                    break;
                case '379': AGBB.App.setcookiepage("TinTuc_CSC_YKienChuyenGia_Page");
                    break;
                default:
                    AGBB.App.setcookiepage("TinTuc_ThiTruong_Page");
                    break;
            };
            //  alert("2");
        }
        else {
            AGBB.App.pageDetail();
        }

    },
    pageDetail: function() {
        var url = location.hash.replace('#', '');

        var arrTT = new Array();
        arrTT = url.split(',,');

        if (url != '') {
            var arrTT = new Array();
            arrTT = url.split(',');

            if (arrTT[2] != '' && arrTT[1] != '' && arrTT[1] != '-1' && arrTT[2] != '-1' && arrTT[0] != '' && arrTT[0] != '-1') {
                // alert("1 : " + arrTT[0] + "2: " + arrTT[1] + "3 : " + arrTT[2]);
                AGBB.App.page = arrTT[0]; $.cookie('page', "TinTuc_Detail");
                AGBB.App.AriticleID = arrTT[1]; $.cookie('articleID', AGBB.App.AriticleID);
                AGBB.App.UrlDetail = arrTT[2]; $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);
                AGBB.App.loadPage();
            }
        }
    },
    GetCategory: function() {
    var _tabidd = AGBB.request("tabid");
        //  alert(_page);
    switch (_tabidd) {
            case '377': return "75";
                break;
            case '379': return "79";
                break;
            case '378': return "78";
                break;
            case '371': return "41";
                break;
            case '372': return "42";
                break;
            case '373': return "87";
                break;
            case '374': return "50";
                break;
            case '375': return "86";
                break;
            case '376': return "41,87,50,86";
                break;
            case '158': return "47";
                break;
            case '91': return "83,85";
                break;
            case '88': return "46,80,81";
                break;
                 case '400': return "39";
                break;
            default:
                return "41,42,43,44,45,46,49,82"; //"tin trong nuoc"
                break;
        }
    },
    // Ngôn ngữ
    language: ($.cookie('language') == 'en') ? 'en' : 'vi',
    // Mã chứng khoán
    symbol: ($.cookie('symbol') == null) ? '-1' : $.cookie('symbol'),
    getSymbol: function() {
        if (AGBB.App.page == "-1") {
            AGBB.App.symbol = "ABT";

            AGBB.App.setTab();
        };

    },

    setSymbol: function(Symbol) {
        AGBB.App.symbol = Symbol;
        $.cookie('symbol', AGBB.App.symbol);
    },

    urlTinTucDetail: ($.cookie('urlTinTucDetail') == null) ? '-1' : $.cookie('urlTinTucDetail'),
    // $.cookie('urlTinTucDetail', urldetail);
    setCookieUrlTinTucDetail: function(UrlDetail) {
        AGBB.App.UrlDetail = UrlDetail;
        $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);
    },

    setPageDetail: function(Page, UrlDetail, Articleid) {
        AGBB.App.page = Page;
        $.cookie('page', AGBB.App.page);

        AGBB.App.AriticleID = Articleid;
        $.cookie('articleID', AGBB.App.AriticleID);

        AGBB.App.UrlDetail = UrlDetail;
        $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);

        category = AGBB.App.GetCategory();

        var url = document.location.href.toLowerCase();
        var i = url.indexOf('path');
        if (i == -1) {
            location.hash = '#' + AGBB.App.page + ',,' + AGBB.App.AriticleID + ',,' + AGBB.App.UrlDetail;
        }

        //AGBB.App.loadPage();

        //AGBB.App.TinTucDetail.LoadDatas(temp);

    },
 goToDetail1: function(uRL, articleID, UrlDetail) {
 // alert("@");
 var URL = "http://" + location.host + "\/" + uRL + "#TinTuc_Detail,," + articleID + ",," + UrlDetail;
 document.location.href = URL;
 }, 
    goToDetail: function(uRL, articleID, UrlDetail) {
        // alert(articleID);
        AGBB.App.page = "TinTuc_Detail";
        $.cookie('page', AGBB.App.page);

        AGBB.App.AriticleID = articleID;
        $.cookie('articleID', AGBB.App.AriticleID);

        AGBB.App.UrlDetail = UrlDetail;
        $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);
        //        var URL = "http://" + location.host + "\/" + uRL;
        //        document.location.href = URL;



        var URL = "http://" + location.host + "\/" + uRL + "?articleID=" + articleID + "&path=" + UrlDetail;
        //   alert(URL);
        document.location.href = URL; //uRL + "?articleid=" + articleID + "&path=" + UrlDetail;
        AGBB.App.setPageDetail('TinTuc_DauGiaCoPhan_Detail', UrlDetail);
        // document.location.href
    },
        goToDetailCPTQT: function(uRL, articleID, UrlDetail) {
        // alert(articleID);
        AGBB.App.page = "TinTuc_CPTQTDetail";
        $.cookie('page', AGBB.App.page);

        AGBB.App.AriticleID = articleID;
        $.cookie('articleID', AGBB.App.AriticleID);

        AGBB.App.UrlDetail = UrlDetail;
        $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);
        //        var URL = "http://" + location.host + "\/" + uRL;
        //        document.location.href = URL;



        var URL = "http://" + location.host + "\/" + uRL + "?articleID=" + articleID + "&path=" + UrlDetail;
        //   alert(URL);
        document.location.href = URL; //uRL + "?articleid=" + articleID + "&path=" + UrlDetail;
        AGBB.App.setPageDetail('TinTuc_CPTQTDetail', UrlDetail);
        // document.location.href
    },
    
    goToCoPhieuQuanTamDetail: function(page, articleID, UrlDetail) {
        // alert(articleID);
        AGBB.App.page = page;
        $.cookie('page', AGBB.App.page);

        AGBB.App.AriticleID = articleID;
        $.cookie('articleID', AGBB.App.AriticleID);

        AGBB.App.UrlDetail = UrlDetail;
        $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);
        var URL = "http://" + location.host + "\/" + "tabid/129/default.aspx#" + page + ',,' + articleID + ',,' + UrlDetail;
        document.location.href = URL; //uRL + "?articleid=" + articleID + "&path=" + UrlDetail;

    },
    //Current Page
    currentPage: ($.cookie('currentPage') == null) ? '1' : $.cookie('currentPage'),
    setCurrentPageDoiTac: function(CurrentPage) {
        AGBB.App.currentPage = CurrentPage;
        $.cookie('currentPage', AGBB.App.currentPage);
        //  AGBB.App.hose.CallBackData(CurrentPage);
        AGBB.App.IndexDoiTac.UpdateCSS(CurrentPage);
        try {
            AGBB.App.DoiTac.GetData("GioiThieu_DoiTac", "/ArticleFiles/Portal0/Gioi-thieu/Doi-tac/Doi-tac.xml", CurrentPage);
        }
        catch (err) {
        }

        AGBB.App.IndexDoiTac.SetTextIndex(CurrentPage);
        AGBB.App.IndexDoiTac.SetIndex(CurrentPage);
        // location.reload();
    },
    setCurrentPage: function(CurrentPage) {

        AGBB.App.Index.currentPage = CurrentPage;
        // $.cookie('currentPage', AGBB.App.Index.currentPage, { path: '/', expires: 2 });
        $.cookie('currentPage', AGBB.App.Index.currentPage);
        // alert(AGBB.App.page);
        AGBB.App.Index.UpdateCSS(CurrentPage);
        switch (AGBB.App.page) {
            case "CoDongKLS_TinHoatDong_Page":
                AGBB.App.Page.Paging(CurrentPage);
                break;
            case "TinTuc_TimKiemNangCao":
                AGBB.App.SearchAdvanced.LoadData(CurrentPage);
                break;
            case "TinTuc_DauGiaCoPhan":
                AGBB.App.SearchNguon.SearchSource(CurrentPage);
                break;
            case "TinTuc_FullText":
                AGBB.KetQuaTimKiemFullText.LoadDataFullText(CurrentPage);
                break;
            case "TinTuc_CongBoThongTin_Page":
                AGBB.App.TinTucCongBoThongTinSearch.TinTucCongBoThongTinSearchMaChungKhoan(CurrentPage);
                break;
            case "TinTuc_LichSuKien_Page":
                AGBB.App.TinTucLichSuKienSearch.TinTucLichSuKienSearchMaChungKhoan(CurrentPage);
                break;
            case "TinTuc_LichSuKien_Page":
                AGBB.App.TinTucLichSuKienSearch.TinTucLichSuKienSearchMaChungKhoan(CurrentPage);
                break;
            case 'TinTuc_ChungKhoanTheGioi_Page':
                AGBB.App.SearchByDate.SearchByPageNumber(CurrentPage);
                break;
            case 'TinTuc_KinhTe_Page': AGBB.App.SearchByDate.SearchByPageNumber(CurrentPage);
                break;
            case 'TinTuc_TaiChinhTienTe_Page': AGBB.App.SearchByDate.SearchByPageNumber(CurrentPage);
                break;
            case 'TinTuc_BatDongSan_Page': AGBB.App.SearchByDate.SearchByPageNumber(CurrentPage);
                break;
            case 'TinTuc_DocNhieuNhat_Page': AGBB.App.SearchByDate.SearchByPageNumber1(CurrentPage,'Count');
                break;
                 case 'TinTuc_TuyenDung_Page': AGBB.App.SearchByDate.SearchByPageNumber(CurrentPage);
                break;
            case 'AD_Page': AGBB.App.TinNoiBat.FillDatas(CurrentPage);
                break;

            default:
                //alert("1");
                AGBB.App.SearchByDate.SearchByPageNumber(CurrentPage)
                break;
        }

        AGBB.App.Index.SetTextIndex(CurrentPage);
        AGBB.App.Index.SetIndex(CurrentPage);
        // location.reload();
    },
    setPaging: function(CurrentPage) {
        AGBB.App.Paging.currentPage = CurrentPage;
        // $.cookie('currentPage', AGBB.App.Index.currentPage, { path: '/', expires: 2 });
        $.cookie('currentPage', AGBB.App.Paging.currentPage);
        // alert(AGBB.App.page);
        AGBB.App.Paging.UpdateCSS(CurrentPage);
        AGBB.App.TinLienQuan.FillDatas(CurrentPage)
        AGBB.App.Paging.SetTextPaging(CurrentPage);
        AGBB.App.Paging.SetPaging(CurrentPage);
        // location.reload();
    },
    // Page number
    pageNumber: ($.cookie('pageNumber') == null) ? '0' : $.cookie('pageNumber'),
    getPageNumber: function() {
        AGBB.App.pageNumber = 'pageNumber';
    },
    setPageNumber: function(PageNumber) {
        AGBB.App.pageNumber = PageNumber;
        $.cookie('pageNumber', AGBB.App.pageNumber);
        // AGBB.App.ChangeData();
    },
    ConvertStringToDayMonthYear: function(strDate) {
        //  alert(strDate);
        var date = new Date(strDate);
        var month;
        if (date.getMonth() == 0) {
            month = "01";
        }
        else {

            month = date.getMonth() + 1;
            if (month <= 9) {
                month = "0" + month;
            }
        };
        var day = date.getDate();
        if (day <= 9) {
            day = "0" + day;
        };
        // alert("2");
        return day + '/' + month + '/' + date.getFullYear();
    },
    ConvertStringToHourMinus: function(strDate) {
        var date = new Date(strDate);
        var gio = date.getHours();
        if (gio == 0) {
            gio = "12";
        }
        else {
            if (gio <= 9) {
                gio = "0" + gio;
            }
        };
        var phut = date.getMinutes();
        if (phut <= 9) {
            phut = "0" + phut;
        }
        return gio + ':' + phut;

    }, ConvertVietNameseToEnglish: function(strDate) {
        // alert(strDate);
        var arrDay = strDate.split('/');

        return arrDay[1] + '/' + arrDay[0] + '/' + arrDay[2];
    }


});



AGBB.App.init();

var prevUrlHistory = '';
setInterval('checkUrlHistory()', 1000);
setInterval('checkSymbolFavorite()', 1000);
var previousSymbolFavarite = '';
function checkSymbolFavorite() {
    //previousSymbolFavarite = listSymbolFavorite;
    if (previousSymbolFavarite != listSymbolFavorite) {
        previousSymbolFavarite = listSymbolFavorite;
        AGBB.App.loadPage();
    }
}

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(',,');
            //alert("2tt" + temp + "mang:" + arrTTDN);

            if (arrTTDN[0] != 'htm') {
                AGBB.App.page = arrTTDN[0];
                $.cookie('page', AGBB.App.page);
            }
            AGBB.App.AriticleID = arrTTDN[1];
            $.cookie('articleID', AGBB.App.AriticleID);
            AGBB.App.UrlDetail = arrTTDN[2];
            $.cookie('urlTinTucDetail', AGBB.App.UrlDetail);
            var _tabidd = AGBB.request("tabid");
            if (_tabidd == '67') {

                $.cookie('articleID', '');
                $.cookie('urlTinTucDetail', '');
            }
            if (arrTTDN[0] == 'TinTuc_Detail') {
                AGBB.App.page = 'TinTuc_Detail';
                $.cookie('page', AGBB.App.page);

            }
            AGBB.App.loadPage();
            var _Page = AGBB.App.page;

            switch (_Page) {
                case 'TinTuc_ChungKhoanTheGioi_Page':

                    tabmain(2, 9);
                    break;
                case 'TinTuc_KinhTe_Page':
                    tabmain(3, 9);
                    break;
                case 'TinTuc_TaiChinhTienTe_Page':
                    tabmain(4, 9);
                    break;
                case 'TinTuc_BatDongSan_Page':
                    tabmain(5, 9);
                    break;
                case 'TinTuc_DocNhieuNhat_Page':
                    tabmain(6, 9)
                    break;
                default:
                    break;
            }
        } 
    }

};





