var postal;
var demoMode = false;
var enabled;

if (location.href.indexOf("Wallpaper")  > 0){
	stylesheet = stylesheetLock;
	iconSet = iconSetLock;
	iconExt = iconExtLock;
	enabled = enableLockScreen;
}else{
	stylesheet = stylesheetWall;
	iconSet = iconSetWall;
	iconExt = iconExtWall;
	enabled = enableWallpaper;
}

if(enabled == true){
if(iconSet == null || iconSet == 'null' || iconSet == ""){
	var iconSet = stylesheet;
}

var headID = document.getElementsByTagName("head")[0];         
var styleNode = document.createElement('link');
styleNode.type = 'text/css';
styleNode.rel = 'stylesheet';
styleNode.href = 'Stylesheets/'+stylesheet+'.css';
headID.appendChild(styleNode);

var scriptNode = document.createElement('script');
scriptNode.type = 'text/javascript';
scriptNode.src = 'Sources/'+source+'.js';
headID.appendChild(scriptNode);
}

function onLoad(){
	if (enabled == true){ 
	if (demoMode == true){
		document.getElementById("weatherIcon").src="IconSets/"+iconSet+"/"+"cloudy1"+iconExt;
		document.getElementById("city").innerText="Somewhere";
		document.getElementById("desc").innerText="Partly Cloudy";
		document.getElementById("temp").innerText="100º";		
		document.getElementById("forecast").innerText="Sun";	
	}else{ 
	document.getElementById("weatherIcon").src="IconSets/"+iconSet+"/"+"dunno"+iconExt;
	validateWeatherLocation(escape(locale).replace(/^%u/g, "%"), setPostal)
	}
	}else{
		document.getElementsByTagName("body")[0].innerText='';
	}
}

function convertTemp(num)
{
	if (isCelsius == true)
		return Math.round ((num - 32) * 5 / 9);
	else
		return num;
}

function setPostal(obj){
	
	if (obj.error == false){
		if(obj.cities.length > 0){
			postal = escape(obj.cities[0].zip).replace(/^%u/g, "%")
			document.getElementById("WeatherContainer").className = "";	
			weatherRefresherTemp();
		}else{
			document.getElementById("city").innerText="Not Found";
			document.getElementById("WeatherContainer").className = "errorLocaleNotFound";	
		}
	}else{
		document.getElementById("city").innerText=obj.errorString;
		document.getElementById("WeatherContainer").className = "errorLocaleValidate";	
		setTimeout('validateWeatherLocation(escape(locale).replace(/^%u/g, "%"), setPostal)', Math.round(1000*60*5));
	}
}

