var kphp_list = {play: 1, highestid: 0, max: 10, pushtime: 1000, row: 0, simple: 0, b: 0};

function kphp_pause()
{
	$('#play').attr('src', 'images/misc/play_up.gif').attr('alt', 'تشغيل');
	$('#pause').attr('src', 'images/misc/pause_down.gif').attr('alt', 'إيقاف');

	_kphp_pause();
};

function kphp_play()
{
	$('#play').attr('src', 'images/misc/play_down.gif').attr('alt', 'تشغيل');
	$('#pause').attr('src', 'images/misc/pause_up.gif').attr('alt', 'إيقاف');

	_kphp_resume();
};

function _kphp_pause()
{
	kphp_list.play = 0;
};

function _kphp_resume()
{
	kphp_list.play = 1;
};


function kphp_init(simple, max, highestid)
{
	if (simple)
	{
		kphp_list.simple = 1;
	}

	if (max > 0)
	{
		kphp_list.max = max;
		kphp_list.b = 1;
	}
	kphp_list.highestid = highestid;
	kphp_list.cmax = max - 5;

	kphp_getJson();
};

function kphp_parseJson(data)
{
	if ((data.substring(0, 1) != '{') && (data.substring(0, 1) != '['))
	{
		return { status: 0, data: data.length ? data : 'Unspecified error' };
	}
	return eval('(' + data + ');');
};

function kphp_getJson()
{
	if (kphp_list.highestid > 0)
	{
		var querystring = "do=json&last=" + kphp_list.highestid;
	}
	else
	{
		var querystring = "do=json";
	}

	if ($('#subscribe').attr('checked'))
	{
		querystring = querystring + "&subscribe=1";
	}

	if (kphp_list.simple)
	{
		querystring = querystring + "&simple=1";
	}

	if (kphp_list.b)
	{
		querystring = querystring + '&d=1';
	}
	querystring = querystring + '&r=' + Math.random();

	var url = "kphp.php";
	var myAjax = null;
	myAjax = $.ajax({
		type: "GET",
		url: url,
		data: querystring,
		success: kphp_process
	});
};

function kphp_process(request)
{
	var events;
	var event;

	try {
		events = kphp_parseJson(request);
	}

	catch (e)
	{
		setTimeout("kphp_getJson()", 5000);
		return;
	}

	kphp_list.highestid = events.pop();

	kphp_list.thread = new Array();
	jQuery.each(events, function (i, data) {
		kphp_list.thread[i] = data;
	});

	kphp_push();
};

function kphp_push()
{
	if (kphp_list.play == 0)
	{
		setTimeout("kphp_push()", 1000);
		return;
	}

	data = kphp_list.thread.pop();
	if (data)
	{
		kphp_row(data);
	}

	if (kphp_list.thread.length > 0)
	{
		setTimeout("kphp_push()", kphp_list.pushtime);
	}
	else
	{
		setTimeout("kphp_getJson()", 7000);
	}
};

function kphp_row(data)
{
	$("#row1").hide();

	$j = 1;
	for (i = (kphp_list.max - 1); i >= 1; i--)
	{
		cell = $("#row" + i);
		cellnext = $("#row" + (i + 1));
		if (cell.html() != '')
		{
			cellnext.html(cell.html());
			cellnext.css('display', '');

			if (kphp_list.cmax < i)
			{
				if ($j == 1)
				{
					cellnext.css('opacity', .05);
				}
				else if ($j == 2)
				{
					cellnext.css('opacity', .25);
				}
				else if ($j == 3)
				{
					cellnext.css('opacity', .45);
				}
				else if ($j == 4)
				{
					cellnext.css('opacity', .65);
				}
				else if ($j == 5)
				{
					cellnext.css('opacity', .85);
				}
				$j++;
			}
		}
	}
	$("#row1").html(data);
	$("#row1").fadeIn('slow');
}

function _kphp_row(data)
{
	var cell;
	var cellnext;
	var text;
	var clip;
	var poster_clip = '';
	var where = '';
	var post_url;
	var style = '';

	kphp_list.highestid = Math.max(parseInt(data.id), kphp_list.highestid);

	if (vB_PHP_Emulator.prototype.stripos(data.etype, 'thread'))
	{
		post_url = 'showthread.php?t=' + data.threadid;
	}
	else
	{
		post_url = 'showthread.php?p=' + data.postid + '#post' + data.postid ;
	}

	if(data.title.length == 0)
	{
		clip = '<strong>غير معروف</strong>';
	}
	else
	{
		clip = '<strong><a href="' + post_url + '">' + data.title + '</a></strong>';
	}
	sclip = clip;

	if (data.preview.length > 0)
	{
	    clip = clip + '<br /><div class=\"smallfont\">' + data.preview ;
	}

    clip = clip + ' (' + data.views + ' المشاهدات, ' + data.replies + ' الردود)</div>';

	if (data.userid.length > 0)
	{
		poster_clip = '<a href="member.php?u=' + data.userid + '">' + PHP.unhtmlspecialchars(data.poster) + '</a>';
	}

    poster_clip = poster_clip + '<br />' + data.date_time;

	if (parseInt(data.forumname.length) > 0)
	{
		where = where + '<strong><a href="forumdisplay.php?f=' + data.forumid + '">' + PHP.unhtmlspecialchars(data.forumname) + '</a></strong>';
	}
	else
	{
		where = '';
	}

	if (data.etype == 'post')
	{
		title = 'مشاركة جديده';
	}
	else
	{
		title = 'موضوع جديده';
	}

	if (kphp_list.simple)
	{
		text = '<td class="alt1" align="center"><div class="smallfont"><strong>' + title + '</strong></div></td><td class="alt1Active">' + sclip + '<br /><div class="smallfont">' + poster_clip + '<br /> (' + data.views + ' المشاهدات, ' + data.replies + ' الردود)</div></td>';
	}
	else
	{
		text = '<td class="alt1" align="center"><div class="smallfont"><strong>' + title + '</strong></div></td><td class="alt2"><div class="smallfont">' + poster_clip + '</div></td><td class="alt1Active">' + clip + '</td><td align="center" class="alt1"><div class="smallfont">' + where + '</div></td>';
	}

	$("#row1").css('opacity', 0.0);

	$j = 1;
	for (i = (kphp_list.max - 1); i >= 1; i--)
	{
		cell = $("#row" + i);
		cellnext = $("#row" + (i + 1));
		if (cell.html() != '')
		{
			cellnext.html(cell.html());
			cellnext.css('display', '');

			if (kphp_list.cmax < i)
			{
				if ($j == 1)
				{
					cellnext.css('opacity', .05);
				}
				else if ($j == 2)
				{
					cellnext.css('opacity', .25);
				}
				else if ($j == 3)
				{
					cellnext.css('opacity', .45);
				}
				else if ($j == 4)
				{
					cellnext.css('opacity', .65);
				}
				else if ($j == 5)
				{
					cellnext.css('opacity', .85);
				}
				$j++;
			}
		}
	}
	$("#row1").html(text);
};