function CStatus(cfg){
	this.list = (cfg.list && cfg.list.length) ? cfg.list : [];
	
	this.id   = (cfg.id) ? parseInt(cfg.id) : 0;
	if (isNaN(this.id)) this.id = 0;
	
	this.time = (cfg.time) ? parseInt(cfg.time) : 0;
	if (isNaN(this.time)) this.time = 0;

	this.text = (cfg.text) ? String(cfg.text) : '';
	this.canSetText = Boolean(cfg.canSetText);

	if (!this.id && this.text != '' && this.canSetText){
		this.id = -1;
	}

	this.obj       = null;
	this.objID     = null;
	this.objFilled = 0;

	this.html = {
		trID   : null,
		trText : null,
		sel  : null,
		inp  : null,
		tblEdit : null,
		dText   : null,
		aText   : null,
		sTime   : null
	};

	this.switchToText = 0;

	this.uid = (cfg.uid) ? parseInt(cfg.uid) : 0;
	if (isNaN(this.uid)) this.uid = 0;
	
}

CSP = CStatus.prototype;
CSP.objectHTML = function(){
return  '<a name="_sf" />'+
		'<div id="dStatus" class="show"><a href="javascript:void(0)" id="aStatus">Установить статус</a>&nbsp;<span id="sStatusTime"></span></div>'+
		'<table cellspacing="0" class="hide" id="tblStatusEdit">'+
		'<tr class="show" id="trStatusSelect"><td class="show""><select id="selStatusID"></select></td>'+
        '<td class="show" width="16"><a href="javascript:void()" id="statusOkSelect"><img src="http://www.stranadruzey.ru/img/ok.gif" /></a></td><td width="16">&nbsp;</td></tr>'+
		'<tr class="hide" id="trStatusInput"><td class="hide"><input id="inpStatus" value="" /></td>'+
        '<td class="hide"><a href="javascript:void()" id="statusOkInput"><img src="http://www.stranadruzey.ru/img/ok.gif" /></a></td>'+
        '<td class="hide"><a href="javascript:void()" id="statusCancelInput"><img src="http://www.stranadruzey.ru/img/delete.gif" border="0" alt="x" width="16" height="16" /></a></td></tr>'+
		'</table>';
}

CSP.trClass = function(_tr, _class){
	_tr.className = _class;
	for(var i=0; i<_tr.childNodes.length; i++){
		if (_tr.childNodes[i].tagName == 'td' || _tr.childNodes[i].tagName == 'TD'){
			_tr.childNodes[i].className = _class;
		}
	}	
}

CSP.init = function(objID){
	var self = this;
	this.objID = objID;
	this.obj   = document.getElementById(objID);

	if (!this.obj){
		if (document.readyState){
			if (document.readyState == 'complete'){
				return;
			}
		}
		var self = this;
        setTimeout(function(){self.init(objID)}, 100);
		return;
	}
	else if (!this.html.sel){
		if (!this.objFilled){
			this.obj.innerHTML = this.objectHTML();
			this.objFilled = 1;
			var self = this;
        	setTimeout(function(){self.init(objID)}, 100);		
        	return;
		}
		else if (!this.html.sel){
			this.html.sel = document.getElementById('selStatusID');
			if (!this.html.sel){
				var self = this;
	        	setTimeout(function(){self.init(objID)}, 100);		
	        	return;
			}
		}
	
	
		var s = this.html.sel;
	    var o = document.createElement('option');
    	o.value = 0;
		o.appendChild(document.createTextNode('Не показывать'));
		s.appendChild(o);
		for (var i=0; i<this.list.length; i++){
			o = document.createElement('option');
			o.value = i+1;
			o.appendChild(document.createTextNode(this.list[i]));
			s.appendChild(o);
		}
		if (this.canSetText){
			o = document.createElement('option');
			o.value = -1;
			o.appendChild(document.createTextNode('другое...'));
			if (this.id == -1){
				o.selected = true;
			}
			s.appendChild(o);
		}		
		s.onchange = function(o){self.onChangeID(o);};
		s.onblur  = function(){var s = self; s.switchToText = setTimeout(function(){s.switcher('text');}, 1500);};
		s.onfocus = function(){if(self.switchToText){clearTimeout(self.switchToText); self.switchToText = 0};};
	}

	this.html.dText   = document.getElementById('dStatus');
	this.html.tblEdit = document.getElementById('tblStatusEdit');
	this.html.trID    = document.getElementById('trStatusSelect');
	this.html.trText  = document.getElementById('trStatusInput');
	
	this.html.inp         = document.getElementById('inpStatus');
	this.html.inp.value   = this.text;
	this.html.inp.onkeyup = function(e){self.onKeyUpInput(e);};
	this.html.inp.onblur  = function(){var s = self; s.switchToText = setTimeout(function(){s.switcher('text');}, 1500);};
	this.html.inp.onfocus = function(){if(self.switchToText){clearTimeout(self.switchToText); self.switchToText = 0};};

	this.html.sTime = document.getElementById('sStatusTime');
	this.html.aText = document.getElementById('aStatus');
	this.html.aText.onclick = function(){self.switcher('edit');};
	this.outText();
	
	this.switcher((this.id == -1) ? 'input' : 'select');
	this.switcher('text');
	

	var a = document.getElementById('statusOkSelect');
	if (a){
		a.onclick = function(){self.submit();};
	}

	a = document.getElementById('statusOkInput');
	if (a){
		a.onclick = function(){self.submit();};
	}

	a = document.getElementById('statusCancelInput');
	if (a){
		a.onclick = function(){self.textCancel();};
	}
}