function dealWithWeather(obj){


	if (obj.error == false){
		document.getElementById("city").innerText=obj.city;
		document.getElementById("desc").innerText=obj.description.toLowerCase();
              translatedesc=obj.description.toLowerCase();

              // *******     TRANSLATE DESCRIPTION   ***************************
		// *******     ALWAYS USE SMALL CAPS (i.e 'sunny')   ***************************
       	if (translatedesc=='description') { document.getElementById("desc").innerHTML='Your Description';	}
		if (translatedesc=='sunny') { document.getElementById("desc").innerHTML='sonnig';    }
		if (translatedesc=='mostly sunny') { document.getElementById("desc").innerHTML='meist sonnig';    }
		if (translatedesc=='partly sunny') { document.getElementById("desc").innerHTML='teils wolkig';    }
		if (translatedesc=='intermittent clouds') { document.getElementById("desc").innerHTML='stellenweise bew&ouml;lkt';    }
		if (translatedesc=='hazy sunshine') { document.getElementById("desc").innerHTML='diesig und sonnig';    }
		if (translatedesc=='mostly cloudy') { document.getElementById("desc").innerHTML='meist bew&ouml;lkt';    }
        	if (translatedesc=='cloudy') { document.getElementById("desc").innerHTML='bew&ouml;lkt';    }
        	if (translatedesc=='overcast') { document.getElementById("desc").innerHTML='bew&ouml;lkt';    }
        	if (translatedesc=='dreary') { document.getElementById("desc").innerHTML='tr&uuml;b';    }
        	if (translatedesc=='retired') { document.getElementById("desc").innerHTML='keine daten verf&uuml;gbar';    }
        	if (translatedesc=='fog') { document.getElementById("desc").innerHTML='nebel';    }
		if (translatedesc=='light fog') { document.getElementById("desc").innerHTML='leichter nebel';    }
		if (translatedesc=='dense fog') { document.getElementById("desc").innerHTML='dichter nebel';    }
        	if (translatedesc=='shower') { document.getElementById("desc").innerHTML='regenschauer';    }
        	if (translatedesc=='partly sunny with showers') { document.getElementById("desc").innerHTML='teils sonnig mit schauern';    }
        	if (translatedesc=='thunderstorm') { document.getElementById("desc").innerHTML='gewitter';    }
        	if (translatedesc=='mostly cloudy with thunder showers') { document.getElementById("desc").innerHTML='meist bew&ouml;lkt mit gewitterschauer';    }
        	if (translatedesc=='partly sunny with thunder showers') { document.getElementById("desc").innerHTML='teils sonnig mit gewitterschauer';    }
if (translatedesc=='light rain') { document.getElementById("desc").innerHTML='leichter regen';    }
        	if (translatedesc=='rain') { document.getElementById("desc").innerHTML='regen';    }
		if (translatedesc=='rain shower') { document.getElementById("desc").innerHTML='regenschauer';    }
        	if (translatedesc=='flurries') { document.getElementById("desc").innerHTML='windig';    }
        	if (translatedesc=='mostly cloudy with flurries') { document.getElementById("desc").innerHTML='meist bew&ouml;lkt und windig';    }
        	if (translatedesc=='partly sunny with flurries') { document.getElementById("desc").innerHTML='teils sonnig und windig';    }
        	if (translatedesc=='snow') { document.getElementById("desc").innerHTML='schneefall';    }
        	if (translatedesc=='mostly cloudy with snow') { document.getElementById("desc").innerHTML='meist bew&ouml;lkt und schnee';    }
        	if (translatedesc=='ice') { document.getElementById("desc").innerHTML='frost';    }
        	if (translatedesc=='sleet') { document.getElementById("desc").innerHTML='graupelschauer';    }
        	if (translatedesc=='freezing rain') { document.getElementById("desc").innerHTML='gefrierender regen';    }
        	if (translatedesc=='rain and snow mixed') { document.getElementById("desc").innerHTML='schneeregen';    }
        	if (translatedesc=='hot') { document.getElementById("desc").innerHTML='heiss';    }
        	if (translatedesc=='cold') { document.getElementById("desc").innerHTML='frostig';    }
        	if (translatedesc=='windy') { document.getElementById("desc").innerHTML='windig';    }
        	if (translatedesc=='clear') { document.getElementById("desc").innerHTML='wolkenlos';    }
		if (translatedesc=='partly clear') { document.getElementById("desc").innerHTML='teils wolkenlos';    }
        	if (translatedesc=='mostly clear') { document.getElementById("desc").innerHTML='etwas bew&ouml;lkt';    }
        	if (translatedesc=='partly cloudy') { document.getElementById("desc").innerHTML='teils bew&ouml;lkt';    }
        	if (translatedesc=='haze') { document.getElementById("desc").innerHTML='diesig';    }
        	if (translatedesc=='partly cloudy with showers') { document.getElementById("desc").innerHTML='teils bew&ouml;lkt mit regenschauern';    }
        	if (translatedesc=='mostly cloudy with showers') { document.getElementById("desc").innerHTML='meist bew&ouml;lkt mit schauer';    }
        	if (translatedesc=='party cloudy with thunder showers') { document.getElementById("desc").innerHTML='teils bew&ouml;lkt mit gewitterschauer';    }
        	if (translatedesc=='mostly cloudy with showers') { document.getElementById("desc").innerHTML='meist bew&ouml;lkt mit gewitterschauer';    }
		if (translatedesc=='foggy') { document.getElementById("desc").innerHTML='neblig';    }
		if (translatedesc=='light snow') { document.getElementById("desc").innerHTML='leichter schneefall'; }
		if (translatedesc=='drizzle') { document.getElementById("desc").innerHTML='nieselregen'; }		
		if (translatedesc=='rain/drizzle') { document.getElementById("desc").innerHTML='nieselregen'; }
		if (translatedesc=='mist') { document.getElementById("desc").innerHTML='diesig und nieselregen'; }
		if (translatedesc=='ground fog') { document.getElementById("desc").innerHTML='bodennebel'; }
				

		// *******   END   TRANSLATE DESCRIPTION   ***************************
              
		if(useRealFeel == true){
			tempValue = convertTemp(obj.realFeel);
		}else{
			tempValue = convertTemp(obj.temp)
		}
		document.getElementById("temp").innerText=tempValue+"Âº";
		document.getElementById("weatherIcon").src="IconSets/"+iconSet+"/"+MiniIcons[obj.icon]+iconExt;
		document.getElementById("desc").innerText=Francais[obj.icon];
		translatedesc=obj.description.toLowerCase();


		
		/*ProductRed*/
		lastResults = new Array;
		lastResults[0] = {hi:obj.hi, lo:obj.lo, now:obj.temp};
		var c = obj.forecast.length;

		if (c > 6) c = 6; // just to be safe
		for (var i=0; i<c; ++i)
		{
			var forecast = obj.forecast[i];

			document.getElementById('hi'+i).innerText = convertTemp(forecast.hi)+"Âº";;
			document.getElementById('low'+i).innerText = convertTemp(forecast.lo)+"Âº";;
			
			lastResults[i+1] = {hi:forecast.hi, lo:forecast.lo};

		}
		/*ProductRed*/

		document.getElementById("WeatherContainer").className = "";	

		
	}else{
		//Could be down to any number of things, which is unhelpful...
		document.getElementById("WeatherContainer").className = "errorWeatherDataFetch";	
	}
	
var this_weekday_name_array = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
var this_month_name_array = new Array("January","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")	//predefine month names
var this_date_timestamp = new Date()
var this_weekday = this_date_timestamp.getDay()
var this_date = this_date_timestamp.getDate()
var this_month = this_date_timestamp.getMonth()
var this_year = this_date_timestamp.getYear()

if (this_year < 1000)
    this_year+= 1900;
if (this_year==101)
    this_year=2001;

document.getElementById("date").innerText=this_weekday_name_array[this_weekday] + ", " + this_month_name_array[this_month] + " " + this_date  
	
}

function weatherRefresherTemp(){ //I'm a bastard ugly hack. Hate me.
	fetchWeatherData(dealWithWeather,postal);
	setTimeout(weatherRefresherTemp, 60*1000*updateInterval);
}
