Date.prototype.dateAndHour = function() {
	var m = this.getMonth()+1;
	var d = this.getDate();
	var h = this.getHours();
	return ''+this.getFullYear()+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d)+'-'+(h<10?'0'+h:h);
};

function GIS(){
	this.weather = {};
	this.city = {id:null};
	this.now = (new Date()).dateAndHour();
	this.table = null;
	this.wrows = null;
	this.currentwrow = null;
	this.currentwrow_html = '';
	this.url_pref = '';
}

GIS.prototype.log = function(arg){
	if(window.console) console.log(arg);
}

GIS.prototype.init = function(cityID, url_pref){
	this.table = jQuery('#wdaily1');
	this.wrows = jQuery('.wrow');
	this.city.id = cityID;
	if(url_pref != undefined) this.url_pref = url_pref;
}

GIS.prototype.getFactsData = function(callback, tm) {
	this.getFactsDataCallback = callback;
	$.ajax({
		type: 'GET',
		url: this.url_pref + '/ajax/facts',
		data: 'id='+this.city.id+'&tm='+tm,
		context: this,
		success: function(data) {
			this.currentwrow_html = data;
			// var items = jQuery.parseJSON(data);
			// this.weather = items;
			if(this.getFactsDataCallback) this.getFactsDataCallback();
		}
	});
}

GIS.prototype.updateFacts = function(){
	this.currentwrow = null;
	var date_parts = [];
	var tm = null;
	for(var i=0, l=this.wrows.length; i<l; i++){
		date_parts = this.wrows[i].id.match(/(\d{4})-(\d{2})-(\d{2})-(\d{2})/);
		if(date_parts[0]<=this.now){
			this.currentwrow = this.wrows[i];
			jQuery(this.wrows[i]).addClass('fact');
			tm = date_parts[4];
		}
		// this.log([this.wrows[i], date_parts[0], this.now, (date_parts[0]<=this.now)]);
	}
	if(this.currentwrow){
		this.currentwrow = jQuery(this.currentwrow);
		this.currentwrow.addClass('last');
		if(this.currentwrow.hasClass('forecast')){
			this.currentwrow.removeClass('forecast');
			this.currentwrow.fadeTo('slow', 0.2);
			this.getFactsData(function(){
				this.currentwrow.fadeTo('slow', 1);
				if(this.currentwrow_html!=='') 
					this.updateFactRow(this.currentwrow, this.currentwrow_html);
				/*
				for(var i=0, l=this.weather.length; i<l; i++){
					var row = jQuery('#wrow-'+this.weather[i].year+'-'+this.weather[i].month+'-'+this.weather[i].day_zeros+'-'+this.weather[i].time);
					if(row.length>0)
						this.updateFactRow(row, this.weather[i]);
				}
				*/
			}, tm);
		}
	}
}

GIS.prototype.updateFactRow = function(row, data){
	row.html(data);
	/*
	data.comfort = parseInt(data.comfort)>0 ? '+'+data.comfort : data.comfort;
	var tds = row.children();
	jQuery(tds[1]).html('<img class="png" src="/static/images/icons/new/'+data.ico+'" title="'+weatherFactFrom+' '+data.dt_utc+', UTC: '+data.dt_utc+', Local: '+data.dt_loc+'">');
	jQuery(tds[2]).html(data.descr);
	jQuery(tds[3]).html(data.temp + '&deg;');
	jQuery(tds[4]).html(data.press);
	jQuery(tds[5]).html('<dl class="wind"><dt class="wicon wind'+data.w_d+'" title="'+data.w_d_tf+'">'+data.w_d_ts+'</dt><dd>'+data.w_s+'</dd></dl>');
	jQuery(tds[6]).html(data.hum);
	jQuery(tds[7]).html(data.comfort + '&deg;');
	jQuery('.fact.last').removeClass('last');
	row.addClass('last');
	*/
}

var g=new GIS();