
var dom = new Object();
dom.table = function(attributes)
{
  this.table = document.createElement('table');
}
dom.table.prototype =
{
  cRow:function()
  {
    this.table.insertRow(0);
  }
}
dom.incdec = function(append, value, size, name, step, min, max, repeat, fillzero)
{
  this.append = append;
  this.value = value;
  this.size = size;
  this.name = name;
  this.step = step;
  this.min = min;
  this.max = max;
  this.repeat = repeat;
  this.fillzero = fillzero;
  this.draw();
}
dom.incdec.prototype =
{
  draw:function()
  {
    this.input = document.createElement('input');
    this.input.size = this.size;
    this.input.value = this.value;
    this.input.className = 'fixed';
    this.append.appendChild(this.input);
    this.inc = document.createElement('img');
    this.dec = document.createElement('img');
    this.inc.src = '/_/tpl/img/inc.gif';
    this.dec.src = '/_/tpl/img/dec.gif';
    this.inc.width = this.dec.width = '16';
    this.inc.height = this.dec.height = '10';
    this.inc.style.position = this.dec.style.position = 'absolute';
    this.inc.style.marginLeft = this.dec.style.marginLeft = '-17px';
    this.inc.style.marginTop = isMSIE ? '2px' : '1px';
    this.dec.style.marginTop = isMSIE ? '11px' : '10px';
    this.append.appendChild(this.inc);
    this.append.appendChild(this.dec);
    this.inc.onmouseover = this.dec.onmouseover = function()
    {
      this.style.backgroundColor = '#CECECE';
    };
    this.inc.onmouseout = this.dec.onmouseout = function()
    {
      this.style.backgroundColor = '#ffffff';
    };
    var _this = this;
    this.inc.onclick = function()
    {
      _this.incr();
    };
    this.dec.onclick = function()
    {
      _this.decr();
    };
    this.input.onblur = function()
    {
      _this.validate();
    };
    this.fillZero();
  },
  incr:function()
  {
    var v = parseInt(this.input.value.replace(/^0+/, ''));
    v = isNaN(v) ? 0 : v;
    this.value = this.input.value = this.repeat ? (v + this.step > this.max ? this.min : v + this.step) : (v + this.step > this.max ? this.max : v + this.step);    this.fillZero();
  },
  decr:function()
  {
    var v = parseInt(this.input.value.replace(/^0+/, ''));
    v = isNaN(v) ? 0 : v;
    this.value = this.input.value = this.repeat ? (v - this.step < this.min ? this.max : v - this.step) : (v - this.step < this.min ? this.min : v - this.step);    this.fillZero();
  },
  validate:function()
  {
    var v = parseInt(this.input.value.replace(/^0+/, ''));
    v = isNaN(v) ? 0 : v;
    if(v < this.min || v > this.max)
    {
      this.input.value = this.value;
    }
    else
    {
      this.value = v;
    }
    this.fillZero();
  },
  fillZero:function()
  {
    if(this.fillzero)
    {
      var l = this.max;
      var fake = this.value;
      while(fake.toString().length < l.toString().length)
      {
        fake = '0' + fake.toString();
      }
      this.input.value = fake;
    }
  }
}

var calendar = new Object();
calendar.months = new Array('Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь');
calendar.week_days = new Array('Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс');
calendar.init = function(id, marks, timestamp)
{
	this.date = new Date(parseInt(timestamp) * 1000);
	this.obj = common.el(id);
	this.marks = marks.split(':');
	this.build();
}
calendar.init.prototype =
{
	build:function()
	{
    this.wday = this.getDay(this.date.getDay());
  	this.day = this.date.getDate();
  	this.month = this.date.getMonth();
  	this.year = this.date.getFullYear();
  	var div = document.createElement('div');
  	div.innerHTML = '<b>' + calendar.months[this.month] + '</b>';
  	this.obj.appendChild(div);
  	this.table = document.createElement('table');
  	this.table.cellSpacing = '2';
  	this.table.cellPadding = '3';
  	this.obj.appendChild(this.table);
  	var fwd = this.getFirstWeekDay();
		var row = new Array();
 		var rl = this.table.rows.length;
  	row[rl] = this.table.insertRow(rl);
  	if(fwd != 0)
  	{
  		var to = this.getDaysIn3Month('-');
  		for(var i = to - fwd + 2; i <= to; i++)
  		{
  			var cell = row[rl].insertCell(row[rl].cells.length);
  			cell.className = 'old';
  			cell.innerHTML =  i;
  			cell.style.width = cell.style.height = '35px';
  		}
  	}
  	var days = this.getDaysIn3Month();
  	for(var i = 1; i <= days; i++)
 		{
 			if(row[rl].cells.length == 7)
 			{
 				row[++rl] = this.table.insertRow(rl);
 			}
 			var cell = row[rl].insertCell(row[rl].cells.length);
 			for(j = 0; j < this.marks.length; j++)
 			{
 				if(i == this.marks[j])
 				{
 				  cell.className = 'ev';
 				  cell.setAttribute('v', i);
 				  var _this = this;
 					cell.onclick = function() {_this.setValue(this.getAttribute('v'));};
 				}
 			}
 			cell.innerHTML = i;
 			cell.style.width = cell.style.height = '35px';	
 		}
 		var l = row[rl].cells.length;
  	if(l != 7)
  	{
  		for(var i = 1; row[rl].cells.length < 7; i++)
  		{
  			var cell = row[rl].insertCell(l++);
  			cell.className = 'old';
  			cell.innerHTML =  i;
  			cell.style.width = cell.style.height = '35px';
  		}
  	}
	},
	getDaysIn3Month:function(month)
	{
    var nMonth = month == '-' ? (this.month == 0 ? 11 : this.month - 1) : (month == '+' ? (this.month == 11 ? 0 : this.month + 1) : this.month);
    var nYear = month == '-' ? (nMonth > this.month ? this.year - 1 : this.year) : (month == '+' ? (nMonth < this.month ? this.year + 1 : this.year) : this.year);
    return (calendar.getDaysInMonth(nYear, nMonth));
	},
  getFirstWeekDay:function()
	{
		return this.getDay(new Date(this.year, this.month, 1).getDay());
	},
	getDay:function(day)
	{
		return day == 0 ? 7 : day;
	},
	setYear:function()
	{
		if(parseInt(this.year_selector.input.value).toString() != this.year_selector.input.value || parseInt(this.year_selector.input.value) < 1970)
		{
			this.year_selector.input.value = this.date.getFullYear();
		}
		this.date.setFullYear(this.year_selector.input.value);
	},
	setMonth:function()
	{
		this.date.setMonth(this.month_selector.value);
	},
	setTime:function()
	{
		this.date.setHours(this.hour_selector.input.value, this.minute_selector.input.value, this.second_selector.input.value);
	},
	setValue:function(v)
	{
		this.date.setMonth(this.month, v);
		this.day = this.date.getDate();
    document.location = http_server + 'events/?date=' + this.fillZero(this.day, 2) + '-' + this.fillZero(this.month + 1, 2) + '-' + this.year;
	},
	fillZero:function(fillzero, l)
	{
		while(fillzero.toString().length < l)
		{
			fillzero = '0' + fillzero.toString();
		}
		return fillzero;
	}
}
calendar.getDaysInMonth = function(year, month)
{
  switch(month)
  {
  	case 1: return year % 4 == 0 ? 29 : 28;
  	case 3:
  	case 5:
  	case 8:
  	case 10: return 30;
  	default: return 31;
  }
}



