  function eMail(span,user,domain,extra,text){
  var r = document.getElementById(span);
  var rdata = r.innerHTML;
  r.innerHTML = '<A href="mailto:'+user+'@'+domain+extra+'"'+' '+rdata+'>'+text+'<\/A>';
  }
  var firstTime = true;
var Mouse = {};
var mouseStatus = false;
var weather;
var rgb = {r:149,g:176,b:182};

function initWeather(location){
        $i('details').style.display = 'none';
        var options = {};

            options.location = 'false';
        if (typeof location != 'undefined'){
            options.location = location;
          } else {
          temp = getCookie(location);
          if (temp) options.location = temp;
        }
        var button = getCookie('button');
        if (button) {
         //$i('changeButton').value = button;
          var temp = getCookie('time');
          if (temp){
          options.time = temp;
          deleteCookie('time');
          }
          var temp = getCookie('weather');
          if (temp){
          options.weather = temp;
          deleteCookie('weather');
          }
          var temp = getCookie('cbLocation');
          if (temp) {
             options.location = temp;
          deleteCookie('cbLocation');
          }

         if (typeof options.time != 'undefined')
         settings.iTime = options.time;
         if (typeof options.weather != 'undefined')
         settings.iWeather = options.weather;
        }
        var temp = getCookie('address');
        options.address = (temp)?temp:'';
        options.wind = settings.iWind;
        options.weather = settings.iWeather;
        options.time = settings.iTime;
        options.rgb = rgb;
        options.dataChanged = initWeather;
        if (typeof weather == 'undefined')
        weather = new Weather();
        weather.init(options);
        $(document).bind("mousemove",function(e) {
                Mouse = e;
                //if (!isMouseInDiv(e,'details')) popUpLog(false);
                if (mouseStatus) {
                  $i('statusPos').innerHTML = 'x='+Mouse.pageX+' y='+Mouse.pageY+'</b>';
                }
        })
        $(document).bind("timeline.begin",function(){
           var same = false;
           if ((weatherApp.coords.longitude == weatherApp.defaultCoords.longitude) &&
                       (weatherApp.coords.latitude == weatherApp.defaultCoords.latitude))
           same = true;
           if (firstTime) {
             $('#weatherIconImg').attr('src',weatherApp.image);
             $i('weatherIcon').style.display = '';
             firstTime = false;
           } else {
             popUpLog(true);
           }
           drawMap(!same);
        });
}

  $(document).ready(function() {
        $("ul.sf-menu").supersubs({
            minWidth:    5,    // minimum width of sub-menus in em units
            maxWidth:    35,   // maximum width of sub-menus in em units
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over
                               // due to slight rounding differences and font-family
        }).superfish();  // call supersubs first, then superfish, so that subs are
                         // not display:none when measuring. Call before initialising
                         // containing tabs for same reason.


        /*
        var w = $i('wrapper').offsetWidth;
        $i('searchText').style.width = (w-20)+'px';
        $i('searchButton').style.top = '0px';
        while(true){
          if   (parseInt($i('#searchButton').offsetLeft,10) >= parseInt($('#searchText').offsetLeft,10)) break;
          var w = parseInt($i('searchText').style.width,10);
          w--;
          $i('searchText').style.width = w+'px';
        }
        */

        $i('sky').style.height = '100px';
        $i('sky').style.width = '600px';
        var image = getHtmlSize($i('#mainImage'));
        var m = $i('menuButtons').offsetWidth;
        var b = $i('searchButton').offsetWidth;

        $i('searchText').style.width = (image.width-(m+b+5))+'px';
        /*
        $i('searchButton').style.top = (image.top-parseInt($i('searchButton').style.height,10))+'px';
        */

        $('#menu li').each(function(i,element){
          element.title ='';
        });

        var location = getCookie('location');
        location = (location)?location:'false';
        if (location == "undefined") {
          deleteCookie('location');
          location = 'false';
        }
        $i('#canvas').style.left = image.left+'px';
        $i('#canvas').style.top = '54px';
        $i('#canvas').style.height = (location == 'true')?Math.round(image.height/3)+'px':'325px'
        $i('#canvas').height = Math.round(image.height/2);
        $i('#canvas').style.width = image.width +'px';
        $i('#canvas').width = image.width;
        var d = getHtmlSize($i('details'));
        var t = (image.top + image.height) - d.height;
        $i('details').style.left = image.left+image.width+'px';
        d = getHtmlSize($i("weatherIconImg"));
        var t = (image.top + image.height) - d.height;
        $i('weatherIcon').style.top = t+'px';
        $i('weatherIcon').style.left = $i('details').style.left;

        var sSearch;
        var sTime = getParam('time');
        if (sTime)  {
          settings.iTime = sTime;
        } else {
          sSearch = (document.location.search.length > 1) ? document.location.search.substring(1) : location;
          sSearch = unescape(sSearch);
        }
        initWeather(sSearch);
        m = getParam('mouse');
        mouseStatus = (m)?true:false;
  eMail("emailg","Graham","GandJLawrence.co.uk","","Graham&nbsp;&nbsp;&nbsp;&nbsp;");
  eMail("emailj","Jane","GandJLawrence.co.uk","","Jane");
  eMail("emaile","Emma","Simply-Flowers.org","","Emma");
  eMail("emails","Syd","SydLawrence.com","","Stephen aka Syd");
  });


