//GPS Live Tracker
//Copyright 2007-2008 Seamless Interactive LLC
window.addEvent('domready', function(){
	var oldresult = -1;
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		map.addControl(new GScaleControl());
		map.enableScrollWheelZoom();
		var marker = 0;
		//var marker = new GMarker();
	}
	GEvent.addListener(map, "dragstart", function() {
		$('tracking').checked = 0;
	});
	$('tracking').addEvent('click', function() {
		if (this.checked) {
			map.panTo(curpos);
		}
	});
	jsonRequest = new Request.JSON({url: "pos.php",
		onComplete: function(result) {
			if (result.lat && result.lon) {
				curpos = new GLatLng(result.lat, result.lon);
				if (firstrun) {
					map.setCenter(curpos, 15);
					marker = new GMarker(curpos);
					map.addOverlay(marker);
					firstrun = 0;
				}
				marker.setLatLng(curpos);
				if ($('tracking').checked) {
					map.panTo(curpos);	
				}
				if (livepage) {
					if (result.live != oldresult){
						if (result.live) {
							$('livebox').removeClass('player-box');
							$('livebox').addClass('player-box-live');
							if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
								$('player').innerHTML = '<video src="http://demo.seamlessinteractive.com/iphone/stream.m3u8" width="405" height="304" ></video>';
							}
							else {
								$('player').innerHTML ='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="405" height="304"><param name="movie" value="http://demo.seamlessinteractive.com/rtv/beta/seamplayer.swf"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://demo.seamlessinteractive.com/rtv/beta/seamplayer.swf" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="405" height="304"></embed></object>';
							}
						}
						else {
							$('livebox').removeClass('player-box-live');
							$('livebox').addClass('player-box');
							$('player').innerHTML = 
							'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
						        'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" '+
						        'width="405" '+
						        'height="304" '+
						        'id="flashMovie"> '+
  						      '<param name="movie" value="fPlayerIn.swf?playList=xml/1.xml" /> '+
   						     '<param name="quality" value="high" /> '+
						        '<param name="bgcolor" value="#ffffff" /> '+
   						     '<embed src="fPlayerIn.swf?playList=xml/1.xml" '+
         					      'quality="high" '+
           					    'bgcolor="#ffffff" '+
           					    'width="405" '+ 
             					  'height="304" '+ 
             					  'name="flashMovie" '+
         					      'allowScriptAccess="sameDomain" '+
              					 'type="application/x-shockwave-flash" '+
         				      'pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
					        '</object>';

						}
						oldresult = result.live;
					}
				}
			}
		periodical = updatemap.periodical(2000);
		}
	});
	var updatemap = function() {
		$clear(periodical);
		jsonRequest.get();
	}
	var firstrun = 1;
        jsonRequest.get();
	var periodical = updatemap.periodical(1500);
});
