/*
 *	keszult: 2007-08-29
 *	keszito: Nagy Csaba 
 *	a opti-hoz
 *	v 1.0
*/

var placeEditor = Class.create();

placeEditor.prototype = {
	oldValue: null,
	rowTemplate: null,

	initialize: function (tabEditor, id, name, url, text) {
		this.tabEditor = $(tabEditor);
		this.url = url;
		this.id = id;
		this.text = text;
		this.name = name;
		this.idName = id + '_' + name;
		this.valtozas = false;
		this.editing = true;
    	this.oldValue = this.tabEditor.innerHTML;
    	this.wysi = false;
    	if(this.text=='textarea')
			this.editorTemplate = new Template(
				'<textarea cols="30" rows="10" id="' + this.id + '" name="' + this.name + '">#{value}</textarea><input type="submit" id="save" name="save" value="ok" /><input type="submit" id="cancel" name="cancel" value="cancel" />'
			);
		else{
			if( this.text == 'wysi' ){
				this.editorTemplate = new Template(
					'<textarea cols="30" rows="10" id="' + this.id + '" name="' + this.name + '">#{value}</textarea><input type="submit" id="save" name="save" value="ok" /><input type="submit" id="cancel" name="cancel" value="cancel" />');
				this.wysi = 'generate_wysiwyg(\''+this.id+'\');';
			}
			else
				this.editorTemplate = new Template(
					'<input type="text" id="' + this.id + '" name="' + this.name + '" value="#{value}" /><input type="submit" id="save" name="save" value="ok" /><input type="submit" id="cancel" name="cancel" value="cancel" />'
				);
		}
	    Event.observe(this.tabEditor, 'click', this.cellClick.bindAsEventListener(this));
    	this.oldBgColor = Element.getStyle(this.tabEditor, 'background-color');
	  	Event.observe(this.tabEditor, 'mouseover', this.mouseOverEsemeny.bindAsEventListener(this));
	    Event.observe(this.tabEditor, 'mouseout', this.mouseOutEsemeny.bindAsEventListener(this));
//		alert(this.tabEditor);
	},

	loadRecord: function(data) {
		new Insertion.Bottom(
			this.tbdEditor,
			this.rowTemplate.evaluate(data)
		);
	},

	cellClick: function(event) {
		var e = Event.element(event);
		this.valtozas = false;
		if ( this.editing )
			this.displayEditor(e);
		this.editing = false;
	},

    displayEditor: function(cell) {
    	if (cell.getElementsByTagName('input').length == 0) {
	    	cell.innerHTML = this.editorTemplate.evaluate({value: this.oldValue});
			eval(this.wysi) ;
	    	this.save = $('save');
	    	this.cancel = $('cancel');
	    	this.newValue = $(this.id);
			Event.observe(this.save, 'click', this.saveRecords.bindAsEventListener(this));
	    	var input = cell.down('input');
//	    	alert ( this.newValue.value );
	    	Event.observe(this.cancel, 'click', this.cellBlur.bindAsEventListener(this));
	    	input.focus();
    	}
    },

    cellBlur: function (event) {
       	this.saveValue(Event.element(event));
    },

    saveValue: function(input) {
    	this.editing = true;
    	this.mouseOutEsemeny();
    	if(	this.valtozas )
	   		this.tabEditor.innerHTML = this.newValue.value;
   		else
   			this.tabEditor.innerHTML = this.oldValue;
    },

	csere: function(szoveg){
		var seged = '';
		for(var i =0 ; i < szoveg.length; i++){
			if(szoveg.charAt(i) == "&" )
				seged += "%26";
			else
				seged += szoveg.charAt(i)

		}
		return seged;
	},

    saveRecords: function () {
    	if( this.text == 'wysi' ){
			document.getElementById(this.id).value = document.getElementById("wysiwyg" + this.id).contentWindow.document.body.innerHTML;
	    	this.newValue = $(this.id);
		}
    	var o =  this.name + '=' + this.csere(this.newValue.value) + '&' + this.idName + '=' + this.id + '&ajaxkeres=1';
    		new Ajax.Updater(
				{success: this.tabEditor.id }, 
    			this.url,
    			{
	    			method: 'post',
	    			parameters: o,
	    			onSuccess: this.recordSaved.bindAsEventListener(this)
    			}
    		);
    },

    recordSaved: function (request) {
		this.valtozas = true;
		this.oldValue = this.newValue.value;
       	this.saveValue();
    },

	mouseOverEsemeny: function (){
		if(this.editing)
			this.tabEditor.style.backgroundColor = "#FFFF99";
	},
	
	mouseOutEsemeny: function (){
		this.tabEditor.style.backgroundColor = this.oldBgColor;
	}
}


