
/* 
	set up basic ajax stuff
*/


/* menuklapper */
$(function(){
	$("#menu").harmonica({
		delay			: 3800,
		animation		: {opacity:"show",height:"show"},
		speed			: "slow"
	});
});


/* accuweather */
$(document).ready(function()
{	
	var xml = $.ajax({
		url: './accuweather/accuweather_test.php',
		processData: false,
		//data: xmlDocument,
		success: handleWeatherResponse
	});	
});

var miniIconTable = new Array;
miniIconTable["sun"]		= './accuweather/minis/sun.png';
miniIconTable["suncloud"]	= './accuweather/minis/sun-cloud.png';
miniIconTable["sunhaze"] 	= './accuweather/minis/sun-haze.png';
miniIconTable["clouds"] 	= './accuweather/minis/clouds.png';	
miniIconTable["fog"] 		= './accuweather/minis/fog.png';
miniIconTable["rain"] 		= './accuweather/minis/rain.png';
miniIconTable["cloudrain"]	= './accuweather/minis/cloud-rain.png';
miniIconTable["sunrain"] 	= './accuweather/minis/sun-rain.png';
miniIconTable["lightening"] = './accuweather/minis/lightning.png';
miniIconTable["flurries"] 	= './accuweather/minis/flurries.png';
miniIconTable["snow"] 		= './accuweather/minis/snow.png';	
miniIconTable["ice"] 		= './accuweather/minis/ice.png';		
miniIconTable["hail"] 		= './accuweather/minis/hail.png';
miniIconTable["snowrain"] 	= './accuweather/minis/snow-rain.png';
miniIconTable["wind"] 		= './accuweather/minis/wind.png';

var accuweatherMiniIcons = 
[
	"sun", 						// 1 Sunny
	"sun",						// 2 Mostly Sunny
	"suncloud",					// 3 Partly Sunny
	"suncloud",					// 4 Intermittent Clouds
	"sunhaze",					// 5 Hazy Sunshione
	"suncloud",					// 6 Mostly Cloudy
	"clouds",					// 7 Cloudy (am/pm)
	"clouds",					// 8 Dreary (am/pm)
	null,						// 9 retired
	null,						// 10 retired
	"fog",						// 11 fog (am/pm)
	"rain",						// 12 showers (am/pnm)
	"cloudrain",					// 13 Mostly Cloudy with Showers
	"sunrain",					// 14 Partly Sunny with Showers
	"lightening",				// 15 Thunderstorms (am/pm)
	"lightening",				// 16 Mostly Cloudy with Thunder Showers
	"lightening",				// 17 Partly Sunnty with Thunder Showers
	"rain",						// 18 Rain (am/pm)
	"flurries",					// 19 Flurries (am/pm)
	"flurries",					// 20 Mostly Cloudy with Flurries
	"flurries",					// 21 Partly Sunny with Flurries
	"snow",						// 22 Snow (am/pm)
	"snow",						// 23 Mostly Cloudy with Snow
	"ice",						// 24 Ice (am/pm)
	"hail",						// 25 Sleet (am/pm)
	"hail",						// 26 Freezing Rain (am/pm)
	null,						// 27 retired
	null,						// 28 retired
	"snowrain",					// 29 Rain and Snow Mixed (am/pm)
	"sun",						// 30 Hot (am/pm)
	"sun",						// 31 Cold (am/pm)
	"wind",						// 32 Windy (am/pm)
	// Night only Icons (shouldn't get these);
	null,						// 33 Clear
	null,						// 34 Mostly Clear
	null,						// 35 Partly Cloudy
	null	,						// 36 Intermittent Clouds
	null,						// 37 Hazy
	null,						// 38 Mostly Cloudy
	null,						// 39 Partly Cloudy with Showers
	null,			 			// 40 Mostly Cloudy with Showers
	null,						// 41 Partly Cloudy with Thunder Showers
	null,						// 42 Mostly Cloudy with Thunder Showers
	null	,						// 43 Mostly Cloudy with Flurries
	null							// 44 Mostly Cloudy with Flurries
];

var daysDutchTable = new Array;
daysDutchTable['Monday'] = 'Ma';
daysDutchTable['Tuesday'] = 'Di';
daysDutchTable['Wednesday'] = 'Wo';
daysDutchTable['Thursday'] = 'Do';
daysDutchTable['Friday'] = 'Vr';
daysDutchTable['Saturday'] = 'Za';
daysDutchTable['Sunday'] = 'Zo';

/**
 * handleWeatherResponse
 *
 * <#Function description#>
 *
 * @access		Public
 * @param		<#VarType#> <#VarName#>	
 * @return		<#VarType#>
 */
