$(document).ready(function () {
	
	separateCommentRow();
})

$(function () {
	$('.messageHeadOption .expand img').click(function () {
		var current = $(this);
		current.parents().filter('div.messageHead').next().toggle('fast');
		if (current.attr('alt') == '[-]') { 
			current.attr('src', 'images/icons/maximize.gif');
			current.attr('alt', '[+]');
		} else {
			current.attr('src', 'images/icons/minimize.gif');
			current.attr('alt', '[-]');
		}	
	});
	
	$('.formInsert .btnCancelTopic').click(function () {
		$('this').parents().filter('.message_body').toggle('fast');
		
	});
	
	
});

$(function () {
	$('.writeAnswer a').click(function () {
		$('.writeAnswer a').css('display', 'block');
		$('.writeAnswer .formInsert').remove();
		$(this).parent()
			.children('a.linkAnswer').css('display', 'none')
			.parent().append($('#messageInsertForm').html())
			.children('.formInsert').toggle('fast');
		$('input[class=messageId]:first').attr('value', 
					$(this).parents().filter('.messageBody').children('a:first').attr('name')) ;
		//$(this).parents().filter('.messageBody').toggle('fast');

		btnClick();
	});
});
$(function () {
	$('.topicNewLink a').click(function () {
		initTinyMce();
		$('#topicNewForm').fadeIn('medium');
	});
	
	$('#topicNewForm #cancel').click(function() {
		$('.error').html('');
		$('#topicNewForm').fadeOut('medium');
	});
});

function btnClick () {
	
	initTinyMce();
	
	$('.formInsert #send').click(function (){ 
			$('.writeAnswer form:first').submit();
	});
	
	$('.formInsert #cancel').click(function () {
		$(this).parents().filter('.formInsert').toggle('fast');
		$('.linkAnswer').css('display', 'block');
					     
	});

	
}

// add comment click event
$('#btnAddComment').live('click', function() {
	var name = $('input[name="name"]').val();
	var email = $('input[name="email"]').val();
	var web = $('input[name="web"]').val();
	var postId = $('input[name="postId"]').val();
	var text = $('#input_comment_text').val();
	var captchaId = $('input[name="captcha[id]"]').val();
	var captchaInput = $('input[name="captcha[input]"]').val();
	$('.write_comment .error').remove();
	$('input[name="captcha"]').remove();
	
	$.ajax({
		   type: "post",
		   url: "post/add-comment.html",
		   dataType: "json",
		   data: {"name": name ,
					"email": email,
					"text": text,
					"web": web,
					"captcha[id]": captchaId,
					"captcha[input]": captchaInput,
					//"captcha": captcha,
					"postId": postId},
		   success: function(data){
		     if (data.error == 1) {
		    	 $('#frmComment').remove();
		    	 $('.write_comment h3').after(data.formComment);
		    	 $('.write_comment h3').after('<div class="error">' + data.message +'</div>');
		     } else {
		    	 $('.no_comments').remove();
		    	 $('.post_comments').append(data.comment);
		    	 separateCommentRow();
		    	 $('#frmComment').remove();	
		    	 $('.write_comment h3').after(data.formComment);
		    	 
		    	 $('input[name="name"]').val('');
		 		 $('input[name="email"]').val('');
		 		 $('input[name="web"]').val('');
		 		 $('input[name="postId"]').val('');
		 		 $('input[name="captcha[input]"]').val('');
		 		 $('#input_comment_text').val('');
		     }	 
		   }, 
			error: function(XMLHttpRequest, textStatus, errorThrown) {
			   //alert('Err:' + textStatus + errorThrown);
		   }   
	
		 });
}); 

function separateCommentRow()
{
	$('.post_comment:odd').addClass('odd');
	$('.post_comment:even').addClass('even');
}

// toggle a forum message.
function ToggleMessage() {
	$('messageIn').click(function () {
		$(this).parents().filter('');
	});
	
}

function initTinyMce() {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		theme_advanced_buttons1 : "mymenubutton,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo,link,unlink",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom"
	});
}


function initTinyMceBE() {
	/*
	// this is a list box for class changing of an pre element on toolbar buttons.
	// it is for the code highlighting for source codes 
	tinymce.create('tinymce.plugins.CodeHighlight', {
		init: function (ed) {
			ed.onNodeChange.add(function(ed, cm, n, co) {
				cm.setDisabled('codehighlight', n.nodeName != 'PRE');
			});
		}, 
	    createControl: function(n, cm) {
	        switch (n) {
	            case 'codehighlight':
	                var mlb = cm.createListBox('mylistbox', {
	                     title : 'for PRE element',
	                     onselect : function(value, cm) {
	                        var selectedElement = tinyMCE.activeEditor.selection.getNode();
	                         
	                         var elementType = typeof selectedElement;
	                         if (elementType == 'object') {
	                        	 var elementName = selectedElement.nodeName.toLowerCase();; 
	                        	 if (elementName = 'pre') {
	                        		 alert(value + selectedElement.getAttributeNode("class"));
	                        			 //selectedElement.setAttributeNode("class") = value;
	                        		 var attrClass = document.createAttribute("class");
	                        		 attrClass.nodeValue = value;
	                        		 selectedElement.setAttributeNode(attrClass);
	                        	 }
	                         }
	                     }
	                });

	                // Add some values to the list box
	                mlb.add('php', 'php');
	                mlb.add('js', 'js');
	                mlb.add('html', 'html');
	                
	                // Return the new listbox instance
	                return mlb;
	        }

	        return null;
	    }
	});

	//Register plugin with a short name
	tinymce.PluginManager.add('codehighlight', tinymce.plugins.CodeHighlight);
	*/
	tinyMCE.init({
		mode : "exact",
		elements: "blog_content",
		theme : "advanced",
		theme_advanced_blockformats : "p,h1,h2,h3,blockquote,pre",
		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 : "codehighlight, bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,backcolor|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image",
		theme_advanced_buttons2 : "cleanup,code,|,search,replace,|,preview,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage,|,fullscreen,|,ltr,rtl,|,print",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom"
	});
}
