﻿var lastIndex = 1;
var pagetitle = document.title.toString();
var timeoutId;
var focused = true;
var isnews = false;
var newtitle = pagetitle;
var lines = new Array();
var newscount = 0;
function OnBtnCloseClick() {
    $('#container-bottom-right').fadeOut('slow', function() {
        $("#container-bottom-right").css('display', 'none');
    });
    $("#container-bottom-right .content").html('');
    lines = new Array(); 
    document.title = pagetitle;
}
function HideBoxNotification(_remove) {
    var _content = $("#container-bottom-right .content").html();
    var _i = 0;
    if (parseInt(_remove) == 0) { lines = new Array(); }
    while (_i < parseInt(_remove)) {
        if (lines.length == 0) { break; }
        _i = _i + 1;    
        var l = lines.shift();
        _content = _content.replace(l, '');
    }
    if (lines.length == 0) { _content = ''; }
    $("#container-bottom-right .content").html(_content);
    if (lines.length == 0) {
        $('#container-bottom-right').fadeOut('slow', function() {
            $("#container-bottom-right").css('display', 'none');
        });
    }
}
function BindNotificationList(_add) {
    var _oldcontent = $("#container-bottom-right .content").html(); 
    var _content = '';
//    while (lines.length > 5) {
//        var l = lines.shift();
//        _content = _content.replace(l, '');
    //    }
    var _start = lines.length - 5;
    if (_start < 0) _start = 0;
    for (var _i = _start; _i < lines.length; _i++) {
        //if (lines.length < _i + 1) break;
        var _row = lines[_i];
        //if (_content.indexOf(_row) >= 0) continue;
        _content = _row + _content;
    }
    $("#container-bottom-right .content").html(_content);
    if (_content != '' && _oldcontent != _content) {        
        $("#container-bottom-right").fadeIn(1000, function() { });
    }
}
function BindNotification(list) {
    if (list.length == 0) return;
    var _count = 0;
    var lasttitle = '';
    for (var _i = list.length-1; _i >= 0; _i--) {
        var _link = list[_i].NewsUrl;
        var _img = ""
        if (_link.indexOf('http://biz.cafef.vn') >= 0) {
            _img = ' <img src="http://cafef3.vcmedia.vn/v2/images/icon_biz.png" alt="" align="top">';
        } else if (_link.indexOf('http://land.cafef.vn') >= 0) {
            _img = ' <img src="http://cafef3.vcmedia.vn/v2/images/icon_land.png" alt="" align="top">';
        }
        _count = _count + 1;
        var _title = list[_i].News_Title;
        var _content = '<p><img src="http://cafef3.vcmedia.vn/v2/images/v2/iconli.png" alt="" align="absmiddle" /> <a href="' + _link + '" title="' + _title + '">' + _title + '</a>' + _img + '</p>';
        lines.push(_content);
        lasttitle = _title;
    }
    if (lasttitle != '') {
        newtitle = lasttitle;
        if (!focused) {
            document.title = lasttitle;
            setTimeout('changetitle(0);', 1000);
        }
    }
    if (focused) {
        BindNotificationList(_count);
        setTimeout("HideBoxNotification("+_count+");", 30000);
    } else {
        newscount = newscount + _count;
    }
}
function changetitle(_i) {
    if (parseInt(_i) >= 10) { document.title = pagetitle; return; } 
    document.title = document.title == pagetitle ? newtitle : pagetitle;
    _i = _i + 1;
    setTimeout('changetitle('+_i+');', 1000);
}
$(document).ready(function() {
    $(window).focus(function() {
        BindNotificationList(newscount);
        setTimeout("HideBoxNotification(" + newscount + ");", 30000);
        focused = true;
    });
    $(window).blur(function() { focused = false; newscount = 0; });
});
