function isMouseLeaveOrEnter(e, handler) {
	if (e.type != 'mouseout' && e.type != 'mouseover')
		return false;
	var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
	while (reltg && reltg != handler) reltg = reltg.parentNode;
  return (reltg != handler);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function call(page, func, id, params, fadeOut, fadeIn) {
	var func = func || "";
	var id = id || "content";
	var params = params || "";
	var fadeIn = fadeIn || "true";
	var fadeOut = fadeOut || "false";
	if (fadeOut == "true") {
		new Effect.Parallel ([
							new Effect.BlindUp  (id, { sync: true }),
							new Effect.Opacity	(id, { sync: true, to: 0.0, from: 1.0 })
						 	],
						 	{
							 	duration:	1,
							 	fps:		100,
							 	afterFinish: function () {
							 		x_call(page, func, id, params, fadeIn);
								}
							});
	} else {
 		x_call(page, func, id, params, fadeIn);
	}
}

function callback(id) {
	new Effect.Parallel ([
						new Effect.BlindDown(id, { sync: true }),
						new Effect.Opacity	(id, { sync: true, to: 1.0, from: 0.0 })
					 	],
					 	{
						 	duration:	0.5,
						 	fps:		100
						});
}


var rollTimeOut = Array();
var RollIt = {
    timeout : Array(),
    showPopup : function(id){
        clearTimeout(rollTimeOut[id]);
        if($(id).style.display == 'none'){
            rollTimeOut[id] = setTimeout(function() {
				new Effect.BlindDown(id, {duration: 0.35, fps:50})
			/*	new Effect.Parallel ([
							new Effect.BlindDown(id, { sync: true }),
							new Effect.Opacity	(id, { sync: true, to: 1.0, from: 0.0 })
						 	],
						 	{
							 	duration:	0.35,
							 	fps:		50
							});*/
			},100);
        }
    },
    hidePopup : function(id){
            clearTimeout(rollTimeOut[id]);
        if($(id).style.display != 'none'){
            rollTimeOut[id] = setTimeout(function() {
				new Effect.BlindUp(id, {duration: 0.35, fps:50})
			/*	new Effect.Parallel ([
							new Effect.BlindUp  (id, { sync: true }),
							new Effect.Opacity	(id, { sync: true, to: 0.0, from: 1.0 })
						 	],
						 	{
							 	duration:	0.35,
							 	fps:		50
							});*/
			},280);
		}
    }
}

// Sample Usage:
//
// function callbackfunc(jsonData) {
//      alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude +
//            '  Longitude = ' + jsonData.ResultSet.Result[0].Longitude);
//      aObj.removeScriptTag();
// }
//
// request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&
//            output=json&callback=callbackfunc&location=78704';
// aObj = new JSONscriptRequest(request);
// aObj.buildScriptTag();
// aObj.addScriptTag();
//
//


// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
//JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");

    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

// removeScriptTag method
//
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    if (!this.headLoc.removeChild(this.scriptObj)) {
    	// No tag to remove
    }
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

// function callbackfunc(jsonData) {
//      alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude +
//            '  Longitude = ' + jsonData.ResultSet.Result[0].Longitude);
//      aObj.removeScriptTag();
// }
//
// request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&
//            output=json&callback=callbackfunc&location=78704';
// aObj = new JSONscriptRequest(request);
// aObj.buildScriptTag();
// aObj.addScriptTag();

var aObj;

function geo(lat, lng){
 request = 'http://ws.geonames.org/findNearbyPlaceNameJSON?lat=' + lat + '&lng=' + lng + '&callback=geoCallback';
 aObj = new JSONscriptRequest(request);
 aObj.buildScriptTag();
 aObj.addScriptTag();
}

function geoCallback(jsonData) {
  $('message').innerHTML = (jsonData.geonames[0].name) 	+ " "
	 						 + (jsonData.geonames[0].adminName1) 	+ " "
							 + (jsonData.geonames[0].countryName);
	$('message').innerHTML += "<br/><br/><button onclick=\"window.location='search.php?city=" + (jsonData.geonames[0].name) + "&prov=" + (jsonData.geonames[0].adminName1) + "&country=" + (jsonData.geonames[0].countryName) + "';\">Search</button>";
    aObj.removeScriptTag();
}
