// (c) 2001 - 2009 Netsilik
// Internet : http://www.netsilik.nl
// E-mail   : info (at) netsilik x nl
var circle = new Array;
var X = 0;
var Y = 1;
var R = 2;
var WAYPOINTID = 3;
var PH = 0;
var LM = 1;
var timerId = 0;
var activeToolTipId = 'none';
var SHOW_DELAY  = 50;
var HIDE_DELAY = 100;
var mousePos = new Array(0, 0);
var dragging = 0;
var dragOrigin = new Array(0, 0);
var dragDest = new Array(0, 0);

Math.PI_2 = 1.57079632679;


function addCircle(x, y, radius, waypointId) {
	var index = circle.length;
	circle[index] = new Array;
	circle[index][X] = x;
	circle[index][Y] = y;
	circle[index][R] = radius;
	circle[index][WAYPOINTID] = waypointId;
}
function pointInCircle(testX, testY) {
	for (n = 0; n < circle.length; n++) {
        if (((circle[n][Y] - testY) * (circle[n][Y] - testY)) + ((circle[n][X] - testX) * (circle[n][X] - testX)) <= circle[n][R] * circle[n][R]) {
        	return n;
    	}
    }
    return -1;
}

function trackMouse(event, elmnt) {
	var tmpMouse, tmpPoint, latLong;
	
	tmpMouse = getMousePos(event, elmnt);

	if (tmpMouse == false) {
		window.status = window.defaultStatus;	
		return false;
	}
	
	tmpPoint = getLatLong(tmpMouse[X], tmpMouse[Y]);
	if (tmpPoint == false) {
		window.status = window.defaultStatus;
		return false;
	}
	mousePos = tmpMouse;
	latLong = formatLatLong(tmpPoint);
	if (dragging == 0) { // not dragging
		window.status = 'Latitude: ' + latLong[PH] + ', Longitude: ' + latLong[LM] + '.';
		
		n = pointInCircle(mousePos[X], mousePos[Y]);
		if (n >= 0) {
			showToolTip(n);
		} else {
			hideToolTip();
		}
	} else if (dragging == 1) { // start dragging
		dragOrigin = mousePos;
		dragging = 2;
		window.status = 'Latitude: ' + latLong[PH] + ', Longitude: ' + latLong[LM] + '.';
	}
	return true;
}

function dragStart() {
	dragging = 1;
	return true;
}

function dragCancel() {
	window.status = window.defaultStatus;
	dragOrigin = new Array(0, 0);
	dragDest = new Array(0, 0);
	dragging = 0;
	return true;
}

function dragStop() {
	if (dragging == 2) {
		dragDest = mousePos;
	}
	dragging = 0;
	return true;
}

function submitCheck() {
	var latLong, nextClickValue;
	if ((dragDest[X] == 0 && dragDest[Y] == 0) || (dragOrigin[X] - dragDest[X] == 0 && dragOrigin[Y] - dragDest[Y] == 0)) {
		return true;	
	}
	var nextUri = nextUri = getBaseUri();
		nextUri += '?ph=' + document.forms['globeForm'].ph.value;
		nextUri += '&lm=' + document.forms['globeForm'].lm.value;
		nextUri += '&zl=' + document.forms['globeForm'].zl.value;
		nextUri += '&ox=' + dragOrigin[X];
		nextUri += '&oy=' + dragOrigin[Y];
		nextUri += '&dx=' + dragDest[X];
		nextUri += '&dy=' + dragDest[Y];
		nextUri += getNextClickValue();
		nextUri += getAlbumNameValue();
		nextUri += getSessionIdValue();
	document.location.href = nextUri;
	return false;	
}

