var gnWaitForMapTipsInterval = -1;
var deferredApplyMapTips = function(szLayer, szAttributes){
	//wait till the maptipsimage is available
	if (!document.getElementById('maptipsimage')) {
		if (gnWaitForMapTipsInterval === -1) {
			gnWaitForMapTipsInterval = setInterval(function(){
				deferredApplyMapTips(szLayer, szAttributes)
			}, 500);
		}
		return;
	}
	else {
		if (gnWaitForMapTipsInterval !== -1) {
			clearInterval(gnWaitForMapTipsInterval);
			gnWaitForMapTipsInterval = -1;
		}
	}
	//alert("deferred applyMapTips has applied maptips");
	//applyMapTips("", "", "", "");
	//$('maptipsimage').useMap = '#nullmap';
}

var gnWaitForLayersContentAvailable = -1;
var deferredOnMapGroupSelected = function(szLayer, szAttributes){
	//wait till the legend is available
	if (!($('layersContent').childNodes && $('layersContent').childNodes.length > 0)) {
		if (gnWaitForLayersContentAvailable === -1) {
			gnWaitForLayersContentAvailable = setInterval(function(){
				deferredOnMapGroupSelected(szLayer, szAttributes)
			}, 500);
			//alert("starting to wait for 'layersContent'");
		}
		
		return;
	}
	else {
		if (gnWaitForLayersContentAvailable !== -1) {
			clearInterval(gnWaitForLayersContentAvailable);
			gnWaitForLayersContentAvailable = -1;
		}
		//alert("deferred mapgroupselected has applied maptips selection");
	}
	
	//make the layer engage the maptips when it's turned on.
	for (var i = 0; i < $('layersContent').childNodes.length; i++) {
		if ($('layersContent').childNodes[i].firstChild.value == szLayer) {
			var oGroup = $('layersContent').childNodes[i].firstChild;
			if (goCWCJSAPI.bIE) {
				oGroup.attachEvent('onclick', function(){
					toggleMapTipsLayer(oGroup, szLayer, szAttributes);
				});
			}
			else {
				oGroup.addEventListener('click', function(){
					toggleMapTipsLayer(oGroup, szLayer, szAttributes);
				}, false);
			}
		}
	}
}

var toggleMapTipsLayer = function(oInput, szLayer, szAttributes){
	if (oInput.checked) {
		//need to wait for the JSAPI code to let the layer status settle
		window.setTimeout(function(){
			applyMapTips(szLayer, szAttributes, "", "");
			MapTipsLayerStatusChanged();
			if ($('myMaptipsLayer')) 
				$('myMaptipsLayer').parentNode.removeChild($('myMaptipsLayer'));
		}, 1000);
	}
}

//login to peopleforms
var gszPFSID;
(function(){
	new Ajax.Request('../form_server/htdocs/transaction.php', {
		method: 'post',
		parameters: 'request=login&user=viewer&password=viewer&fid=222',
		onComplete: function(xmlResults){
			if (xmlResults.responseJSON.authenticated === true) {
				gszPFSID = xmlResults.responseJSON.ssid;
			} else {
				//error authenticating...hmm.
			}
		}
	});
})();


