var Timeline = Class.create({
  initialize: function(path, uid, lh, lc, first, last, auth) {
	var cur;

    this.path = path;
	this.backend = path + 'backend/';
	this.uid = uid;
	this.cookie = 'timeline_'+uid;
	this.layer_head = $(lh); 
	this.layer_content = $(lc);
	this.months = 3;

	cur = new Date(first * 1000);
	this.first_month = cur.getMonth() + 1; 
	this.first_year = (cur.getYear() < 1000) ? cur.getYear() + 1900 : cur.getYear();
	cur = new Date(last * 1000);
	this.last_month = cur.getMonth() + 1; 
	this.last_year = (cur.getYear() < 1000) ? cur.getYear() + 1900 : cur.getYear();

	this.sel_year = this.last_year;
	this.sel_month = this.last_month;
	this.auth = auth;
	this.head;
	this.blogs;
	this.bobj = new Blogs(path, lc, auth); 

    var tmp = cookie_read(this.cookie);
	if (tmp != null) {
		var ym = tmp.split('-');
		this.setYear(parseInt(ym[0]));
		this.setMonth(parseInt(ym[1]));
    }
  },

  getMonth: function() {
	return this.sel_month;
  },

  setMonth: function(month) {
	var old_month = this.sel_month;
	var old_year = this.sel_year;

	this.sel_month = parseInt(month);
	if (this.sel_month > 12) {
		this.sel_year++;
		this.sel_month -= 12;
	}

	if (this.sel_year == this.last_year)
		if (this.sel_month > this.last_month)
			this.sel_month = this.last_month;
	else if (this.sel_year == this.first_year) {
		if (this.sel_month < this.first_month)
			this.sel_month = this.first_month;
	}

  },

  getYear: function() {
	return this.sel_year;
  },

  setYear: function(year) {
	this.sel_year = parseInt(year);
	if (this.sel_year > this.last_year)
		this.sel_year = this.last_year;
	else if (this.sel_year < this.first_year)
		this.sel_year = this.first_year;
  },

  getDateStr: function(year, month) {
	var mon;	
	mon = month < 10 ? "0"+month : month;	
	return year+''+mon; 
  },

  getHeader: function() {
  	var right, left;
	var month, tmp, tmp2;
	var j = (this.months * 2) + 1;

	year = this.sel_year;
	month = this.sel_month - this.months;
	if (month <= 0) {
		month += 12;
		year--;

	}

	if (year == this.last_year) {
		diff = (this.sel_month + this.months) - this.last_month;
		if (diff > 0)
			month -= diff;
		if (month <= 0) {
			month += 12;
			year--;
		}
	}
	else if (this.sel_year == this.first_year) {
		if (month <= this.first_month || year < this.first_year) {
			month = this.first_month;
			year = this.first_year;
		}
	}

	if (this.last_year == this.first_year) {
		diff = this.last_month - this.first_month;
		if (month < this.first_month || year <= this.first_year) {
			month = this.first_month;
			year = this.first_year;
			j = diff + 1;
		}
	}

	var pars = 'uid='+this.uid+'&dy='+year+'&dm='+month+'&c='+j
	    + '&dummy='+Math.floor(Math.random()*10000);

	var obj = this;
	var handler = function(req) {
		var data = JSON.parse(req.responseText);
		obj.head = data.timeline;
		obj.renderHeader();
	};

	var req = new Ajax.Request(backend+'getTimeline.php',
		{ method: 'get', parameters: pars, onComplete: handler});
  },

  update: function() {
	this.getHeader();

	var datestr = this.getDateStr(this.sel_year, this.sel_month);
	var pars = 'uid='+this.uid+'&dls=' + datestr + '01&dle=' +
		datestr + '31&s=recvdate&dummy='+Math.floor(Math.random()*10000)

	var obj = this;
	var handler = function(req) {
		var data = JSON.parse(req.responseText);
		obj.bobj.blogs = data.blogs; 

		var modifyHandler = function(obj, result, arg) {
			arg.update();
		}; 

		obj.bobj.auth = obj.auth;	
		obj.bobj.renderBlogs(modifyHandler, obj);
		cookie_create(obj.cookie, obj.sel_year+'-'+obj.sel_month);
	};
	
	var req = new Ajax.Request(backend+'getBlogs.php',
		{ method: 'get', parameters: pars, onSuccess: handler});
  },

  renderHeader: function() {
	this.layer_head.innerHTML = '';

	var tmp = document.createElement('h3');
	tmp.innerHTML = this.sel_year;
	this.layer_head.appendChild(tmp);

	var tbl = document.createElement('table');
	var tblhead = document.createElement('tbody'); 
	var row = document.createElement('tr');

	tblhead.appendChild(row);
	tbl.appendChild(tblhead);
	this.layer_head.appendChild(tbl);

	for (i = 0; i < this.head.length; i++) {
		var c;
		var td = document.createElement('td');
		var cur = this.head[i];
		var month = cur.month; 
		var year = cur.year; 
		var blog = cur.blogs;

		if (month == this.sel_month)
			Element.addClassName(td, 'timeline_selected');
		row.appendChild(td);

		c = document.createElement('div');
		if (blog.id == 0) {
			c.innerHTML = ' &nbsp; ';
			td.appendChild(c);
		}
		else {
			var imgpath = this.path + 'thumbs/60/' + blog.id;
			var img = document.createElement('img');

			img.src = imgpath;
			img.border = '0';

			c.appendChild(img);
		}
		c.innerHTML += '<br/>';
		c.innerHTML += getMonthAbr(month-1);

		var obj = this;
		var lnk = document.createElement('a');
		lnk.href = 'javascript:nop()';
		c.onclick = timeline_month_select;	
		c.backref = this;
		c._month = month;
		c._year = year;
		lnk.appendChild(c);
		td.appendChild(lnk);
	}
  }
});

function timeline_month_select()
{
	var obj = this.backref;
	var month = this._month;
	var year = this._year;

	obj.setYear(year);
	obj.setMonth(month);
	obj.update();
}

function timeline_popup_show()
{
	var obj = this._popup_obj;
	obj.show();
}

