//this is what's used to put it into the page
//						<div class="newsWeatherText" id="newsWeatherText">
//						<script type="text/javascript" language="javascript">loadRSS('newsWeatherText', 'Eau_Claire.xml');</script>
//						</div>

/*	function :: whichCondition(condition)
	This function takes in the Condition from the XML Document, and returns the proper weather category.
*/
function whichCondition(condition)
{

	var output = "";
	if (condition.search("Thunderstorm") != -1 || condition.search("Funnel") != -1 || condition.search("Squal") != -1)
	{
		output = "Warning";
	}
	else if (condition.search("Fog") != -1 || condition.search("Haze") != -1)
	{
		output = "Fog";
	}
	else if (condition.search("Snow") != -1 || condition.search("Ice") != -1 || condition.search("Hail") != -1 ) 
	{
		output = "Snow";
	}	
	else if (condition.search("Drizzle") != -1 || condition.search("Mist") != -1 || condition.search("Spray") != -1 )
	{
		output = "Drizzle";
	}
	else if (condition.search("Rain") != -1 || condition.search("Drizzle") != -1 || condition.search("Mist") != -1 )
	{
		output = "Rain";
	}
	else if (condition.search("Clear") != -1 )
	{
		output = "Clear";
	}
	else if (condition.search("Partly Cloudy") != -1 || condition.search("Overcast") != -1 || condition.search("Scattered Cloud") != -1)
	{
		output = "Partly_Cloudy";
	}
	else if (condition.search("Mostly Cloudy") != -1 )
	{
		output = "Mostly_Cloudy";
	}
	else if (condition.search("Cloudy") != -1 )
	{
		output = "Cloudy";
	}
	else if (condition.search("Smoke") != -1 )
	{
		output = "Smoke";
	}
	else
	{
		output = "Na";
	}
	return output;
}

/*
	This function is the actual requester of the url to be retrieved, calling the proper calls depending on which browser
*/
function loadRSS(divId, url) {
    //linksDiv = document.getElementById(divId);
    var req, wrapper;
    wrapper = new Object();
    wrapper.divId = divId;
    wrapper.url = url;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        wrapper.request = req;
        req.onreadystatechange=function() {
            processLinksRequest(wrapper);
        }
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            wrapper.request = req;
            req.onreadystatechange=function() {
              processLinksRequest(wrapper);
            }
            req.open("GET", url, true);
            req.send();
        }
    }
}


function processLinksRequest(wrapper) {
    if (wrapper.request) {
      // only if req shows "loaded"
      if (wrapper.request.readyState == 4) {
          // only if "OK"
          if (wrapper.request.status == 200) {
              writeHeadlines(wrapper);
           } else {
              alert("Status: " + wrapper.request.status);
              alert("There was a problem retrieving the XML data:\n" +
                  wrapper.request.statusText);
           }
      }
    }
    else {
      alert('Request is undefined');
    }
}

/*
	this function is actually the one that writes out to the browser window
*/
function writeHeadlines(wrapper) {
    var linksDiv, items, i, title, link, description;
    linksDiv = document.getElementById(wrapper.divId);
	//this is an array of all the items from the parsed xml/rss document
    items = wrapper.request.responseXML.getElementsByTagName('item');
	{
	//this takes the first item, which for the weather is the current conditions, and takes it's description which is the text we want to take data from
      description = getElementData(items[0].getElementsByTagName('description')[0]);
	


	//replaces &#176; with °
	//replaces &deg; with °
	description = description.replace("&amp;deg;","\u00B0");
	description = description.replace("&deg;","\u00B0");
	description = description.replace("&amp;deg;","\u00B0");
	description = description.replace("&deg;","\u00B0");
	
		// alert("\u00B0");
	
		// finds the location of the first bar and colon, so that that the temperature data may be extracted
		var indexofFirstColon = description.indexOf(':') + 2;
		var indexOfFirstBar = description.indexOf('|') - 1;
		//this tokenizes the description into an array of words based on the bar
		var token = description.split("|");
		var f = description.substring(indexofFirstColon, description.indexOf('\u00B0'));
		f = Math.round(f);
		var c = description.substring(description.indexOf('/') + 1, description.indexOf('C') - 1);
		c = Math.round(c);
		
		// finds the token string that has Conditions in it
		var whichElement = -1;
		for ( var i=0; i < token.length; i++)
		{
			if (whichElement == -1)
			{
				if ( token[i].search("Conditions") != -1 )
				{
					whichElement = i;
				}
			}
		}
		//grab the value of the condition from the whole string
		var condition;
		if (whichElement != -1){
			condition = token[whichElement].substring((token[whichElement].indexOf(':')+2), token[whichElement].length-1);
		}
		// !!(not used anymore!! ) this replaces the strings in the string with '_''s for file names mostly is the reason
/*		var tempStr = '';
		for ( i=0; i<condition.length; i++)
		{
			if (condition.charAt(i) != ' ')
			{
				Math.round(tempStr) += condition.charAt(i);
			}else{
				Math.round(tempStr) += '_';
			}
		}
		condition = tempStr;*/
		condition = whichCondition(condition);
      var anchor = document.createElement('a');
      anchor.href = "http://www.wunderground.com/US/WI/Eau_Claire.html";
      anchor.appendChild(document.createTextNode(f + "\u00B0F / " + c + "\u00B0C "));
	  var pic = document.createElement('img');
	  /* auto sensing picture url; makes the url https if the document it's used in is  */
	  var sourceType = document.URL;
	  var appendage = "http:";
	  if(sourceType.indexOf("https:") > -1) {
		  appendage = "https:"
	  }
	  pic.src = appendage + '//www.uwec.edu/images/weather/'+condition+'.gif';
	  pic.border = '0';
	  pic.alt = condition;
	  pic.title = condition;
	  pic.height = 23;
	  pic.width = 32;
	  pic.align = 'middle';
	  anchor.appendChild(pic);
	  linksDiv.appendChild(anchor);
    }
}

function getElementData(elem) {
    var value;
    if (!elem) {
      value = 'N/A';
    }
    else {
      value = elem.firstChild.nodeValue;
    }
    return value;
}
