// Browser detection
var ie4 = document.all;
var ns6 = document.getElementById && !document.all;

// Initial quote URL
var url = "getQuote.php?mode=$mode&param="; 
var deleteUrl = "deleteQuote.php?param=";

// Other variables
var reqnum = 0;	
var param = 0;
var max = 0;
var pPlus = 0;
var pMinus = 0;

var first = "";
var last = "";
var middle = "";

// Change up the buttons when switching between posts
function manageButtons() {
	if (document.getElementById('vcr') != null) {
		pPlus = param + 1;
		pMinus = param -1;
		if (param == 1) {
		  first = "<img src=\"/home/images/start-dis.gif\" alt=\"Already on first post!\" border=0>\n <img src=\"/home/images/previous-dis.gif\" alt=\"No previous posts!\" border=0>\n <a id=\"goNext\" href=\"" + self + "?p=" + pPlus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/next.gif\" title=\"next post [c]\" alt=\"Next post [c]\" border=0></a>\n <a id=\"goLast\" href=\"" + self + "?p=" + max + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/end.gif\" title=\"Last post [v]\" alt=\"Last post [v]\" border=0></a>\n";
			document.getElementById('vcr').innerHTML = first;
		}
		else if (param == max) {
		  last = "<a id=\"goFirst\" href=\"" + self + "?p=1&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/start.gif\" title=\"First post [z]\" alt=\"First post [z]\" border=0></a>\n <a id=\"goBack\" href=\"" + self + "?p=" + pMinus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/previous.gif\" title=\"previous post [x]\" alt=\"Previous post [x]\" border=0></a>\n <img src=\"/home/images/next-dis.gif\" alt=\"No further posts!\" border=0>\n <img src=\"/home/images/end-dis.gif\" alt=\"Already on last post!\" border=0>\n";
			document.getElementById('vcr').innerHTML = last;
		}
		else {
//	alert("p: " + p + " param: " + param + " max: " + max + " pplus: " + pPlus + " pMinus: " + pMinus);
		  middle = "<a id=\"goFirst\" href=\"" + self + "?p=1&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/start.gif\" title=\"First post [z]\" alt=\"First post [z]\" border=0></a>\n <a id=\"goBack\" href=\"" + self + "?p=" + pMinus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/previous.gif\" title=\"previous post [x]\" alt=\"Previous post [x]\" border=0></a>\n <a id=\"goNext\" href=\"" + self + "?p=" + pPlus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/next.gif\" title=\"next post [c]\" alt=\"Next post [c]\" border=0></a>\n <a id=\"goLast\" href=\"" + self + "?p=" + max + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/end.gif\" title=\"Last post[v]\" alt=\"Last post [v]\" border=0></a>\n";
			document.getElementById('vcr').innerHTML = middle;		
		}
	}
}

//var mode = 'comments';

// Show/hide the comments for the current post
function showComments() {
	if (document.getElementById('comment')) {
		elementIs = document.getElementById('comment');
		if (elementIs.style.display == "none") {
			elementIs.style.display="block";
			mode = 'comments';
			manageButtons();
		}
		else if (elementIs.style.display =="block") {
			elementIs.style.display="none";
			mode = 'nocomments';
			manageButtons();
		}
		return true;
	}
	else {
		return true;
	}
	return;
}


// Run the trip function on load
window.onload = trip;

// Show the 'add a comment' section
function showAdd() {
	if (document.getElementById('addacomment').style.display == "none") {
		document.getElementById('addCommentButton').style.display = "none";
		document.getElementById('addacomment').style.display = "block";
		document.getElementById('user').focus();
		document.getElementById('bothide').style.display = "none";
		document.getElementById('bothide1').style.display = "none";
		}
	else {
		document.getElementById('addacomment').style.display = "none";
		document.getElementById('addCommentButton').style.display = "block";
	}
}
	
// Move the corner, initialize the quote, setup the post parameters,
// add the quote reload methods and load the post variables
function trip() {
	moveCorner();
	updateQuote(id);
	window.status = '';
	param = p;
	pPlus = p + 1;
	pMinus = p - 1;
	//max = p;
	document.getElementById('reload').onmousedown=function(){quoteDown();};
	document.getElementById('reload').onmouseup=function(){quoteUp();};
	document.getElementById('reload').onclick=function(){updateQuote(0);};
	//document.getElementById('bothide1').style.display="none";
	//document.getElementById('bothide').style.display="none";
  first = "<img src=\"/home/images/start-dis.gif\" alt=\"Already on first post!\" border=0>\n <img src=\"/home/images/previous-dis.gif\" alt=\"No previous posts!\" border=0>\n <a id=\"goNext\" href=\"" + self + "?p=" + pPlus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/next.gif\" title=\"next post [c]\" alt=\"Next post [c]\" border=0></a>\n <a id=\"goLast\" href=\"" + self + "?p=" + max + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/end.gif\" title=\"Last post [v]\" alt=\"Last post [v]\" border=0></a>\n";
  last = "<a id=\"goFirst\" href=\"" + self + "?p=1&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/start.gif\" title=\"First post [z]\" alt=\"First post [z]\" border=0></a>\n <a id=\"goBack\" href=\"" + self + "?p=" + pMinus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/previous.gif\" title=\"previous post [x]\" alt=\"Previous post [x]\" border=0></a>\n <img src=\"/home/images/next-dis.gif\" alt=\"No further posts!\" border=0>\n <img src=\"/home/images/end-dis.gif\" alt=\"Already on last post!\" border=0>\n";
  middle = "<a id=\"goFirst\" href=\"" + self + "?p=1&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/start.gif\" title=\"First post [z]\" alt=\"First post [z]\" border=0></a>\n <a id=\"goBack\" href=\"" + self + "?p=" + pMinus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/previous.gif\" title=\"previous post [x]\" alt=\"Previous post [x]\" border=0></a>\n <a id=\"goNext\" href=\"" + self + "?p=" + pPlus + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/next.gif\" title=\"next post [c]\" alt=\"Next post [c]\" border=0></a>\n <a id=\"goLast\" href=\"" + self + "?p=" + max + "&mode=" + mode + "\" target=\"_self\"><img src=\"/home/images/end.gif\" title=\"Last post[v]\" alt=\"Last post [v]\" border=0></a>\n";
}

