function notify_delete_result(respone,json)
{

	if(respone.status!=200 || json==null)
	{
		alert("Błędna odpowiedź od serwera :(");
		return false;
	}

	if(json.status=='ok')
	{
		if(json.nid=='all')
		{
			$$('.notify').each(function (e) { e.hide(); });	
		}else{
			div=$('notify_'+json.nid);
			if(div) div.hide();
		}
		
	}else{
		alert(json.message);
	}
}


function notify_readed(div_id)
{
	$(div_id).down('.options').down('img').src='/images/loading.gif';
	id=div_id.replace('notify_','');
	new Ajax.Request(
			'/notify/delete',
			 {asynchronous:true, evalScripts:false, onComplete:function(request, json){notify_delete_result(request,json);},
			parameters:'nid='+id});
	return false;
	
}


function notify_readed_all(all_pages)
{
	
	$$('.options').each(function (e) {e.down('img').src='/images/loading.gif';});
	if(all_pages==true)
	{
		var ids="all";
	}else{	
		var notifies=$$('.notify');
		var ids="";
		notifies.each(function (notify) { ids+=notify.id.replace("notify_","")+","; });
	}
	

	
	new Ajax.Request(
			'/index.php/notify/delete_all',
			  {asynchronous:true, evalScripts:false, onComplete:function(request, json){notify_delete_result(request,json);},
			parameters:'nids='+ids});
	return false;
	
}

function notify_flash_result(respone,json)
{
	if(respone.status!=200) return;
	if(json==null) return true;

	if(json.count==0) return true;
	contener=$('alert_area');
	if(contener==null) return true;
	
	
	var x=0;
	var flashes='';
	flashes=contener.innerHTML;
	for(x=0;x<json.count;x++)
	{
		flashes+=(json.notifies[x]);
	}
	contener.update(flashes);
}


function notify_get_flash()
{
	if(numia_user_id==0) return;

	if(getCookie('last_flash_display')=='1') return;
	setCookie('last_flash_display','1',60);
	setTimeout('notify_get_flash',62000);
	
	new Ajax.Request(
			'/notify/flash/r/'+(Math.round(Math.random()*100000)),
			 {asynchronous:true, evalScripts:false, onComplete:function(request, json){notify_flash_result(request,json)}});
	
}

function notify_test_result(respone,json)
{
	if(respone.status!=200) return;
	alert(respone.getAllHeaders()	);
	alert(json.count);
}

function notify_test()
{
	new Ajax.Request(
			'/notify/flash/r/'+(Math.round(Math.random()*100000)),
			 {asynchronous:true, evalScripts:true, onComplete:function(request, json){notify_test_result(request,json)}});
	
}


function setCookie(cookieName,cookieValue,sec) { 
	var today = new Date(); 
	var expire = new Date(); 
	expire.setTime(today.getTime() + 1000*sec); 
	document.cookie = cookieName+"="+escape(cookieValue) 
	+ ";expires="+expire.toGMTString(); 
}

function getCookie(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}

	return (cookieValue);
}
Event.observe(window,'load',notify_get_flash,false);


