/**
 * 浏览器版本检测程序
 *
 * @author tim.chen <tim.chen@jeharrison.com>
*/

/**
 * JavaScript 写入 Coolies 函数
 * 
 * @param string name   Cookie 要设置的健
 * @param mixed  value  Cookie 要设置的值
 * @param integer hours Cookie 有效期
 * @return void
*/
function setCookie(name, value, hours) {
     var argv = arguments;
     var argc = arguments.length;
     var expires = (argc > 2) ? new Date((new Date).getTime() + (argv[2] + 0) * 1000) : null;
     var path = (argc > 3) ? argv[3] : '/';
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
}

/**
 * JavaScript 读取 Cookie 函数
 *
 * @param string name Cookie 键名
 * @return mixed
*/
function getCookie(name) {
    var cookieValue = "";
    var search = name + "=";
    if(document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    
    return cookieValue;
}
    
/**
 * 获取浏览版本信息
 *
 * @return array 0 浏览标识 1 浏览器名称 2 浏览版本
*/
function getBrowser(){
    var ua = navigator.userAgent.toLowerCase();
    var regex =/(msie|firefox|chrome|opera|version).*?([\d.]+)/;
    var mb = ua.match(regex);    // 用正则读取浏览的名称与版本号
    
    // 如果是 safari 浏览器则换回它的名称
    mb[1] = mb[1].replace(/version/, "safari");
    
    return mb;
}

/**
 * 低版本浏览提示信息
 *
 * @return void
*/
function setBrowseVersionTip(){
    // 如果今天己显示过就不显示提示了
    if(getCookie('show_browse_tip_day') == (new Date).getMonth() + 1){
        return false;
    }
    
    var browse = getBrowser();
    
    // 如果浏览器是IE且版本号低于8.0就显示提示信息
    if('msie' == browse[1] && browse[2] < '8.0'){
        showBrowseVersionLowerTip();
        return false;
    }
    
    // 如果浏览器是FF且版本号低于3.5就显示提示信息
    if('firefox' == browse[1] && browse[2] < '3.5'){
        showBrowseVersionLowerTip();
        return false;
    }
}

/**
 * 显示浏览器版本低的提示
 *
 * @return void
*/
function showBrowseVersionLowerTip(){
    var obj_dom = document.getElementsByTagName('body')[0].getElementsByTagName('*');
    
    // 隐藏所有 Body 标签里的内容
    for(var i = obj_dom.length - 1; i >= 0; i--){
        var tagName = obj_dom[i].tagName.toLowerCase();
        
        // 路过处理 Script 标签
        if('script' == tagName){
            continue;
        }
        
        //obj_dom[i].style.display = 'none';
    }
    
    // 创建提示信息的半透明背景层
    var borwse_tip_element_bg  = document.createElement("DIV"); 
    
    borwse_tip_element_bg.style.position  = 'absolute';
    borwse_tip_element_bg.style.top       = '0px';
    borwse_tip_element_bg.style.left      = '0px';
    borwse_tip_element_bg.style.width     = '100%';
    borwse_tip_element_bg.style.filter    = 'alpha(opacity=' + 25 + ')';
    borwse_tip_element_bg.style.opacity   = 25 / 100;
    borwse_tip_element_bg.style.background= '#000000';
    borwse_tip_element_bg.style.height    = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + 'px';
    borwse_tip_element_bg.id              = 'browse_detection_element_bg';
    borwse_tip_element_bg.name            = 'browse_detection_element_bg';
    borwse_tip_element_bg.setAttribute('id','browse_detection_element_bg');
    
    window.document.body.appendChild(borwse_tip_element_bg);
    
    // 创建提示信息的内容层
    var browse_tip_element_div = document.createElement("DIV"); 
    
    browse_tip_element_div.style.position  = 'absolute';
    browse_tip_element_div.style.top       = '150px';
    browse_tip_element_div.style.left      = '0px';
    browse_tip_element_div.style.width     = '100%';
    browse_tip_element_div.style.textAlign = 'center';
    browse_tip_element_div.style.overflow  = 'hidden';
    browse_tip_element_div.id              = 'browse_detection_element_div';
    browse_tip_element_div.name            = 'browse_detection_element_div';
    browse_tip_element_div.setAttribute('id','browse_detection_element_div');
    
    window.document.body.appendChild(browse_tip_element_div);
    
    // 添加提示信息到提示信息层
    browse_tip_element_div.innerHTML = '<table width="420" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td width="10" height="9" valign="top"><img src="/front/images/table/tt_01.gif" width="10" height="9" alt="" /></td><td height="9" valign="top" background="/front/images/table/tt_02.gif"></td><td width="9" height="9" valign="top"><img src="/front/images/table/tt_03.gif" width="9" height="9" alt="" /></td></tr><tr><td valign="top" background="/front/images/table/tt_04.gif">&nbsp;</td><td align="center" valign="top" background="/front/images/table/tt_05.gif"><table width="100%" border="0" cellspacing="3" cellpadding="0"><tr><td align="center" class="ad_qu" style="font-size:14px;color:#006400;font-weight:bold;line-height:20px;"><img src="/front/images/sicon/warn.gif" width="30" height="30" align="absmiddle" />To get the best experience of using Villable.com, <br />please use Internet Explorer 8 / Firefox 3.</td></tr><tr><td align="center" class="ad_qu" style="font-size:10px;color:#006400;font-weight:bold;line-height:20px;padding:0px 0px;"><a alt="Continue" title="Continue" onclick="hideBrowseVersionLowerTip()" href="#" class="zhuxiao"  style="background:transparent url(/front/images/send_02.gif) no-repeat scroll center 0; width:50px;font-size:12px;line-height:20px;padding:5px 15px;color:#FFFFFF;" >Continue</a></td></tr></table></td><td valign="top" background="/front/images/table/tt_06.gif">&nbsp;</td></tr><tr><td valign="top"><img src="/front/images/table/tt_07.gif" width="10" height="11" alt="" /></td><td height="11" valign="top" background="/front/images/table/tt_08.gif"></td><td valign="top"><img src="/front/images/table/tt_09.gif" width="9" height="11" alt="" /></td></tr></table>';
}

/**
 * 关闭提示层 
 *
 * @return void
*/
function hideBrowseVersionLowerTip(){
    var obj_tip_div = document.getElementById("browse_detection_element_div");
    var obj_tip_bg  = document.getElementById("browse_detection_element_bg");
    
    // 如果存在内容提示层就关闭它, 防止因为意外导致浏览报错误
    if('object' == typeof(obj_tip_div) && obj_tip_div){
        obj_tip_div.style.display = "none";
        obj_tip_div.style.height  = '0px';
    }
    
    // 如果存在内容提示层的背景就关闭它
    if('object' == typeof(obj_tip_bg) && obj_tip_bg){
        obj_tip_bg.style.display = "none";
        obj_tip_bg.style.height  = '0px';
    }
    
    setCookie('show_browse_tip_day', (new Date).getMonth() + 1, 99999999);
}

/**
 * 加载浏览器低版本提示信息
 *
*/
if (window.addEventListener) {
    window.addEventListener("load", setBrowseVersionTip, false)
} else {
    if (window.attachEvent) {
        window.attachEvent("onload", setBrowseVersionTip)
    }
}

