jQuery(window).bind('load', function() {
	var imagesToPreload = new Array ();
	
	
	//imagesToPreload.push ('url of image');

	if (imagesToPreload.length > 0) {
		var img = jQuery.create ('img', {});
		jQuery(img).bind('load', function() {
			if(imagesToPreload[0]) {
				this.src = imagesToPreload.shift();
			}		
		}).trigger('load');
	}

});

var lwCmd = {cmd : 'getData', service: 'com.radiatic.liveweather.LiveWeatherService', params: '[[{"id":1,"measure":"CurrentValues"}]]'};


jQuery(document).ready(function(){
	jQuery(document).pngFix();
	resizeSpacer ();
	repositionContent ();
	jQuery(window).resize(function(){
		resizeSpacer ();
		repositionContent ();
	});
			
	refreshWeatherInfo ();
});

function refreshWeatherInfo () {
	jQuery.ajax({
		url: '/liveweather/service/service.php',
		type: 'POST',
		data: lwCmd, 
		dataType: 'json',
		success: weatherInfoReceived
	});
}

function weatherInfoReceived (data) {
	if (data.result == 'OK') {
		var measures = data.returnValue[0].result.measures;
		var temperature = parseFloat (measures.Temperature);
		var windSpeed = parseFloat (measures.WindSpeed);
		var windDirDeg = parseFloat (measures.WindDirectionDegrees);
		var windDir = measures.WindDirection;
		jQuery('#weather-info').html(temperature.toFixed(0) + '<span style="color: #ccf;">&deg;C</span> &nbsp; ' + windSpeed.toFixed(1) + ' <span style="color: #ccf;">m/s</span> &nbsp; ' + windDir + ' (' + windDirDeg.toFixed(0) + '&deg;)');
	}
	jQuery.timer(10000, function (timer) {
		refreshWeatherInfo ();
		timer.stop();
	});
}

var bodyHeight = 0;

function resizeSpacer() {
	if (bodyHeight == 0)
		bodyHeight = jQuery('#body').height () + jQuery('#footer').height();
	var height = jQuery(window).height () - bodyHeight;
	if (jQuery.browser.safari)
		height -= 405;
	else
		height -= 327;
	if (height > 0)
		jQuery('#spacer').css('height', '' + height + 'px');
	else
		jQuery('#spacer').css('height', '1px');
	jQuery('#footer').css('padding-top', '15px');
	jQuery('#footer').css('padding-bottom', '8px');
}

function isNumberEven (num) {
	if (num % 2 == 0)
		return true;
	return false;
}

function repositionContent () {
	if (jQuery.browser.safari) {		
		var winWidth = jQuery(window).width();
		var margin = Math.ceil((winWidth - 916) / 2) - 8;
		if (isNumberEven (winWidth))
			margin++;
		if (margin > 0) {
			jQuery('#content').css('margin-right', '0px');
			jQuery('#content').css('margin-left', ''+margin+'px');
		}
		
	}
}