(function($) {

	var _zIndex = 10000;
	var _mouse = {ox:0,oy:0,mx:0,my:0,mxs:0,mys:0};
	
	var jWino = window.jWino = function(args) {

		this._id = 'jwino';
		
		this._options = {
			width:300,
			height:'auto',
				
			top: 100,
			left: 100,
			
			title:'',
			content:'',
			contentId:'',
			
			close:true,
			move:false
		}		
		
		this._elm = {
			winObj: null,
			titleObj: null,
			contentObj: null,
			frameObj: null			
		};
					
		return (this instanceof jWino ? 
			this.init.apply( this, args.callee ? args : arguments ) : new jWino( arguments ));
	}
	
	jWino.prototype = {
		init: function(o) {
			this._options = $.extend({},this._options,o);
			this._id += '_' + (new Date().getTime());
			this.createWinObj();
			this.updateContent();
			
			this.resize(this._options.width,this._options.height);
			return this;
		},
		
		createWinObj: function()
		{
			var win = $('	<div id="'+this._id+'" class="wino2">'+
					'		<iframe class="wframe" frameborder="0" src="about:blank"></iframe>'+
					'		'+
					'		<div class="wh">' +
					'			<div class="wtcl"><!-- - --></div>' +
					'			<div class="wtc acolor"></div>' +
					'			<div class="wtcr"><!-- - --></div>' +
					'		</div>' +
					'		<div class="wb clearfix">' +
					'			<div class="wbc clearfix"></div>' +
					'			<div class="wbl"><!-- - --></div><div class="wbr"><!-- - --></div>' +
					'		</div>' +
					'		<div class="wf">' +
					'			<div class="wfcl"><!-- - --></div><div class="wfc"><!-- - --></div><div class="wfcr"><!-- - --></div>' +
					'		</div><div class="wmove"></div>' +
					'		<div class="wcls" title="Schliessen"><!-- - --></div>' +
					'		<div class="wload" title="Daten laden"><!-- - --></div>' +
					'		<div class="wresize" title="Gr&ouml;sse &auml;ndern"><!-- - --></div>' +
					'	</div>')
				.hide()
				.appendTo(document.body);

			this._elm.winObj = win;
			this._elm.titleObj = $('.wtc',win);
			this._elm.contentObj = $('.wbc',win);
			this._elm.frameObj = $('.wframe',win);
			//cf = $('.wb',win);
			
			win.bind('mousedown',this,this.down);

			if(this._options.close)	$('.wcls',win).bind('mousedown',this,this.close).show();
			if(this._options.move)	$('.wmove',win).bind('mouseup',this,this.drop).show();
			if(this._options.resize)$('.wresize',p).show();
			if(jQuery.browser.msie)	this._elm.frameObj.show();
		},
		
		updateContent: function(text)
		{
			this._elm.titleObj.html(this._options.title);
			
			if(typeof text == 'string') 
				this._elm.contentObj.html(text);
			else if(this._options.content != '') 
				this._elm.contentObj.html(this._options.content);
			else if(this._options.contentId != '')		
				this._elm.contentObj.html($('#'+this._options.contentId).html());
			else if(this._options.contentUrl != '')	{
				var url = this._options.contentUrl;
				var box = this;
				$.ajax({
					type: 'POST', url: url,
					success: function(msg)	{ 
						box._elm.contentObj.html(msg);		
						if($.browser.msie)
							box.resize(box._elm.winObj.width(),box._elm.winObj.height());
					}			
				});
			}				

			return this;
		},
		
		open: function(evt)
		{
			this.setFocus();
						
			this._elm.winObj.moveTo(this._options.left,this._options.top);
			this._elm.winObj.show();
			
			
			this.checkPosition();
			
			return this;
		},
		
		close: function(evt)
		{
			evt.data._elm.winObj.hide();
		},
		
		down: function(evt)
		{
			evt.data.setFocus();
			e = evt.originalEvent;
			
			var elm = evt.target || evt.srcElement;
			var cssClass = elm.className;
			
			switch(cssClass) {
				case 'wmove':
					$.selectionOff();
					_mouse.ox = e.layerX|e.offsetX;
					_mouse.oy = e.layerY|e.offsetY;
					$(document).bind('mousemove',evt.data,evt.data.move);
				break;
			}
		},
		
		drop: function(evt) {
			$(document).unbind('mousemove',evt.data.move).unbind('mousemove',evt.data.r_move);
			$.selectionOn();
		},
		
		move: function(evt) {		
			_mouse.mx = evt.clientX;
			_mouse.my = evt.clientY;
		
			var dx = _mouse.mx - _mouse.ox;
			var dy = _mouse.my - _mouse.oy;
		
			evt.data._elm.winObj.moveTo(dx,dy);
		},

		r_move: function(evt) {
			//nh = winSize.height + (evt.clientY - mouse.mys);		
			//nw = winSize.width + (evt.clientX - mouse.mxs);
			//resize(nw,nh);
		},
		
		setFocus: function()
		{
			this._elm.winObj.css('zIndex',++_zIndex);
		},
		
		resize: function(w,h)
		{
			this._elm.winObj.width(w);

			if(h !== 'auto')
			{
				this._elm.contentObj.height(h);
				this._elm.winObj.height(h);
			}
			else 
			{
				this._elm.winObj.height(h);
			}			
			
			if(jQuery.browser.msie) 
			{
				this._elm.frameObj.height(h);
				this._elm.frameObj.width(w);
			}
		},
		
		position: function(top,left,bottom,right)
		{
			if(top)
				this._elm.winObj.moveTo(null,top);
			if(left)
				this._elm.winObj.moveTo(left,null);
			if(bottom)
				this._elm.winObj.moveTo(null, (bottom - this._elm.winObj.height()));
			if(right)
				this._elm.winObj.moveTo((right - this._elm.winObj.width()), null);
		},
		
		checkPosition: function() 
		{			
			var elm = this._elm.winObj.get(0);
			
			var dw = document.body.clientWidth || document.documentElement.clientWidth;
			var dh = document.body.clientHeight || document.documentElement.clientHeight;
			
			var w = elm.offsetWidth;
			var h = elm.offsetHeight;
			
			var x = elm.offsetLeft;
			var y = elm.offsetTop;
			
			var dx = ((x+w)>dw) ? dw-w:x;
			var dy = ((y+h)>dh) ? dh-h:y;

			this._elm.winObj.moveTo(dx,dy);
			
			return this;
		},
		
		center: function()
		{
			var elm = this._elm.winObj.get(0);
			
			var h = jQuery.getWinH();
			var w = jQuery.getWinW();
			
			var ew = elm.offsetWidth;
			var eh = elm.offsetHeight;
			
			if(h > eh) {
				this._elm.winObj.moveTo((w - ew)/2,(((h - eh)/2)+document.documentElement.scrollTop));
			}
			else
				this._elm.winObj.moveTo((w - ew)/2,(10+document.documentElement.scrollTop));
			
			return this;
		}
	}
	
	$.fn.extend({
		moveTo: function(x,y) {
			this.each(function() {
				if(typeof x == 'number')	this.style.left = x+'px';
				if(typeof y == 'number')	this.style.top = y+'px';
			});
		}
	});

}
)(jQuery);
