// $(obj).center([parentObject],[z-index]);
jQuery.fn.center = function(){
	var parent = $(window);
	var zindex = 9999;
	for (i=0;i<arguments.length;i++) {
		if (typeof arguments[i] == 'object') {parent = arguments[i];}
		if (typeof arguments[i] == 'number') {zindex = arguments[i];}
	}
	var win = {'h': $(parent).height(),'w': $(parent).width()}
	var obj = {'h': $(this).height(),'w': $(this).width()}
	var offset = {};
	offset.h = (win.h - obj.h)/2 + $(window).scrollTop();
	offset.w = (win.w - obj.w)/2 + $(window).scrollLeft();
	$(this).css({
		'display':'block',
		'position':'absolute',
		'left':offset.w,
		'top':offset.h,
		'z-index':999
	});
}

// $(obj).showBlock();
jQuery.fn.showBlock = function(){
	
	var id = 'ITTIjUtilsBlock';
	var opacity = 0.5;
	var zindex = 1;
	var opacityStr = 'filter:alpha(opacity='+Number(opacity*100)+');-moz-opacity:'+opacity+';opacity:'+opacity+';';   
	if(document.getElementById(id)) return;
	
	doc = {'w': $(document).width(),'h': $(document).height()};
	blockDiv = document.createElement("div");
	$(blockDiv).attr({'id' :id,'style':opacityStr});
	$(blockDiv).css({
		'position':'absolute',
		'top':0,
		'left':0,
		'width':doc.w,
		'height':doc.h,
		'z-index':zindex,
		'background-color':'#000',
		'display':'none'
	});
	document.body.appendChild(blockDiv);
	$(blockDiv).fadeIn();
}

jQuery.fn.hideBlock = function() {
	var bl = document.getElementById('ITTIjUtilsBlock');
	if (!bl) return;
	var pr = bl.parentNode;
	pr.removeChild(bl);
	
}

jQuery.fn.showAsWindow = function() {
	var that = this;
	that.old = null;
	var move = arguments[0];
	var close = arguments[1];
	// move
	$(document).mousemove(function(e){
		if (!that.isMouseDown) return;
		var offset = $(that).offset();
		if (that.old) {
			offset.left = offset.left + (e.pageX - that.old.x);
			offset.top = offset.top + (e.pageY - that.old.y);			
			that.css({'top':offset.top+'px', 'left':offset.left+'px'});
		}		
		that.old = {x:e.pageX, y:e.pageY};		
	});	
	$(move).mousedown(function(){
		that.isMouseDown = 1;
	});
	$(document).mouseup(function(){
		that.isMouseDown = 0;
		that.old = null;
	});
	
	// close 
	$(close).click(function(){
		$(this).hideWindow(that);
		$(document).hideBlock();
	});
	
	//init 
	$(document).showBlock();					
	$(this).center();
}

jQuery.fn.hideWindow = function(obj) {
	$(obj).hide();
}

