var READY_STATE_COMPLETE = 4;

function initXmlHttp()
{
	var xmlHttp;
	try {
	  	// Firefox, Opera 8.0+, Safari
  		xmlHttp = new XMLHttpRequest();
  	}
	catch (e) {
  		// Internet Explorer
  		try {
	    	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e) {
    		try {
      			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	      	}
			// no-support for AJAX
	    	catch (e) {
      			return null;
			}
      	}
 	}
	return xmlHttp;
}

var xmlHttp = initXmlHttp();

function setBookmark(recipeId)
{
	if (xmlHttp != null) {
	    xmlHttp.open('get', 'add_book.php?recipe_id=' + recipeId);
	    xmlHttp.onreadystatechange = onBookmark;
    	xmlHttp.send(null);
	}
}

function setMadeIt(recipeId)
{
	if (xmlHttp != null) {
	    xmlHttp.open('get', 'madeit.php?recipe_id=' + recipeId);
	    xmlHttp.onreadystatechange = onMadeIt;
    	xmlHttp.send(null);
	}
}

function setVote(recipeId, vote)
{
	if (xmlHttp != null) {
	    xmlHttp.open('get', 'vote.php?recipe_id=' + recipeId + '&vote=' + vote);
	    xmlHttp.onreadystatechange = onVoteIt;
    	xmlHttp.send(null);
	}
}

function onBookmark()
{
    if(xmlHttp.readyState == READY_STATE_COMPLETE) {
		objAddBookmark = document.getElementById('add-bookmark');
		/* Return new recipe rating after vote */
		objAddBookmark.style.display = 'none';
	}
}

function onMadeIt()
{
    if(xmlHttp.readyState == READY_STATE_COMPLETE) {
        document.getElementById('madeit_button').className = 'active';
    }
}

function onVoteIt()
{
    if(xmlHttp.readyState == READY_STATE_COMPLETE) {
		objStars = document.getElementById('stars');
		/* Return new recipe rating after vote */
		posY = xmlHttp.responseText * 20;
		objStars.style.backgroundPosition = 'left -' + posY + 'px';
		objStars.innerHTML = '&nbsp;';
	}
}