CSP.outTime = function(){
	if (!this.id){
		this.html.sTime.innerHtml = '';
		return;
	}

	var nowD = new Date();
	var edtD = new Date();
	edtD.setTime(this.time*1000);
	var h = edtD.getHours(); if (h < 10) h = '0'+h;
	var m = edtD.getMinutes(); if (m < 10) m = '0'+m;
	var out = h+':'+m;
	

	if (nowD.getDate() != edtD.getDate() || nowD.getMonth() != edtD.getMonth()){
		var d = edtD.getDate();
		if (d < 10) d += '0'+d;
		
		m = edtD.getMonth(); if (m < 10) m = '0'+m;
		out += ' '+d+'.'+m;
	}

	if (nowD.getFullYear() != edtD.getFullYear()){
		out += '.'+edtD.getFullYear();
	}

	this.html.sTime.innerHTML = out+' ';
}

CSP.outText = function (){
	if (!this.id){
		this.html.aText.innerHTML = 'Установить статус';
	}
	else if (this.id == -1){
		this.html.aText.innerHTML = this.text;
	}
	else{
		this.html.aText.innerHTML = this.list[this.id-1];
	}

	this.outTime();
}

CSP.getID = function(){
	this.id = 0;
	var s  = this.html.sel;
	for (var i=0; i<s.options.length; i++){
		if (s.options[i].selected){
			return this.id  = parseInt(s.options[i].value);
		}
	}
	return this.id;
}

CSP.onChangeID = function(o){
	if (-1 == this.getID()){
		this.switcher('input');
		this.html.inp.focus();
	}
}

CSP.onKeyUpInput = function(e){
	if (!e && event) e = event;
    		
	var which = (e.which) ? e.which : e.keyCode;
	if (which == 27){
		this.textCancel();
	}
	else if (which == 13){
		this.submit();
	}
}

CSP.textCancel = function(){
	this.id   = 0;
	this.text = this.html.inp.value = '';
	this.switcher('select');
	var self = this;
	setTimeout(function(){self.html.sel.focus()}, 100);
}

CSP.selectID = function(){
	for (var i=0; i<this.html.sel.options.length; i++){
		this.html.sel.options[i].selected = (this.html.sel.options[i].value == this.id);
	}
}

CSP.switcher = function(type){
	if (type == 'select'){
		this.trClass(this.html.trID, 'show');
		this.trClass(this.html.trText, 'hide');
		this.selectID();
	}
	else if (type == 'input'){
		this.trClass(this.html.trID, 'hide');
		this.trClass(this.html.trText, 'show');
	}
	else if (type == 'text'){
		this.html.dText.className = 'show';
		this.html.tblEdit.className = 'hide';
		if (this.switchToText){
			clearTimeout(this.switchToText);
			this.switchToText = 0;
		}		
	}
	else if (type == 'edit'){
		this.html.dText.className = 'hide';
		this.html.tblEdit.className = 'show';
		var self = this;
		if (this.id == -1){
			setTimeout(function(){self.html.inp.focus();}, 100);
		}		
		else{
			setTimeout(function(){self.html.sel.focus();}, 100);
		}
		if (this.switchToText){
			clearTimeout(this.switchToText);
			this.switchToText = 0;
		}
	}
}           

CSP.submit = function(){
	document.domain = 'stranadruzey.ru';
	this.onSaveBefore();
	var send = {};
	var url = 'http://www'+this.getCookie('id')+'.stranadruzey.ru/cgi-bin/sd.fcgi';
	if (-1 == this.getID()){
		send['status_id'  ] = 0;
		this.text = this.html.inp.value;
        send['status_text'] = this.text;
		//url += '?status_text='+escape(this.text);
		//send['status_text'] = encodeURIComponent(send['status_text']);
	}
	else{
		send['status_id'] = this.id;
	}
	var d = new Date();
	this.time = d.getTime()/1000;

	this.switcher('text');
	this.outText();

	send['user'] = 'status-set-ajax';
	var self = this;
	$.get(url, send, function(){self.onSaveComplete(statusResponse);}, 'script');	
}

CSP.getCookie = function(name){
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

CSP.editTime

CSP.onSaveBefore = function(){
}

CSP.onSaveComplete = function(data){
}



