/**
* Javascript för blog
*/
var destBlog = Class.create();
destBlog.prototype={
	initialize:function(){
		this.current=0;
		this.catTpl=new Template('<div id="cat_#{catid}"><input type="button" id="delcat_#{catid}" onclick="Blog.delCategory(\'#{catid}\');" value="'+lang('REMOVE')+'" /><input type="button"  id="editcat_#{catid}" onclick="Blog.editCategory(\'#{catid}\');" value="'+lang('EDIT')+'" /><input type="button"  id="aborteditcat_#{catid}" onclick="Blog.abortEditCategory(\'#{catid}\');" value="'+lang('ABORT')+'" style="display:none";/><span id="catname_#{catid}">#{catname}</span></div>');
	},
	editBlog:function(){
		/*Hämta blog i redigeringsläge*/
		//var cb=Blog.showEdit.bind(Blog);
		var sel='PA_a=1&blogid='+this.current+'&pageid='+pageId;
		new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:Blog.showEdit, method:'POST'});
	},
	abortEdit:function(){
		//if(this.blogid==0){
			window.location.reload();
			return true;
		//}
			/*
		removeTiny();
		objBnk.removeObjBank();
		setToolboxHeight();
		var cb=Blog.showEdit.bind(Blog);
		var sel='blogid='+this.current+'&pageid='+pageId;		
		new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:cb, method:'POST'});
		*/		
	},
	deleteBlog:function(){
		if(Blog.current==0){
			return true;
		}
		if(confirm(lang('CONFDEL'))){
			var sel='blogid='+Blog.current+'&pageid='+pageId+'&doWCM=removeBlog';		
			new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:function(){window.location='/'+pageId+'.php';}, method:'POST'});
		}
	},
	showEdit:function(t){
		Blog.blogid=t.headerJSON.blogid;
		if($('mainblog')){
			$('mainblog').replace(t.responseText);
		}else{
			if($('blogs')){
				Element.insert($('blogs'), {'top':t.responseText});
			}
		}
		if($('bloglist-items')){$('bloglist-items').hide();}
		if($('blogprops')){$('blogprops').hide();}
		if($('blogs')){$('blogs').show();}
		if($('blogmenu_menu')){
			Blog.switchLiItems($('blogmenu_menu'), $('blognew_menu'));
		}
		if($('destinator-Tool-tags')) {
			var js=$('destinator-Tool-tags').value.evalJSON();
			blogTags=new destTag('auto_Tool_tag', {tagobj:'blogTags', valuefield:'destinator-Tool-tags', displayfield:'destinator_disp_tags', autoAdd:true,tags:js});
		}
		
		/*Kolla tiny hållaren*/
		if(!$('destinator_tiny_container')){
			Element.insert('destinator_verktyg_holder', '<div id="destinator_tiny_container" style="display:none;"><div id="tiny_spacer" style=""> </div></div>');
		}
		objectHolders=t.headerJSON.objectHolders;
		activeObjects=t.headerJSON.activeObjects;
		makeImageHolders();
		reloadTiny();
	},
	saveEdit:function(){
		removeTiny();
		objBnk.removeObjBank();
		setToolboxHeight();
		var sel=Form.serialize('blogpost');
		sel=sel+'&doWCM=saveblog&blogid='+this.current+'&pageid='+pageId;		
		new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:function(t){
					//Blog.current=t.headerJSON.blogid;$('mainblog').replace(t.responseText);Blog.switchLiItems($('blogmenu_menu'), $('bloglist_menu'));
					window.location.reload();
				}, method:'POST'});		
	},
	changeTab:function(evt){
		id=Event.findElement(evt,'li').id;
		if($('blog_menu')) {
			$$('#blog_menu li').each(function(elm){
				if(elm.id==id){
					if($('blog_'+id)) {
						$('blog_'+id).show();
					}
					elm.addClassName('active');
				}
				else {
					elm.removeClassName('active');
					if($('blog_'+elm.id)) {
						$('blog_'+elm.id).hide();
					}
				}
			});
		}
	},
	addCategory:function(){
		var name=$F('catnamn');
		if(trim(name).length>0){
		var sel='doWCM=addCategory&pageid='+pageId+'&catname='+encodeURIComponent(name);
		var cb=Blog.displayNewCat.bind(Blog);		
		new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:cb, method:'POST'});
		}
	},
	displayNewCat:function(t){
		var name=$F('catnamn');
		$('catnamn').value='';
		$('categories').insert(Blog.catTpl.evaluate({catid:t.responseText,catname:name}));		
	},
	delCategory:function(catid){
		var sel='doWCM=deleteCat&catid='+catid;
		new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:function(){$('cat_'+catid).remove();}, method:'POST'});
	},
	editCategory:function(catid){
		var html='<input type="text" id="editcat_'+catid+'" value="'+$('catname_'+catid).innerHTML+'" /><input type="button" value="'+lang('save')+'" onclick="Blog.saveEditedCategory('+catid+');" />';
		$('catname_'+catid).update(html);
		$('editCat_'+catid).hide();
		$('abortEditCat_'+catid).show();
	},
	abortEditCategory:function(catid){
		$('editcat_'+catid).form.reset();
		var html=$('editcat_'+catid).value;
		$('catname_'+catid).update(html);
		$('editCat_'+catid).show();
		$('abortEditCat_'+catid).hide();		
	},
	saveEditedCategory:function(catid){
		var newname=$('editcat_'+catid).value;
		var sel='doWCM=saveUpdatedCat&catname='+encodeURIComponent(newname)+'&catid='+catid;
		var cb=Blog.updateEditedCategory.bind(Blog);
		new Ajax.Request('/admin/ajax/ajax_blog.php', {postBody:sel,onSuccess:cb, method:'POST'});
	},
	updateEditedCategory:function(t){
		var js=t.headerJSON;
		$('catname_'+js.catid).update(js.catname);
	},
	switchBlogMenu:function(evt){
		var cur=Event.findElement(evt, 'li');
		var ul=Event.findElement(evt, 'ul');
		Blog.switchLiItems(ul,cur);
	},
	switchLiItems:function(ul,cur){
		if(ul != null){
		ul.childElements().each(function(elm){if(elm.id==cur.id){elm.addClassName('active');}else{elm.removeClassName('active');}});
		}
	},
	returnToList:function(){
		window.history.back(-1);
	}
}
var blogTags=null;
var Blog=new destBlog();