	
	var miniCms_Admin = false;
	
	//Active the admin mode
	function setMiniCmsAdminMode(){
		miniCms_Admin = true;
	}

	//track all <cmsEdit> tags and add edit tools if admin user:
	function displayMiniCmsEditor(){
		$('div.cmsEdit').each(function(){
			var separator = 3;		
			var imgObj = $(this).children('img');
			var stateBlock= $(this).attr("state");
			//Change the background color and texture, when onHover event
			//and scale down the opacity
			$(this).hover(function(){
				if (stateBlock=="DRAFT"){
					$(this).css({"background":"url(/styles/images/mini_cms/cms_bacground_orange.png) repeat top left", "border":"1px solid orange"});
				}else if(stateBlock=="HIDDEN"){
					$(this).css({"background":"url(/styles/images/mini_cms/cms_bacground_orange.png) repeat top left", "border":"1px solid red"});
				}else{
					$(this).css({"background":"url(/styles/images/mini_cms/cms_bacground_grey.png) repeat top left", "border":"1px solid grey"});
				}
			},function(){
				$(this).css({"background":"none", "border":"0px"});
			});
					
					
					
			var widthCmsEdit = $(this).width();
			var heightCmsEdit = $(this).height();	
			var widthImgObj = imgObj.width();
			var heightImgObj = imgObj.height();		
			var ratioImgObj = imgObj.width() / imgObj.height();
					
			if (widthCmsEdit - (2*separator)  < widthImgObj){
				widthImgObj = widthCmsEdit - (2*separator);
				heightImgObj = widthImgObj / ratioImgObj;
			}
			
			if (heightCmsEdit - (2*separator)  < heightImgObj){
				heightImgObj = heightCmsEdit - (2*separator);
				widthImgObj = heightImgObj * ratioImgObj;
			}
			
			imgObj.css("width",widthImgObj);
			imgObj.css("height",heightImgObj);			
			imgObj.css("margin-top",heightCmsEdit/2-heightImgObj/2);
			imgObj.css("display",'block');
							
		});
	};
	
	//Add the miniCms tool bar in the header
	function displayMiniCmsToolBar(htmlControl){
		$(htmlControl).appendTo("#headerTools > table tr:last");
	};
	
	//Open the info box
	function openInfoCMSDialog(){		
		//Open the dialog
		$("#cmsInfoDialog").dialog({
			bgiframe: true,	
			resizable: true,
			width: 570,
			height: 440,
			modal: true
		});
	}
	
	//Open the miniCms box
	function openEditCMSDialog (contentId,preffix){		
		
		if (preffix == null)
			preffix = "";
		
		var htmlCode = "";
		var editorRole = ""
		
		//contentId = preffix+contentId;
		
		//Display edit control bar or not (Add button)
		if (contentId == -1){
			document.getElementById(preffix+"cmsAddControl").style.display='block';
			document.getElementById(preffix+"cmsEditControl").style.display='none';
		}else{			
			if (document.getElementById(contentId) != null){				
				editorRole = document.getElementById(contentId).getAttribute("editorRole");
				statusMiniCms = document.getElementById(contentId).getAttribute("state");
				if (document.getElementById(preffix+"cancelDraftButton") != null){
					if (statusMiniCms == 'DRAFT')
						document.getElementById(preffix+"cancelDraftButton").style.display='block';
					else
						document.getElementById(preffix+"cancelDraftButton").style.display='none';
				}
				if (document.getElementById(preffix+"hideBlockButton") != null){
					if (statusMiniCms == 'PUBLISHED' || statusMiniCms == 'DRAFT')
						document.getElementById(preffix+"hideBlockButton").style.display='block';
					else
						document.getElementById(preffix+"hideBlockButton").style.display='none';
				}
				if (document.getElementById(preffix+"showBlockButton") != null){
					if (statusMiniCms == 'HIDDEN')
						document.getElementById(preffix+"showBlockButton").style.display='block';
					else
						document.getElementById(preffix+"showBlockButton").style.display='none';
				}
				
				if (document.getElementById(preffix+"toolIds") != null)
					document.getElementById(preffix+"toolIds").value=editorRole;
				
				document.getElementById(preffix+"cmsEditControl").style.display='block';
				if (document.getElementById(preffix+"cmsAddControl") != null)
					document.getElementById(preffix+"cmsAddControl").style.display='none';
				document.getElementById(preffix+"contentId").value=contentId;
				if (statusMiniCms == 'HIDDEN'){
					htmlCode = document.getElementById(contentId+"_hidden").innerHTML;
				}else{
					htmlCode = document.getElementById(contentId).innerHTML;
				}
			}
		}
		
		//If admin, we show the root path else, only the editor rootpath
		rootpath = "{0}/";
		if (miniCms_Admin!=true)
			rootpath += editorRole+"/";
				
		//Open the dialog
		$("#"+preffix+"cmsEditionDialog").dialog({
			bgiframe: true,	
			resizable: true,
			width: 770,
			height: 570,
			modal: true,
			close: closeDialog
		});
						
		
		//Create a temp textarea for tinymce
		document.getElementById(preffix+"editable").innerHTML = '<textarea id="'+preffix+'contentText" name="'+preffix+'contentText" class="'+preffix+'tinymce">'+htmlCode+"</textarea>";
				
		//Load tiny mce for the temp textearea
		$(function() {
			$('textarea.'+preffix+'tinymce').tinymce({
				// Location of TinyMCE script
				script_url : '/styles/tiny_mce/tiny_mce.js',					
				// General options								
				theme : "advanced",
				plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",

				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,outdent,indent,|,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,code,|,forecolor,backcolor",
				theme_advanced_buttons3 : "tablecontrols,|,visualaid,|,sub,sup,|,charmap,emotions,|,image,insertfile,|,fullscreen,|,alignLeft,alignRight",
				height : "440px",
				width : '100%',
				imagemanager_rootpath: rootpath,
				filemanager_rootpath: rootpath,
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				setup : function(ed) {
					// Add a custom button
					ed.addButton('alignLeft', {
						title : 'Align to the left',
						image : '/styles/images/mini_cms/button_pics_left.gif',
						onclick : function() {
							// Add you own code to execute something on click
							ed.focus();						
							ed.selection.getNode().setAttribute('align','left');
							ed.selection.getNode().setAttribute('hspace','10');
							ed.selection.getNode().setAttribute('vspace','10');
						}
					});
					ed.addButton('alignRight', {
						title : 'Align to the right',
						image : '/styles/images/mini_cms/button_pics_right.gif',
						onclick : function() {
							// Add you own code to execute something on click
							ed.focus();
							ed.selection.getNode().setAttribute('align','right');
							ed.selection.getNode().setAttribute('hspace','10');
							ed.selection.getNode().setAttribute('vspace','10');
						}
					});
				}
			});
		});
		
		
	}
	
	//Control for mini_Cms box
	function goAction (action,preffix){
		if (preffix == null)
			preffix = "";
		
		if (action == "cms_delete"){
			if (!confirm("are you sure to delete this block?  ATTENTION: manual tag deletion is needed in the PHP file."))
				return;
		}
		
		document.getElementById(preffix+"actionMiniCms").value=action;
		document.getElementById(preffix+"cmsEditionDialogForm").submit();
	}
	
	function deleteCMSContent (id,preffix){
		if (preffix == null)
			preffix = "";
		
		document.getElementById(preffix+"contentId").value=id;
		goAction ("cms_delete",preffix);
	}

	function closeDialog(preffix){
		if (preffix == null){
			preffix = "";
		}
		id = preffix+"contentText";
		tinyMCE.execCommand('mceRemoveControl',false,id);
        $('#'+preffix+'cmsEditionDialog').dialog('destroy');
		$('#textarea.'+preffix+'tinymce').remove();
		
	}
	
