if (typeof win == 'undefined') {
	var win = null;
}

$.fn.systemMessages = function(config) {
    config = config || {};
    var defaults = {
            
    };   
        
    config = $.extend(defaults, config);
        
    var $container = $(this);
    var mask = null;
    var request = null;
        
    var countNewMessages = function(callback) { // Prüft nach neuen System Nachrichten
    	if (request) {
			request.abort();
		}
		
		request = $.ajax({
			url: config.count_url,
			cache: false,
			type: 'post',
			dataType: 'json',
			success: function(json_data) {
		    	if (callback) {
		    	    callback(json_data);
		    	} else {
		    	    counters = json_data;
    	    	}
		    	
		    	window.setTimeout(function() {
		    		countNewMessages();
				}, 10000);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				if (win) {
		    		 win.close();
		    	 }
		    	 
				counters = {user_messages: 0, messages: 0};
			}
		});
    };
    
    var opened = false;
    var showSystemMessage = function(data) {
		if (data != false && !opened) {
		    if (win) {
		    	return false;
		    }
		    
		    var message = new Ext.Panel({
				autoScroll: true,
				region: 'center',
		        split: false,
		        id: 'system-message-block',
		        collapsible: false,
		        margins:'7 5 7 5',
		        cmargins:'7 5 7 5',
		        bodyStyle: {
				'-moz-border-radius': '5px',
			    	'-webkit-border-radius': '5px',
			    	'border-radius': '5px',
			    	padding: '8px 10px'
		        },
	        	listeners: {
		        	afterrender: function() {
			        	mask = new Ext.LoadMask(Ext.get('system-message-block'), {msg:"Nachricht wird geladen..."})
		        	}
		        }
		    });

		    if (data.smail_type == 'newsletter') {
				var width = 900;
				var height = 500;
				var title = 'Newsletter: '+data.smail_subject;
		    } else {
				var width = 500;
				var height = 180;
				var title = 'System Nachricht';
		    }
		    
		    var mailing_list_uid = null;
	    
		    win = new Ext.Window({
				title: title,
				closable:true,
				width: width,
				height: height,
				id: 'system-message-window',
				plain: false,
				maximizable: true,
				layout: 'border',
				items: [message],
				buttons: [
				    {
						text: 'schlie&szlig;en',
						handler: function() {
							win.close();
				     	}
				    }
				],
				listeners: {
				    beforeshow: function() {
				    	
		    		},
		    		show: function() {
		    			mask.show();
		    			$.post(config.get_message_url, {}, function(data) {
		    				mailing_list_uid = data.mailing_list_uid;
				    		message.update(data.message);
				    		mask.hide();
			        	}, 'json');
		    	    },
		    	    close: function() {
			    		win.destroy();
			    		win = null;
			    		opened = false;
			    		
			    		$.post(config.mark_as_opened_url, {mailing_list_uid: mailing_list_uid}, function(data) {
				    		mailing_list_uid = null;
				    		countNewMessages(showSystemMessage);
			    		}, 'json');
		    	    }
		    	}
		    });	
		    
		    win.show(this);
		    opened = true;
		} 
    };
    
    countNewMessages(showSystemMessage);
}; 
