naviki.dialog.EditInPlaceDlg = function( el , userConfig){
	
	naviki.dialog.EditInPlaceDlg.superclass.constructor.call(this, el, userConfig);
	
	if (userConfig['editable']){
		var cmd = [		{	text: naviki.Lang.getLL('edit'),
							handler: this.handleSubmit  
						} ];
						
		this.cfg.queueProperty("buttons", cmd);
	}
	
	this.edit = false;
	
	this.callback.argument = this;
	
	this.callback.success = function(o){
		this.argument.setBody(o.responseText);
		
		this.argument.edit = (document.getElementById('edit'+this.argument.id).style.display!='none');;
		this.argument.switchEditMode();
	};
	
	this.callback.upload = function(o){
		this.success(o);
	};
	
	this.callback.cache = false;
};


naviki.dialog.EditInPlaceDlg.prototype.handleSubmit = function(){

	if ((this.edit === true) || (this.edit == true)){

		var elements = document.getElementsByTagName("input");
		for(var i=0;i<elements.length;i++){
			if(elements[i].type == "checkbox"){
				if(elements[i].checked){
					elements[i].value="1";
				}
				else{
					elements[i].value="0";
				}
			}
		}
		this.submit();
	} else {
		this.edit = !this.edit;
		this.switchEditMode();
	}	
};


naviki.dialog.EditInPlaceDlg.prototype.switchEditMode = function(){
	// update the gui
	document.getElementById('view'+this.id).style.display = (this.edit)?'none':'';
	document.getElementById('edit'+this.id).style.display = (this.edit)?'':'none';

	var buttons = this.getButtons();
	if (typeof(buttons[0]) != 'undefined'){
		if (this.edit){
			buttons[0].set('label', naviki.Lang.getLL('save'), true);
		} else {
			buttons[0].set('label', naviki.Lang.getLL('edit'), true);
		}
	}
};


// needed functionality for not to focussing anything within a dialog
// if not empty overloaded, the function simply try to find the first
// element, which could have a focus and set this. Not good, if the first
// focusable element is far down a page, so the browser will by default 
// scroll to the position.
naviki.dialog.EditInPlaceDlg.prototype.focusDefaultButton = function(){};
naviki.dialog.EditInPlaceDlg.prototype.focusFirst = function(type, args, obj){};
