
/*

 IMPORTANT: var comment_table must be defined in calling page!

*/


// send this review thumb vote as an ajax call
function thumb_submit (f,id,yn)
{
	var el = document.getElementById('thumb_'+f+'_'+id);
	if (!el) { return false; }  // error check
	yn = (typeof(yn) == "undefined" || yn < 1 ? 0 : 1);
	sendAJAXrequest('/ajax/comment/' + comment_table + '/' + f + '/' + id + '/' + yn, function (data) { confirmajax_thumb_submit(f, id, data); });
	return false;
}

function thumb_up_hi () { this.firstChild.src = '/pictures/thumb_up_hi.png'; };
function thumb_up_lo () { this.firstChild.src = '/pictures/thumb_up_lo.png'; };
function thumb_down_hi () { this.firstChild.src = '/pictures/thumb_down_hi.png'; };
function thumb_down_lo () { this.firstChild.src = '/pictures/thumb_down_lo.png'; };


function confirmajax_thumb_submit (f, id, data)
{
	// get the response
	var el = document.getElementById('thumb_'+f+'_'+id);
	if (data[0]['success'] > 0)
	{
		var imgs = el.getElementsByTagName('img');
		var spans = el.getElementsByTagName('span');
		if (imgs.length == 2)
		{
			// just voted it thumbs up
			if (data[0]['yn'] > 0)
			{
				// highlight the image to recognize their vote
				imgs[0].src = '/pictures/thumb_up_hi.png';
				imgs[1].src = '/pictures/thumb_down_lo.png';

				// remove link for up and adjust counts
				imgs[0].parentNode.parentNode.replaceChild(imgs[0],imgs[0].parentNode);
				spans[0].replaceChild(document.createTextNode(parseInt(spans[0].innerHTML)+1),spans[0].firstChild);
				spans[1].replaceChild(document.createTextNode(parseInt(spans[1].innerHTML)-1),spans[1].firstChild);

				// if changing their vote right after voting, there can already be a link for this image too
				if (imgs[1].parentNode.tagName == 'A') { imgs[1].parentNode.parentNode.replaceChild(imgs[1],imgs[1].parentNode);
				 spans[1].replaceChild(document.createTextNode(parseInt(spans[1].innerHTML)+1),spans[1].firstChild); }  // adjust for first vote

				// make a link out of down
				var a2 = document.createElement('a'); a2.href = '#';
				a2.onclick = function () { if (confirm('Are you sure you want to change your vote to down?')) { return thumb_submit(f,id,0); } return false; };
				a2.onmouseover = thumb_down_hi; a2.onmouseout = thumb_down_lo;
				a2.appendChild(imgs[1].parentNode.replaceChild(a2,imgs[1]));
			}

			// just voted it thumbs down
			else
			{
				// highlight the image to recognize their vote
				imgs[0].src = '/pictures/thumb_up_lo.png';
				imgs[1].src = '/pictures/thumb_down_hi.png';

				// remove link for down and adjust counts
				imgs[1].parentNode.parentNode.replaceChild(imgs[1],imgs[1].parentNode);
				spans[0].replaceChild(document.createTextNode(parseInt(spans[0].innerHTML)-1),spans[0].firstChild);
				spans[1].replaceChild(document.createTextNode(parseInt(spans[1].innerHTML)+1),spans[1].firstChild);

				// if changing their vote right after voting, there can already be a link for this image too
				if (imgs[0].parentNode.tagName == 'A') { imgs[0].parentNode.parentNode.replaceChild(imgs[0],imgs[0].parentNode);
				 spans[0].replaceChild(document.createTextNode(parseInt(spans[0].innerHTML)+1),spans[0].firstChild); }  // adjust for first vote

				// make a link out of up
				var a1 = document.createElement('a'); a1.href = '#';
				a1.onclick = function () { if (confirm('Are you sure you want to change your vote to up?')) { return thumb_submit(f,id,1); } return false; };
				a1.onmouseover = thumb_up_hi; a1.onmouseout = thumb_up_lo;
				a1.appendChild(imgs[0].parentNode.replaceChild(a1,imgs[0]));
			}
		}
	}
	else { alert('There was a problem voting your thumb up/down!'); }
}


// take an element (div) already with a pair of review thumbs up/down img links,
//  and add the appropriate functionality for this logged in member
function thumb_fill (f, id, yn)
{
	// fill all the content into the space/element/div
	var el = document.getElementById('thumb_'+f+'_'+id);
	if (el)
	{
		var imgs = el.getElementsByTagName('img');
		if (imgs.length == 2)
		{
			// they haven't picked a choice yet, either one is fine
			if (typeof(yn) == 'undefined')
			{
				var a1 = document.createElement('a'); a1.href = '#';
				a1.onclick = function () { return thumb_submit(f,id,1); };
				a1.onmouseover = thumb_up_hi; a1.onmouseout = thumb_up_lo;
				a1.appendChild(imgs[0].parentNode.replaceChild(a1,imgs[0]));

				var a2 = document.createElement('a'); a2.href = '#';
				a2.onclick = function () { return thumb_submit(f,id,0); };
				a2.onmouseover = thumb_down_hi; a2.onmouseout = thumb_down_lo;
				a2.appendChild(imgs[1].parentNode.replaceChild(a2,imgs[1]));
			}

			// already picked a thumbs up, this one is fixed
//			if (imgs[0].src == '/pictures/thumb_up_hi.png')
			else if (yn)
			{
				var a2 = document.createElement('a'); a2.href = '#';
				a2.onclick = function () { if (confirm('Are you sure you want to change your vote to down?')) { return thumb_submit(f,id,0); } return false; };
				a2.onmouseover = thumb_down_hi; a2.onmouseout = thumb_down_lo;
				a2.appendChild(imgs[1].parentNode.replaceChild(a2,imgs[1]));
			}

			// already picked a thumbs down, this one is fixed
//			else if (imgs[1].src == '/pictures/thumb_down_hi.png')
			else if (!yn)
			{
				var a1 = document.createElement('a'); a1.href = '#';
				a1.onclick = function () { if (confirm('Are you sure you want to change your vote to up?')) { return thumb_submit(f,id,1); } return false; };
				a1.onmouseover = thumb_up_hi; a1.onmouseout = thumb_up_lo;
				a1.appendChild(imgs[0].parentNode.replaceChild(a1,imgs[0]));
			}
		}
	}
}
