  var getnumcount=0;
  function makeRequestnotify(t,v) {
    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
      }
    }

    if (!http_request) {
      alert('Giving up :( Cannot create an XMLHTTP instance');
      return false;
    }
    // 定義事件處理函數為 alterContents()
    now = new Date();
    if('getnum' == t ) {
      http_request.onreadystatechange = function() { getnum(http_request); };
      http_request.open('GET', '/ajax/notify.php?a=1&time='+now.getTime(), true);
      http_request.send(null);
      if( getnumcount < 60 ) {
        getnumcount++;
      }else{
        clearInterval(run30);
      }
    }
    if('show20' == t ) {
      if( document.getElementById('BH-msgup').style.display == '' ) {
        document.getElementById('BH-msgup').style.display = 'none'
        $('#div_closenotify').get()[0].style.display='none';
      }else{

      if( newnotify == 0 && document.getElementById('BH-msgup').innerHTML != '' ) {
        document.getElementById('BH-msgup').style.display = '';
        $('#BH-msgup2').css('max-height',(document.documentElement.clientHeight-190)+'px');
        $('#div_closenotify').get()[0].style.display='';
      }else{
        http_request.onreadystatechange = function() { show20(http_request); };
        http_request.open('GET', '/ajax/notify.php?a=2&time='+now.getTime(), true);
        http_request.send(null);
      }

      }
    }
    if('onoff' == t ) {
      http_request.onreadystatechange = function() { onoff(http_request); };
      http_request.open('GET', '/ajax/notify.php?a=3&'+v+'&time='+now.getTime(), true);
      http_request.send(null);
    }
  }

  function getnum(http_request) {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        newnotify = parseInt(http_request.responseText);
        document.getElementById('BH-message').innerHTML = '即時通知：<span>'+http_request.responseText+'</span>';
      }
    }
  }
  function show20(http_request) {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        var xmldoc = http_request.responseXML;
        var content = xmldoc.getElementsByTagName('content');
        var mtime = xmldoc.getElementsByTagName('mtime');
        var onoff = xmldoc.getElementsByTagName('onoff');
        var isnew = xmldoc.getElementsByTagName('new');
        var code = xmldoc.getElementsByTagName('code');
        var oo = document.getElementById('BH-msgup');
        var newcount = 0;

        show = '<a href="javascript:makeRequestnotify(\'show20\');"><img src="http://pic.bahamut.com.tw/icon31.gif" border="0"/></a><div id="BH-msgup2" >';
        for(i=0;i<content.length;i++) {
          if( '1' == isnew[i].firstChild.nodeValue ) {
            var tmpc = ' class="msgnew"';
            newcount ++;
          }else{
            var tmpc = '';
          } 

          if( '1' == onoff[i].firstChild.nodeValue ) {
            var imgalt = '停止這則通知';
            var imgtxt = '<a id="onoff'+code[i].firstChild.nodeValue+'" href="javascript:makeRequestnotify(\'onoff\',\'c='+code[i].firstChild.nodeValue+'&t='+onoff[i].firstChild.nodeValue+'\')"><img src="http://pic.bahamut.com.tw/msg-icon'+onoff[i].firstChild.nodeValue+'.gif" alt="'+imgalt+'" title="'+imgalt+'"/></a>'
          }
          if( '0' == onoff[i].firstChild.nodeValue ) {
            var imgalt = '開啟這則通知';
            var imgtxt = '<a id="onoff'+code[i].firstChild.nodeValue+'" href="javascript:makeRequestnotify(\'onoff\',\'c='+code[i].firstChild.nodeValue+'&t='+onoff[i].firstChild.nodeValue+'\')"><img src="http://pic.bahamut.com.tw/msg-icon'+onoff[i].firstChild.nodeValue+'.gif" alt="'+imgalt+'" title="'+imgalt+'"/></a>'
          }

          if( '2' == onoff[i].firstChild.nodeValue ) {
            var imgtxt = '';
          }

	  show = show + '<p'+tmpc+'>'+content[i].firstChild.nodeValue+'<span>'+imgtxt+mtime[i].firstChild.nodeValue+'</span></p>';
        }

        show = show + '</div>';
        if( newnotify > 20 ) {
          show = show + '<p><a href="http://home.gamer.com.tw/notifylist.php" target="_blank">看所有通知( 尚有未讀通知 '+(newnotify-newcount)+' 則 )</a>'; 
        }else{
          show = show + '<p><a href="http://home.gamer.com.tw/notifylist.php" target="_blank">看所有通知</a>';
        }

        show = show + '　<a href="http://home.gamer.com.tw/setFavorite1.php" target="_blank">設定</a></p>';

        oo.innerHTML = show; 
        newnotify = 0;
        document.getElementById('BH-message').innerHTML = '即時通知：<span>0</span>';
        oo.style.display = ''; 
        $('#BH-msgup2').css('max-height',(document.documentElement.clientHeight-190)+'px');
        $('#div_closenotify').get()[0].style.display='';
      }
    }
  }

  function onoff(http_request) {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        var xmldoc = http_request.responseXML;
        var code = xmldoc.getElementsByTagName('code');
        var onoff = xmldoc.getElementsByTagName('onoff');
        if( code.length ) {
        var oo = document.getElementById('onoff'+code[0].firstChild.nodeValue);
        var oo2 = document.getElementById('onoff2'+code[0].firstChild.nodeValue);
        if( oo ) {
          if( '1' == onoff[0].firstChild.nodeValue ) {
            var imgalt = '停止這則通知';
          }else{
            var imgalt = '開啟這則通知';
          }

          oo.href='javascript:makeRequestnotify(\'onoff\',\'c='+code[0].firstChild.nodeValue+'&t='+onoff[0].firstChild.nodeValue+'\')';
          oo.innerHTML = '<img src="http://pic.bahamut.com.tw/msg-icon'+onoff[0].firstChild.nodeValue+'.gif" alt="'+imgalt+'" title="'+imgalt+'"/>';
        }
        if( oo2 ) {
          if( '1' == onoff[0].firstChild.nodeValue ) {
            var imgalt = '停止這則通知';
          }else{
            var imgalt = '開啟這則通知';
          }

          oo2.href='javascript:makeRequestnotify(\'onoff\',\'c='+code[0].firstChild.nodeValue+'&t='+onoff[0].firstChild.nodeValue+'\')';
          oo2.innerHTML = '<img src="http://pic.bahamut.com.tw/msg-icon'+onoff[0].firstChild.nodeValue+'.gif" alt="'+imgalt+'" title="'+imgalt+'"/>';
        }
        //$.msgbox('設定將在一分鐘後正式生效，請稍後。');

        }
      }
    }
  }


