
var _map = null;
var _places = [];
var _recent_webcams = [];
var _current_recent_webcam = 0;
var _max_recent_webcams = 0;
var _markerlist = [];
function load() {
setTimeout(load_map, 200);
var pos = getAbsolutePosition('find_place');
$('find_place_res').style.left = pos.x + 'px';
$('find_place_res').style.top = (pos.y + 23) + 'px';
$('find_place').onkeydown = function(e) {
if(!e)
e = window.event;
var key = null;
if(e.which)
key = e.which;
else
key = e.keyCode;
if(key == 10 || key == 13)
find_place();
return;
}
_max_recent_webcams = $('recent_webcams').getElementsByTagName('li').length;
setTimeout(switch_recent_webcams, 7000);
}
function load_map() {
if(GBrowserIsCompatible()) {
_map = new GMap2(document.getElementById("map"));
//_map.addControl(new GSmallMapControl());
_map.setCenter(new GLatLng(_lat, _lng), _zoom);
_map.setMapType(G_SATELLITE_MAP);
setTimeout(load_map_webcams, 1200);
}
}
function load_map_webcams() {
var bounds = _map.getBounds();
var sw_lat = bounds.getSouthWest().lat();
var sw_lng = bounds.getSouthWest().lng();
var ne_lat = bounds.getNorthEast().lat();
var ne_lng = bounds.getNorthEast().lng();
var zoom = _map.getZoom();
var a = new AJAX('/ajax/webcams.php', function(response, data) {
if(response == null)
return;
if(response.status != 200)
return;
display_map_webcams(response.webcams);
return;
}, null, false);
a.setParameter('a', 'fullscreen');
a.setParameter('sw_lat', sw_lat);
a.setParameter('sw_lng', sw_lng);
a.setParameter('ne_lat', ne_lat);
a.setParameter('ne_lng', ne_lng);
a.setParameter('zoom', zoom);
a.send();
}
function display_map_webcams(webcams) {
var marker = null;
var picked_number = Math.floor(webcams.length * Math.random());
for(var i = 0; i < webcams.length; i++) {
marker = new ThumbMarker(32, 32, webcams[i], new GLatLng(webcams[i].lat, webcams[i].lng));
marker.wt_webcam = webcams[i];
GEvent.addListener(marker, 'click', function() {
for(var j = 0; j < _markerlist.length; j++)
_markerlist[j].unselect();
this.select();
});
_map.addOverlay(marker);
_markerlist.push(marker);
if(i == picked_number)
marker.select();
}
}
function find_place() {
var n = $('find_place');
var place = n.value;
if(place.length == 0)
return;
display('find_place_res', '');
display('find_place_none', 'none');
display('find_place_results', 'none');
display('find_place_close', 'none');
display('find_place_loading', '');
var a = new AJAX('/ajax/findplace.php', function(response, data) {
display('find_place_loading', 'none');
display_places(response);
return;
}, null, false);
a.setParameter('p', place);
a.send();
}
function goto_place(p) {
close_places();
var place = _places[p];
var zoom = 12;
if(place.fcl == 'A')
zoom = 6;
location.href = '/map/#lat=' + place.lat + '&lng=' + place.lng + '&z=' + zoom + '&t=h';
}
function display_places(places) {
var n = $('find_place_results');
if(places == null) {
display('find_place_close', '');
display('find_place_none', '');
return;
}
if(places.totalResultsCount == 0) {
display('find_place_close', '');
display('find_place_none', '');
return;
}
clear(n);
display('find_place_close', '');
display(n, '');
_places = new Array();
var text = '';
var link = null;
var place = null;
for(var i = 0; i < places.geonames.length; i++) {
place = places.geonames[i];
if(place.fcl == 'A') {
text = place.name + ' (' + place.countryCode + ')';
}
else {
text = place.name + ' (';
if(place.adminName1)
text = text + place.adminName1 + ', ';
text = text + place.countryName + ')';
}
link = $Link('javascript: goto_place(' + _places.length + ')', text);
n.appendChild(link);
n.appendChild($Element('br'));
_places.push(place);
}
if(_places.length == 1)
goto_place(0);
}
function close_places() {
display('find_place_res', 'none');
}
function switch_recent_webcams() {
if(_recent_webcams.length == 0) {
load_recent_webcams();
return;
}
var li = $('recent_webcams').getElementsByTagName('li');
var webcamids = new Array();
for(var i = 0; i < li.length; i++)
webcamids.push(li[i].id.match(/recent_(.*)/)[1]);
var temp_webcams = new Array();
for(var i = 0; i < _recent_webcams.length; i++) {
if(webcamids.indexOf(_recent_webcams[i].webcamid) != -1)
continue;
temp_webcams.push(_recent_webcams[i]);
}
delete(_recent_webcams);
_recent_webcams = temp_webcams;
if(_recent_webcams.length == 0) {
load_recent_webcams();
return;
}
var webcam = _recent_webcams[0];
var params = {
li: li[_current_recent_webcam],
img: li[_current_recent_webcam].getElementsByTagName('img')[0],
a: li[_current_recent_webcam].getElementsByTagName('a')[0],
newid: webcam.webcamid,
thumbnail: webcam.thumbnail,
title: webcam.title + ', ' + webcam.country,
dir: 'fadeout'
};
start_animation('recent', params, 400, fadeout_fadein_tick, fadeout_fadein_end);
return;
}
function load_recent_webcams() {
var a = new AJAX('/ajax/webcams.php', function(response, data) {
if(response == null)
return;
if(response.status != 200)
return;
if(response.webcams.length != 0) {
_recent_webcams = response.webcams;
switch_recent_webcams();
}
return;
}, null, false);
a.setParameter('a', 'recent');
a.send();
}
function fadeout_fadein_end(elapsed, length, params) {
if(params.dir == 'fadeout') {
params.img.src = params.thumbnail;
params.li.id = 'recent_' + params.newid;
params.a.title = params.title;
params.a.href = '/webcam/' + params.newid;
params.dir= 'fadein';
start_animation('recent', params, length, fadeout_fadein_tick, fadeout_fadein_end);
}
else {
_current_recent_webcam += 1;
if(_current_recent_webcam >= _max_recent_webcams) {
_current_recent_webcam = 0;
setTimeout(switch_recent_webcams, 9000);
}
else
switch_recent_webcams();
}
return;
}
function fadeout_fadein_tick(elapsed, length, params) {
var d = Math.round(elapsed / length * 100);
if(params.dir == 'fadeout')
d = 100 - d;
params.img.style.opacity = (d / 100.0);
params.img.style.MozOpacity = (d / 100.0);
params.img.style.filter = 'alpha(opacity = ' + d + ')';
}
function unload() {
GUnload();
}