function getHtml( url, div, sdiv, uzen ){
		var oldValue = $(sdiv).innerHTML;
		var myAjax = new Ajax.Updater(
					{success: div }, 
					url, 
					{
						onLoading: loadUzi(sdiv, uzen),
						onFailure: errorUzi,
						evalScripts: true
					});
}
function getHtmlParam( url, div, sdiv, parameter, uzen ){
		var oldValue = $(sdiv).innerHTML;
		var myAjax = new Ajax.Updater(
					{success: div }, 
					url, 
					{
						method: 'post', 
						parameters: parameter,
						onLoading: loadUzi(sdiv, uzen),
						onFailure: errorUzi,
						evalScripts: true
					});
}
function kuldHtml( url, div, sdiv, rdiv, param, uzen ){
		$(rdiv).innerHTML='';
		var oldValue = $(sdiv).innerHTML;
		if( $(param).value.length > 2 && url.indexOf('keres')!=-1 )
		{
  		var parameter = param + '=' + $(param).value;
  		var myAjax = new Ajax.Updater(
  					{success: div }, 
  					url,
  					{
  						method: 'post', 
  						parameters: parameter,
  						onLoading: loadUzi(div, uzen),
  						onComplete: loadUzi(sdiv,oldValue),
  						onFailure: errorUzi,
  						evalScripts: true
  					});
    }
    else
      alert ( 'Rövid kereső szöveg, min: 3 betü!');
		return false;
}
function loadUzi(sdiv, uzi){
	$(sdiv).innerHTML = uzi;
}
function errorUzi(){
	alert('nem gyo');
}
function csere(mit, mivel, mikor){
	var tmp = $(mit).className;
	if( $(mit).className != mikor){
		$(mit).className = $(mivel).className;
		$(mivel).className = tmp;
	}
}
/*getHtml.prototype = {
	oldValue: null,
	rowTemplate: null,
	
	initialize: function( url, div, sdiv, uzen ){
		this.url = url;
		this.div = div;
		this.sdiv = $(sdiv);
		this.uzen = uzen;
		this.load;
	},
	
	load: function()
	{
		var myAjax = new Ajax.Updater(
					{success: this.div }, 
					this.url, 
					{
						onLoad: this.loading,
						onFailure: this.reportError,
						evalScripts: true
					});
	},
	
	loading: function()
	{
		this.sdiv.innerHTML = this.uzen;
	},
	
	reportError: function()
	{
		alert('nem gyo');
	}
}*/
	var torol = new Class.create();
	torol.prototype = {
		initialize: function( box, box2, linknev, linkurl){
			this.box = $(box);
			this.box2 = box2;
			this.linknev = linknev;
			this.linkurl = linkurl;
			this.box.onclick = this.torol.bindAsEventListener(this);
		},
		torol: function(evt){
			$(this.linknev).value = "";
			if(this.linkurl != 'null')
				$(this.linkurl).value = "";
			Element.hide(this.box2);
		}
	};

	var hi=0;
	var hozzafuz = Class.create(); //az osztály implementációjának definíciója
	hozzafuz.prototype = {
	   initialize: function(chkBox, message, box2, box3) {
			this.chkBox = $(chkBox);
			this.message = message;
			this.box2 = $(box2);
			this.box3 = $(box3);
			this.box2nev = box2;
			this.box3nev = box3;
			//adjuk meg a metódust az eseményhez
			this.chkBox.onclick = 
			   this.showMessage.bindAsEventListener(this);
	   },
	   showMessage: function(evt) {
		   hi++;
		if(this.box2.value != ""){
				var sz = this.box2.value;
				this.box2.value="";
				new Insertion.Bottom(this.message, "<div id='"+this.message+"t"+hi+"'>"+
							"<p>"+
							"<input type='hidden' id='"+this.box2nev+hi+"' name='"+this.box2nev+"[]' value='"+sz+"'>"+
							"Hivatkoz&#225;s: "+sz+
							"<br>"+
							"<a style='text-decoration: underline; cursor: pointer;' id='torol"+hi+"' onClick=torol>t&#246;r&#246;l</a></p>"+
							"<script>"+
							"var toroles =new torol('torol"+hi+"', '"+this.message+"t"+hi+"', '"+this.box2nev+hi+"', 'null');"+
							"<\/script>"+
							"</div>");
		}
	   }
	};
