$(document).ready(function() { 
	addResizeElement('left', 58, true); 
	addResizeElement('left_content', 78, true); 
	addResizeElement('day_hours', 190, true); 
	addResizeElement('main', 150, false); 
	addResizeElement('main', 39, true); 
	
	getViewport();
	resizeElements();

	// init hours div elements
	init_hours_events();
	
	// autoresize callbck
	$(window).resize(function(){
		getViewport();
		resizeElements();
		if (map!=undefined) map.checkResize();
	});
	
	// format date in form input
	var form = document.getElementById("formDate");
	if (form.date.value==null || form.date.value.length==0){
		form.date.value = formatDate(new Date(),'yyyy-MM-dd');
	}
	
	// autodetect GMT offset
	var gmtOffset = (new Date().getTimezoneOffset()) / (-60);
	var timezone = document.getElementById('timezone_field');
	timezone.value = gmtOffset;
	
	// send date request
	sendDataRequest();
});

function init_hours_events(){
	$('.hour_entries').hide();
	
	$('.hour_title').click(function(){
		$(this).next('.hour_entries').toggle();
	});
	
	$('.hour_entry').mouseover(function(){
		$('.hour_entry').removeClass('hour_entry_previewed');
		$(this).addClass('hour_entry_previewed');
	});
	
	$('.hour_entry').mouseout(function(){
		$(this).removeClass('hour_entry_previewed');
	});
	
	$('.hour_entry').click(function(){
		$('.hour_entry').removeClass('hour_entry_selected');
		$(this).addClass('hour_entry_selected');
		locationShow();
	});

	$('.hour_entry').mouseout(function(){
		locationPreviewHide();
	});
}



google.load("maps", "2",{"other_params":"sensor=true"});

var map;
var iconPreview;
var iconShow;
function initializeMap() {
	var locLat = 52.0;
	var locLng = 19.25;
	var locZoom = 6;
	
	if (google.loader.ClientLocation) {
		var loc = google.loader.ClientLocation;
		locLat = loc.latitude;
		locLng = loc.longitude;
		var locZoom = 9;
	}

	map = new google.maps.Map2(document.getElementById("map"));
	map.setCenter(new google.maps.LatLng(locLat, locLng), locZoom);
	map.setUIToDefault();
	
	iconPreview = new GIcon(G_DEFAULT_ICON);
	iconPreview.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png";

	iconShow = new GIcon(G_DEFAULT_ICON);
	iconShow.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
}

google.setOnLoadCallback(initializeMap);


var cal = new CalendarPopup("calendar");
//cal.showNavigationDropdowns();
cal.setReturnFunction('dateChanged');
cal.offsetX = -100;
cal.offsetY = 0;

function pickDate(){
	var form = document.getElementById("formDate");
	cal.select(form.date,'anchor1cal','yyyy-MM-dd');
}

function dateChanged(y,m,d){
	var dt = new Date(y,m-1,d,0,0,0);
	var dateFormatted = formatDate(dt,window.CP_dateFormat);
	if (window.CP_calendarObject!=null) { window.CP_calendarObject.copyMonthNamesToWindow(); }
	if (window.CP_targetInput.value!=dateFormatted){
		window.CP_targetInput.value = dateFormatted;
		 sendDataRequest();
	}
}

var ajax_link = '';

function sendDataRequest(){
	var input = document.getElementById('date_field');
	var timezone = document.getElementById('timezone_field');
	var minspeed = document.getElementById('min_speed_field');
	ajax_link = './data.php?d=' + input.value +  '&tz=' + timezone.value + '&mins=' + minspeed.value;
	
	var dh = document.getElementById('day_hours');
	dh.innerHTML = 'Loading...';
	
	$.get(ajax_link, function(data, status){
			var dh = document.getElementById('day_hours');
			dh.innerHTML = data;
			init_hours_events();
	});
}


var locationMarker;
var locationTime;
var locationSpeed;
var previewMarker;
var keepPreview;
var removePreviewTimeout;
var movePreviewTimeout;
var previewTargetLatLng;
var previewMoveDelay = 50;
function locationPreviewShow(time, latitude, longitude, speed) {
	if (map==undefined) return;
	
	keepPreview = true;
	if (removePreviewTimeout!=undefined && removePreviewTimeout!=null){
		clearTimeout(removePreviewTimeout);
		removePreviewTimeout = null;
	}
	
	var point = new GLatLng(latitude, longitude);
	var info = '<b>'+time+'</b><br/>'+speed+' km/h';
	if (previewMarker==undefined || previewMarker==null){
		previewMarker = new GMarker(point);
		map.addOverlay(previewMarker);
		previewMarker.setImage("http://labs.google.com/ridefinder/images/mm_20_orange.png");
	} else {
		previewTargetLatLng = point;
//		previewMarker.setLatLng(point);
		previewMarker.show();
		if (movePreviewTimeout!=null){
			clearTimeout(movePreviewTimeout);
		}
		movePreviewTimeout = setTimeout("movePreview()", previewMoveDelay);
	}
	//previewMarker.openInfoWindowHtml(info);
	
	locationTime = time;
	locationSpeed = speed;
}

function movePreview(){
	var cLatLng = previewMarker.getLatLng();
	var cLat = cLatLng.lat();
	var cLng = cLatLng.lng();
	var tLat = previewTargetLatLng.lat();
	var tLng = previewTargetLatLng.lng();
	
	if (cLat==tLat && cLng==tLng){
		return;
	}
	
	var diffLat = (tLat-cLat);
	var diffLng = (tLng-cLng);
	
	var diff = 0.00005;
	
	if (Math.abs(diffLat)<diff || Math.abs(diffLng)<diff) {
		cLat = tLat;
		cLng = tLng;
	} else {
		cLat += (diffLat/4);
		cLng += (diffLng/4);
	}
	
	var point = new GLatLng(cLat, cLng);
	previewMarker.setLatLng(point);

	movePreviewTimeout = setTimeout("movePreview()", previewMoveDelay);
}

function locationPreviewHide(){
	if (map==undefined) return;

	if (previewMarker!=undefined && previewMarker!=null){
		keepPreview = false;
		removePreviewTimeout = setTimeout("removePreview()", 500);
	}
}

function removePreview(){
	if (!keepPreview){
		previewMarker.hide();
//		map.removeOverlay(previewMarker);
//		previewMarker = null;
		removePreviewTimeout = null;
	}
}

function locationShow(){
	if (map==undefined) return;

	var zoomMin = 9;
	var zoomMax = 16;
	var speedMax = 150;
	var zoom = zoomMax;
	if (locationSpeed!=0){
		if (locationSpeed>speedMax) {
			zoom = zoomMin;
		} else {
			var offset = ((speedMax-locationSpeed)*(zoomMax-zoomMin))/speedMax;
			zoom = zoomMin + offset;
		}
	}
	zoom = Math.round(zoom);
	
	if (locationMarker!=undefined) map.removeOverlay(locationMarker);

	var latlng = (previewTargetLatLng==undefined) ? previewMarker.getLatLng : previewTargetLatLng;
	locationMarker = new GMarker(latlng);
	map.addOverlay(locationMarker);
	map.setZoom(zoom);
	map.panTo(locationMarker.getLatLng());
//	map.setCenter(locationMarker.getLatLng());
	map.savePosition();
	
	var info = locationTime+'<br/>'+locationSpeed+' km/h';
//	locationMarker.openInfoWindowHtml(info);
}
