(function ($) {
	var simplePopIn = function(settings) {
		var inst = this;
		
		this.overlay = [];
		this.container = [];
		this.containerWrap = [];
		this.closeBtn = [];
		
		// Default-Settings
		this.settings = {
			"popin_container_id" : 'simplepopin_container',
			"popin_container_class" : 'simplepopin_container',
			"popin_overlay_class" : 'simplepopin_overlay',
			"popin_wrap_class" : 'simplepopin_wrap',
			"popin_close_btn_class" : 'simplepopin_close_btn',
			"popin_overlay_opacity" : 0.5
		};
		
		var init = function(settings) {
			$.extend(inst.settings, settings);
			
			if ( $('#'+inst.settings.popin_container_id).length ) {
				inst.container = $('#'+inst.settings.popin_container_id);
				inst.containerWrap = inst.container.children('.' + inst.settings.popin_wrap_class);
				inst.closeBtn = inst.container.children('.' + inst.settings.popin_close_btn_class);
				inst.overlay = inst.container.prev();
			} else {
				inst.container = $('<div id="' + inst.settings.popin_container_id + '" class="' + inst.settings.popin_container_class + '"/>');
				inst.containerWrap = $('<div class="' + inst.settings.popin_wrap_class + '"/>');
				inst.closeBtn = $('<div class="' + inst.settings.popin_close_btn_class + '">X</div>');
				inst.container.append(inst.closeBtn).append(inst.containerWrap);
				inst.overlay = $('<div class="' + inst.settings.popin_overlay_class + '"/>');
				inst.overlay.add(inst.container).hide();
				$('body').append(inst.overlay);
				$('body').append(inst.container);
			}
			
			inst.closeBtn.click(inst.close);
		};
		
		this.request = function(url, data, type, callback) {
			var data = data || [];
			if( $.isArray(data) ) {
				data.push({name:'popin',value:'1'});
			}
			var _open = function() {
				inst.overlay.show().css('opacity', 0).fadeTo('fast', inst.settings.popin_overlay_opacity).click(function() {
					inst.overlay.fadeOut('fast');
					inst.container.fadeOut('fast');
				});
				inst.containerWrap.html('');
				
				if( url.indexOf(' ') > 0 ) {
					$.ajax({
			            "url": myty.basePath + url.substr(0,url.indexOf(' ')),
			            "data" : data,
						"dataType" : 'html',
						"type" : type,
						"success": function(html){
							console.log(html);
			            	var stuff = $(url.substr((url.indexOf(' ')+1)), html);
			            	inst.containerWrap.empty();
			            	inst.containerWrap.append(stuff);
			            	var result = {"result":true};
							
			            	inst.processResult.call(this, result);
							if ($.isFunction(callback)) {
								callback.call(this, result);
							}
		                }
			        });
				} else {
					$.ajax({
						"url" : myty.basePath + url,
						"data" : data,
						"dataType" : 'json',
						"type" : type,
						"success" : function(result) {
							inst.processResult.call(this, result);
							if ($.isFunction(callback)) {
								callback.call(this, result);
							}
						}
					});
				}
			}

			if (inst.container.is(':visible')) {
				inst.close(_open);
			} else {
				_open();
			}
		};
		
		this.open = function(url, data, callback) {
			return this.request(url, data, 'GET', callback);
		};
		
		this.post = function(url, data, callback) {
			return this.request(url, data, 'POST', callback);
		};
		
		this.message = function(message, callback) {
			var _open = function() {
				inst.overlay.show().css('opacity', 0).fadeTo('fast', inst.settings.popin_overlay_opacity).click(function() {
					inst.overlay.fadeOut('fast');
					inst.container.fadeOut('fast');
				});
				inst.containerWrap.html('');
				
				$(this).css('padding','20px');
				var result = {"result":true, "data" : { "html" : message }};
				inst.processResult.call(null, result);
				if ($.isFunction(callback)) {
					callback.call(this, result);
				}
			}

			if (inst.container.is(':visible')) {
				inst.close(_open);
			} else {
				_open();
			}
		};
		
		this.processResult = function(res) {
			if ( res.result === true ) {
				if( res.data ) {
					inst.containerWrap.html(res.data.html);
				}
				inst.container.fadeIn('slow');
				inst.center();
				inst.bindEvents();
				
				if( res.data && res.data.complete && $.isFunction(inst[res.data.complete]) ) {
					inst[res.data.complete].call(inst, res);
				}
			} else {
				alert('Es ist ein Fehler aufgetreten. Diese Einstellung kann zur Zeit nicht bearbeitet werden.');
			}
		};
		
		this.center = function() {
			inst.container.css('marginLeft', -(Math.floor(inst.container.width() / 2))+'px');
			inst.container.css('marginTop', -(Math.floor(inst.container.height() / 2))+'px');
		};
		
		this.bindEvents = function() {
			inst.containerWrap.find('[rel]').each(function(idx) {
				var rel = $(this).attr('rel');
				if (rel.indexOf('action:') == 0) {
					rel = rel.split(':');
					if (typeof inst[ rel[1] ] != "undefined" && $.isFunction( inst[ rel[1] ] )) {
						$(this).click(inst[ rel[1] ]);
					}
				}
			});
			inst.containerWrap.find(':reset').click(inst.close);
		};
		
		this.close = function(callback) {
			var callback = ($.isFunction(callback) ? callback : function(){});
			inst.container.fadeOut('fast',function() {
				inst.containerWrap.html('');
			});
			inst.overlay.fadeOut('fast', callback);
		};
		
		init(settings);
	}
	
	$.extend({"simplePopIn" : simplePopIn});
})(jQuery);