function handleWeatherResponse(xml)
{
	//alert(xml);
	
	//console.log(xml);
	
	var obj = {error:false, errorString:null};
	
	obj.forecast = new Array;
	
	var adc_Database = findChild (xml, "adc_Database");
	
	var Forecast = findChild (adc_Database, "Forecast");
	
	//if (Forecast == null) {callback(constructError("no <Forecast>")); return;}
	
	// assume the days are in order, 1st entry is today
	var child;
	var j=0;
	var firstTime = true;
	
	for (child = Forecast.firstChild; child != null; child = child.nextSibling)
	{
		if (child.nodeName == 'day')
		{
			if (firstTime) // today
			{
				
			}

			var foreobj = {description:null, hi:0, lo:0, icon:-1};
			
			tag = findChild(child, 'TXT_Long');
			if (tag != null) // replace the old description 
				foreobj.description = trimWhiteSpace(tag.firstChild.data);
			
			tag = findChild(child, 'WindDirection');
			if (tag != null) // replace the old description 
				foreobj.winddir = trimWhiteSpace(tag.firstChild.data);
			
			tag = findChild(child, 'WindSpeed');
			if (tag != null) // replace the old description 
				foreobj.windspd = roundNumber((parseInt(tag.firstChild.data)/1.6),0);
				
			tag = findChild(child, 'High_Temperature');
			if (tag != null)
				foreobj.hi = convertToCelcius(parseInt(tag.firstChild.data));
				
			tag = findChild(child, 'Low_Temperature');
			if (tag != null)
				foreobj.lo = convertToCelcius(parseInt(tag.firstChild.data));
			
			tag = findChild(child, 'WeatherIcon');
			if (tag != null)
			{
				foreobj.icon = parseInt (tag.firstChild.data, 10);
				foreobj.ouricon = accuweatherMiniIcons[foreobj.icon-1];
			}
				
			tag = findChild (child, "DayCode");
			if (tag != null)
			{
				foreobj.daycode = daysDutchTable[trimWhiteSpace(tag.firstChild.data)];
				
				//foreobj.daycode = parseDayCode(tag.firstChild.data);
			}
			else
				foreobj.daycode = null;
			
			
			
			obj.forecast[j++]=foreobj;
			
			if (j == 5) break; // only look ahead 7 days
		}
	}
		
	//console.log(obj);
	
	for(var i=0; i<obj.forecast.length; i++)
	{
		$('#weather div.weather_results').append('<p class="forecast">'
			+ '<span class="day">' + obj.forecast[i].daycode + '</span>'
			+ '<img src="'+miniIconTable[obj.forecast[i].ouricon]+'"/>'
			+ '<span class="temp">' + obj.forecast[i].hi + '&#176;</span>'
			+ '<span>' + obj.forecast[i].winddir + '</span>'
			+ '<span>' + obj.forecast[i].windspd + '</span>'
			//+' - '+obj.forecast[i].description
			+'</p>'
			
		);
	}
	
}


function findChild (element, nodeName)
{
	var child;
	
	for (child = element.firstChild; child != null; child = child.nextSibling)
	{
		if (child.nodeName == nodeName)
			return child;
	}
	
	return null;
}

function trimWhiteSpace (string)
{
	return string.replace(/^\s*/, '').replace(/\s*$/, '');
}

function convertToCelcius(num)
{
	return Math.round ((num - 32) * 5 / 9);
}

function roundNumber(number,decimal_points)
{
	if(!decimal_points) return Math.round(number);
	if(number == 0)
	{
		var decimals = "";
		for(var i=0;i<decimal_points;i++) decimals += "0";
		return "0,"+decimals;
	}
	
	var exponent = Math.pow(10,decimal_points);
	var num = Math.round((number * exponent)).toString();
	return num.slice(0,-1*decimal_points) + "." + num.slice(-1*decimal_points)
}
/* 
	form checker
*/
function check_this_form()
{
	args = check; // check_this_form.arguments;
	
	var ret = new Array();
	
	for (i=0; i<(args.length); i++)
	{	
		if((o = document.getElementById(args[i])) && args[i] != 'to')
		{
			if(o.value == '')
			{
				// fout
				o.style.backgroundColor = "#DDDDDD";
				ret[ret.length] = false;
			}
			else
			{
				// goed
				o.style.backgroundColor = "#FFFFFF";
			}
		}
	}
	
	if(ret.length > 0)
	{
		if(o = document.getElementById('form_error_report'))
		{	
			o.innerHTML = 'De gemarkeerde velden zijn verplicht om in te vullen.';
		}
		
		return false;
	}
	else
	{
		return true;
	}
}