var homeMap;
function drawMap(draw){
        if(!draw){
          $('#gMap').hide();
          $('#Langlands').show();
          $('#Address').hide();
          $i('canvas').style.zIndex = weatherApp.minZIndex;
          weatherApp.showingMap = false;
        } else {
        $('#Langlands').hide();

        $('#gMap').show();
        var gStrt = new google.maps.LatLng(weatherApp.coords.latitude,weatherApp.coords.longitude);
        setCookie('location',weatherApp.myLocation);
        var gMapOptions = {
        zoom:10,
        center: gStrt,
        scaleControl: true,
 mapTypeControl: true,
    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.RIGHT_BOTTOM
    },
    panControl: false,
    panControlOptions: {
        position: google.maps.ControlPosition.BOTTOM_RIGHT
    },
    zoomControl: true,
    zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.RIGHT_CENTER
    },
    scaleControl: true,
    scaleControlOptions: {
        position: google.maps.ControlPosition.BOTTOM_LEFT
    },
    streetViewControl: false,
    streetViewControlOptions: {
        position: google.maps.ControlPosition.CENTER_RIGHT
    },
        mapTypeId: google.maps.MapTypeId.HYBRID};
        homeMap = new google.maps.Map(document.getElementById("gMap"),gMapOptions);
        if (weatherApp.maxZIndex == 0) weatherApp.maxZIndex = 1+maxZIndex();
        if (weatherApp.address != '') {
           $('#Address').html(weatherApp.address);
           setCookie('address',weatherApp.address);
           $('#Address').show();
           $i('canvas').style.zIndex = weatherApp.maxZIndex;
           $i('menu').style.zIndex = maxZIndex()+1;
        }
        weatherApp.showingMap = true;
        }
}

function isMouseInDiv(mouse,div){
  if (typeof mouse.pageX == 'undefined') return false;
  var size = getHtmlSize($i(div));
  if (mouseStatus) {
    $i('statusDiv').innerHTML = 'left='+size.left+' top='+size.top+' width='+size.width+' height='+size.height+'<br>';
  }
  if (mouse.pageX < size.left) return false;
  if (mouse.pageY < size.top) return false;
  if (mouse.pageX > (size.left+size.width)) return false;
  if (mouse.pageY > (size.top+size.height)) return false;
  return true;
}

function popUpLog(state) {
 if ((state) && ($i('details').style.display == '')) return;
 if (weatherApp.gettingData) return;
 var html = $i('log').innerHTML.replace(/<p>/g,'').replace(/<\/p>/g,'<br/>');
 if ((state) && (html.indexOf('Temperature') < 0)) {
 if (weatherApp.gettingData) return;
 }

       if (weatherApp.showingMap) {
          var latlong = 'Latitude: '+weatherApp.coords.latitude.toString().substr(0,8)+'<br/>';
          latlong += 'Longitude: '+weatherApp.coords.longitude.toString().substr(0,8)+'<br/>';
          html = latlong+html;
       }

 $i('#detailsImage').src = weatherApp.image;
 $i('fromLog').innerHTML = html;
 $i('clearButton').style.display  = ($i('changeButton').value=='Settings') ?'none':'';
 $i('reloadButton').style.display =  $i('clearButton').style.display;
 if (weatherApp.data != '') {
 $i('fromLog').innerHTML += '<br/>'+weatherApp.forecast;
 if (typeof homeMap != 'undefined') {
 html = '<a href="Maps/?'+homeMap.getCenter().toUrlValue()+'" target="_blank';
 html += ' title="Show street view for '+ weatherApp.address+'">Street View</a>';
 if (weatherApp.showingMap) $i('fromLog').innerHTML += '&nbsp;&nbsp;'+html;

 }

 } else {

 }
 if (!state) {
   //if (isMouseInDiv(Mouse,'details') || isMouseInDiv(Mouse,'mainImage')) return;
   $i('details').style.display = 'none';
   $i('weatherIcon').style.display='';
 } else {
   $i('details').style.display = '';
   $i('weatherIcon').style.display='none';
 }
 if ($i('details').style.display == ''){
   var sizeD = getHtmlSize($i('details'));
   var sizeI = getHtmlSize($i('mainImage'));
   if (weatherApp.showingMap) sizeI = getHtmlSize($i('gMap'));
   var top = (sizeI.top+sizeI.height)-sizeD.height;
   $i('details').style.top = top+'px';
 }
}


function reloadData(){
  var button = $i('reloadButton').value;
  var reload = false;
  switch(button){
    case 'Reset':
       $i('changeButton').value = 'Settings';
       $i('reloadButton').value = 'Reload';
       $i('changeData').style.display = 'none';
       $i('detailData').style.display = '';
       showClass('changeButtons',false);
       settings = {iLocation:'',iWeather:'',iTime:'',iWind:''};
       weatherApp.showingMap = false;
       deleteAllCookies();
       initWeather('false');
       break;
    case 'Reload':
  initWeather();
       break;
 }

  //window.location.reload();

}