// IE CSS bug fix to move the corner picture left by 3 pixels
function moveCorner() {
	if (ie4) {
		document.getElementById('corner').style.position = "relative";
		document.getElementById('corner').style.left = "-3px";
	}	
}

// Reload the post content with the retrieved quote and move the corner picture
function handleContent() {
 if (http.readyState == 4 && http.status == 200) {
 	 document.getElementById('content').innerHTML = http.responseText;
   moveCorner();
 }
}

// Shortcut keys
// Turns off shortcut keys when adding a comment or editing a quote
//function keyCall(e) {
//	if (document.getElementById('addacomment')) {
//		if (document.getElementById('addacomment').style.display == "block") {
//			return;
//		}
//	}
//	if (document.getElementById('quoteAdd')) {
//		if (document.getElementById('quoteAdd').style.display == "block") {
//			return;
//		}
//	}
//	if (document.getElementById('editQuote')) {
//		if (document.getElementById('editQuote').style.display == "block") {
//			return;
//		}
//	}
//	
//	var keyCode = (window.event) ? event.keyCode : e.keyCode;
//	var	contentURL = "getContent.php?mode=" + mode + "&param=";
//		
//	var keyChar = (String.fromCharCode(keyCode).toLowerCase());
//	// Z, X, C, V - top buttons
//		if ((keyChar == 'z') && (param != 1)) {
//			param = 1;
//			ajax("content", contentURL + param, handleContent);
//			manageButtons();
//			return;
//		}
//		if ((keyChar == 'x') && (param > 1)) {
//			param -= 1;
//			ajax("content", contentURL + param, handleContent);
//			manageButtons();
//			return;
//		}
//		if ((keyChar == 'c') && (param < max)) {
//			param += 1;
//			ajax("content", contentURL + param, handleContent);
//			manageButtons();
//			return;
//		}
//		if ((keyChar == 'v') && (param < max)) {
//			param = max;
//			ajax("content", contentURL + param, handleContent);
//			manageButtons();
//			return;
//		}		
//		// Q refreshes quote, S shows comments, A adds comment, W adds a quote
//		if (keyChar == 'q')
//			updateQuote(0);
//		if (keyChar == 's')
//			showComments();
//		if (keyChar == 'a')
//			showAdd();
//		if (keyChar == 'w' && document.getElementById('adminSpan'))
//			addQuote();
//}
//
//// Checks keypresses on keyup
//document.onkeyup = keyCall

//
// COMMON JAVASCRIPT FUNCTIONS
//

// XMLHttpRequest object (AJAX)
var http = getHTTPObj();

// Reload the quote tag with the retrieved quote and raise up the button
function handleQuote() {
 if (http.readyState == 4 && http.status == 200) {
 	 document.getElementById('quote').innerHTML = http.responseText;
   document.getElementById('reload').src = '/home/images/reload.gif';
   if (document.getElementById('quoteAdd'))
   	document.getElementById('quoteAdd').focus();
 }
}

function deleteQuote(id) {
	var answer = confirm ("Delete quote " + id + " ?")
	if (answer) {
		reqnum += 1;
		idparam = "&id=";
		ajax("quote", deleteUrl + reqnum + idparam + id, handleQuote);
	}
	else
		updateQuote(id);
}

// Start the update quote thread and depress the button
// Reqnum is so the browser thinks that it's a new page and
// doesn't retrieve it from cache
function updateQuote(id) {
	document.getElementById('reload').src = '/home/images/reload_down.gif';
	reqnum += 1;
	idparam = "&id="
	ajax("quote", url + reqnum + idparam + id, handleQuote);
}

// Start the add quote thread
function addQuote() {
	ajax("quote", "addquote.php?mode=" + mode, handleQuote);
}

// Start the edit quote thread
function editQuote(id) {
	ajax("quote", "editquote.php?id=" + id + "&mode=" + mode, handleQuote);
}

// Depresses the reload quote button
function quoteDown() {
	document.getElementById('reload').src = '/home/images/reload_down.gif';	
}

// Unpresses the reload quote button
function quoteUp() {
	document.getElementById('reload').src = '/home/images/reload.gif';	
}

// Get a new AJAX object
function getHTTPObj() {
	if (window.XMLHttpRequest) {
    reqXML = new XMLHttpRequest();
    reqXML.overrideMimeType('text/xml');
  }
  else if(window.ActiveXObject)
    reqXML = new ActiveXObject("Microsoft.XMLHTTP"); 
  return reqXML;
}

// Run the ajax - change the html to a load screen and
// setup the thread
function ajax(id, url, func) {
	document.getElementById(id).innerHTML = "<p><i>Loading...</i></p>";
	http.open("GET", url, true);
	http.onreadystatechange = func;
	http.send(null);
}	
