function BaseBlogsHandler(phpDir){
	this.dbBlog=dbBlog(phpDir+'blog.php');
	this.isInit=false;
	this.currentBlog=null;
	this.txtBlogLinesCnt=null; //textnode met: Aantal reacties:
	this.setPwd=function(pwd){
		this.dbBlog.pwd=pwd;
	};
}

BaseBlogsHandler.prototype={
	
	loadCurrentBlog: function(){
		this.currentBlog=this.dbBlog.getCurrentBlog();
	},
	
	hasPwd: function (){
		if (this.dbBlog.pwd && this.dbBlog.pwd!==''){
			return true;
		}
		return false;
		
	},
	
	removeBlogLineFromCurrentBlog : function(blogLine){
		var blog=this.currentBlog;
		if (blog){
			var idx=-1;
			for (var i=0;i<blog.blogLines.length; i++){				
				if (parseInt(blog.blogLines[i].id,10)==parseInt(blogLine.id,10)){
					idx=i;
					break;					
				}				
			}
			if (idx>=0){
				blog.blogLines.splice(idx,1);				
			}			
		}		
	},
	
	setOpenBlogMenuBtnVisib: function(visib){
		if (!this.hasPwd()){
			//niet in beheer
			var smi = getSubMenuItem('blog', 'openOldBlog');
			smi.visible = visib;
			drawBtns();				
		}		
	},
	
	getCurrentBlog: function(){
		if (!this.currentBlog){
			this.loadCurrentBlog();
		}
		return this.currentBlog;		
	},
	
	addBlogLineClickCB: function(){//er wordt op de button 'sla op' geklikt bij plaatsen reaktie
		var that=this;
		function addBlogLineClick(){
			var txt=$('blogLineTxt').value;
			//var txt=encodeURIComponent($('blogLineTxt').innerHTML);
			if (trim(txt)==''){
				alert('De stelling mag niet leeg zijn');
				$('blogLineTxt').focus();
				return;				
			}
			var blogLine=new BlogLine();
			blogLine.email=$('blogLineEmail').value;
			if (blogLine.email !== '') {
				errorMsg = isValidEmail(blogLine.email);
			}
			else {
				errorMsg=''; //leeg emailadres mag
			}
			if (errorMsg != '') {
				alert(errorMsg);
				$('blogLineEmail').focus();
				return;
			}
			blogLine.name=$('blogLineNaam').value;
			blogLine.txt=txt;
			blogLine.blogId=that.currentBlog.id;
			var dbResult=that.dbBlog.saveNewBlogLine(blogLine);
			if (showResult(dbResult)) {
				//savenew heeft de id gezet en de createTime vastgelegd
				blogLine.id=dbResult.blogLine.id;
				blogLine.createTime=dbResult.blogLine.createTime;
				that.currentBlog.addBlogLine(blogLine);
				//toon extra blogLine				
				that.addBlogLineUI($('blogLines'),blogLine,that.hasPwd());
				//verberg blogLinePanel
				$('addBlogLinePanel').style.display = 'none';				
				that.showBlogLinesCount();
			}
		}
		return addBlogLineClick;		
	},
	
	blogLineCancelClickCB: function(){//er word op de button 'afbreken' geklikt
		var that=this;
		function blogLineCancelClick(){
			$('addBlogLinePanel').style.display='none';				
		}
		return blogLineCancelClick;
	},
	
	startBlogLineAddCB: function(){ //er wordt begonnen  met het maken van een nieuwe reaktie
		function startBlogLineAdd(){
			$('addBlogLinePanel').style.display='block';					
			$('blogLineTxt').focus();
		}
		return startBlogLineAdd;
	},
	
	initOpenOldBlogCB: function(blog){ 
		var that=this;
				
		function initOpenOldBlog(){
			that.setOpenBlogMenuBtnVisib(true);
			if (that.hasPwd()){
				//aanroepen vanuit beheer
				that.initBlogCB('beheer_',true,blog.id,false)();
			}
			else {
				var hash = new Hash(window.location.hash);
				that.initBlogCB('old_', false, hash.blogId,true)();
			}			
		}
		return initOpenOldBlog;		
	},
	
	dateFormatBlogLineHeader:function (d){
		var days=d.getDate(); //niet getDay
		var month=d.getMonth()+1;
		var year=d.getFullYear();
		var hour=d.getHours();
		var min=d.getMinutes();
		return strZero(days,2)+'-'+strZero(month,2)+'-'+year+' om '+hour+':'+strZero(min,2);		
	},
	
	dateFormatBlogHeader: function (d){
		var days=d.getDate(); //niet getDay
		var month=d.getMonth()+1;
		var year=d.getFullYear();
		return(strZero(days,2)+'-'+strZero(month,2)+'-'+year);
	},
	
	showBlogLinesCount: function(){
		if (this.txtBlogLinesCnt){
			this.txtBlogLinesCnt.nodeValue='Aantal reacties: '+this.currentBlog.blogLines.length;			
		}		
	},
	
	refillBlogHeader: function(blog,headerDiv,editable){
		var warnOldBlogDiv;
		removeChilds(headerDiv);		
		headerDiv.className='blogTitleHeader';		
		createTextNode(headerDiv,'Geplaatst op '+this.dateFormatBlogHeader(blog.createTime));
		createElt(headerDiv,'br');
		this.txtBlogLinesCnt=createTextNode(headerDiv,'');		
		this.showBlogLinesCount();
		if (!editable){
			warnOldBlogDiv=createElt(headerDiv,'div');
			warnOldBlogDiv.id='warnOldBlogDiv';
			createTextNode(warnOldBlogDiv,'Deze blog is ondertussen gesloten.');
			createElt(warnOldBlogDiv,'br');
			createTextNode(warnOldBlogDiv,'U kunt geen reakties meer geven.')				
		}	
	},
	
	getRemoveBlogLineCB: function(blogLine,btn){
		var that=this;
		
		function removeBlogLine(){
			//alert('removeBlogLine');			
			var res=that.dbBlog.deleteBlogLine(blogLine.id);
			if (showResult(res)){
				that.removeBlogLineFromCurrentBlog(blogLine);
				if (that.hasPwd()){
					prefix='beheer_'
				}
				else {
					prefix='old_'
				}
				that.initBlogCB(prefix,true,blogLine.blog.id,false)();
			}
		}		
		return removeBlogLine;
	},
	
	addBlogLineUI: function(linesDiv,blogLine,editable){
		var btn;
		blogLineDiv=document.createElement('div');//over hele blogLine
		blogLineDiv.className='blogLine';
		
		headerDiv=createElt(blogLineDiv,'div');
		headerDiv.className='blogLineHeader';
		
		if (blogLine.name==''){
			persoonStr='?'
		}
		else {
			persoonStr=blogLine.name;
		}
		
		txt=createTextNode(headerDiv,persoonStr+' heeft op '+this.dateFormatBlogLineHeader(blogLine.createTime)+' onderstaande reactie geplaatst:');
		if (editable){
			btn=createButton(headerDiv,'verwijder',this.getRemoveBlogLineCB(blogLine,btn));			
			btn.className='btnVerwijderBlogLine';
		}                                                                                      						
		lineDiv=createElt(blogLineDiv,'div');
		fillDOMLines(lineDiv,blogLine.txt);
		//lineDiv.value=blogLine.txt;
		lineDiv.className='blogLineContent';
		//het is handiger om de nodes vooraan in te voegen (reuse voor saveNew)
		if (linesDiv.hasChildNodes) {
			linesDiv.insertBefore(blogLineDiv, linesDiv.firstChild);
		}
		else {
			linesDiv.appendChild(blogLineDiv);
		}
	},

	
	initBlogCB: function(prefixIds,editable,blogId,visibMenuBtn){
		/*deze functie wordt gebruikt voor de currentBlog, maar ook voor het
		 * tonen van een oude blog. Steeds maar 1 blog.
		 */
		
		var that=this;
		
		function initBlogScherm(){
			//toont het hele blog scherm
			that.setOpenBlogMenuBtnVisib(visibMenuBtn);
			var headerDiv,linesDiv,lineDiv,i,blog,warnOldBlogDiv,linesDivId,
			blogTitleId=prefixIds+'blogTitle',
			blogTitleDiv=$(blogTitleId);
			if (blogTitleDiv){
				removeChilds(blogTitleDiv);//verwijder oude contentdiv
			}
			contentDiv=createElt(blogTitleDiv,'div');
			contentDiv.className='blogTitleContent';
			if (editable && (!that.hasPwd())) { //bij beheer hoef je geen reaktie te plaatsen
				$('blogLineSave').onclick = that.addBlogLineClickCB();
				$('blogLineCancel').onclick = that.blogLineCancelClickCB();
				$('btnStartAddBlogLine').onclick = that.startBlogLineAddCB();
			}			
			
			if (blogId<0){
				blog=that.getCurrentBlog();
			}
			else {
				var res=that.dbBlog.getBlogById(blogId);				
				if (showResult(res)){
					blog=res.blog;
				}
				else {
					return;
				}
			}
			//contentDiv.innerHTML=blog.txt;			
			fillDOMLines(contentDiv,blog.txt);
			
			headerDiv=createElt($(prefixIds+'blogTitle'),'div');
			headerDiv.id=prefixIds+'blogHeaderInfo'
			that.refillBlogHeader(blog,headerDiv,editable);		
			
			//vullen van de blogLines
			
			linesDivId=prefixIds+'blogLines';
			linesDiv=$(linesDivId); 
			if (linesDiv){
				removeChilds(linesDiv);
			}
			else {
				linesDiv=createElt($(prefixIds+'blog'),'div');
				linesDiv.id=linesDivId;				
			}
			//bloglines toevoegen
			for (i=0; i<blog.blogLines.length; i++){
				blogLine=blog.blogLines[i];
				that.addBlogLineUI(linesDiv,blogLine,that.hasPwd());
			}			
		}
		return initBlogScherm;		
	},
	
	createOldBlogsTableHeaders: function(editable,table){
		var head=createElt(table,'thead'),headers;
		var row=createElt(head,'tr');

		if (editable){
			//open, delete, current
			headers=['openen','actief','wis','startdatum','stelling'];
		}
		else {
			headers=['openen','startdatum','stelling'];	
		}
		headers.each(function (headerTxt){
			createTh(row,headerTxt);
		});
		return head;
	},
	
	getDeleteOldBlog: function(blog){ //beheer
		that=this;
		function deleteOldBlog(){			
			var currBlog=that.getCurrentBlog();
			if (blog.id==currBlog.id){
				alert('Je kunt de actieve blog niet wissen');
				return;				
			}
			if (confirm('Weet je zeker dat je deze blog wilt wissen?')) {
				var res = that.dbBlog.deleteBlog(blog.id);
				if (showResult(res)) {
					//scherm refreshen
					that.initOldBlogsCB(true)();
				}
			}
		}
		return deleteOldBlog;
	},
	
	getOpenOldBlog: function (blog){
		var that=this;
		
		function openOldBlog(){ //de gebruiker heeft op de knop 'open' geklikt
			var hasPwd=that.hasPwd();
			if (hasPwd) {
				//aangeroepen vanaf beheer
				//scherm leeg maken		
				removeChilds($('oldBlogs'))
				that.initOpenOldBlogCB(blog)();			
			}
			else {
				that.setOpenBlogMenuBtnVisib(true)
				window.location.hash = '#main=blog&sub=openOldBlog&blogId=' + blog.id;
			}
		}
		return openOldBlog;
		
	},
	
	getSetCurrentBlogCB:function (rb,blog){
		var that =this;
		
		function setCurrentBlog(){
			if (rb.checked) {
				var res=that.dbBlog.setActiveBlog(blog.id);
				if (showResult(res)) {
					//reset current blog
					that.currentBlog = null;
					//refresh screen
					that.initOldBlogsCB(true)();
				}				
			}
		}		
		return setCurrentBlog;		
	},
	
	fillRowOldBlog: function (editable,body,blog){
		var row=createTr(body),td,img,rb;
		var currBlog=this.getCurrentBlog();
		row.id='blog'+blog.id;
		//open button
		td=createTd(row);
		createButton(td,'open',this.getOpenOldBlog(blog));
		td.style.width='15px';
		
		if (editable){
			//actief radiobutton
			td=createTd(row);
			rb=createRadioButton(td,'',currBlog.id==blog.id,null);
			rb.onclick=this.getSetCurrentBlogCB(rb,blog)
			td.style.width='20px';
			td.style.textAlign='center';
			//wis image
			td=createTd(row,'');
			img=createElt(td,'img');
			img.src='../../artikelBeheer/images/wis.gif';
			td.onclick=this.getDeleteOldBlog(blog);
			td.style.width='20px';
			td.style.textAlign='center';
		}
		td=createTd(row,this.dateFormatBlogHeader(blog.createTime));
		td.style.width='100px';
		
		//de stelling
		td=document.createElement('td');
		row.appendChild(td);
		fillDOMLines(td,blog.txt);
		return row;
	},
	
	createNewBlogScreen: function(){
		alert('newBlogScreen');		
	},
	
	initOldBlogsCB: function(editable){ //laat alle blogs zien. De gebruiker heeft op de knop 'oude blogs' geklikt in het hoofdmenu
		var that=this;

		function initOldBlogsScreen(){
			//als pwd='' dan het overzicht zoals een gewone gebruiker
			that.setOpenBlogMenuBtnVisib(false);
			var res=that.dbBlog.getAllBlogs(),oldBlogs;
			if (showResult(res)){
				oldBlogs=res.blogs;
				var div=$('oldBlogs');
				removeChilds(div);
				if (res.blogs.length==0){
					createTextNode(div,'Er zijn geen oude blogs');
					return null;
				}
				if (that.pwd==''){
					var warningDiv=createElt(div,'div');
					warningDiv.id='warningDiv';
					createTextNode(warningDiv,'Hieronder staan oude niet actieve blogs. U kunt hier niet meer op reageren.');				
				}
				var oldBlogsTable=createElt(div,'table');
				that.createOldBlogsTableHeaders(editable,oldBlogsTable);
				var body=createElt(oldBlogsTable,'tbody');
				for (var i=0; i<oldBlogs.length;i++){
					
					if (oldBlogs[i].id!==that.getCurrentBlog().id || editable){
						that.fillRowOldBlog(editable,body,oldBlogs[i]);						
					}					
				}
				
			}			
		}
		return initOldBlogsScreen;		
	},
	
	init: function(mainMenuItem){
		if (!this.isInit) {
			this.isInit = true;
		
		}
	},	
	//------------- beheer -------------
	initBeheer: function(mainDiv){
		var newBlogBtn=$('newBlog');
		newBlogBtn.onclick=this.newBlogCB(newBlogBtn);
		var saveNewBlogBtn=$('saveNewBlog');
		saveNewBlogBtn.onclick=this.saveNewBlogCB(saveNewBlogBtn);
		var cancelNewBlogBtn=$('cancelNewBlog');
		cancelNewBlogBtn.onclick=this.cancelNewBlogCB(cancelNewBlogBtn);
		var refreshBtn=$('refresh');
		refreshBtn.onclick=this.startCB(refreshBtn);
		
		this.initOldBlogsCB(true)();				
	},
	
	newBlogCB : function (btn){
		var that=this;
		
		function newBlog(){//de gebruiker heef in beheer op nieuwe blog geklikt
			$('pnlNewBlog').style.display='block';			
		}
		return newBlog;		
	},
	
	startCB : function (btn){
		
		function start(){
			window.location.reload(true);			
		}
		return start;
		
	},
	
	saveNewBlogCB : function (btn){
		var that=this;
		
		function saveNewBlog(){//de gebruiker heef in beheer op nieuwe blog geklikt
			var txt=$('newBlogTitle');
			if (trim(txt.value)==''){
				alert('De stelling mag niet leeg zijn');
				txt.focus();
				return;				
			}
			var res=that.dbBlog.saveNewBlog(txt.value);
			if (showResult(res)) {
				$('pnlNewBlog').style.display = 'none';
				that.initOldBlogsCB(true)(); //refill screen, met grid
				txt.value='';
			}
		}
		return saveNewBlog;		
	},

	cancelNewBlogCB : function (btn){
		var that=this;
		
		function cancelNewBlog(){//de gebruiker heef in beheer op afbreken geklikt
			$('pnlNewBlog').style.display='none';			
		}
		return cancelNewBlog;		
	}
	
} //end class BlogHandler

function Blog(id){
	this.id=id;
	this.blogLines=[];
}

Blog.prototype={
	id : -1,
	txt : '',
	lines : [],
	loadById: function(id){
		
	},
	
	addBlogLine: function (blogLine){
		blogLine.blog=this;
		this.blogLines.push(blogLine);
	},	
	
	
	saveNew: function (){
			
	}
}
