//////////////////////////////////////////////////////////
// common.js
// contains common javascript functions for millihelens, including:
//   - methods to create collapsible sections
//   - asynchronous content fetching (Not yet implemented)
/////////////////////////////////////////////////////////

// collapse / uncollapse the element with the passed id,
// if that element is collapsible
function toggleCollapse(id) {
  var div = document.getElementById(id);
  if (div.className == 'collapsed') {
    div.className = 'collapsible';
  } else if (div.className == 'collapsible') {
    div.className = 'collapsed';
  }
  return false;
}


// Just hide the complexity of creating an XMLHttpRequest object
// on different browsers
function createXmlHttpRequest() {
  if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return req;
}

function sendAsyncRequest(url, callback) {
  req = createXmlHttpRequest();
  req.open("GET", url, true);
  req.onreadystatechange = createCallback(req, callback);
  req.send("");

}

function createCallback(req, callback) {
  return function() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
	  if (callback != null) {
	    callback(req);
	  }
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
  }
}

function createElementContentCallback(element_id, 
				      success_response, 
				      success_message, 
				      failure_message) {
  return function(req) {
    if (req.responseText == success_response) {
      document.getElementById(element_id).innerHTML = success_message;
    } else {
      document.getElementById(element_id).innerHTML = failure_message;
    }
  }
}

function fillElementCallback(element_id) {
  return function(req) {
    document.getElementById(element_id).innerHTML = req.responseText;
  }
}

/* This probably doesn't belong here */
function addComment(pid, text, element_id, num) {
  sendAsyncRequest("add_comment.php?pid=" + pid + "&text=" 
		   + text + "&num=" + num + "&element_id=" + element_id,
		   fillElementCallback(element_id));
  return false;
}

function getComments(pid, eid, num, page) {
  sendAsyncRequest("get_comments.php?pid=" + pid + "&element_id=" + eid + "&num=" + num + "&page=" + page,
		   fillElementCallback(eid));
}

function deleteComment(comment_id, picture_id, element_id, num, page) {
  sendAsyncRequest("delete_comment.php?comment_id=" + comment_id + 
		   "&num=" + num + "&page=" + page + "&element_id=" + 
		   element_id + "&pid=" + picture_id,
		   fillElementCallback(element_id));
  return false;
}

function rateComment(comment_id, score, picture_id, element_id, num, page) {
  sendAsyncRequest("rate_comment.php?comment_id=" + comment_id + 
		   "&score=" + score + "&num=" + num + "&page=" + page + 
		   "&element_id=" + element_id + "&pid=" + picture_id,
		   fillElementCallback(element_id));
  return false;
}

function flagPicture(picture_id, reason, element_id) {
  sendAsyncRequest('score_edit.php?action=flag&picture_id=' + picture_id + '&reason=' + reason,
		   createElementContentCallback(element_id, 
						1, 
						'<h6>Ok, we\'ll review it</h6>', 
						'<h6>Something went wrong. Try again later. </h6>')); 
  document.getElementById(element_id).innerHTML = '<h6>flagging...</h6>';
}