
/**
 * project code name 'nepal': earthpoi.com v1.0
 * a free global public/private points of interest storage/sharing system
 * (c) 2006 by Bas van Gaalen & Ana Henneberke. all rights reserved.
 * $Id: utils.v1.0.js 465 2006-10-05 21:20:55Z bas $
 */

// window handling
var Win = {

	isOpen: false,
	oSize: null,

	open: function(sTpl, iWidth, iHeight, onCompleteFunc, sPars) {
		if (Win.isOpen) Win.close();
		Dialog._openDialog(App.msgLoading,
		{
			windowParameters: {
				className: 'dialog',
				width: 225
			}
		});
		Win.isOpen = true;
		if (!onCompleteFunc) onCompleteFunc = null;
		if (!sPars) sPars = '';
		Win._openDialog({
			url: Server.sUrl,
			options: {
				method: 'post',
				parameters: 'a=ght&f=html&t=' + sTpl + '&' + sPars,
				onComplete: onCompleteFunc
			}
		},
		{
			windowParameters: {
				className: 'dialog',
				width: iWidth
			}
		});

	},

	_openDialog: function(content, parameters) {
		if (typeof content != "string") {
			Dialog._runAjaxRequest(content, parameters, Win._openDialog);
			return
		}
		parameters = parameters || {};
		Win.close();
		Dialog._openDialog(content, parameters);
		Win.isOpen = true;
		Win.oSize = Dialog.win.getSize();
		Dialog.onCompleteFunc();
	},

	close: function(sHandler) {
		Dialog.closeInfo();
		Win.isOpen = false;
	},

	updateHeight: function() {
		Dialog.win.setSize(Win.oSize.width, Win.oSize.height + 65);
	},

	displayError: function(msg) {
		if (Win.isOpen) {
			Element.show('dialog_error_msg');
			$('dialog_error_msg').innerHTML = msg;
			Win.updateHeight();
		}
	}

}

// client-server communication
var Server = {

	sUrl: null,

	init: function(sUrl) {
		Server.sUrl = sUrl;
	},

	request: function(sParms, oFunc) {
		var myAjax = new Ajax.Request(Server.sUrl, {
			method: 'post', parameters: sParms, onComplete: oFunc});
	},

	requestAndUpdate: function(sElm, sParms, oFunc) {
		var oAjax = new Ajax.Updater(sElm, Server.sUrl, {
			method: 'get', parameters: sParms, onComplete: oFunc});
	}

}

// cookie handling
var Cookie = {

	get: function(name) {
		var start = document.cookie.indexOf(name + '=');
		if (start == -1 ) return null;
		if ((!start) && (name != document.cookie.substring(0, name.length))) return null;
		var len = start + name.length + 1;
		var end = document.cookie.indexOf(';', len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));
	},

	remove: function(name, path, domain) {
		if (Cookie.get(name)) document.cookie = name + '=' +
			((path) ? ';path=' + path : '') +
			((domain) ? ';domain=' + domain : '') +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}

}

// some utils
var Utils = {

	formatCoord: function(fLat, fLng) {

		var iLat		  = parseInt(fLat);
		var fLatDecPart   = fLat - iLat;
		var fLatMinutes   = Math.abs(fLatDecPart * 60);
		var iLatMinutes   = Math.floor(fLatMinutes);
		var iLatSeconds   = Math.round((fLatMinutes - iLatMinutes) * 60);
		var sLatDirection = (iLat >= 0) ? 'N' : 'S';
		var sLat		  = Math.abs(iLat) + '&deg;' + Utils.lz(iLatMinutes, 2) + '&prime;' + Utils.lz(iLatSeconds, 2) + '&Prime;&nbsp;' + sLatDirection;

		var iLng		  = parseInt(fLng);
		var fLngDecPart   = fLng - iLng;
		var fLngMinutes   = Math.abs(fLngDecPart * 60);
		var iLngMinutes   = Math.floor(fLngMinutes);
		var iLngSeconds   = Math.round((fLngMinutes - iLngMinutes) * 60);
		var sLngDirection = (iLng >= 0) ? 'E' : 'W';
		var sLng		  = Math.abs(iLng) + '&deg;' + Utils.lz(iLngMinutes, 2) + '&prime;' + Utils.lz(iLngSeconds, 2) + '&Prime;&nbsp;' + sLngDirection;

		return sLat + ', ' + sLng;

	},

	lz: function(v, f) {
		v = String(v);
		while (v.length < f) v = '0' + v;
		return v;
	}

}
