var xmlhttp;
function getAjaxObject() {
	try {
		return (typeof XMLHttpRequest != "undefined" ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"))
	} catch (e) { 
		return null; 
	}
}

function get_update(id){
	var last_update_time = getCookie('last_update_time');
	var d = new Date();
	var dtime = d.getTime();
	
	if(document.getElementById('ListReply').style.display == '')
		document.getElementById('ListReply').style.display = 'none';
	
	if(document.getElementById('ListReply').style.display == 'none' && ((last_update_time == '') || (parseInt(dtime) - parseInt(last_update_time) > 300000)))
	{
		var url = "notifybar.php?act=all&sid=" + dtime;
		xmlhttp = getAjaxObject();
		xmlhttp.onreadystatechange = function () {
	        if (xmlhttp.readyState == 4) {
				if(xmlhttp.responseText){
					var respon = xmlhttp.responseText;
					update_status_notifybar(respon);
					setCookie('last_update_time_value',respon);
				}
	        }
	    };
	    xmlhttp.open("GET", url, true);
		xmlhttp.send(null);
		setCookie('last_update_time', dtime);
	}
	else 
	{
		if(document.getElementById('ListReply').style.display == 'none')
		{
			var last_update_time_value = getCookie('last_update_time_value');
			if (last_update_time_value != '' && last_update_time_value != 'undefined')
			{
				update_status_notifybar(last_update_time_value);
			}
		}
	}
	setTimeout('get_update(1)', 300000);
}

function update_status_notifybar(respon)
{
	var json_data		= eval('['+respon+']');
	var json_data_alert = json_data[0];
	if(parseInt(json_data_alert['reply']) > 0)
	{
		document.getElementById('num_reply_alert').innerHTML = json_data_alert['reply'];
		document.getElementById('num_reply_alert').style.display = 'block';
	}
	else
	{
		document.getElementById('num_reply_alert').style.display = 'none';
	}
}

function get_reply_notify_bar(){
	setCookie('last_update_time_value','');
	get_notify_bar('reply','ListReply','notify_reply','reply', 'ListReplyContent');
}

function get_notify_bar(type, blockid, btid, act, contentid){
	if(document.getElementById(blockid).style.display=="" || document.getElementById(blockid).style.display=="none"){
		document.getElementById(blockid).style.display = 'block';
		document.getElementById(btid).className='notify_reply_alert';
		if (document.getElementById(contentid).innerHTML == '') {
			document.getElementById(contentid).innerHTML = '<center><img hspace="7" vspace="7" valign="bottom" style="padding-top:5px;" src="images/muare/nb_loading.png"/></center>';
			var randomnumber = Math.random();
			var url = "notifybar.php?act="+act+"&sid="+randomnumber;
			xmlhttp = getAjaxObject();
			xmlhttp.onreadystatechange = function () {
		        showResultBox(type, blockid, btid, contentid);
		    };
		    xmlhttp.open("GET", url, true);
	   	 	xmlhttp.send(null);
		}
		
	}
	else{
		fn_close_nbar(blockid);
		document.getElementById(btid).className='notify_reply_hover';
	}
}

function showResultBox(type, blockid, btid, contentid){
	if (xmlhttp.readyState == 4) {
		if(xmlhttp.responseText && xmlhttp.responseText!='[]'){
			var json_data = eval(xmlhttp.responseText);
			var content = '';
			for(i in json_data){
				if(type == 'reply')
				{
					if (json_data[i]['type'] == 1)
					{
						icon_name 	= '@_';
						div_text 	= ' nhắc tới bạn';
					}
					else
					{
						icon_name 	= 'quote_';
						div_text 	= ' trả lời bạn';
					}
					if(json_data[i]['read'] == 'new')
					{
						icon_name  += 'new.gif';
						link_syle	= 'font-size: 12px; color: #3c5899; font-family: Arial,Helvetica,sans-serif;';
					}
					else
					{
						icon_name  += 'old.gif';
						link_syle	= 'font-size: 12px; color: #8c9cc0; font-family: Arial,Helvetica,sans-serif;';
					}
					
					if (i%2==0)
						background_color = '#FFFFFF';
					else
						background_color = 'rgb(245,245,245)';
					content += '<div class="notify_bar_text" style="background:url(images/muare/'+icon_name+') 8px 11px no-repeat; background-color:' + background_color + '; padding:5px 5px 5px 28px;"><div style="padding-bottom:5px;padding-top:5px;"><a href="member-' + json_data[i]['postuserid'] + '" target="_blank" style="'+link_syle+'">' + json_data[i]['postusername'] + '</a> đã' + div_text + ' trong <a href="home/p-' + json_data[i]['postid'] + '#post' + json_data[i]['postid'] + '" style="'+link_syle+'">' + json_data[i]['title'] + '</a> - <span class="timeupdate">lúc ' + json_data[i]['created'] + '</span></div></div>'
				}
			}
			document.getElementById(contentid).innerHTML = content;
		}
		else
			document.getElementById(contentid).innerHTML = '<div style="text-align:center; padding: 3px 0 3px"><i>không có thông tin</i></div>';
	}
}

function fn_close_nbar(id){
	document.getElementById(id).style.display = 'none';
	document.getElementById('num_reply_alert').style.display = 'none';
	document.getElementById('notify_reply').className='notify_reply_';
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}
