//place for generic js functions

var _popupContainer = null;
//create a page in the middle of the parent document
function popupPageInIFrame(parentDocument, url, width, height) {
    var winW, winH;
    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName=="Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.documentElement.clientWidth;
            winH = document.documentElement.clientHeight;
        }
    }
    var top = (winH - height)/2;
    var left = (winW - width)/2;

    var iframe = document.createElement('iframe');

    iframe.style.border = "solid 2px #000000";
    iframe.style.position = "absolute";
    iframe.style.top = top + "px";
    iframe.style.left = left + "px";
    iframe.style.width = width + "px";
    iframe.style.height = height + "px";
    iframe.style.zIndex = "1000";
    iframe.style.backgroundColor = "#ffffff";
    iframe.src = url;
    iframe.id = "popupDiv";

    _popupContainer = document.createElement('div');
    _popupContainer.style.backgroundColor = "#ffffff";
    _popupContainer.appendChild(iframe);

    fadePageOn();
    parentDocument.appendChild(_popupContainer);
}

function closePopup() {
	if (_popupContainer != null) {
		_popupContainer.style.display = 'none';
		_popupContainer.parentNode.removeChild(_popupContainer);
		_popupContainer = null;
	}
	fadePageOff();
}


function showLoginWindow(msg) {
	popupPageInIFrame(document.body, 'loginWindow.php?msg='+msg, 260, 140);
}




//-----------------------
//code related javascript
//-----------------------

var xmlHttp = new Array();
var httpRequestNum = 0;
var MAX_REQUESTS = 10; //the number of xmlHTTPrequests in the queue
var SEARCH_DELAY = 200;

function SearchSubmit(searchValue) {
	var textBox = document.getElementById('TextBoxSearch');
	if ((textBox.value != '') && (textBox.value != 'Enter a search query...') && (textBox.value == searchValue)) {
		document.getElementById('codeLoadingImage').src = "code/Resources/Images/loading.gif";
		var poststr = "search=" + encodeURI(textBox.value);
		xmlHttp[httpRequestNum] = sendRequestToPhpFile('code/Submits/searchSubmit.php', poststr, SearchCallback);
		httpRequestNum++;
		if (httpRequestNum == MAX_REQUESTS) //allow up to 'MAX_REQUESTS' requests in a cycle;
			httpRequestNum = 0;
	}

}

function SearchCallback() {
	for (var i = 0; i < MAX_REQUESTS; i++) { //we have to find the request that was sent..
		if ((xmlHttp[i] != null) && (xmlHttp[i].readyState == 4)) {
			data = xmlHttp[i].responseText.split('\n');
			var onErrorDiv = document.getElementById('onErrorDiv');
			if (data[0] == 'error') {
				if (onErrorDiv != null) {
					onErrorDiv.innerHTML = "";
					for (var j = 1; j < data.length; j++) {
						onErrorDiv.innerHTML += data[j] + "<br>";
					}
					document.getElementById('contentDiv').innerHTML = "";
				}
			}
			else { //success
				onErrorDiv.innerHTML = "";
				//data[1] holds the search string that created this request
				//only display results in the tree if its the current search textfield value
				if (data[1] == document.getElementById('TextBoxSearch').value) {
					if (!isNaN(data[2])) {
						 var rowCount = parseInt(data[2]);
						 var contentDiv = document.getElementById('contentDiv');

						 while (contentDiv.childNodes.length > 0)
								contentDiv.removeChild(contentDiv.childNodes[0]);

						 var currentLanguage = '';
						 var div;
						 var numValues = 0;

						 for (var j = 3; j < rowCount + 3; j++) {
							  var keyValuePair = data[j].split('%&%', 4);
							  if (currentLanguage != keyValuePair[1]) {
								  if (div != null) {
									  div.setHeadingText(currentLanguage + "<b>&nbsp;(" + numValues + ")</b>");
									  numValues = 0;
								  }
								  currentLanguage = keyValuePair[1];
								  div = new PopopenDiv(document.getElementById('contentDiv'), currentLanguage, "", null, false);
							  }
							  numValues++;
							  var id = keyValuePair[2];
							  var codeDiv = new PopopenDiv(div.getContentDiv(), keyValuePair[0], "<pre>" + keyValuePair[3] + "</pre>", makeMenuDiv(id), true);
							  codeDiv.getWholeDiv().id = "code" + id;
						 }
						 div.setHeadingText(currentLanguage + "<b>&nbsp;(" + numValues + ")</b>");
						 //sometimes this is called from a popup, in which case we want to close the popup
						 closePopup();
					}
				}
			}
			document.getElementById('codeLoadingImage').src = "code/Resources/Images/emptyLoading.gif";
			xmlHttp[i] = null;
		}
	}
}

function makeMenuDiv(id) {
	  var menuDiv = document.createElement('div');
	  var editButton = document.createElement('img');
	  var deleteButton = document.createElement('img');
	  editButton.src = "code/Resources/Images/editButton.jpg";
	  deleteButton.src = "code/Resources/Images/deleteButton.jpg";
	  editButton.onclick = function () { popupPageInIFrame(window.document.body, 'code/insertCode.php?id='+id, 520, 450); };
	  deleteButton.onclick = function() { if (confirm('Delete code?')) submitToPhpFile('code/Submits/deleteCode.php', 'id=' + encodeURI(id), 'removeDiv(\''+id+'\')'); };
	  editButton.style.cursor = 'pointer';
	  deleteButton.style.cursor = 'pointer';
	  editButton.title = 'Edit this code snippet';
	  deleteButton.title = 'Delete this code snippet';

	  menuDiv.appendChild(editButton);
	  menuDiv.appendChild(deleteButton);
	  return menuDiv;
}

function removeDiv(id) {
	  var div = document.getElementById('code'+id);
	  div.parentNode.removeChild(div);
}

