/*
 * @author Erik Anders aka --=GT-O=--Hellfire
 */

// reference local blank image
Ext.BLANK_IMAGE_URL = 'images/spacer.gif';
 
// create namespace
Ext.namespace('gto');
 
// create application
gto.portal = function() {
    // do NOT access DOM from here; elements don't exist yet
 
    // private variables
		
    // private functions
 
    // public space
    return {
		// public properties, e.g. strings to translate		
		currentPage: 1,
		newsEditForm: true,
		
		// public methods
		init: function()
		{
			Ext.QuickTips.init();  // enable tooltips

			Shadowbox.init();
			
			this.loadPage(this.currentPage);
			
			this.spot =  new Ext.Spotlight
			(
				{
					easing: 'easeOut',
					duration: .3
				}
			);
		},
		
		loadPage: function(p)
		{
			if (p)
			{
				this.currentPage = p;
			}
			
			var n = Ext.get('news-ct');
			n.load
			(
				{
					url: 'content.php',
					params: 'load=ajax-portal&mode=loadpage&p='+this.currentPage,
					text: 'Loading...',
					callback: this.makeEditable
				}
			);
			n.show();
		},
		
		makeEditable: function()
		{
			Ext.select('a[id^="edit_n_"]').addListener('click', gto.portal.editNews, gto.portal);
			Ext.select('a[id="create_news"]').addListener('click', gto.portal.editNews, gto.portal);
			Ext.select('a[id^="del_n_"]').addListener('click', gto.portal.delNews, gto.portal);
			GTOdecorateItemLinks();
			
			var dh = Ext.DomHelper;
			var i = Ext.select('span.newsmessage img[alt="Bild"]');
			i.each(function(s,a,idx)
			{				
				var s = Ext.get(s);
				var img = 
				{
					tag: 'img',
					id: 'bigimg-'+idx,
					src: s.dom.src
				}					
				
				var div = 
				{
					tag: 'div',
					cls: 'image-ct',
					children:
					[
						img
					]
				}
				
				dh.insertBefore(s, dh.markup(div));
				s.remove();
				
				Ext.get('bigimg-'+idx).up('div').mask();
			});
			
			Ext.select('[id^="bigimg-"]').on('load', gto.portal.shrinkImages, gto.portal);
		},
		
		shrinkImages: function(e, t)
		{			
			var t = Ext.get(t);
			var d = t.up('div')
			d.unmask();			
			
			if (t.getWidth() > 500)
			{
				d.insertFirst(
				{
					tag: 'div',
					cls: 'shadowbox-ol',
					children:
					[
						{
							tag: 'a',
							href: t.dom.src,
							rel: 'shadowbox',
							html: 'Dieses Bild wurde verkleinert. Klicke hier, um es gr&ouml;&szlig;er darzustellen.'
						}
					]
				});
			
				t.setWidth(500);				
				Shadowbox.setup();
			}
		},
		
		editNews: function (e, t, o)
		{
			var id = t.id.substr(7);
			var c = 0;
			if (id == 'news')
			{
				c = 1;
			}
			var renderCT = (c == 0) ? 'n_'+id : 'create-news-ct';
			
			this.newsEditForm = new Ext.FormPanel({
				id: 'news-editor',
				formId: 'news-editor-form',
		        labelAlign: 'top',
		        frame:true,
		        title: (c == 0) ? Ext.get('t_'+id).dom.innerHTML : 'News erstellen',
		        bodyStyle: 'padding:5px 5px 0;',
		        width: 600,
		        items: [
				{
					xtype:'textfield',
					fieldLabel: 'Titel',
					id: 'msg_title',
					width: 200,
					value: (c == 0) ? Ext.get('t_'+id).dom.innerHTML : '',
					allowBlank: false,
					blankText: "Das Feld darf nicht leer sein"
				},
				{
		            xtype: 'textarea',
		            fieldLabel:'Nachrichtentext (BBCode erlaubt)',
					id: 'msg_text',
					value: (c == 0) ? Ext.get('nd_'+id).dom.innerHTML : '',
		            height:200,
		            anchor:'98%',
					allowBlank: false,
					blankText: "Das Feld darf nicht leer sein"
		        },
				{
		            xtype: 'hidden',
					id: 'msg_id',
					value: id
		        },
				{
		            xtype: 'hidden',
					id: 'poster_id',
					value: (c == 0) ? Ext.get('pi_'+id).dom.innerHTML : 0
		        },
				{
		            xtype: 'hidden',
					id: 'postdate',
					value: (c == 0) ? Ext.get('pd_'+id).dom.innerHTML : 0
		        }],

		        buttons: [{
		            text: 'Speichern',
					type: 'submit',
					scope: this,
					handler: function (btn, e)
					{
						this.newsEditForm.form.submit({
							url: 'content.php',
							params: 'load=ajax-portal&mode=savenews',
				            waitMsg: "Speichern...",
				            success:function(form, e) {
				                Ext.Msg.show({
				                    title:"Information",
				                    msg: "Speichern erfolgreich",
				                    buttons: Ext.Msg.OK,
				                    icon: Ext.MessageBox.INFO
				                });
								gto.portal.spot.hide();
								Ext.get('news-editor').remove();
								gto.portal.loadPage(gto.portal.currentPage); 
				            },
				            failure:function(form, e) {
				                Ext.Msg.show({
				                    title:"Information",
				                    msg: "Speichern fehlgeschlagen",
				                    buttons: Ext.Msg.OK,
				                    icon: Ext.MessageBox.ERROR
				                });
				            }
				        });
					}
		        },{
		            text: 'Abbrechen',
					handler: this.cancelNewsEdit
		        }]
		    });

		    this.newsEditForm.render(renderCT);
			gto.portal.spot.show('news-editor');
	
			return false;
		},
		
		delNews: function (e, t, o)
		{
			var id = t.id.substr(6);
			
			// Prompt for user data and process the result using a callback:
			Ext.Msg.show(
			{
				title: Ext.get('t_'+id).dom.innerHTML,
				msg: 'Willst du diesen Beitrag wirklich l&ouml;schen?',
				buttons: Ext.Msg.YESNO,
				scope: this,
				icon: Ext.MessageBox.QUESTION,
				fn: function(btn, text)
				{
				    if (btn == 'yes')
					{
						Ext.Ajax.request(
						{
							url: 'content.php',
							waitMsg: "Speichern...",
							success: this.loadPage,
							params: 'load=ajax-portal&mode=delnews&id='+id
						});
					}
				}
			});

			/*
			Ext.Msg.prompt('Beitrag l&ouml;schen', 'Willst du diesen Beitrag wirklich l&ouml;schen?', function(btn, text){
			    if (btn == 'ok'){
			        var id = t.id.substr(6);
			
					Ext.Ajax.request({
						url: 'content.php',
						waitMsg: "Speichern...",
						success: this.loadPage,
						params: 'load=ajax-portal&mode=delnews&id='+id
					});
			    }
			});
			*/
			return false;
		},
		
		cancelNewsEdit : function (btn, e)
		{
			gto.portal.spot.hide();
			Ext.get('news-editor').remove();
		}
	}
}(); // end of app
 
// end of file