var gFavTimerID = 0;
var gFavID = 0;

// mode
//	0 = unset
//	1 = Was off, waiting to set on.
//  2 = was on, waiting to set off.
var gFavMode = 0;

try { console.assert(1); } catch(e) { console = { log: function() {},  assert: function() {} } }

// This function is given an article ID, but we dont know anything else about it at this point.
function Fave(aid) {

	if (gSessionID.length < 34) {
		alert("Favorites functionality is not available unless you are logged in.");
	}
	else {
		var skip = 0;

		if (gFavTimerID > 0) {
			console.assert(gFavID > 0);
			clearTimeout(gFavTimerID);
			gFavTimerID = 0;
			if (gFavID == aid) {
				if (gFavMode == 1)	{ GrayStar(aid); }
				else 				{ YellowStar(aid); }
				skip = 1;
			}
			else {
				console.log("Sending Fave now for "+gFavID+".");
				FaveSend();
				console.assert(gFavID == 0);
				skip = 0;
			}
		}

		console.assert(gFavTimerID == 0);
		if (skip == 0) {
			if (IsFavSet(aid) == 1) {
				GrayStar(aid);
				gFavMode = 2;
			}
			else {
				YellowStar(aid);
				gFavMode = 1;
			}
			console.assert(gFavMode > 0);
			gFavID = aid;
			gFavTimerID = setTimeout("FaveSend()", 3000);
			console.assert(gFavTimerID > 0);
		}
	}
}



// Timer has expired, we send the request now, and reset the values for additional clicks.  Assume action succeeds, display results immediately.  Delayed result is ignored unless it is a failure, and then we recover as best we can.
function FaveSend() {

	var cmd;

	console.assert(gFavID > 0);
	console.assert(gSessionID.length >= 34);
	console.assert(gFavMode > 0);

	ajaxReset();
	ajaxArg('aid', gFavID);
	ajaxArg('sid', gSessionID);
	if (gFavMode == 1) {
		console.log("Sending request to SET fav for aid="+gFavID);
		cmd = '/api/setfav';
	}
	else  {
		console.log("Sending request to CLEAR fav for aid="+gFavID);
		cmd = '/api/clearfav';
	}
	ajaxRequest(cmd, resultFavChange);
	gFavTimerID = 0;
	gFavID = 0;
	gFavMode = 0;
}

function resultFavChange(xml) {
	var result = parseInt(xmlData(xml, 'result'));
	if (result == 1) {
		gFavList = xmlData(xml, 'favlist');
	}
}

function SendFavRequest() {

	if (gSessionID < 34) {
		alert("This functionality is only available if logged in.");
	}
	else {
		if (gFavTimerID > 0) {
			console.assert(gFavID > 0);
			clearTimeout(gFavTimerID);
			gFavTimerID = 0;
			FaveSend();
			console.assert(gFavID == 0);
			setTimeout("SendFavRequestActual()", 5);
		}
		else {
			SendFavRequestActual();
		}
	}
}

function SendFavRequestActual() {
	ajaxReset();
	ajaxArg('sid', gSessionID);
	ajaxRequest('/api/favs', resultFavList);
}

function resultFavList(xml) {
	if (gMode == 4) {
		var result = parseInt(xmlData(xml, 'result'));
		if (result == 1) {
			var ta = document.getElementById('t_articles');
			ta.innerHTML = "";
			var list = xml.getElementsByTagName('article');

			var item = 0;
			while (item < list.length) {
				var article = list[item];

				var aid=parseInt(xmlData(article, 'aid'));
				var uid=parseInt(xmlData(article, 'uid'));
				var username=xmlData(article, 'username');
				var title=xmlData(article, 'title');
				var url=xmlData(article, 'url');
				var content=xmlData(article, 'content');

				DisplayArticle(aid, uid, username, title, url, content);
				item ++;
			}
			hide("more");
			hide("loading");
		}
		else {
			alert("There was an error submitting your details: " + xmlData(xml, 'comment'));
		}
	}
}



function GrayStar(aid) {
	var vv = document.getElementById("i_star_"+aid);
	console.assert(vv != null);
	vv.src = "http://static-rhokz.com/static/images/icons/star-grey.gif";
	vv.alt = "Add to Favorites list";
	vv.title = "Add to Favorites list";
}

function YellowStar(aid) {
	var vv = document.getElementById("i_star_"+aid);
	console.assert(vv != null);
	vv.src = "http://static-rhokz.com/static/images/icons/star.gif";
	vv.alt = "Remove from Favorites list";
	vv.title = "Remove from Favorites list";
}


function FaveRemove(aid) {

	console.assert(gSessionID.length >= 34);


	// if there is an existing timer, we need to process it.
	if (gFavTimerID > 0) {
		console.assert(gFavID > 0);
		clearTimeout(gFavTimerID);
		gFavTimerID = 0;

		console.log("Sending Fave now for "+gFavID+".");
		FaveSend();
	}

	// Now we need to send a CLEAR immediately.
	console.assert(gFavID == 0);
	console.assert(gFavMode == 0);

	ajaxReset();
	ajaxArg('aid', aid);
	ajaxArg('sid', gSessionID);
	console.log("Sending request to CLEAR fav for aid="+aid);
	ajaxRequest('/api/clearfav', resultFavChange);
}

gLoadFav = 2;
