var baseURL;

$(document).ready(function(){

	// set theme base URL
	baseURL = "http://" + top.location.host.toString(), url = '';
	baseURL = baseURL + "";

 	// get weather status for belfast
 	$.yql("select * from rss where url='http://newsrss.bbc.co.uk/weather/forecast/2338/Next3DaysRSS.xml'", getWeatherStatus);
	$.yql("select * from rss where url='http://newsrss.bbc.co.uk/weather/forecast/2338/Next3DaysRSS.xml'", getWeatherStatus1);
 	
});

function getWeatherStatus(data){
	
	// function processes weather description from
	// BBC weather feed & constructs a simple description

	var strPosStart;
	var strPosEnd;
	var tempString;

	// get todays forecast	
	var weatherForecast = data.query.results.item[0].title;
	
	// remove day from string
	strPosStart = weatherForecast.search(":");
	weatherForecast = weatherForecast.slice(strPosStart+1);
	
	// remove temp from string
	for (i = 1; i <= 2; i++){
		weatherForecast = weatherForecast.replace("Temp", "");
		weatherForecast = weatherForecast.replace(" :", ":");
	}

	// remove fahrenheit
	for (i = 1; i <= 2; i++){
		strPosStart = weatherForecast.indexOf("(");
		strPosEnd 	= weatherForecast.indexOf(")");
		tempString 	= weatherForecast.slice(strPosStart, strPosEnd+1);
		weatherForecast = weatherForecast.replace(tempString, "");
		weatherForecast = weatherForecast.replace(" ,", ",");
	}
	
	// get hours of day light
	var daylight = data.query.results.item[0].description;
	
	// search description for sunrise
	strPosStart = daylight.indexOf("Sunrise:");
	var sunrise = daylight.substr(strPosStart + 9, 5);
	
	// search description for sunset
	strPosStart = daylight.indexOf("Sunset:");
	var sunset 	= daylight.substr(strPosStart + 8, 5);
	
	// update forecast string
	daylight = "Daylight: " + sunrise + " - " + sunset + ".";
	weatherForecast = weatherForecast + " " + daylight;
	
	// capitalise first letter of string
	var char = weatherForecast.substr(1, 1);
	capChar = char.toUpperCase();
	weatherForecast = weatherForecast.replace(char, capChar);

	
		// location is Laceby
		$("#weather-today p").html(weatherForecast);
		setWeatherIcon(weatherForecast, "today");
		
}

