function getIFrameDocument(aID){
        // if contentDocument exists, W3C compliant (Mozilla)
    if (document.getElementById(aID).contentDocument){
        return document.getElementById(aID).contentDocument;
    } else {
        // IE
        return document.frames[aID].document;
    }
}

function modifyText(frameID, aName, aArg){   
    if(!aArg){
        aArg = null;
    }
   

    try{
        getIFrameDocument(frameID).execCommand(aName, false, aArg);
       
        document.getElementById(frameID).contentWindow.focus()
    }
    catch(e){
        alert(e);
    } 
} 

var CommentManager = function(){
	
	var SubmitForm = false;
	var StaticEditor = false;
	var FloatingEditor = false, FloatingContainer = false;
	var StateCache = {visible: false, action: false, commentId: false, activeLink: false};
	var Settings = {imagesUrl: "", previewUrl: "", baseUrl: ""};
	
	return{
		init: function(formId, imagesUrl, previewUrl){
			SubmitForm = document.getElementById(formId);
			Settings.imagesUrl = imagesUrl;
			Settings.previewUrl = previewUrl;
		},
		
		setBaseUrl: function(url){
			Settings.baseUrl = url;
		},
		
		createStaticEditor: function(containerId){
			$('#'+containerId).rte({
				css: 'default.css',
				width: '99%',
				height: '150',
				controls_rte: rte_toolbar,
				controls_html: html_toolbar
			});
		},

		showCreateForm: function(parentCommentId){
			if(StateCache.visible == true && StateCache.commentId != parentCommentId){
				$(FloatingContainer).slideUp('fast',function(){
					StateCache.visible = false;
					CommentManager.showCreateForm(parentCommentId);
				});
			}else{
				if(StateCache.activeLink){
					StateCache.activeLink.style.fontWeight = "normal";
				}
				var activeLink = $('#comment_anchor_'+parentCommentId).find("a[name='create_link']")[0];
				activeLink.style.fontWeight = "bold";
				StateCache.activeLink = activeLink;
				
				if(StateCache.action == "edit"){
					$('#comment_descr_'+StateCache.commentId).show();
				}
				
			 	$("#comment_editor_"+parentCommentId).show();
			 	$("#comment_veditor_"+parentCommentId).show();
			 	
			 	//$('#comment_veditor_'+parentCommentId).contentWindow.document.write('');
			 	
			 	if(!$('#comment_editor_'+parentCommentId+' .rte-zone').html()){
			 		loadEditor(parentCommentId);
			 	}else{
			 		reloadEditor(parentCommentId);
			 	}
				
				$(FloatingContainer).find("div[id='edit']").hide();
				$(FloatingContainer).find("div[id='create']").show();
				FloatingContainer = "#comment_editor_"+parentCommentId;
				
				$(FloatingContainer).slideDown('fast',function(){
					StateCache.commentId = parentCommentId;
					StateCache.action = "create";
					StateCache.visible = true;
				});
			}
		},
		
		showEditForm: function(commentId){	
			if(StateCache.visible == true && StateCache.commentId != commentId){
				$(FloatingContainer).slideUp('fast',function(){
					StateCache.visible = false;
					CommentManager.showEditForm(commentId);
				});
			}else{
				if(StateCache.activeLink){
					StateCache.activeLink.style.fontWeight = "normal";
				}
				var activeLink = $('#comment_anchor_'+commentId).find("a[name='edit_link']")[0];
				activeLink.style.fontWeight = "bold";
				StateCache.activeLink = activeLink;
				
				if(StateCache.action == "edit"){
					$('#comment_descr_'+StateCache.commentId).show();
				}
				
				var descrEl = document.getElementById('comment_descr_'+commentId);
				var html = descrEl.innerHTML;
				$(descrEl).hide();				
				
			 	$("#comment_editor_"+commentId).show();
			 	$("#comment_veditor_"+commentId).show();
			 	$("#comment_veditor_"+commentId).css({'display': 'block', 'margin-left': '0px', 'padding-left': '0px'});

			 	if(!$('#comment_editor_'+commentId+' .rte-zone').html()){
			 		var textarea = jQuery('textarea#comment_veditor_'+commentId);
				 	textarea.val(html);
			 		loadEditor(commentId);
			 	}else{
			 		reloadEditor(commentId, html);
			 	}
			 					
				$(FloatingContainer).find("div[id='edit']").show();
				$(FloatingContainer).find("div[id='create']").hide();
				FloatingContainer = "#comment_editor_"+commentId;
				
				$(FloatingContainer).slideDown('fast',function(){
					StateCache.commentId = commentId;
					StateCache.action = "edit";
					StateCache.visible = true;
				});
				
			}
		},
		
		hide: function(){
			
			if(StateCache.activeLink){
				StateCache.activeLink.style.fontWeight = "normal";
			}
			
			$(FloatingContainer).slideUp(function(){
				if(StateCache.action == "edit"){
					var descrEl = document.getElementById('comment_descr_'+StateCache.commentId);
					$(descrEl).show();
				}
				StateCache.visible = false;
			});
		},
		
		create: function(commentId){
			
			var html = document.getElementById('comment_veditor_'+commentId).contentWindow.document.body.innerHTML;
			
			if($.trim(html) != ""){
				SubmitForm['comment_id'].value = StateCache.commentId;
				SubmitForm['action'].value = "create_comment";
				SubmitForm['description'].value = html;
				SubmitForm.submit();
			}else{
				alert('Комментарий не может быть пустым!!!');
			}
		},
		
		staticCreate: function(){
			var html = '';

			if(document.getElementById('create_comment_anchor').value){
				html = document.getElementById('create_comment_anchor').value;
			}else{
				html = document.getElementById('create_comment_anchor').contentWindow.document.body.innerHTML;
			}
				
			if($.trim(html) != "" && $.trim(html) != "<br>"){
				SubmitForm['comment_id'].value = "0";
				SubmitForm['action'].value = "create_comment";
				SubmitForm['description'].value = html;
				SubmitForm.submit();
			}else{
				alert('Комментарий не может быть пустым');
			}
		},

		update: function(commentId){
			var html = document.getElementById('comment_veditor_'+commentId).contentWindow.document.body.innerHTML;
			
			if($.trim(html) != ""){
				SubmitForm['comment_id'].value = StateCache.commentId;
				SubmitForm['action'].value = "update_comment";
				SubmitForm['description'].value = html;
				SubmitForm.submit();
			}else{
				alert('Комментарий не может быть пустым');
			}
		},

		deleteById: function(commentId){
			if(confirm("Вы действительно хотите удалить этот комментарий?")){
				location.href = Settings.baseUrl+"&action=delete_comment&comment_id="+commentId;
			}
		}
	}
}();

function reloadEditor(comment_id, text){
	$('#comment_editor_'+comment_id+' .rte-zone').wrap('<textarea rows="10" id="comment_veditor_'+comment_id+'" name="description" style="display: none;"></textarea>');
	
	if(text){
	 	var textarea = jQuery('textarea#comment_veditor_'+comment_id);
	 	textarea.val(text);
	}
	
	loadEditor(comment_id);
}

function loadEditor(comment_id){
	$('#comment_veditor_'+comment_id).rte({
		css: 'default.css',
		width: '99%',
		height: '150',
		controls_rte: rte_toolbar,
		controls_html: html_toolbar
	});
}