var map;
var userMarker;
var marks = [];
var marker;
var count;
var control;

function GetWindowWidthHeight () {
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number')
	{
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	return {'width' : myWidth, 'height' : myHeight};
}

function sizeMap (s) {
	var w = GetWindowWidthHeight().width;
	var h = GetWindowWidthHeight().height;
	
	center_w = w/2;
	$('map').style.top = 20+'px'; 
	$('map').style.left = 0+'px';  
	$('map').style.width=w+'px'; 
	$('map').style.height=(h-20)+'px';
}
function addMark(lat, lon, info, id) {
	marks.push(arguments);
}
function controls(q){
	control = q;
	if(control == 'play'){ $('loadedMarks').innerHTML = '&nbsp;Playing!'; showMarks(); return;}
	if(control == 'pause'){ 
		$('loadedMarks').innerHTML = '&nbsp;Paused...';return;
	}
}
function loadData(delay,older) {
	Element.show('msg'); Element.show('loading');
	new Ajax.Request('backend/showmarks.php', 
		{ method:'post', postBody:'older='+older, onSuccess : function(update) {
			eval(update.responseText);
		}}
	);
}

function showMarks() {
	var t;
	
	if (control == 'pause') return;
	
	if (typeof marks != 'object') return;

	tl = marks.length;
	
	if( control == 'play' ){
		
		if (marks.length == 0) {
			if(count == 0){
				return;
			}else{
				loadData(0,0);
				return;
			}
		}else{
			count = count-1;
		}
	
		if ( t = marks.pop() ) {
			map.closeInfoWindow();
			if(marker) map.removeOverlay(marker);
			marker = new GMarker(new GLatLng(t[1],t[0]));
			map.addOverlay(marker); marker.openInfoWindowHtml(t[2],{maxWidth:250});
			$('loadedMarks').innerHTML = '&nbsp;Loaded messages: '+(count);
			
			new Ajax.Request('backend/viewed.php', 
				{method:'post',postBody:'id='+t[3]+'count='+count, onSuccess : function(response) {
					eval(response.responseText);
				}}
			);
		}
	
		setTimeout( function () {
			showMarks();
		}, 6000);
	}
}

window.onresize = function() { sizeMap() };

window.onload = addCodeToFunction(window.onload,function() {
	
	if (GBrowserIsCompatible()) {    
		sizeMap('normal');
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(60,25),5);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
		control = 'play';
		Event.observe(document,'keyup',   
			function(event){    
				if(event.keyCode == 32){
					if(control == 'play')
						controls('pause');
					else
						controls('play');
				}
			}
		);  
	} else {
		alert("Sorry, your browser isn't compatible with Jaikuvision!")
	}

  loadData(0,0);
});
