/*
 * jQuery Margonem plugin
 * @requires jQuery v1.2 or above
 * 
 * Designed specially for Margonem MMORPG (www.margonem.pl)
 * Do not use anywhere else.
 *  
 */
 
(function($) {
	$.each({
		// centers any absolute positioned element
		absCenter: function() {
			var $$ = $(this);
			$$.css({left:Math.round(document.body.scrollLeft+$(window).width()/2-$$.width()/2),
				top:Math.round(document.body.scrollTop+$(window).height()/2-$$.height()/2)});
		},
		/* insert text at current cursor position in textarea
		 original: http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
		*/
		insertAtCaret: function(myValue){
			if (document.selection) {
        this.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
        this.focus();
			}
      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();
      }
    }
	}, function(name, fn){
		jQuery.fn[ name ] = function(){
			return this.each( fn, arguments );
		};
	});

})(jQuery);

/* mouse events */
$(document).mouseover(function(e){
	if($(e.target).attr('rollover')) $(e.target).css('backgroundPosition','0 -'+$(e.target).attr('rollover')+'px');
	if($(e.target).attr('tip')) {
		$('#oTip').html($(e.target).attr('tip')).stop(1,1).removeClass().css({left:0,top:0});
		if($(e.target).attr('ctip')) $('#oTip').addClass('ctip tip_'+$(e.target).attr('ctip'));
		else $('#oTip').addClass('ctip');
		var xy=$(e.target).offset();
		var th=$('#oTip').outerHeight(), tw=$('#oTip').outerWidth();
		xy.left-=Math.round(tw/2-$(e.target).outerWidth()/2);
		if(xy.top-th<0) xy.top+=$(e.target).outerHeight();
		else xy.top-=th;
		if(xy.left<0) xy.left=0;
		if(xy.left+tw>$('body').width())xy.left=$('body').width()-tw;
		$('#oTip').css(xy).fadeIn('fast');
	}
});
$(document).mouseout(function(e){
	if($(e.target).attr('rollover')) $(e.target).css('backgroundPosition','0 0');
	if($(e.target).attr('tip')) $('#oTip').fadeOut('fast');
});
//$(document).mousemove(function(e){
//});
function tippos(e){
	var dx=e.clientX+10, dy=e.clientY+10;
	if(e.clientX>600) dx=e.clientX-10-$('#oTip').width();
	if(e.clientY>400) dy=e.clientY-10-$('#oTip').height();	
	return {left:dx,top:dy};
}

function round(n,precise,sep)
{
	var sign=(n<0)?'-':'';
	n=Math.abs(parseFloat(n));
	if(precise==3) {
		n=n.toString();
		if(n.length<5) return sign+n;
		var newn='';
		if(!isset(sep)) var sep=" ";
		while(n.length>3) {
			var last3=n.substr(n.length-3,3);
			n=n.substring(0,n.length-3);
			newn=sep+last3+newn;
		}		
		return sign+n+newn;
	} else
	if(precise==2) {
		if(n<1000) return sign+n;
		if(n<1000000) return sign+Math.round(n/100)/10+"k";
		if(n<1000000000) return sign+Math.round(n/100000)/10+"m";
		if(n<10000000000) return sign+Math.round(n/100000000)/10+"g";
		return sign+Math.round(n/1000000000)+"g";
	} else
	if(precise==1) {
		if(n<10000) return sign+n;
		if(n<100000) return sign+Math.round(n/100)/10+"k";
		if(n<10000000) return sign+Math.round(n/1000)+"k";
		if(n<100000000) return sign+Math.round(n/100000)/10+"m";
		if(n<1000000000) return sign+Math.round(n/1000000)+"m";
		if(n<10000000000) return sign+Math.round(n/100000000)/10+"g";
		return sign+Math.round(n/10000000000)+"g";		
	} else
	{
		if(n<1000) return sign+n;
		if(n<1000000) return sign+Math.round(n/1000)+"k";
		if(n<1000000000) return sign+Math.round(n/1000000)+"m";
		return sign+Math.round(n/1000000000)+"g";
	}
}
