var cc=false;
function comment_edit()
{
	$(".edit_link").click(function(){
		var comment = $(this).next(".content").children(".u_comment");
	
		var id = comment.attr("com_id");
		text = comment.text();
		new_html = '<textarea class="com_text">'+text+'</textarea><div><input type="button" class="com_submit" value="Редагувати" /></div>';	
		comment.html(new_html);
		$(".com_submit").click(function(){	
			var edit = $(this).parent().prev(".com_text");
			edit_text = edit.val();				
			$(".com_text").ajaxSuccess(function(){
				comment_edit();
			});
			$.post("/ajax/edit_comment.php", { 'text': edit_text, 'id': id},
		   		function(result){					
		     			comment.html(result);
			});	
		});
		return false;				
	});
	$(".com_edit_link").click(function(){
		var comment = $(this).next(".content").children(".u_comment");
		var id = comment.attr("com_id");
		text = comment.text();
		new_html = '<textarea class="com_com_text">'+text+'</textarea><div><input type="button" class="com_com_submit" value="Редагувати" /></div>';	
		comment.html(new_html);
		$(".com_com_submit").click(function(){	
			var edit = $(this).parent().prev(".com_com_text");
			edit_text = edit.val();				
			$(".com_com_text").ajaxSuccess(function(){
				comment_edit();
			});
			$.post("/ajax/edit_comment.php", { 'text': edit_text, 'id': id},
		   		function(result){					
		     			comment.html(result);
			});	
		});
		return false;				
	});

}	
function comment_comment()
{
	$(".comment_link").click(function(){		
		if(cc==false)
		{
			var comment = $(this).next(".content").children(".u_comment");
			var comment_div = $(this).parent();
			var beforeMe = comment_div.parent().children(".beforeMe");
			var id = comment.attr("com_id");

			new_html = $('<div class="new_comment"><textarea class="newcom_text"></textarea><div align="right"><input type="button" class="newcom_submit" value="Коментувати" /></div></div>');

			new_html.insertAfter(comment_div);
			cc=true;
			$(".newcom_submit").click(function(){	
			
				var edit = $(this).parent().prev(".newcom_text");
				edit_text = edit.val();				
				$(".newcom_text").ajaxSuccess(function(){
					comment_comment();
				});
				new_html.remove();
				cc = false;	
				$.post("/ajax/comment_comment.php", { 'text': edit_text, 'id': id},
	   				function(result){																
	  	   				$(result).insertBefore(beforeMe);										
						comment_edit();
				});			
			});
			return false;				
		}
		return false;
	});
}

$(document).ready(function(){

// Редактирование комментария
comment_edit();
comment_comment();


//Ajax-статус [загрузка ...]
$("#loading").bind("ajaxSend", function(){
    $(this).show();
	}).bind("ajaxComplete", function(){   
		$(this).hide();
});

//Promo-objects
$('#next_obj').click(function(){
                $('#promo_content').load('mod/promo-objects.php?show=next'); 
				//return false;    
                });	
$('#prev_obj').click(function(){
                $('#promo_content').load('mod/promo-objects.php?show=prev'); 
				//return false;    
                });	
//	
//Главное меню
$("div[class='main_menu']").click(function()
		{
			
		//Появление баннеров по клику на 'who' или 'kids'
		//alert(this.id);
		switch(this.id)
		{
		   	case '1':
				$('#ajax_banners').load('/inc/banners.php?section=main');
			break;
			case '4':
				$('#ajax_banners').load('/inc/banners.php?section=6');
			break;
			default:
				$('#ajax_banners').load('/inc/banners.php?section=main');
			break;
		}
		//$("#main_cursor").animate({"top": "50px"}, "slow");
		// hide & show
		$("div[id='main_cursor']").css({'visibility':'hidden'});
		$(this).find('div').css({'visibility':'visible'});
		//
		var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);
		$('#cloud').css({'visibility':'visible'});
		if(!isIE) {$('#cloud').fadeOut(0);$('#cloud').fadeIn("slow");}//$('#cloud').show("slow");			

		$('#cloud').load('/mod/cloud.php?parent_id='+this.id);
		return false;
		});

//Ajax-подгрузчик по клику на динамический id
//по id div'а определяется id страницы
$("div[class='ajax_btn']").click(function()
		{
		// hide & show cursor
		$("div[id='main_cursor']").css({'visibility':'hidden'});
		$(this).find('div').css({'visibility':'visible'});
		// hide cloud_block
		var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);
		if(!isIE) {$('#cloud').fadeOut("slow");} else {$('#cloud').css({'visibility':'hidden'});}
		//
		$('#doc_area').load('/mod/ajax_pages.php?mod=page&id='+this.id);
		//	
		});
// Голосования
$(".q_vote").click(function(){						
	q_id = $(this).attr("q_id");	
	 $(this).parent(".q_cont").load("/ajax/vote.php?id="+$(":radio[name=q_check_"+q_id+"]").filter(":checked").val());
	 //$(".q_cont[q_id="+q_id+"]")
});

// Элементы галереи
//FancyBox
var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);

if(isIE) 
	{
		$("a.img").fancybox({'zoomOpacity': false,'overlayShow': false,'zoomSpeedIn': 1,'zoomSpeedOut': 1});
	} 
	else {
		$("a.img").fancybox({'zoomOpacity': true,'overlayShow': false,'zoomSpeedIn': 0,'zoomSpeedOut': 0});
	}

//При инициации FancyBox скрываем cloud
$("a.img").click(function()
						  {
						  $('#cloud').css({'visibility':'hidden'});
						  });

// не менять местами с объявлением fancybox

	 
});
