function limitChars(id, limit, result, err) {
	text = $('#' + id).val(); 
	$('#' + result).show();
	var length = text.length;
	
	if(length >= limit) {
		$('#' + result).html(err + ' ' + limit + '.');
		$('#' + id).val(text.substr(0, limit));
		return false;
	}
	
	$('#' + result).html(limit - length);
 	return true;
}

function fix_png() {
	for (var i = 0; i < document.images.length; i++) {
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if (imgName.substr(imgName.length-3, 3) == "PNG") {
			var imgID = (img.id)? "id='" + img.id + "' ": "";
			var imgClass = (img.className)? "class='" + img.className + "' ": "";
			var imgTitle = (img.title)? "title='" + img.title + "' ": "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block; position: relative; float: left;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			
			var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
			img.outerHTML = strNewHTML;
			i = i-1;
		}
	}
}

function clickIE4() {
	if (event.button == 2) {
		return false;
	}
}
function clickNS4(e) {
	if (document.layers||document.getElementById&&!document.all) {
		if (e.which==2||e.which==3) {
			return false;
		}
	}
}

if (document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = clickNS4;
} else if (document.all && !document.getElementById) {
	document.onmousedown = clickIE4;
}
document.oncontextmenu = new Function("return false");

function openwindow(theURL, winName, features) {
	window.open(theURL, winName, features);
	return false;
}

$(function() {
	$('.users a img').mouseover(function() {
		var li = $(this).parents('li:first');
		var position = li.position();
		var left = new Number(position.left);
		left += 98;
		var top = new Number(position.top);
		var position = $(li).parents('ul:first').position();
		if ($(li).parents('ul:first').attr('id') == 'fav') {
			left += 20;
			position = $(li).parents('form:first').position();
		}
		top += 5 + position.top;
		
		$('.tooltip').css('top', top + 'px');
		$('.tooltip').css('left', left + 'px');
		
		var a = $(this).parents('a:first');
		var id = a.attr('href').replace($('#tooltip_url0').val(), '');
		id = id.replace('/', '');
		id = id.replace(';search:1;', '');
		$('#tooltip_id').val(id);
		
		$('.tooltip').show();
	}).mouseout(function() {
		$('.tooltip').hide();
	});
	
	$('.ttip').mouseover(function() {
		var el = $(this).parents('table:last');
		var position = el.position();
		var top = new Number(position.top) + new Number(el.css('padding-top').replace('px', '')) + new Number(el.css('margin-top').replace('px', ''));
		var left = position.left;
		
		if (el.attr('id') == 'topic') {
			el = $(this).parents('td:last');
			position = el. position();
			top += position.top;
			position = $(this).position();
			top += new Number(position.top) + 2;
			left += new Number(position.left) + new Number($(this).width()) - 4;
		} else {
			el = $(this).parents('td:first');
			position = el. position();
			top += position.top;
			position = $(this).position();
			left += new Number(position.left) + new Number($(this).width()) - 7;
		}
		
		$('.tooltip').css('top', top + 'px');
		$('.tooltip').css('left', left + 'px');
		
		var a = $(this).parents('a:first');
		var id = a.attr('href').replace($('#tooltip_url0').val(), '');
		id = id.replace('/', '');
		id = id.replace(';search:1;', '');
		$('#tooltip_id').val(id);
		
		$('.tooltip').show();
	}).mouseout(function() {
		$('.tooltip').hide();
	});
	
	$('.tooltip').mouseover(function() {
		$('.tooltip').show();
	}).mouseout(function() {
		$('.tooltip').hide();
	});
	
	$('textarea.auto').autogrow();
	
	$('#comm').focus(function() {
		if ($(this).val() == $('#def').val()) {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val($('#def').val());
		}
	});
	
	$('#select_lang #flang a').click(function() {
		$('.lang_h').toggle();
	});
	
	$('ul#nav li').mouseover(function() {
		$(this).addClass('current2');
		var id = $(this).attr('id');
		id = id.replace('tn', '');
		
		var position = $(this).position();
		$('#dd' + id).css('top', $(this).height());
		$('#dd' + id).css('left', position.left);
		$('#dd' + id).show();
	}).mouseout(function() {
		var id = $(this).attr('id');
		id = id.replace('tn', '');
		
		$('#dd' + id).hide();
		$(this).removeClass('current2');
	});
	
	$('.dd').mouseover(function() {
		var id = $(this).attr('id');
		id = id.replace('dd', '');
		$('#tn' + id).addClass('current2');
		$(this).show();
	}).mouseout(function() {
		var id = $(this).attr('id');
		id = id.replace('dd', '');
		$('#tn' + id).removeClass('current2');
		$(this).hide();
	});
});

function tooltipAction(type) {
	var id = $('#tooltip_id').val();
	if (type == 1) {
		document.location.href = $('#tooltip_url1').val() + id;
	} else if (type == 2) {
		openwindow('/ajax/popup.php?id=' + id, 'm' + id, 'width=920,height=600,scrollbars=yes');
	} else if (type == 3) {
		document.location.href = $('#tooltip_url3').val() + id;
	}
	
	return false;
}

function changeStatus(status) {
	$.get('/ajax/status.php?status=' + status);
	$('#status_selection li:first').attr('id', 'status_first');
	$('#status_selection li:last').attr('id', 'status_last');
	if (status == '0') {
		status = 'online';
		$('#status_selection li:first').attr('id', 'status_first_sel');
	} else {
		status = 'offline';
		$('#status_selection li:last').attr('id', 'status_last_sel');
	}
	$('#select_status img').attr('src', '/img/status_' + status + '.png');
}

function rememberMe() {
	$.get('/ajax/remember.php');
	$('#remember_me').hide();
	return false;
}

(function($) {
	$.fn.autogrow = function(options) {    
		this.filter('textarea').each(function() {
			var $this = $(this),
				minHeight = $this.height(),
				lineHeight = $this.css('lineHeight');
			var shadow = $('<div></div>').css({
				position: 'absolute',
				top: -10000,
				left: -10000,
				width: $(this).width() - parseInt($this.css('paddingLeft')) - parseInt($this.css('paddingRight')),
				fontSize: $this.css('fontSize'),
				fontFamily: $this.css('fontFamily'),
				lineHeight: $this.css('lineHeight'),
				resize: 'none'
			}).appendTo(document.body);
			var update = function() {
				var times = function(string, number) {
					var _res = '';
					for(var i = 0; i < number; i ++) {
						_res = _res + string;
					}
					return _res;
				};
                
				var val = this.value.replace(/</g, '&lt;')
                                    .replace(/>/g, '&gt;')
                                    .replace(/&/g, '&amp;')
                                    .replace(/\n$/, '<br/>&nbsp;')
                                    .replace(/\n/g, '<br/>')
                                    .replace(/ {2,}/g, function(space) { return times('&nbsp;', space.length -1) + ' ' });
                shadow.html(val);
                $(this).css('height', Math.max(shadow.height() + 20, minHeight));
            }
            $(this).change(update).keyup(update).keydown(update);
            update.apply(this);
        });
        return this;
    }
})(jQuery);