var settings = {iLocation:'',iWeather:'',iTime:'',iWind:''};
function changeData(clear){
  var button = $i('changeButton').value;
  var reload = false;
  clear = (typeof clear != 'undefined');
  if (clear) button = 'Apply';
  switch(button){
    case 'Settings':
       if (settings.iTime == '') {
         var iTime = weatherApp.localTime.split(' ');
         settings.iTime = iTime[1];
         weatherApp.HHMM = settings.iTime;
       }
       if (settings.iWeather == '') {
         settings.iWeather = weatherApp.weather;
       }
       if (weatherApp.showingMap) {
         settings.iLocation = weatherApp.weatherStation;
         } else {
         settings.iLocation = '';
         }

       if (settings.iWind == '') {
          settings.iWind = weatherApp.wind;
       }
       $i('changeData').style.display = '';
       $i('detailData').style.display = 'none';
       $i('changeButton').value = 'Apply';
       $i('reloadButton').value = 'Reset';
       $i('reloadButton').title = 'Reset to the original home page.';
       showClass('changeButtons',true);
       $i('iLocation').value = settings.iLocation;
       $i('cbLocation').checked = false;
       createYcodesList(settings.iWeather,'listWeatherSelect');
       createTimeList(settings.iTime,'listHoursSelect','listMinsSelect');
       createWindList(settings.iWind,'listWindSelect');
       break;
    case 'Apply':
       $i('reloadButton').value = 'Reload';
       $i('reloadButton').title = 'Load the weather data again.';
       $i('changeData').style.display = 'none';
       $i('detailData').style.display = '';
       showClass('changeButtons',false);
       var iTime = $i('iHours').value+':'+$i('iMins').value;
       var iWind = $i('iWind').value;
       var iLocation = ($i('iLocation').value == '')?'false':$i('iLocation').value;
       var iLocation = $i('iLocation').value;
       var iWeather = $i('iWeather').value;
       if (clear) {
         iTime = iWind = iWeather = '';
       }
       var changedData = (iLocation != settings.iLocation)  ||
                         ($i('cbLocation').checked) ||
                         (iWeather != settings.iWeather) ||
                         (iWind != settings.iWind) ||
                         (iTime != settings.iTime) ;
       $i('changeButton').value = 'Settings';
       if (changedData) {
         var location = '';
         if ($i('cbLocation').checked) location = 'true';
         if ((location == '') && ($i('iLocation').value.trim() != '')) {
           location = $i('iLocation').value;
         }
         if (location == settings.iLocation) {
            if (iTime != weatherApp.HHMM)
            settings.iTime = iTime;
            if (iWeather != weatherApp.weather)
            settings.iWeather = iWeather;
            if (iWind != weatherApp.wind)
            settings.iWind = iWind;
            if (iWind != '') {
              settings.iWind = ('kph' == $("input[name='wUnits']:checked").val())?(5*iWind)/8:iWind;
              }
         } else {
           settings.iWind = settings.iTime = settings.iWeather = '';
         }
         reload = true;

       }
       //popUpLog(false);
       break;
  }
  if (reload) {
     setCookie('button',$i('changeButton').value);
     if (location != '') { initWeather(location); }
     else initWeather();
  }
}

function createTimeList(hhmm,idH,idM) {
                hhmm = hhmm.split(':');
                var oh = "";
                for(var i=0;i<24;i++){
                        var vi = ('0'+''+i).substr(-2);
                        oh += "<OPTION VALUE='"+vi+"'";
                        if(i == hhmm[0])
                        oh += " SELECTED ";
                        oh += ">" + vi;
                }
                var on = "";
                for(var i=0;i<60;i++){
                        on += "<OPTION VALUE='"+i+"'";
                        if(i == hhmm[1])
                        on += " SELECTED ";
                        on += ">" + i;
                }
        setInnerHTML(idH,oh);
        setInnerHTML(idM,on);
}

function createWindList(wind,idW) {
                var oh = "";
                for(var i=1;i<100;i++){
                        oh += "<option value='"+i+"'";
                        if(i == wind)
                        oh += " selected ";
                        oh += ">" + i;
                }
        setInnerHTML(idW,oh);
}

function createYcodesList(chosen,id){
       chosen = 1*chosen;
        var options = ''
            options += '<option value"-1">Select the weather</option>'+"\n";
           for(var i in ycode){
                   var text = ycode[i].text.trim();
                   var selected = (i==chosen)?'SELECTED':'';
                   options += '<option '+selected+' value="'+i+'">' + text+ "</option>\n";
           }
        setInnerHTML(id,options);
}

// workaround for MS IE deficiency
function setInnerHTML(id,options){
 var div = $i(id);
 if (div == null) alert(id+' is null');
 var innerHTML = div.innerHTML;
 var p = innerHTML.indexOf('>');
 innerHTML = innerHTML.substr(0,p+1);
 div.innerHTML = innerHTML+options+'</select>';
}