function setWeatherIcon(weatherForecast, location){

	// function processes weather forecast from getWeatherStatus
	// and assigns an icon related to the description
	
	var selector = "#weather-" + location + " img";
	
	// RAIN
	if (	weatherForecast.search("rain") 		!= -1 || 
			weatherForecast.search("Rain") 		!= -1 ||	
			weatherForecast.search("drizzle")	!= -1 ||
			weatherForecast.search("Drizzle")	!= -1 ||
			weatherForecast.search("shower")	!= -1 ||
			weatherForecast.search("Shower")	!= -1 
			
	){		$(selector).attr("src", baseURL + "/images/rain-day.png"); }
	
	// SNOW
	else if (	weatherForecast.search("snow") 		!= -1 || 
				weatherForecast.search("Snow") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/snow-day.png"); }	
	
	// SLEET
	else if (	weatherForecast.search("sleet") 		!= -1 || 
				weatherForecast.search("Sleet") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/sleet-day.png"); }	
	
	// OVERCAST
	else if (	weatherForecast.search("overcast") 		!= -1 || 
				weatherForecast.search("Overcast") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/overcast-day.png"); }
	
	
	// CLOUDY
	else if (	weatherForecast.search("cloud") 		!= -1 || 
				weatherForecast.search("Cloud") 		!= -1 	
				
	){			$(selector).attr("src", baseURL + "/images/cloudy-day.png"); }

	
	// SUNNY INTERVALS
	else if (	weatherForecast.search("Sunny intervals")	!= -1 ||
				weatherForecast.search("sunny intervals")	!= -1

	){			$(selector).attr("src", baseURL + "/images/cloudy-sun.png"); }

	
	// HAIL
	else if (	weatherForecast.search("hail") 		!= -1 || 
				weatherForecast.search("Hail") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/hail-day.png"); }
	
	// FOG
	else if (	weatherForecast.search("fog") 		!= -1 || 
				weatherForecast.search("Fog") 		!= -1 ||
				weatherForecast.search("mist")		!= -1 ||
				weatherForecast.search("Mist")		!= -1
						
				
	){			$(selector).attr("src", baseURL + "/images/fog-day.png"); }

	
	// SUNNY
	else if (	weatherForecast.search("sun") 		!= -1 || 
				weatherForecast.search("Sun") 		!= -1 ||
				weatherForecast.search("sunny") 	!= -1 ||
				weatherForecast.search("Sunny") 	!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/sun.png"); }
	
	// THUNDER STORMS
	else if (	weatherForecast.search("thunder") 		!= -1 || 
				weatherForecast.search("Thunder") 		!= -1 ||
				weatherForecast.search("lightening") 	!= -1 ||
				weatherForecast.search("Lightening") 	!= -1 ||
				weatherForecast.search("storm")		 	!= -1 ||
				weatherForecast.search("storm")		 	!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/thunder-day.png"); }


	// CLEAR
	else if (	weatherForecast.search("clear") 		!= -1 || 
				weatherForecast.search("Clear") 		!= -1 
								
	){			$(selector).attr("src", baseURL + "/images/clear-day.png"); }


	// ALL OTHERS
	else { $(selector).attr("src", baseURL + "/images/clear-day.png"); }	

}

function getWeatherStatus1(data){
	
	// function processes weather description from
	// BBC weather feed & constructs a simple description

	var strPosStart;
	var strPosEnd;
	var tempString;

	// get todays forecast	
	var weatherForecast = data.query.results.item[1].title;
	
	// remove day from string
	strPosStart = weatherForecast.search(":");
	weatherForecast = weatherForecast.slice(strPosStart+1);
	
	// remove temp from string
	for (i = 1; i <= 2; i++){
		weatherForecast = weatherForecast.replace("Temp", "");
		weatherForecast = weatherForecast.replace(" :", ":");
	}

	// remove fahrenheit
	for (i = 1; i <= 2; i++){
		strPosStart = weatherForecast.indexOf("(");
		strPosEnd 	= weatherForecast.indexOf(")");
		tempString 	= weatherForecast.slice(strPosStart, strPosEnd+1);
		weatherForecast = weatherForecast.replace(tempString, "");
		weatherForecast = weatherForecast.replace(" ,", ",");
	}
	
	// get hours of day light
	var daylight = data.query.results.item[0].description;
	
	// search description for sunrise
	strPosStart = daylight.indexOf("Sunrise:");
	var sunrise = daylight.substr(strPosStart + 9, 5);
	
	// search description for sunset
	strPosStart = daylight.indexOf("Sunset:");
	var sunset 	= daylight.substr(strPosStart + 8, 5);
	
	// update forecast string
	daylight = "Daylight: " + sunrise + " - " + sunset + ".";
	weatherForecast = weatherForecast + " " + daylight;
	
	// capitalise first letter of string
	var char = weatherForecast.substr(1, 1);
	capChar = char.toUpperCase();
	weatherForecast = weatherForecast.replace(char, capChar);

	
		// location is Laceby
		$("#weather-tomorrow p").html(weatherForecast);
		setWeatherIcon1(weatherForecast, "tomorrow");
		
}

function setWeatherIcon1(weatherForecast, location){

	// function processes weather forecast from getWeatherStatus1
	// and assigns an icon related to the description
	
	var selector = "#weather-" + location + " img";
	
	// RAIN
	if (	weatherForecast.search("rain") 		!= -1 || 
			weatherForecast.search("Rain") 		!= -1 ||	
			weatherForecast.search("drizzle")	!= -1 ||
			weatherForecast.search("Drizzle")	!= -1 ||
			weatherForecast.search("shower")	!= -1 ||
			weatherForecast.search("Shower")	!= -1 
			
	){		$(selector).attr("src", baseURL + "/images/rain-day.png"); }
	
	// SNOW
	else if (	weatherForecast.search("snow") 		!= -1 || 
				weatherForecast.search("Snow") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/snow-day.png"); }	
	
	// SLEET
	else if (	weatherForecast.search("sleet") 		!= -1 || 
				weatherForecast.search("Sleet") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/sleet-day.png"); }	
	
	// OVERCAST
	else if (	weatherForecast.search("overcast") 		!= -1 || 
				weatherForecast.search("Overcast") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/overcast-day.png"); }
	
	
	// CLOUDY
	else if (	weatherForecast.search("cloud") 		!= -1 || 
				weatherForecast.search("Cloud") 		!= -1 	
				
	){			$(selector).attr("src", baseURL + "/images/cloudy-day.png"); }

	
	// SUNNY INTERVALS
	else if (	weatherForecast.search("Sunny intervals")	!= -1 ||
				weatherForecast.search("sunny intervals")	!= -1

	){			$(selector).attr("src", baseURL + "/images/cloudy-sun.png"); }

	
	// HAIL
	else if (	weatherForecast.search("hail") 		!= -1 || 
				weatherForecast.search("Hail") 		!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/hail-day.png"); }
	
	// FOG
	else if (	weatherForecast.search("fog") 		!= -1 || 
				weatherForecast.search("Fog") 		!= -1 ||
				weatherForecast.search("mist")		!= -1 ||
				weatherForecast.search("Mist")		!= -1
						
				
	){			$(selector).attr("src", baseURL + "/images/fog-day.png"); }

	
	// SUNNY
	else if (	weatherForecast.search("sun") 		!= -1 || 
				weatherForecast.search("Sun") 		!= -1 ||
				weatherForecast.search("sunny") 	!= -1 ||
				weatherForecast.search("Sunny") 	!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/sun.png"); }
	
	// THUNDER STORMS
	else if (	weatherForecast.search("thunder") 		!= -1 || 
				weatherForecast.search("Thunder") 		!= -1 ||
				weatherForecast.search("lightening") 	!= -1 ||
				weatherForecast.search("Lightening") 	!= -1 ||
				weatherForecast.search("storm")		 	!= -1 ||
				weatherForecast.search("storm")		 	!= -1 
				
	){			$(selector).attr("src", baseURL + "/images/thunder-day.png"); }


	// CLEAR
	else if (	weatherForecast.search("clear") 		!= -1 || 
				weatherForecast.search("Clear") 		!= -1 
								
	){			$(selector).attr("src", baseURL + "/images/clear-day.png"); }


	// ALL OTHERS
	else { $(selector).attr("src", baseURL + "/images/clear-day.png"); }	

}



