var H= {};

H.Time = {};
H.Time.int = {};

H.Input = {};

H.Link = {};

H.Time.int.mysql2human = function( param ){
	if ( param == '0000-00-00 00:00:00') return '';
	
	param = param.split(' ');
	var date = param[0].split('-');
	
	return ''+date[2]+'.'+date[1]+'.'+date[0]+' в '+param[1];
}

H.Time.ReplaceTime = function(){
	$('.H_mysqltime').each( function(){ $(this).text( H.Time.int.mysql2human( $(this).text() ) ) } );
}

H.Link.Replace = function(){
	$('.StdLink, .StdTag').each( function (){
		var t = $(this);
		t.replaceWith( $('<a href="'+t.attr('rel')+'">'+t.html()+'</a>') );
	}) ;
	
}

H.Input.SelEvents = function(){
	$('.form_input').
	focus(
			function(){ $(this).addClass('form_input_selected'); }
			).
	blur( 
			function() {$(this).removeClass('form_input_selected'); } 
			);
}

$.fn.insertAtCaret = function (myValue) {
    return this.each(function(){
            //IE support
            if (document.selection) {
                    this.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                    this.focus();
            }
            //MOZILLA/NETSCAPE support
            else if (this.selectionStart || this.selectionStart == '0') {
                    var startPos = this.selectionStart;
                    var endPos = this.selectionEnd;
                    var scrollTop = this.scrollTop;
                    this.value = this.value.substring(0, startPos)
                                  + myValue
                          + this.value.substring(endPos,
this.value.length);
                    this.focus();
                    this.selectionStart = startPos + myValue.length;
                    this.selectionEnd = startPos + myValue.length;
                    this.scrollTop = scrollTop;
            } else {
                    this.value += myValue;
                    this.focus();
            }
    });

};

$.fn.getAtCaret = function () {
	var o = this[0];
    //IE support
    if (document.selection) {
            o.focus();
            sel = document.selection.createRange();
            return o.text;
    }
    //MOZILLA/NETSCAPE support
    else if (o.selectionStart || o.selectionStart == '0') {
            var startPos = o.selectionStart;
            var endPos = o.selectionEnd;
            var scrollTop = o.scrollTop;
            return o.value.substring(startPos, endPos);
    } else {
    		return '';
    }
};




$(function(){
	H.Time.ReplaceTime();
	H.Link.Replace();
	H.Input.SelEvents();
});

