var weekdays = new Array ('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
var currentInterval = 0; 
			
// determine and write weekday 
function getWeekday (date) {
	return weekdays[date.getUTCDay()-1]
}

// returns formatted date
function getFormattedDate (date) {
	var month = date.getUTCMonth() + 1;
	var day = date.getUTCDate();
	if (month < 10) {
		month = '0' + month;
    }
	if (day < 10) {
		day = '0' + day;
    }
	return day + '.' +  month + '.' + date.getUTCFullYear();
}

//returns a time interval
function getTimeInterval (date1, date2) {
	var minutes1 = date1.getUTCMinutes();
	var minutes2 = date2.getUTCMinutes();
	if (minutes1 < 10) {
		minutes1 = '0' + minutes1;
    }
	if (minutes2 < 10) {
		minutes2 = '0' + minutes2;
    }
	return date1.getUTCHours() + ':' + minutes1 + '-' + date2.getUTCHours() + ':' + minutes2;
}

// returns weekday, date and time
function getWeekdayDateTimeInterval (date1, date2) {
	var result = '';
	return getWeekday(date1) + ', ' + getFormattedDate(date1) + ', ' + getTimeInterval(date1, date2);
}

// create time slider for future prediction
/*$(document).ready( function() {
	$('#slider_callout').hide(); 
	var calloutVisible = false;
	// create slider
	$("#slider").slider({
		handle: '#slider_handle', 
		minValue: 0, 
		maxValue: 24, 
		start: function(e, ui) { 
			//$('#slider_callout').fadeIn('fast', function() { calloutVisible = true;}); 
		}, 
		stop: function(e, ui) { 
			//if (calloutVisible == false) { 
			//	$('#slider_callout').fadeIn('fast', function() { calloutVisible = true;}); 
			//	$('#slider_callout').css('left', ui.handle.css('left')).text(ui.value); 
			//} 
			//$('#slider_callout').fadeOut('fast', function() { calloutVisible = false; });
		}, 
		slide: function(e, ui) { 
			//$('#slider_callout').css('left', ui.handle.css('left')).text(ui.value); 
		} 
	});
	//slide handle smoothly when user click outside handle on the bar
	$('.selector').slider({ animate: true });
	$('.selector').slider('option', 'animate', true);
});*/

// set image of current prediction active
function setCurrentPredictionImage (id) {   
	currentInterval = (parseInt(id,10) - 1);
	// set all images to deactivated
	for (var i=1; i<5; i++) {
		document.getElementById('clock' + i + '_active').style.display ='none';
		if (document.getElementById('clock' + i + '_unavailable').style.display == 'block') {
			document.getElementById('clock' + i + '_unavailable').style.display ='block';
        }
		else {
			document.getElementById('clock' + i).style.display ='block';
        }
	}
	// activate image for active prediction
	document.getElementById('clock' + id + '_active').style.display ='block';
	document.getElementById('clock' + id).style.display ='none';
	document.getElementById('validityPeriod').value = validityIntervals[currentInterval].referenceTime;
	// set summary
	document.getElementById('predictionSummaryContent').innerHTML = getWeekdayDateTimeInterval (validityIntervals[currentInterval].start, validityIntervals[currentInterval].end);
	// hide functionality available only for current interval
    toggleCurrentIntervalFunc (id);
    // update weather icons
	if (enableWeather == 'true') {
		reloadWeatherIcons (true);
	}
	// update map
	mdvMap.mdvMap.update();
}

// This function shows/hides functionality that is only available for the current Interval.
function toggleCurrentIntervalFunc (state) {
    if (state == 1) {
        // show cameras 
        if (document.getElementById('itdLPxx_layer_externalMarkers')) {
            document.getElementById('itdLPxx_layer_externalMarkers').disabled = "";
            mdvMap.externalMarkersLayer.setVisibility(document.getElementById('itdLPxx_layer_externalMarkers').checked);
        }
        // enable highway situation
        if (enableHighwayMap == 'true') {
            toggleHighwaySituation(true);
        }
    }
    else {
        // hide cameras
        if (document.getElementById('itdLPxx_layer_externalMarkers')) {
            document.getElementById('itdLPxx_layer_externalMarkers').disabled = "disabled";
            mdvMap.externalMarkersLayer.setVisibility(false);
        }
        // disable highway situation
        if (enableHighwayMap == 'true') {
            toggleHighwaySituation(false);
        }
    }
}

// toggle prediction tabs
function togglePredictionTabs (id) {
	// future
	if (id == '2') {
		// toggle body
		document.getElementById('predictionBody_1' ).style.display = 'none';
		document.getElementById('predictionBody_2' ).style.display = 'block';
		// set cursor
		document.getElementById('predictionTab_1' ).style.cursor = 'pointer';
		document.getElementById('predictionTab_2' ).style.cursor = 'default';
		// set tab style
		document.getElementById('predictionTab_1' ).className = 'its-efa-prediction-tab its-efa-prediction-inactive';
		document.getElementById('predictionTab_2' ).className = 'its-efa-prediction-tab its-efa-prediction-active';
	}
	// current
	else {
		// toggle body
		document.getElementById('predictionBody_1' ).style.display = 'block';
		document.getElementById('predictionBody_2' ).style.display = 'none';
		// set cursor
		document.getElementById('predictionTab_1' ).style.cursor = 'default';
		document.getElementById('predictionTab_2' ).style.cursor = 'pointer';
		// set tab style
		document.getElementById('predictionTab_1' ).className = 'its-efa-prediction-tab its-efa-prediction-active';
		document.getElementById('predictionTab_2' ).className = 'its-efa-prediction-tab its-efa-prediction-inactive';
	}
}