function formatLatLong(point) {
	var phDegrees = Math.abs(point[X]);
	var phMinutes = (phDegrees - Math.floor(phDegrees)) * 60;
	var phSeconds = (phMinutes - Math.floor(phMinutes)) * 60;
	phDegrees = Math.floor(phDegrees) + String.fromCharCode(176) + ' ' + Math.floor(phMinutes) + "'' " + Math.round(phSeconds) + "' ";
	phDegrees += (point[X] < 0) ? 'S' : 'N';
	
	var lmDegrees = Math.abs(point[Y]);
	var lmMinutes = (lmDegrees - Math.floor(lmDegrees)) * 60;
	var lmSeconds = (lmMinutes - Math.floor(lmMinutes)) * 60;
	lmDegrees = Math.floor(lmDegrees) + String.fromCharCode(176) + ' ' + Math.floor(lmMinutes) + "'' " + Math.round(lmSeconds) + "' ";
	lmDegrees += (point[Y] < 0) ? 'W' : 'E';
	
	return Array(phDegrees, lmDegrees);
}

function rightClick(event, elmnt) {
	event.cancelBubble = true;
	event.returnValue = false;
	
	if (document.forms['globeForm'].zl.value == 1) {
		return false;
	}
	
	mousePos = getMousePos(event, elmnt);
	
	var point = getLatLong(mousePos[X], mousePos[Y]);
	if (point != false) {			
		
		formattedPoint = formatLatLong(point);
		
		clearTimer();
		document.getElementById('toolTipDiv').innerHTML = '<table cellspacing="0" cellpadding="0" border="0"><tr><td>Latitude:&nbsp;</td><td>' + formattedPoint[PH] + '</td></tr><tr><td>Longitude:&nbsp;</td><td>' + formattedPoint[LM] + '</td></tr></table>';
		timerId = setTimeout("toggleToolTip('toolTipDiv', " + mousePos[X] + ", " + mousePos[Y] + ")", 10);
	}
	return false;
}
function getMousePos(event, elmnt) {
	var mouseX, mouseY;
	
	if (typeof(event.offsetX) != 'undefined') {
		mouseX = event.offsetX + 1;
		mouseY = event.offsetY + 1;
		
		return Array(mouseX, mouseY);
	} else if(typeof(event.clientX) != 'undefined' && typeof(elmnt.offsetParent) != 'undefined' && typeof(window.scrollX) != 'undefined') {
		// The mozilla way
		mouseX = event.clientX + window.scrollX + 1;;
		mouseY = event.clientY + window.scrollY + 1;;
		
		while (elmnt.offsetParent != null) {
			elmnt = elmnt.offsetParent;
			mouseX -= elmnt.offsetLeft;
			mouseY -= elmnt.offsetTop;
		}
		
		return Array(mouseX, mouseY);
	}
	return false;	
}
function showToolTip(num) {
	var waypointId, tooltipHtml;
	var uri;
	clearTimer();
	
	if (typeof(circle[num]) == 'object') {
		waypointId = circle[num][WAYPOINTID];
		if (typeof(waypointInfo) == 'object') {
			tooltipHtml = '<div class="nobr">'+waypointInfo[waypointId]['num']+': <b>'+waypointInfo[waypointId]['name']+'</b>';
			if (typeof(document.forms['globeForm'].sessionId) != 'undefined') {
				
				
				tooltipHtml += ' <a href="/globe/';
				tooltipHtml += (waypointInfo[waypointId]['name'] == '') ? 'nameWaypoint' : 'renameWaypoint';
				tooltipHtml += '/waypointId-' + circle[num][WAYPOINTID] + '/index.php?sessionId=' + document.forms['globeForm'].sessionId.value + '">[ ';
				tooltipHtml += (waypointInfo[waypointId]['name'] == '') ? 'Enter name' : 'Rename';
				tooltipHtml += ' ]</a>';
			}
			tooltipHtml += '</div>\n';
			
			if (waypointInfo[waypointId]['travelLog'].length > 0) {
				
				
				tooltipHtml += '<span class="nobr">';
				tooltipHtml += (waypointInfo[waypointId]['travelLog'].length == 1) ? 'TravelLog' : 'TravelLogs';
				tooltipHtml += '</span><ul>\n';
				
				for ( var i in waypointInfo[waypointId]['travelLog'] ) {
					uri = waypointInfo[waypointId]['travelLog'][i][0];
					if (typeof(document.forms['globeForm'].sessionId) != 'undefined') {
						uri += 'index.php?sessionId=' + document.forms['globeForm'].sessionId.value;
					}
					tooltipHtml += '\t<li class="nobr"><a href="'+uri+'">'+waypointInfo[waypointId]['travelLog'][i][1]+'</a></li>\n';
				}
				tooltipHtml += '</ul>\n';
			}
			if (waypointInfo[waypointId]['album'].length > 0) {
				tooltipHtml += '<span class="nobr">';
				tooltipHtml += (waypointInfo[waypointId]['album'].length == 1) ? 'Photo Album' : 'Photo Albums';
				tooltipHtml += '</span><ul>\n';
				for ( var i in waypointInfo[waypointId]['album'] ) {
					uri = waypointInfo[waypointId]['album'][i][0];
					if (typeof(document.forms['globeForm'].sessionId) != 'undefined') {
						uri += 'index.php?sessionId=' + document.forms['globeForm'].sessionId.value;
					}
					tooltipHtml += '\t<li class="nobr"><a href="'+uri+'">'+waypointInfo[waypointId]['album'][i][1]+'</a></li>\n';
				}
				tooltipHtml += '</ul>\n';
			}
		} else {
			tooltipHtml = waypointName[waypointId];
		}
		document.getElementById('toolTipDiv').innerHTML = tooltipHtml;
		
		timerId = setTimeout("toggleToolTip('toolTipDiv', " + circle[num][X] + ", " + circle[num][Y] + ")", SHOW_DELAY);
	}
}
function hideToolTip() {
	clearTimer();
	timerId = setTimeout("toggleToolTip('none', 0, 0)", HIDE_DELAY);
}
function toggleToolTip(toolTipId, toolTipX, toolTipY) {
	if (toolTipId != 'none') { //show
		document.getElementById('toolTipPtrDiv0').style.left = toolTipX + 'px';
		document.getElementById('toolTipPtrDiv0').style.top = toolTipY + 'px';
		document.getElementById('toolTipPtrDiv0').style.visibility = 'visible';
		
		document.getElementById('toolTipPtrDiv1').style.left = (toolTipX + 2) + 'px';
		document.getElementById('toolTipPtrDiv1').style.top = (toolTipY + 3) + 'px';
		document.getElementById('toolTipPtrDiv1').style.visibility = 'visible';
		
		if (activeToolTipId != 'none') { // hide;
			document.getElementById(activeToolTipId).style.visibility = 'hidden';
		}
		document.getElementById(toolTipId).style.left = (toolTipX + 5) + 'px';
		document.getElementById(toolTipId).style.top = (toolTipY + 6) + 'px';
		document.getElementById(toolTipId).style.visibility = 'visible'
		
		activeToolTipId = toolTipId;
	} else if (activeToolTipId != 'none') { // hide;
		document.getElementById(activeToolTipId).style.visibility = 'hidden';
		document.getElementById('toolTipPtrDiv1').style.visibility = 'hidden';
		document.getElementById('toolTipPtrDiv0').style.visibility = 'hidden';
		activeToolTipId = 'none';
	}
}
function clearTimer() {
	if (timerId) {
		clearTimeout(timerId);
		timerId = 0;
	}
}