var gbSticking = false;
function docmanPhotosMaptipsOver(e, content){
	if (gbSticking) return;
	e = (e) ? e : ((event) ? event : null);
	if ($('myMaptipsLayer')) 
		$('myMaptipsLayer').parentNode.removeChild($('myMaptipsLayer'));
	
	document.getElementsByName('NAV_SUBMIT')[0].value = "-1";
	
	
	var d = document.getElementById('myMaptipsLayer');
	// create the layer if necessary
	if (d == null) {
		d = document.createElement('div');
		d.id = 'myMaptipsLayer';
		document.body.appendChild(d);
	}
	
	// style the layer
	d.style.textAlign = "center"
	d.style.position = 'absolute';
	d.style.backgroundColor = "#ffffcf";
	d.style.border = "1px solid #000000";
	d.style.fontFamily = "arial";
	d.style.fontSize = "12px";
	d.style.zIndex = 501;
	d.style.top = (e.clientY + 10) + "px";
	d.style.left = (e.clientX + 10) + "px";
	d.style.visibility = "visible";
	d.style.padding = "2px";
	d.style.overflow = "visible";
	d.style.maxWidth = "350px";
	
	var sep = "";
	var result = "";
	
	d.innerHTML = 'loading photos...';
	// fetch matching documents
	new Ajax.Request('../form_server/htdocs/transaction.php', {
		method: 'post',
		parameters: 'field=mo_id&format=json&id=222&output_fields=doc_url&request=record_search&search=' + content[0] + '&ssid=' + gszPFSID,
		onComplete: function(xmlResponse){
			var nNodeIndex = findNodeByName(xmlResponse.responseXML, 'response');
			if (nNodeIndex == -1) {
				alert('Invalid response from server.');
				return;
			}
			var nHeight = 0;
			var szHtml = '';
			szJson = getNodeValue(xmlResponse.responseXML.childNodes[nNodeIndex]);
			aResults = eval("(" + szJson + ")");
			// process content
			for (var i = 0; i < aResults.length; i++) {
				var oItem = aResults[i];
				switch (oItem.doc_url.substr(oItem.doc_url.length - 3)) {
					case 'jpg':
					case 'png':
					case 'gif':
						var szDocUrlasPdf = oItem.doc_url.substring(0,oItem.doc_url.length-3) + "pdf";
						szDocUrlasPdf = szDocUrlasPdf.replace("jpg_188/","");
						szHtml += '<a href="' + szDocUrlasPdf + '" target="_blank"><img src="' + oItem.doc_url + '" style="height: 100px; margin: 5px 5px 5px 5px; border: 1px solid #000000;"/></a>';
						nHeight += 110;
						break;
				}
				
			}
			//d.style.height = nHeight + "px";
			if (szHtml.length == 0) {
				d.innerHTML = "no photos";
				d.nFadeOutDuration = .5;
				d.nFadeOutDelay = 200;
			} else {
				d.innerHTML = szHtml;
				d.nFadeOutDuration = 1;
				d.nFadeOutDelay = 2000;
			}
		}
	});
	
	
}

function docmanPhotosMaptipsOut(e, content){
	if (gbSticking) return;
	e = (e) ? window.event : e;
	document.getElementsByName('NAV_SUBMIT')[0].value = "1";
	
	var d = document.getElementById('myMaptipsLayer');
	if (d) d.style.display = 'none';
	
}

function docmanPhotosMaptipsClickToLoad(e, content){
	docmanPhotosMaptipsOut(e, content);
	window.open("forms/standalone.html.php?id=559&jump=" + content[1]);
}

function docmanPhotosMaptipsClickToStick(e, content){
	if (gbSticking) return;
	gbSticking = true;
	var d = document.getElementById('myMaptipsLayer');
	var nPhotoLoaded = window.setInterval(function() {
		if (d.innerHTML == 'loading photos...') {
			return;
		}
		window.clearInterval(nPhotoLoaded);
		var szClose = "<div style='text-align: right'><a style='color:transparent; padding: 1px 8px 2px 15px; background: transparent url(http://yui.yahooapis.com/2.7.0/build/assets/skins/sam/sprite.png) no-repeat scroll 0 -300px' onclick='docmanPhotosMaptipsClose()' href='#'>&nbsp;</a></div>";
		d.innerHTML = szClose + d.innerHTML;
	}, 200);
}

function docmanPhotosMaptipsClose() {
	gbSticking = false;
	var d = document.getElementById('myMaptipsLayer');
	d.style.display = 'none';
	document.getElementsByName('NAV_SUBMIT')[0].value = "1";
}