﻿var c_pix_height = 48;

var g_weather_class = [];
g_weather_class["50"] = "sunny";
g_weather_class["51"] = "sunny-periods";
g_weather_class["52"] = "sunny-intervals";
g_weather_class["53"] = "sunny-periods-with-showers";
g_weather_class["54"] = "sunny-intervals-with-showers";

g_weather_class["60"] = "cloudy";
g_weather_class["61"] = "overcast";
g_weather_class["62"] = "light-rain";
g_weather_class["63"] = "rain";
g_weather_class["64"] = "heavy-rain";
g_weather_class["65"] = "thunderstorms";

g_weather_class["70"] = "fine";
g_weather_class["71"] = "fine2";
g_weather_class["72"] = "fine3";
g_weather_class["73"] = "fine4";
g_weather_class["74"] = "fine5";
g_weather_class["75"] = "fine6";
g_weather_class["76"] = "mainly-cloudy";
g_weather_class["77"] = "mainly-fine";

g_weather_class["80"] = "windy";
g_weather_class["81"] = "dry1";
g_weather_class["82"] = "dry2";
g_weather_class["83"] = "fog";
g_weather_class["84"] = "mist";
g_weather_class["85"] = "haze";

g_weather_class["90"] = "hot";
g_weather_class["91"] = "warm";
g_weather_class["92"] = "cool";
g_weather_class["93"] = "cold";


function jsSetWeather() {
	var q = 'dt=weather';
	$.getJSON("/services/query.aspx", q, function (data) {
		if ($(data).length > 0) {
			var _iWeatherCode = data.weather_icon_code;
			//utils.log(_iWeatherCode);
			$("#weather").attr("class", g_weather_class[""+_iWeatherCode]);
		}
	});
}

function jsMoveSecond() {
	var y = jsGetBgPos($("#second"), 'y');

	var ny = (y - c_pix_height) + "px";
	var bg = ($.browser.mozilla) ? { 'background-position': '0px ' + ny } : { 'background-position-y': ny };

	$("#second").animate(bg, 1000, 'linear', function () {
		if ((y - c_pix_height) <= -c_pix_height * 60) {
			jsSetBgYPos($("#second"), '0px');
			jsMoveMinute();
		}
		jsMoveSecond();
	});
}

function jsMoveMinute() {
	var y = jsGetBgPos($("#minute"), 'y');

	var ny = (y - c_pix_height) + "px";
	var bg = ($.browser.mozilla) ? { 'background-position': '0px ' + ny} : { 'background-position-y': ny };

	$("#minute").animate(bg, 1000, 'linear', function () {
		if ((y - c_pix_height) <= -c_pix_height * 60) {
			jsSetBgYPos($("#minute"), '0px');
			jsMoveHour();
		}
	});
}


function jsMoveHour() {
	var y = jsGetBgPos($("#hour"), 'y');

	var ny = (y - c_pix_height) + "px";
	var bg = ($.browser.mozilla) ? { 'background-position': '0px ' + ny} : { 'background-position-y': ny };

	$("#hour").animate(bg, 1000, 'linear', function () {
		if ((y - c_pix_height) <= -c_pix_height * 24) {
			jsSetBgYPos($("#hour"), '0px');
			jsChangeBg();
		}
	});
}

function jsGetBgPos(obj, di) {
	di = di.toLowerCase();
	if ($.browser.mozilla) {
		return parseInt($(obj).css('background-position').split(' ')[(di == 'x' ? 0 : 1)].replace('px', ''));
	} else {
		return parseInt($(obj).css('background-position-' + di).replace('px', ''));
	}
}

function jsSetBgXPos(obj, x) {
	if ($.browser.mozilla) {
		$(obj).css('background-position', x + ' ' + jsGetBgPos(obj, 'y') + 'px ');
	} else {
		$(obj).css('background-position-x', x);
	}
}

function jsSetBgYPos(obj, y) {
	if ($.browser.mozilla) {
		$(obj).css('background-position', jsGetBgPos(obj, 'x') + 'px ' + y);
	} else {
		$(obj).css('background-position-y', y);
	}
}

function jsChangeBg() {
	var bg = '/images/24hrs_bg/' + utils.padleft(((new Date()).getHours() + 1).toString(), 2, '0') + '00.jpg';
	$('img.stretch-bg').fadeOut(200, function () {
		$(this).attr('src', bg).fadeIn(500);
	});
}

function jsSetClock() {
	var date = new Date();

	var s = date.getSeconds();
	jsSetBgXPos($("#second"), '0px');
	jsSetBgYPos($("#second"), ((s) * -c_pix_height) + 'px');

	var m = date.getMinutes();
	jsSetBgXPos($("#minute"), '0px');
	jsSetBgYPos($("#minute"), ((m) * -c_pix_height) + 'px');

	var h = date.getHours();
	jsSetBgXPos($("#hour"), '0px');
	jsSetBgYPos($("#hour"), ((h) * -c_pix_height) + 'px');

	jsMoveSecond();

	var y = date.getFullYear();
	jsSetBgXPos($("#year"), '-48px');
	jsSetBgYPos($("#year"), -(720 + (y - 2011) * c_pix_height) + 'px');

	var month = date.getMonth();
	jsSetBgXPos($("#month"), '-48px');
	jsSetBgYPos($("#month"), (month * -c_pix_height) + 'px');

	var d = date.getDate();
	jsSetBgXPos($("#date"), '0px');
	jsSetBgYPos($("#date"), ((d) * -c_pix_height) + 'px');
}

function jsResetClock() {
	utils.log("jsResetClock");
	$("#second").stop();
	$("#minute").stop();
	$("#hour").stop();
	jsSetClock();
}

$(function () {
	jsSetClock();
	setInterval(jsResetClock, 3 * 60 * 1000);
	$(window).focus(jsResetClock);
	jsSetWeather();
});