function setZoom(zl) {
	var nextUri = getBaseUri();
		nextUri += '?ph=' + document.forms['globeForm'].ph.value;
		nextUri += '&lm=' + document.forms['globeForm'].lm.value;
		nextUri += '&zl=' + zl;
		nextUri += getNextClickValue();
		nextUri += getAlbumNameValue();
		nextUri += getSessionIdValue();
	document.location.href = nextUri;
	return false;
}


function getBaseUri() {
	
	var baseUri = document.forms['globeForm'].action.substr(0, document.forms['globeForm'].action.indexOf('index.php'));
	if (typeof(document.forms['globeForm'].waypointId) != 'undefined') {
		baseUri += 'waypointId-' + document.forms['globeForm'].waypointId.value + '/';
	}
	if (typeof(document.forms['globeForm'].countryPointId) != 'undefined') {
		baseUri += 'countryPointId-' + document.forms['globeForm'].countryPointId.options[document.forms['globeForm'].countryPointId.selectedIndex].value + '/';
	}
	if (typeof(document.forms['globeForm'].cityPointId) != 'undefined') {
		baseUri += 'cityPointId-' + document.forms['globeForm'].cityPointId.options[document.forms['globeForm'].cityPointId.selectedIndex].value + '/';
	}
	baseUri += 'index.php'

	return baseUri;
}
function getAlbumNameValue() { // for create new album only
	if (typeof(document.forms['globeForm'].albumName) == 'undefined') {
		return '';
	}
	return '&albumName=' + document.forms['globeForm'].albumName.value;
}
function getNextClickValue() {
	if (typeof(document.forms['globeForm'].nextClick) == 'undefined') {
		return '';	
	}
	nextClickValue = 'centerView';
	for (n = 0; n < document.forms['globeForm'].nextClick.length; n++) {
		if (document.forms['globeForm'].nextClick[n].checked) {
			nextClickValue = document.forms['globeForm'].nextClick[n].value;
		}
	}
	return '&nextClick=' + nextClickValue;
}
function getSessionIdValue() {
	if (typeof(document.forms['globeForm'].sessionId) == 'undefined') {
		return '';
	}
	return '&sessionId=' + document.forms['globeForm'].sessionId.value;
}
//Inverse Orthographic Transformation
function getLatLong(x, y) {
	var ph1, lm0;
	var ph, lm;
	var rho, tmp, c;
	
	if (document.forms['globeForm'].zl.value == 1) {
		return false;	
	}
	
	x = x - CENTER_X;
	y = CENTER_Y - y;
	ph1 =  document.forms['globeForm'].ph.value * Math.PI / 180;
	lm0 =  document.forms['globeForm'].lm.value * Math.PI / 180;
	
	rho = Math.sqrt( x * x + y * y);
	tmp = rho / RADIUS / ZOOMFACTOR;
	
	if (Math.abs(tmp) > 1) {
		return false;
	}
	
	c = Math.asin(tmp);	
	ph = (rho > 0) ? ph = Math.asin( Math.cos(c) * Math.sin(ph1) + (y * Math.sin(c) * Math.cos(ph1) / rho)) : ph1;
	if (ph1 == 90) {
		lm = lm0 + Math.atan(x / - y);
	} else if (ph1 == - 90) {
		lm =  lm0 + Math.atan(x / y);
	} else {
		lm = lm0 + Math.atan2(x * Math.sin(c) , (rho * Math.cos(ph1) * Math.cos(c) - y * Math.sin(ph1) * Math.sin(c)));
	}
	
	if (ph > Math.PI_2) {
		ph -= Math.PI;
	} else if (ph < - Math.PI_2) {
		ph += Math.PI;
	}
	if (lm > Math.PI) {
		lm -= Math.PI * 2;
	} else if (lm < - Math.PI) {
		lm += Math.PI * 2;
	}
	
	ph = ph * 180 / Math.PI;
	lm = lm * 180 / Math.PI;
	
	return Array(ph, lm);
}
function cancelEvent(event) {
	if(event.stopPropagation) {
		event.stopPropagation();
	}
	if(event.preventDefault) {
		event.preventDefault();
	}
	event.cancelBubble = true;
	event.cancel = true;
	event.returnValue = false;
	return false;
}