var m__error_message       = new String ("");

var m__default_weather_location = 'Glasgow';
/*
 * 
 */

function m__check_error () {
    if (m__error_message.length) {
	var myErr = '<div style="padding: 5px; width: 620px; clear: both; background-color: #FADCEA; ' + 
	    ' border-bottom: 2px solid rgb(204, 204, 204); border-right: 2px solid rgb(204, 204, 204); background-repeat: no-repeat;" id="comment_box">' +
	    '<h3 style="margin-top: 3px; font-size: 18px; color: #FF0000;" class="h3">We' + "'" + 're sorry</h3>' +
	    '<p style="font-family: Arial,Helvetica,sans-serif; font-size: 12px;">' +
	    m__error_message +
	    '</p></div>';

	$("div.thirds-two").prepend(myErr);
    }
    return true;
}

function temp_nav_fix () {
	if (document.location.hostname.match(/login(dev)?\.thescottishfarmer\.co\.uk/)){
		$("div#topnav ul li").removeClass();
		$("#subnav ul").hide();
	}
    return false;
}

$(document).ready(function () {
		      m__check_error();
		      temp_nav_fix();
		      return true;
		  });

$(function () { // weather widget
      var toFixed = function(val, n) {
	  var result = val.toFixed(n);
	  if (result == val) {
	      return val.toString();
	  }
	  return result;
      };
      
      var weatherWidgetCSS = "<style type='text/css'>" + 
	  "#weather-widget{float: left;background: #fff;width: 184px; height: 50px; border: solid 2px #10888c; border-radius:5px; padding: 5px; position:relative; left:480px; top:-20px;}" + 
	  "#weather-widget h3 {color:#2c4989; font-size:12px; }" +
	  "#weather-location {font-size: 18px !important; font-weight: normal;}" +
	  "#temp-holder{font-size: 18px !important; position: relative; top:-37px; left: 90px; color: #10888c;}" +
	  "#weather-widget img {width: auto; height: auto; position:relative;float:right;top:-55px;}" +
	  "#logo p {left: 0px;}" +
	  "#change-loc {background: url(http://www.thescottishfarmer.co.uk/ui/img/icon-sprite.gif); background-position: -424px -80px; background-repeat: no-repeat; padding: 0 10px 5px 10px;}" +
	  "</style>",
      weatherWidgetHTML = "<div id='weather-widget'><h3>WEATHER</h3><h3 id='weather-location'>Glasgow</h3><a href='http://www.thescottishfarmer.co.uk/weather/' id='change-loc'>Change location</a>" +
	  "<div id='weather-icon'><img src='http://" + myLogin + myHost + "/weather/images/fair-small-night.png'/></div>" +
	  "<div id='temp-holder'><span id='maxtemp'></span>&deg;C</div>" +
	  "</div>";

      weatherWidgetCSS += weatherWidgetHTML;
      $("#logo").append(weatherWidgetCSS);

      var writeWeatherTable = function (data) {
	  var weatherIcon = "",
	  isClear = false,
	  currentHour,
	  indexOfMatch;

	  // hide the weather video on weekends
	  if (typeof(data[0].wdaynow) != "undefined" && (data[0].wdaynow == 0 || data[0].wdaynow == 6))
	      $("iframe#weatheriframe").hide();

	  currentHour = data[0].hournow + 6;
          
	  if (currentHour < 6) {
	      currentHour = "0";
	  } else if (currentHour < 12) {
	      currentHour = "6";
	  } else if (currentHour < 18) {
	      currentHour = "2";
	  } else {
	      currentHour = "8";
	  }

	  if (typeof data[0].forecast !='undefined') {
              $(data[0].forecast).each(function(index) {
					   if (currentHour === this.datetime.substr(12,1)) {
					       indexOfMatch = index;
					       return false;
					   }
					   return true;
				       });
	      
              var currentWeatherCondition = data[0].forecast[indexOfMatch].weathercondition;
              var currentTemperature      = toFixed((parseFloat(data[0].forecast[indexOfMatch].tempmax) + parseFloat(data[0].forecast[indexOfMatch].tempmin))/2, 1);
              var currentChanceOfRain     = data[0].forecast[indexOfMatch].chancerain;
	  };

	  $("#weather-condition").text(currentWeatherCondition);
	  $("#maxtemp"          ).text(currentTemperature);
	  $("#chance-of-rain"   ).text(currentChanceOfRain);
	  
	  switch (currentWeatherCondition) {
	  case "Clear":
	      weatherIcon += "fair";
	      isClear = true;
	      break;
	  case "Mostly clear":
	      weatherIcon += "fair";
	      isClear = true;
	      break;
	  case "Clear spells":
	      weatherIcon += "fair";
	      isClear = true;
	      break;
	  case "Broken cloud":
	      weatherIcon += "fair";
	      isClear = true;
	      break;
	  case "Cloudy":
	      weatherIcon += "cloudy";
	      isClear = false;
	      break;
	  case "Fog":
	      weatherIcon += "fog";
	      isClear = false;
	      break;
	  case "Dense Fog":
	      weatherIcon += "fog";
	      isClear = false;
	      break;
	  case "Fog patches":
	      weatherIcon += "fog";
	      isClear = false;
	      break;
	  case "Mist":
	      weatherIcon += "mist";
	      isClear = false;
	      break;
	  case "Mostly Cloudy":
	      weatherIcon += "cloudy";
	      isClear = false;
	      break;
	  case "Light rain":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Drizzle":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Light showers":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Light hail showers":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Hail shower":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Thunderstorm":
              weatherIcon += "thundery";
              isClear = false;
              break;
          case "Thundery":
              weatherIcon += "thundery";
              isClear = false;
              break;
          case "Snow":
              weatherIcon += "snow";
              isClear = false;
              break;
          case "Snowy":
              weatherIcon += "snow";
              isClear = false;
              break;
          case "Sleet":
              weatherIcon += "sleet";
              isClear = false;
              break;
	  case "Moderate showers":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Heavy showers":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Patchy drizzle":
	      weatherIcon += "showers";
	      isClear = false;
	      break;
	  case "Rain":
	      weatherIcon += "rain";
	      isClear = false;
	      break;
	  default:
	      break;
	  }
          
	  var theDate = 0, 
	  timeOfDay = "";

	  currentHour = data[0].hournow + 6;
	  if ((currentHour < 6) || (currentHour > 18)) {
	      if (isClear) {
		  timeOfDay = "-night";
	      }
	  }

	  $("#weather-icon").html("<img src='http://" + myLogin + myHost + "/weather/images/" + weatherIcon + "-small" + timeOfDay + ".png'/>");

	  return true;
      };

      var getWeather = function (location) {
          $.ajax({url:"http://" + myLogin + myHost + "/make-weather.cgi?location=" + escape(location),
              dataType: "jsonp",
              success: function(data) {
                  writeWeatherTable(data);
                  return true;
              }
         });
         return true;
      };
      
      if ($.cookie("herald-weather-location")) {
	  getWeather($.cookie("herald-weather-location"));
	  $("#weather-location").text($.cookie("herald-weather-location"));
      } else {
	  getWeather(m__default_weather_location);
	  $("#weather-location").text(m__default_weather_location);
      }

      var currentLoc = $.cookie("herald-weather-location");

      var checkWeatherLocation = function () {
	  if (currentLoc != $.cookie("herald-weather-location")) {
	      getWeather($.cookie("herald-weather-location"));
	      $("#weather-location").text($.cookie("herald-weather-location"));
	  }
	  return true;
      };
      checkWeatherLocation();
      return true;      
  });

true;

