/***
**		Atomic Update v.4.0 
**		js controller 
**		author jb sigit n 
**		April 22, 2009
**		http://www.antibiasa.net
**/



URL = location.protocol+'//'+location.host+'/auv4/index.php/';
//URL = location.protocol+'//127.0.0.1/totalpet/auv4/index.php/';

$(document).ready(function(){
	initMenu();
});

function initMenu()
{	
	$("span[class='auDiv']").each(function(){
		var auKeyword;
		auKeyword = $(this).attr("id");
		$.post(URL+'content/getContent', {Keyword:auKeyword,PageURL:location.href}, function(data){
			if(data.isLoggedIn==true)
			{
				$("#"+auKeyword).css('width','auto');
				//$("#"+auKeyword).next().css('border','1px dashed #ff0000');
				
				$("#"+auKeyword).html('<span class="content-menu" style="color:#ff0000;font-size:14px;font-weight:bold;position:absolute;"><a onClick="enableEditor(\''+auKeyword+'\')">edit</a></span>'+data.Content);
				$("#"+auKeyword+" span a").mouseover(function(){ $(this).css('cursor','pointer'); });
			}
			else
			{
				$("#"+auKeyword).css('width','auto');
				$("#"+auKeyword).html(data.Content);	
			}
		},'json');		
	});
}

function hideMenu()
{
	$(".content-menu").hide();
}

function disableEditor(auElement)
{
	$.post(URL+'content/getContent', {Keyword:auElement}, function(data){
		$("#"+auElement).fadeOut('fast',function(){
			if(data.isLoggedIn==true)
			{
				$("#"+auElement).css('width','auto');
				//$("#"+auElement).css('border','1px dashed #ff0000');
				$("#"+auElement).css("position","");
				$("#"+auElement).html('<span style="color:#ff0000;font-size:14px;font-weight:bold;position:absolute;"><a onClick="enableEditor(\''+auElement+'\')">edit</a></span>'+data.Content);
				$("#"+auElement+" span a").mouseover(function(){ $(this).css('cursor','pointer'); });
			}
			else
			{
				$("#"+auElement).css('width','auto');
				$("#"+auElement).html(data.Content);	
			}
			tinyMCE.execCommand('mceRemoveControl', true, "editor_"+auElement);
			$("#"+auElement).fadeIn('fast',function(){initMenu();});
		});
			
	},'json');	
}

function enableEditor(auElement)
{
	hideMenu();
	$.post(URL+'content/getContent', {Keyword:auElement}, function(data){
		if(data.isLoggedIn==true)
		{			
			$("#"+auElement).fadeOut('fast',function(){
				$("#"+auElement).html('<span style="color:#ff0000;font-size:14px;font-weight:bold;"><a onClick="saveContent(\''+auElement+'\',\'editor_'+auElement+'\');">Save</a>&nbsp;&nbsp;<a onClick="disableEditor(\''+auElement+'\');">Cancel</a></span><br><textarea style="height:350px;" id="editor_'+auElement+'">'+data.Content+'</textarea>');
				$("#"+auElement+" span a").mouseover(function(){ $(this).css('cursor','pointer'); });
				$("#"+auElement).css("position","absolute");
				
				tinyMCE.settings = tinymceConfigs[0];
				tinyMCE.execCommand('mceAddControl', true, "editor_"+auElement);
				
				$("#"+auElement).fadeIn('fast');
			});
			
		}
	},'json');
}

var tinymceConfigs = [{
		// General options
		mode : "none",
		theme : "advanced",
		skin : "o2k7",
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		apply_source_formatting : false,
		forced_root_block: "",
		file_browser_callback : "ajaxfilemanager"
		// Example content CSS (should be your site CSS)
	}];
	
function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = "auv4/public/js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";
			switch (type) {
				case "image":
					break;
				case "media":
					break;
				case "flash": 
					break;
				case "file":
					break;
				default:
					return false;
			}
            tinyMCE.activeEditor.windowManager.open({
                url: "auv4/public/js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php",
                width: 782,
                height: 440,
                inline : "yes",
                close_previous : "no"
            },{
                window : win,
                input : field_name
            });
		}
		
function saveContent(keyword,editorID)
{
	content = tinyMCE.get(editorID).getContent();
	$.post(URL+'content/saveContent',{Keyword:keyword,Content:content},function(data){
		disableEditor(keyword);
	},'json')
}

	
