//
//  GeoPostcodes - JS (C) 2009
//

var ggmap, ggmap2;
var gmark, gicon, gmark2, gpin;
var gmenu = false;
var mapinit = false;

function tab_over(obj)
{
	if (obj.className != "tabsel") obj.className = "tabover";
}

function tab_out(obj)
{
	if (obj.className != "tabsel") obj.className = "tab";
}

function download($niv, $id)
{
	showdiv("pop");
	// alert("Téléchargement non disponible !");
}

function showmenulng()
{
	x = getLeft(document.getElementById("imglng"))-178;
	document.getElementById("menulng").style.left = x+"px";
	document.getElementById("imglng").style.border='1px solid #DDDDDD';
	document.getElementById("menulng").style.visibility = "visible";
	document.getElementById("lngmtxt").style.color = "#E0E0E0";
}

function hidemenulng()
{
	document.getElementById("imglng").style.border='1px solid #666666';
	document.getElementById("menulng").style.visibility = "hidden";
	document.getElementById("lngmtxt").style.color = "";
}

function changelng(lng, path)
{
	var date = new Date();
	date.setTime(date.getTime()+(100*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();

	// Attention : changer les cookies en fonction du menu
	if (lng == 0) document.cookie = "NavLng=en"+expires+"; path=/";
	if (lng == 1) document.cookie = "NavLng=es"+expires+"; path=/";
	if (lng == 2) document.cookie = "NavLng=fr"+expires+"; path=/";
	if (lng == 3) document.cookie = "NavLng=de"+expires+"; path=/";
	if (lng == 4) document.cookie = "NavLng=pt"+expires+"; path=/";
	if (lng == 5) document.cookie = "NavLng=it"+expires+"; path=/";
	if (lng == 6) document.cookie = "NavLng=nl"+expires+"; path=/";
	if (lng == 7) document.cookie = "NavLng=tr"+expires+"; path=/";
	if (lng == 8) document.cookie = "NavLng=ro"+expires+"; path=/";

	hidemenulng();
	window.location.href = "http://www.geopostcodes.com"+path;
}

function showdiv(id)
{
	var curY = 0;

	if (self.pageYOffset)
		curY = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		curY = document.documentElement.scrollTop;
	else if (document.body)
		curY = document.body.scrollTop;

	document.getElementById(id).style.marginTop = curY-140;
	document.getElementById(id).style.visibility = "visible";
}

function getLeft(obj)
{
	var left = !!obj.offsetLeft ? obj.offsetLeft : 0;
	while(obj = obj.offsetParent) {
		left += !!obj.offsetLeft ? obj.offsetLeft : 0;
	}
	return left;
}

function getDocHeight()
{
    var D = document;

	return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function preload(image)
{
   img = new Image();
   img.src = image;
}

// AddThis

var  addthis_url   = '';
var  addthis_title = '';

function addthis_click(obj, str) {
	var aturl  = 'http://www.addthis.com/bookmark.php?v=10&url='+encodeURIComponent(addthis_url)+'&title='+encodeURIComponent(addthis_title)+'&pub=geopostcodes';
	window.open(aturl, 'addthis', 'scrollbars=yes,menubar=no,width=620,height=600,resizable=yes,toolbar=no,location=no,status=no,screenX=200,screenY=100,left=200,top=100');
	return false;
}

// Google Maps

function fullscr()
{
	window.open('http://maps.google.com/maps?q='+glat+'+'+glng+'&z=12');
}

function clicMenu()
{
	if (gmenu) {
		gmenu = false;
		document.getElementById("mapmenu").style.visibility = "hidden";
	} else {
		gmenu = true;
		document.getElementById("mapmenu").style.visibility = "visible";
	}
}

function GMapInit()
{
	if (GBrowserIsCompatible()) {
		ggmap = new GMap2(document.getElementById("map2"));
		ggmap.setUIToDefault();
		ggmap.removeMapType(G_HYBRID_MAP);
		ggmap.removeMapType(G_SATELLITE_MAP);
		ggmap.removeMapType(G_PHYSICAL_MAP);

		gicon = new GIcon();
		gicon.image = "/pictures/panel.png";
		gicon.iconSize = new GSize(54, 45);
		gicon.iconAnchor = new GPoint(27, 44);

//		gmark = new GMarker(new GLatLng(0, 0), {icon:gicon; draggable: true});	
		gmark = new GMarker(new GLatLng(0, 0), {icon:gicon});	
		ggmap.addOverlay(gmark);

//		GEvent.addListener(ggmap, 'tilesloaded', function(a) {
//			document.getElementById('map').style.visibility = 'visible';
//		});

		ggmap2 = new GMap2(document.getElementById("map3"));
		ggmap2.setMapType(G_PHYSICAL_MAP);
		ggmap2.removeMapType(G_HYBRID_MAP);
		ggmap2.removeMapType(G_SATELLITE_MAP);
		ggmap2.removeMapType(G_PHYSICAL_MAP);
		// ggmap2.addControl(new GSmallZoomControl());

		gpin = new GIcon();
		gpin.image = "/pictures/pin2.gif";
		gpin.iconSize = new GSize(10, 10);
		gpin.iconAnchor = new GPoint(5, 5);

		gmark2 = new GMarker(new GLatLng(0, 0), {icon:gpin});	
		ggmap2.addOverlay(gmark2);

		GEvent.addListener(gmark2 ,"click", function() {
			ggmap.returnToSavedPosition();
		});

		mapinit = true;
	}
}

function ShowMap(id)
{
	if (mapinit) {
		document.getElementById("mapcache").style.height = getDocHeight()+"px";

		var el = ajax('http://www.geopostcodes.com/ajax_map.php?id='+id).split("!");
		var iso  = el[0];
		var cp   = el[1];
		var tag  = el[2];
		var glat = el[3];
		var glng = el[4];
		var utc  = el[5];
		var prox = el[6];

		var yoffset = (window.pageYOffset) ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		yoffset = yoffset-258;
		soffset = yoffset+22;

		gicon.image = "/pan.php?id="+cp;
		ggmap.clearOverlays();
		gmark = new GMarker(new GLatLng(0, 0), {icon:gicon});	
		ggmap.addOverlay(gmark);
		gmark.setLatLng(new GLatLng(glat, glng));
		ggmap.setCenter(new GLatLng(glat, glng), 12);
		gmark2.setLatLng(new GLatLng(glat, glng));
		ggmap2.setCenter(new GLatLng(glat, glng), 2);
		ggmap.savePosition();

		document.getElementById("maptag").innerHTML = tag;
		document.getElementById("maputc").innerHTML = utc;
		document.getElementById("plces").innerHTML = prox;
		document.getElementById("mapflag").style.backgroundImage = "url(/flags/"+iso+".gif)";
		document.getElementById("map").style.marginTop = yoffset+"px";
		document.getElementById("popshad").style.marginTop = soffset+"px";

		document.getElementById("mapcache").style.visibility = "visible";
		document.getElementById("popshad").style.visibility = "visible";
		document.getElementById("map").style.visibility = "visible";
	}
}

function HideMap()
{
	gmenu = false;
	document.getElementById("mapcache").style.visibility = "hidden";
	document.getElementById("mapmenu").style.visibility = "hidden";
	document.getElementById("popshad").style.visibility = "hidden";
	document.getElementById("map").style.visibility = "hidden";
}

function sendReport()
{
	r1 = (document.getElementById("rep1").checked)?1:0;
	r2 = (document.getElementById("rep2").checked)?1:0;
	r3 = (document.getElementById("rep3").checked)?1:0;
	tx = document.getElementById("mptxt").value;
	// preload("report.php?id="+gid+"&n="+r1+"&r="+r2+"&p="+r3+"&tx="+tx);
	alert("Thank you !");
	clicMenu();
	document.getElementById("rep1").checked = false;
	document.getElementById("rep2").checked = false;
	document.getElementById("rep3").checked = false;
	document.getElementById("mptxt").value = "";
}

var posterr = "";
var postpays = "";

function trim(txt)
{
	return txt.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function chgpays()
{
	postpays = document.getElementById("frmCountry").value;
}

function post(to, p)
{
	var myForm = document.createElement("form");
	myForm.method="post";
	myForm.action = to;
	for (var k in p) {
		var myInput = document.createElement("input");
		myInput.setAttribute("name", k) ;
		myInput.setAttribute("value", p[k]);
		myForm.appendChild(myInput);
	}
	document.body.appendChild(myForm);
	myForm.submit();
	document.body.removeChild(myForm);
}

function postsearch()
{
	var txt = trim(document.getElementById("frmSearch").value);

	if (txt.length < 2)
		alert(posterr);
	else {
		if (postpays != "")
			post('index.php', {pg:'search', country:postpays, search:txt});
		else
			post('index.php', {pg:'search', search:txt});
	}
}

function postkey(e)
{
	if (!e) e = window.event;
	if (e.keyCode == 13) postsearch();
}

function ajax(url)
{
	if (window.XMLHttpRequest) {
		xml = new XMLHttpRequest();
	} else {
		xml = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xml) {
		xml.open("GET", url, false);
		xml.send(null);
		return xml.responseText;
	} else {
		return "error";
	}
}
