var markers = [];
var infowindow = [];
var countryopen = "";
var opennow = "";
var showdrop = "";

function scrollcountry(e){
	if(showdrop == "ok"){
		astr = String.fromCharCode(e.which)
		$('div.countries').scrollTo($('#letr'+astr),200);
	}
}

function stopRKey(evt) { 
  	var evt = (evt) ? evt : ((event) ? event : null); 
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  	if ((evt.keyCode == 13) && (node.type=="text"))  {
		getloc();
		return false;
	}
} 

document.onkeypress = stopRKey; 

function selectcountrynow(country){
	countryopen = "";
	document.getElementById("locdropdownmenu").innerHTML = "&nbsp;&nbsp;"+country;
}

function opencountry(){
	if(opennow == "ok"){
		$("#countryselect").css({display: "block"});
		opennow = "";
		showdrop = "ok";
	} else {
		$("#countryselect").css({display: "none"});
		showdrop = "";
	}
}

function clickaddress(id){
	closeaddress();
	infowindow[id].open(map, markers[id]);
}

function closeaddress(){
	for(var i=0; i < maxaddress; i++){
		if (infowindow[i]) infowindow[i].close();
	}
}

function clearmarkers(){
	for(var i=0; i < maxaddress; i++){
		if (markers[i]) markers[i].setMap(null);
	}
}


function addmarker(addlat,addlng,title,id,infotxt,infolink,zoom) {   
	infotxt = unescape(infotxt);
	infotxt = infotxt.replace(/##/g," ");

	closeaddress();

	var latlng = new google.maps.LatLng(addlat, addlng);

	var shape = {
		coord: [1, 1, 1, 20, 18, 20, 18 , 1],
		type: 'poly'
	};

   	markers[id] = new google.maps.Marker({
		position: latlng, 
		map: map,
		shape: shape,
		title: title
	});


	infowindow[id] = new google.maps.InfoWindow({
        content: '<div class="locinfo">'+infotxt+'</div>'
    });

	infowindow[id].open(map, markers[id]);
	
	google.maps.event.addListener(markers[id], "click", function() {
		closeaddress()
      	infowindow[id].open(map, markers[id]);
    });

	if(zoom > 0){
		map.setZoom(zoom);
	} else {
		bounds.extend(latlng);
		map.fitBounds(bounds);
	}
    	
    return markers[id];
}

function getloc(){
	bounds = new google.maps.LatLngBounds;
	country = escape(document.getElementById("locdropdownmenu").innerHTML);
	city = escape(document.getElementById("s").value);
	clearmarkers();
	$("#midloc").load("/includes/locator.php?country="+country+"&city="+city);
}


function checkfield(){
	if(document.getElementById("s").value == "Enter City"){
		document.getElementById("s").value = "";
	} else {
		document.getElementById("s").select();
	}
}

function initialize(centerlat, centerlng, zoom) {
	var myLatlng = new google.maps.LatLng(centerlat, centerlng);
	if(zoom == ""){
		zoom = 6;
	}
	var myOptions = {
		mapTypeControlOptions: {
			style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		mapTypeControl: false,
		navigationControl: true,
		scrollwheel: false,
		center: myLatlng,
		zoom: zoom,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.ZOOM_PAN
		},
      		mapTypeId: google.maps.MapTypeId.ROADMAP
    	};

	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}


