
var orig_time = new Date();
var orig_ts = Math.round(orig_time.getTime()/1000);

function main_init() {
	Event.observe("idMetar", "mouseover", metar_show);
	Event.observe("idMetar", "mouseout", metar_hide);
}

function metar_show() {
	try {
		
		new Ajax.Request('/?target=rmetar&junk='+get_junk() , 
                {method:'get',asynchronous:true,evalScripts:true,
                  onComplete: function(req) {
					 var metar = eval('(' + req.responseText + ')');
					 try {
					   var str = metar.metar + " " + metar.season + " " + metar.tod + " " + metar.wx + " ("+ metar.banner +")";
					   //console.log(str);
					   $("idMetar").innerHTML = str;
					 } catch(err) {
						 //console.log(err.message);
					 }
                  }})
		
	} catch(err) {
		//console.log("err: "+err.message);
	}
}

function metar_hide() {
	try {
		$("idMetar").innerHTML = "";
	} catch(err) {
		//console.log("err: "+err.message);
	}
}

function start_updates() {
		
	set_orig_time(new Date());
	var counter = new PeriodicalExecuter(function() { 
	
	var time = new Date();
	var new_ts = Math.round( (time.getTime())/1000);
	var conds = new Array();
	conds['ageh'] = time.getHours() - orig_time.getHours();
	conds['agem'] = time.getMinutes() - orig_time.getMinutes();
	conds['ages'] = time.getSeconds()  - orig_time.getSeconds();
	$('idUpdateSeconds').update(convertTime(new_ts - orig_ts)); 

},2);

  new PeriodicalExecuter(function(pe) {
      new Ajax.Updater('idCurrentConditions','/?ajax=1&ajaxid=idCurrentConditions&target=current-conditions&junk='+get_junk() , 
                                      {method:'get',asynchronous:true,evalScripts:true,
                                        onComplete: function() {
                                           new Effect.Highlight('idcclastupdate', {duration: 5.0});
                                           set_orig_time(new Date());
                                        }})
  },
  63);
  
  new PeriodicalExecuter(function(pe) {
      var junk = get_junk();
	   
		var temp_img = $('idTempDewLineGraph');
		temp_img.src= 'http://wx.hemna.com/?target=temp-dew-point-line-graph&junk='+junk;

		var press_img = $('idPressureLineGraph');
		press_img.src= 'http://wx.hemna.com/?target=pressure-line-graph&junk='+junk;

		var rain_img = $('idRainLineGraph');
	   rain_img.src= 'http://wx.hemna.com/?target=rain-line-graph&junk='+junk;

		var wind_img = $('idWindSpeedLineGraph');
	   wind_img.src= 'http://wx.hemna.com/?target=wind-speed-line-graph&junk='+junk;
      
/*      Effect.Fade('idTempDewLineGraph', {duration: 0.5, afterFinish: function() {           
           img = new Image();
           img.src = '/?target=temp-dew-point-line-graph&junk='+junk;
           img.id='idTempDewLineGraph';
           $('idTempDewLineGraphdiv').update();
           $('idTempDewLineGraphdiv').appendChild(img);
           //$('idTempDewLineGraph').src='/?target=temp-dew-point-line-graph&junk='+d.getSeconds();      
           Effect.Appear('idTempDewLineGraph', {duration: 0.5});     
      }});
		
      
      Effect.Fade('idPressureLineGraph', {duration: 0.6, afterFinish: function() {          
           img = new Image();
           img.src = '/?target=pressure-line-graph&junk='+junk;
           img.id='idPressureLineGraph';
           $('idPressureLineGraphdiv').update();
           $('idPressureLineGraphdiv').appendChild(img);
           //$('idPressureLineGraph').src='/?target=pressure-line-graph&junk='+d.getSeconds();      
           Effect.Appear('idPressureLineGraph', {duration: 0.6});     
      }});
		
      
      Effect.Fade('idRainLineGraph', {duration: 0.7, afterFinish: function() {
           img = new Image();
           img.src = '/?target=rain-line-graph&junk='+junk;
           img.id='idRainLineGraph';
           $('idRainLineGraphdiv').update();
           $('idRainLineGraphdiv').appendChild(img);
           //$('idRainLineGraph').src='/?target=rain-line-graph&junk='+d.getSeconds();      
           Effect.Appear('idRainLineGraph', {duration: 0.7});     
      }});
      
      Effect.Fade('idWindSpeedLineGraph', {duration: 0.7, afterFinish: function() {
           img = new Image();
           img.src = '/?target=wind-speed-line-graph&junk='+junk;
           img.id='idWindSpeedLineGraph';
           $('idWindSpeedLineGraphdiv').update();
           $('idWindSpeedLineGraphdiv').appendChild(img);      
           Effect.Appear('idWindSpeedLineGraph', {duration: 0.7});     
      }});
		*/
      
      Effect.Fade('idRadarImg', {duration: 0.7, afterFinish: function() {
           img = new Image();
           img.src = "http://radblast-mi.wunderground.com/cgi-bin/radar/WUNIDS_map?station=DAX&brand=wui&num=1&delay=15&type=N0R&frame=0&scale=0.326&noclutter=0&t=1235942643&lat=38.81977081&lon=-121.02596283&label=Pilot+Hill%2C+CA&showstorms=0&map.x=400&map.y=240&centerx=145&centery=359&transx=-255&transy=119&showlabels=1&severe=0&rainsnow=0&lightning=0&smooth=0&junk="+junk;
           img.id='idRadarImg';
           img.width=480;
           img.height=320;
           $('idRadardiv').update();
           $('idRadardiv').appendChild(img);      
           Effect.Appear('idRadarImg', {duration: 0.7});     
      }});
  },
  65);


   //update the image
   new PeriodicalExecuter(function(pe) {
	  var img = $('idWebCamImg');
	  img.src="http://wx.hemna.com/cam/cam.jpg?junk="+get_junk();
   },15);
   
   new PeriodicalExecuter(function(pe) {
	      new Ajax.Updater('idWebCam','/?ajax=1&ajaxid=idWebCam&target=web-cam&junk='+get_junk() , 
	                                      {method:'get',asynchronous:true,evalScripts:true,
	                                        onComplete: function() {
	    	  								   new Effect.Pulsate('idWebCamFlasUpdate', {pulses: 5, duration: 1.5});
	                                        }})
	  },
	  300); 
         
   
   
        
   new Tip('idMagic', {
		  title : 'Ajax tooltip',
		  ajax: {
		    url: '/?target=rmetar&junk='+get_junk(),
			options: {
			  onComplete: function(transport) {
			     // you could do something here after the ajax call is finished
		         console.log("anus! "+get_junk());
			  }
			}
	      },
		  className: 'pinktip',
		  hook: { target: 'bottomLeft', tip: 'topRight' }
		});   
}



function get_junk() {
  var d = new Date();  
  var junk=d.getMonth()+1+''+d.getDay()+''+d.getHours()+''+d.getMinutes()+''+d.getSeconds();
  return junk;
}

function set_orig_time(new_time) {
  orig_time = new_time;
  orig_ts =Math.round(orig_time.getTime()/1000);
}

function set_orig_time_ts(ts) {
  orig_ts = ts;
}

function convertTime(ts) {
	var ageh = Math.floor(ts / (60*60));
	var agem = Math.floor(ts / 60);
	var ages = ts;
	
	if (ageh > 0) ages = ts - (ageh * 60 * 60);
	if (agem > 0) ages = ages  - (agem * 60);

	newHTML = "";
	if (ageh>0)
	{
		newHTML = ageh+" hr "+agem+" min "+ages+" sec ago";
	}
	else if (agem>0)
	{
		newHTML = agem+" min "+ages+" sec ago";
	}
	else
	{
		newHTML = ages+" sec ago";
	}

	return newHTML;
}

