
var Pinboard = function(){
	this.wordWait = "loading...";
	
	this.pinboard = {};	
	//id zwischenspeichern
	this.pinboard.id = 0;
	//wieviele formulare sind offen
	this.details_open = 0;
	
	this.getPinboardTopLevel = function(id, p){
		//uebersicht holen
		
		if(typeof id == "undefined"){
			id = 0;
		}
		if(typeof p == "undefined"){
			p = 0;
		}
	
		//id zwischenspeichern
		this.pinboard.id = id;
	
		this.details_open = 0;
		
		var container = Ext.get('cmp.pinboards.container'+id);
		
		//inhalt per ajax nachladen
		container.getUpdater().update({
			url: '/index.cfm',
		    method: 'GET',
		    params: {
				'event': 'cmp.pinboards.getpinboardtoplevel',
				'cmp.pinboards.id' : id,
				'cmp.pinboards.p' : p
			},
		    text: this.wordWait,
		    scripts: true,
		    nocache: true
		});		
	};
	
	this.getPinboardDetail = function(id){
		//Detailansicht
		
		if(typeof id == "undefined"){
			id = 0;
		}
		
		//scope weitergeben
		var that = this;
	
		var container = Ext.get('cmp.pinboards.pinboarddetail'+id);
		
		//header ausblenden
		Ext.get('cmp.pinboards.pinboardhead'+id).setVisibilityMode(Ext.Element.DISPLAY);		
		Ext.get('cmp.pinboards.pinboardhead'+id).hide();
		
		//id fuer callback verfuegbar machen
		this.commentid = id;
		
		//inhalt per ajax laden
		container.getUpdater().update({
			url: '/index.cfm',
		    method: 'GET',
		    params: {
				'event': 'cmp.pinboards.getpinboarddetail',
				'cmp.pinboards.comment.id' : id,
				'cmp.pinboards.id' : this.pinboard.id
			},
		    text: this.wordWait,
		    scripts: true,
		    callback: that.doDetailUpdateCallback,
		    scope: this,
		    nocache: true
		});	
		
		//detail sichtbar machen
		Ext.get('cmp.pinboards.pinboarddetail'+id).setVisibilityMode(Ext.Element.DISPLAY);		
		Ext.get('cmp.pinboards.pinboarddetail'+id).show();
		
		

	};
	
	this.doDetailUpdateCallback = function(el,success,response,options){
		//callback nach detail update
		
		id = options.scope.commentid;
		
		//antwortform generieren
		this.showNewAnswer(id);
	}
	
	this.closePinboardDetail = function(id){
		//Detailansicht schliessen
		
		if(typeof id == "undefined"){
			id = 0;
		}
		
		//detail ausblenden
		Ext.get('cmp.pinboards.pinboarddetail'+id).setVisibilityMode(Ext.Element.DISPLAY);
		Ext.get('cmp.pinboards.pinboardhead'+id).setVisibilityMode(Ext.Element.DISPLAY);
		
		//header einblenden
		Ext.get('cmp.pinboards.pinboarddetail'+id).hide();
		Ext.get('cmp.pinboards.pinboardhead'+id).show();

		this.changeNewTopicBtn(-1);
	};
	
	this.showNewAnswer = function(id){
		//neue antwortform anzeigen
		
		if(typeof id == "undefined"){
			id = 0;
		}
		
		if(Ext.get('cmp.pinboards.snewanswer'+id)){
			//wenn container fuer antwort existiert
			
			this.renderNewAnswer(id);
			
			//antwortform sichtbar machen
			Ext.get('cmp.pinboards.snewanswer'+id).setVisibilityMode(Ext.Element.DISPLAY);
			//Ext.get('cmp.pinboards.answerbtn'+id).setVisibilityMode(Ext.Element.DISPLAY);
			
			Ext.get('cmp.pinboards.snewanswer'+id).show();
			//Ext.get('cmp.pinboards.answerbtn'+id).hide();
					
			this.changeNewTopicBtn(+1);
			
		}

	};
	
	this.showNewTopic = function(id){
		//formular fuer neues Thema anzeigen
		
		if(typeof id == "undefined"){
			id = 0;
		}
				
		this.renderNewTopic(id);
		
		//sichtbar machen
		Ext.get('cmp.pinboards.newtopic'+id).show();
		
		this.changeNewTopicBtn(+1);

	};
	
	
	this.changeNewTopicBtn = function(op){
		//button fuer neues Thema ein/ausblenden

		//je nach parameter den zaehler verringern/erhoehen
		if(op>0){
			this.details_open++;
		}
		else if(op < 0){
			this.details_open--;
		}
		else{
			this.details_open = 0;
		}
				
		if(this.details_open < 0){
			this.details_open = 0;
		}
		
		if(Ext.get('cmp.pinboards.btnnewtopicenabled'+this.pinboard.id)){
			//wenn container fuer button existiert
			
			Ext.get('cmp.pinboards.btnnewtopicenabled'+this.pinboard.id).setVisibilityMode(Ext.Element.DISPLAY);
			
			if(this.details_open == 0){
				//button anzeigen
				Ext.get('cmp.pinboards.btnnewtopicenabled'+this.pinboard.id).show();	
			}
			else{
				//button ausblenden
				Ext.get('cmp.pinboards.btnnewtopicenabled'+this.pinboard.id).hide();	
			}
		}
		
	};
	
	this.renderNewAnswer = function(id){
		//antwortform generieren
		
		if(typeof id == "undefined"){
			id = 0;
		}
		
		
		Ext.get('cmp.pinboards.snewanswer'+id).update("");
		
		var that = this;	
		
		if(typeof newAnswerForm == "undefined"){
			//wenn noch nicht existiert, dann array fuer alle antwortformulare erzeugen
			newAnswerForm = new Array();
		}
		
		newAnswerForm[id] = new Ext.FormPanel({			
	 		renderTo: 'cmp.pinboards.snewanswer' + id,
	 		frame:true,
	 		autoWidth: true,
	 		labelWidth: 300,
			formId: 'cmp.pinboards.newanswerform'+id,
	  		url: '/index.cfm?event=cmp.pinboards.savepinboardanswer&cmp.pinboards.id=' + this.pinboard.id + '&cmp.pinboards.comment.id=' + id,		
	 		items: [
	 		    /*
	 		    { html: '<span style="font-weight:bold;">Antwort eingeben</span>' },
				*/
	 		    { html: '<h3>RE: ' + document.getElementById('cmp.pinboards.stitle' + id).innerHTML + '</h3>'},
	 		    /*
	 		    {
					xtype: 'textfield',
					fieldLabel: 'Betreff',
					labelSeparator: '',
					id: 'cmp.pinboards.subject'+id,
					name: 'cmp.pinboards.subject',
					width: 490,				
					labelStyle: 'width:210px; margin-top:10px;',
					ctCls: 'cb pad0',
					allowBlank: false,
					msgTarget: 'under',
					value: 'RE: ' + document.getElementById('cmp.pinboards.stitle' + id).innerHTML,
					maxLength: 60,
					disabled: true
				},
				*/
				{
					xtype: 'hidden',
					id: 'cmp.pinboards.subject'+id,
					name: 'cmp.pinboards.subject',
					value: 'RE: ' + document.getElementById('cmp.pinboards.stitle' + id).innerHTML
				},
				
				{
					xtype: 'textarea',
					fieldLabel: 'Deine Antwort',
					labelSeparator: '',
					id: 'cmp.pinboards.desc'+id,
					name: 'cmp.pinboards.desc',
					width: 490,				
					labelStyle: 'width:210px;',
					ctCls: 'cb pad0',
					allowBlank: false,
					msgTarget: 'under',
					maxLength: 200
				},				
				{ html: '<div class="flr mt10"><a class="mr10 ma_button" href="#" onclick="newAnswerForm[' + id + '].doAbort(); return false;"><span>Abbrechen</span><img alt="Abbrechen" onmouseout="src=\'/img/buttons/btn_abbrechen_01_77x21.gif\'" onmouseover="src=\'/img/buttons/btn_abbrechen_02_77x21.gif\'" src="/img/buttons/btn_abbrechen_01_77x21.gif"/></a><a href="#" class="ma_button" onclick="newAnswerForm[' + id + '].doSubmit(); return false;"><span>Speichern</span><img alt="Speichern" onmouseout="src=\'/img/buttons/btn_speichern_01_77x21.gif\'" onmouseover="src=\'/img/buttons/btn_speichern_02_77x21.gif\'" src="/img/buttons/btn_speichern_01_77x21.gif"/></a></div>' }

	 		],
			key: {
				key: [10, 13],
				fn: function(){
					newAnswerForm[id].doSubmit();
				}
			},
			doSubmit: function(){		
				if (newAnswerForm[id].getForm().isValid()) {
					newAnswerForm[id].getForm().submit({
							success: function (pForm, pAction){
								//detail aktualisieren
								that.getPinboardDetail(id);								
								that.changeNewTopicBtn(-1);
								//header aktualisieren
								that.getLastPost(id);
							},
							failure: function(pForm, pAction){
								Ext.MessageBox.alert('Fehler', pAction.result.message);							
							}
						}
					);
				}
			},
			doAbort: function(){				
				//detail schliessen
				that.closePinboardDetail(id);
				window.location.href = '#pinboard_detail'+id;
			}
	 	})

	};
	
	
	this.renderNewTopic = function(id){
		//themen-formular generieren
		var that = this;
		
		Ext.get('cmp.pinboards.snewtopic'+id).update("");
		
		
		if(typeof newTopicForm == "undefined"){
			//wenn noch nicht existiert, dann array fuer alle topicformulare erzeugen
			newTopicForm = new Array();
		}
		
		
		
		newTopicForm[id] = new Ext.FormPanel({
			
	 		renderTo: 'cmp.pinboards.snewtopic' + id,
	 		frame:true,
	 		autoWidth: true,
	 		labelWidth: 300,
			formId: 'cmp.pinboards.newtopicform'+id,
	  		url: '/index.cfm?event=cmp.pinboards.savepinboardtoplevel&cmp.pinboards.id=' + this.pinboard.id,		
	 		items: [
	 		    { html: '<span style="font-weight:bold;">Neuen Eintrag erstellen</span>' },
				{
					xtype: 'textfield',
					fieldLabel: 'Betreff*',
					labelSeparator: '',					
					id: 'cmp.pinboards.subject'+id,
					name: 'cmp.pinboards.subject',			
					width: 490,				
					labelStyle: 'width:210px; margin-top:10px;',
					ctCls: 'cb pad0',
					allowBlank: false,
					msgTarget: 'under',
					maxLength: 60
				},
				{
					xtype: 'textarea',
					fieldLabel: 'Text*',
					labelSeparator: '',
					id: 'cmp.pinboards.desc'+id,
					name: 'cmp.pinboards.desc',			
					width: 490,				
					labelStyle: 'width:210px;',
					ctCls: 'cb pad0',
					allowBlank: false,
					msgTarget: 'under',
					maxLength: 200
				},
				{ html: '<div class="fll mt10 mandatory">*Pflichtfelder</div><div class="flr mt10"><a class="mr10 ma_button" href="#" onclick="newTopicForm[' + id + '].doAbort(); return false;"><span>Abbrechen</span><img alt="Abbrechen" onmouseout="src=\'/img/buttons/btn_abbrechen_01_77x21.gif\'" onmouseover="src=\'/img/buttons/btn_abbrechen_02_77x21.gif\'" src="/img/buttons/btn_abbrechen_01_77x21.gif"/></a><a href="#" class="ma_button" onclick="newTopicForm[' + id + '].doSubmit(); return false;"><span>Speichern</span><img alt="Speichern" onmouseout="src=\'/img/buttons/btn_speichern_01_77x21.gif\'" onmouseover="src=\'/img/buttons/btn_speichern_02_77x21.gif\'" src="/img/buttons/btn_speichern_01_77x21.gif"/></a></div>' }

	 		],
			key: {
				key: [10, 13],
				fn: function(){
					newTopicForm[id].doSubmit();
				}
			},
			doSubmit: function(){		
				if (newTopicForm[id].getForm().isValid()) {
					newTopicForm[id].getForm().submit({
							success: function (pForm, pAction){
								//Ext.MessageBox.alert('Gut zu wissen!', pAction.result.message);
								that.getPinboardTopLevel(id, 1);
								//that.changeNewTopicBtn(-1);
							},
							failure: function(pForm, pAction){
								Ext.MessageBox.alert('Fehler', pAction.result.message);							
							}
						}
					);
				}
			},
			doAbort: function(){
				//formular ausblenden/leeren
				Ext.get('cmp.pinboards.newtopic'+id).setVisibilityMode(Ext.Element.DISPLAY);
				
				Ext.get('cmp.pinboards.newtopic'+id).hide();
				Ext.get('cmp.pinboards.snewtopic'+id).update("");
				
				that.changeNewTopicBtn(-1);
				
			}
	 	})
		
	};
	
	
	this.getLastPost = function(id){
		//antwortzaehler erhoehen
		var container = Ext.get('cmp.pinboards.sanswers'+id);

		container.getUpdater().update({
			url: '/index.cfm',
		    method: 'GET',
		    params: {
				'event': 'cmp.pinboards.getlastpost',
				'cmp.pinboards.comment.id' : id				
			},
		    text: this.wordWait,
		    scripts: true
		});		
	};
}


/*instanz des community-objects erzeugen*/
//myPinboard = new Pinboard();
