/**
 * @author Kirk Ouimet
 * @website http://www.yougetsignal.com/tools/network-location/
 * @copyright 2008 Kirk Ouimet Design. All rights reserved.
 */

function changeNav(div) {
	document.getElementById(div).style.background = "url('/img/link_bg_selected.gif') no-repeat";
}

/* AJAX request to initialize the Google Map to the default remote address
-------------------------------------------------------------------*/
var map;
var load = 1;
function loadMap() {
	if(GBrowserIsCompatible()) {
		if(load == 1)
		{
		map = new GMap2(document.getElementById("map"));
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		gKeyboardHandler = new GKeyboardHandler(map);
		updateMap(document.getElementById('ip').value); // Empty string puts the location to the user's IP
		// Fix the page scroll on zoom issue
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
		map.getContainer().onmousewheel = wheelevent; 				
		load = 2;
		}
		else
		{
		updateMap(document.getElementById('ip').value); // Empty string puts the location to the user's IP
		}
	}
}

/* AJAX request to locate a new location on the Google Map
-------------------------------------------------------------------*/
var centerSet = false; // Used for special start case
var oldLatLng;
var distanceInMiles = "0";
var gLatLng;
var ipJsonData;
var geoSource = "";
var ajaxUpdater = new Ajax.Request('', {});
function updateMap(remoteAddress) {

		// Notify the user a request is in process
		// Perform the request
		//url = "/tools/network-location/php/get-network-location-json.php";
		url = "/wp-content/themes/wpthemegen/ipLocationBackend.php";
		geoSource = "geoip";
		ajaxUpdater.transport.abort(); // Cancel the previous request		
		ajaxUpdater = new Ajax.Request(url, {
		method: 'get',
		parameters: {'remoteAddress': remoteAddress, 'geoSource': geoSource},
		onFailure: function(transport) {
			alert("Service currently unavailable (Failure).");
		},
		onException: function(transport, exception) {
			//alert("Service currently unavailable (Exception).");
			alert("This is an exception\n" + exception);
		},				
		onSuccess: function(transport) {
			ipJsonData = transport.responseText.evalJSON(); // Place the results into JSON
			if(ipJsonData.status == "Fail" || ipJsonData.latitude == "Unknown") {
				//document.getElementById('networkLoaderStatus').innerHTML = "<span style=\"color: #DF454B;\">Unable to locate network.</span>";
				//document.getElementById('networkInfo').innerHTML = ipJsonData.message;
			}
			else {
				gLatLng = new GLatLng(ipJsonData.latitude, ipJsonData.longitude); // Create a new Google point
				// Special start case
				if(centerSet == false) {
					map.setCenter(new GLatLng(ipJsonData.latitude, ipJsonData.longitude), 7);
					centerSet = true;
				}
				else
				{
				map.clearOverlays();
				}
		
				// Create a marker
				markerHTML = ipJsonData.ipAddress + "<br />" + ipJsonData.baseDomain + "<br />" + ipJsonData.city + ", " + ipJsonData.region + "<br />"  + ipJsonData.countryName + "&nbsp;" + ipJsonData.countryFlag;
				placeMarker(gLatLng, markerHTML);
				
				map.setCenter(new GLatLng(ipJsonData.latitude, ipJsonData.longitude), 7);
				map.savePosition(new GLatLng(ipJsonData.latitude, ipJsonData.longitude)) ;
				
				// Notify the user the request has finished
				//document.getElementById('networkLoaderStatus').innerHTML = "Location identified.";
				//document.getElementById('networkInfo').innerHTML = "Found!";
				setTimeout("updateNetworkInfo()", 400);
				//new Effect.Fade('networkLoaderStatus', { duration: 1.5 });
				
				// Set the new point to the old point
				oldLatLng = gLatLng;
			}
		}
	});
}

/* Function to place a new marker on the map
-------------------------------------------------------------------*/
function placeMarker(point, html) {
	var gMarker = new GMarker(gLatLng);
	map.addOverlay(gMarker);	
	GEvent.addListener(gMarker, "click", function() {
		gMarker.openInfoWindowHtml(html);
	});
}

/* Submit AJAX request using enter key
-------------------------------------------------------------------*/
function submitUsingEnter(e) {
	var characterCode;
	if(e && e.which){ // If which property of event object is supported (NN4)
		e = e;
		characterCode = e.which // Character code is contained in NN4's which property
	}
	else {
		e = event;
		characterCode = e.keyCode; // Character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ // If generated character code is equal to ASCII 13 (the enter key)
		updateMap(document.getElementById('remoteAddress').value);
		return false
	}
	else {
		return true
	}
}

/* Function to fix the page scroll issue with map zoom
-------------------------------------------------------------------*/
function wheelevent(e) {
	if (!e){
		e = window.event
	}
	if (e.preventDefault){
		e.preventDefault()
	}
	e.returnValue = false;
}


function sendRequest()
{
                              new Ajax.Request("/wp-content/themes/wpthemegen/emailHeadersBackend.php",
                              {
                                  method: 'post',
                                  parameters: {'emailHeader': document.f1.emailHeader.value, 'infosent': 1, 'timezone': document.f1.timezone.value},
                                  onComplete: showResponse
                              });
                          }



  function sendRequestBL(IP,DIV)
                          {
                              $(DIV).innerHTML= "<img src=\"/images/loader.gif\">";
                                //alert(IP);
                              new Ajax.Request("/wp-content/themes/wpthemegen/bl.php",
                              {
                                  method: 'get',
                                  parameters: {'domain': IP, 'show': 'errors'},
                                  onComplete: function(req){;
                                        if(req.responseText != '' && req.responseText.indexOf('orry, but you are looking for something that isn\'t') == -1)
                                         $(DIV).innerHTML = req.responseText;
                                         else if(req.responseText.indexOf('orry, but you are looking for something that isn\'t ') >= 0)
                                         $(DIV).innerHTML = "Error occured, my bad.";
                                        else $(DIV).innerHTML = "No Blacklists from what I see.";}
                              });
                          }

function updateNetworkInfo() {
        var networkInfo =  "<p>Originating IP: <b>" + ipJsonData.ipAddress + "</b></p>" +
        "<p>Country: <b>" + ipJsonData.countryName + " " + ipJsonData.countryFlag + "</b></p>" +
        "<p>Region: <b>" + ipJsonData.region + "</b></p>" +
        "<p>City: <b>" + ipJsonData.city + "</b></p>" +
        "<p>Latitude: <b>" + ipJsonData.latitude + "</b></p>" +
        "<p>Longitude: <b>" + ipJsonData.longitude + "</b></p>" +
        "<p>Area Code: <b>" + ipJsonData.areaCode + "</b></p>" +
        "<p>Postal Code: <b>" + ipJsonData.postalCode + "</b></p>"
        ;
	document.getElementById('networkInfo').innerHTML = networkInfo;
}


