$.fn.myToggle = function(visible_display)
{
    return this.each(function()
    {

        if(this.style.display=='none')
        {
            $(this).css('display','block');
 
        }
        else  
        {
            $(this).css('display','none');  

        }
    } )
}

function promo_block_close()
{
    $('#promo_block_t').hide();
    $.ajax({
       url: '/ajax/dig.php',
       data:{type: "close_promo_block"},
       type: 'GET'
    });
}


function promo_block_close2()
{
    $('#promo_block_t').hide();
    $.ajax({
       url: '/ajax/dig.php',
       data:{type: "close_promo_block2"},
       type: 'GET'
    });
}



function change_load(to)
{
   if(to=='computer')
   {
       $('#preview_radio').val('computer');
       $('#comment_pr').removeClass('input'); 
       $('#comment_pr').css('background','none');
       $('#preview_from_computer').show();
       $('#preview_from_url').hide();
   }else if(to=='url')
   {
       
       $('#preview_radio').val('url');
       $('#comment_pr').addClass('input');  
       $('#comment_pr').css('background','transparent url(images/input_new.gif) no-repeat scroll 0 0');  
       $('#preview_from_computer').hide();
       $('#preview_from_url').show();       
   } 
}

function login_click(e)
{
  e = e || window.event;
  if(e.keyCode==13)
  {
      $('#login_f_f').submit();
  }
}

function isValidEmail (email)
{
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}

function isLatinString(str)
{   
 
  return (/^[a-z0-9]*$/i).test(str);  
}


function refresh_menu_links()
{   
usersystem = navigator.platform;                                                  
  x = usersystem.indexOf(' ');
  if(x!=-1) {                                                                   
   l = usersystem.length;                                                       
   usersystem = usersystem.substr(1, x-1); 
  }                                     
if($.browser.opera)
{         
    $('.personals ul li').css('margin-bottom','5px');      
    if(usersystem.indexOf('Mac')!=-1)
    {                           
        $('.menu').css('padding-top','26px');
    }
}else if($.browser.safari&&usersystem.indexOf('Mac')!=-1)
{
   $('.menu').css('padding-top','27px'); 
}
else if($.browser.mozilla&&usersystem.indexOf('Mac')!=-1)
{
    $('.menu').css('padding-top','26px'); 
}

}


function CloseFirstPopup() {
	$("#fbaloon").css("display", "none");
	$.ajax({  
        type: "POST",  
        dataType: "json",
        data: "act=setfirst",
        url: "/ajax.php",
        success: function (data) {

        }
    });
}

$(function(){
refresh_menu_links();
});

//Freshitems
function ReadAll(){
		$.ajax({type: "get",
				url: "/ajax.php?act=readall",
				dataType: "text",
				success: function(done)
  		 	 	{
					if(done)
						window.location.reload();
					else
						alert('Извините, произошла ошибка');
  		 	 	}
	   	});
}

function ReadAllComms() {
		$.ajax({type: "get",
				url: "/ajax.php?act=readallcomms",
				dataType: "text",
				success: function(done)
  		 	 	{
					if(done)
						window.location.reload();
					else
						alert('Извините, произошла ошибка');
  		 	 	}
	   	});
}

//Comments
var add_story_process = false;

function AddCom_mycomm(storyid, comment, parentid, outlink){
    comm_unic_id = parentid;
    parentid = $("#real_id_"+parentid).val();
    AddCom(storyid, comment, parentid, outlink, comm_unic_id);
}


function AddCom(storyid, comment, parentid, outlink, comm_unic_id){
		if(outlink == undefined)outlink = '';
		
		if(!add_story_process)
		{
			add_story_process = true;
			//$('#submitBut').attr('disabled',true);

            $("#comm_loading_img").show();


            // "мои комментарии"
            if(comm_unic_id>0) {
                include = $('#com'+comm_unic_id).attr('incl')*1 + 1;
            }
            // стандартное добавление комментария
            else if(parentid!=0) {
				include = $('#com'+parentid).attr('incl')*1 + 1;
            }
			else {
				include = 0;
            }

            // картинки
            comment_images = $("#comment_images").val();
            
			$.ajax({type: "POST",
	  		 	 	data: "act=addcom&id="+storyid+"&comment="+encodeURIComponent(comment)+"&parentid="+parentid+"&include="+include+"&comment_images="+comment_images,
					dataType: "json",
					url: "/ajax.php",
	  		 	    success: function(data) {
						switch(data.type)
					    {
					    	case "error":
					            alert(data.text);
					            break;
					        case "done":

                                // "мои комментарии"
                                if(comm_unic_id>0) {
                                    parentid = comm_unic_id;
                                }

                                $(".comment_reply_btn").fadeOut();

					        	if(parentid==0) {
					        		$("#commentsDiv").append(data.text);
                                    comment_reply_link = $("#commentsDiv .comment_reply_btn:last");
                                }
					        	else{
					        		$("#com"+parentid+'_subcom').append(data.text);
                                    comment_reply_link = $("#com"+parentid+"_subcom .comment_reply_btn");
					        		replycancel();
					        	}
                                comment_reply_link.show();
                                comment_reply_link.prev().prev(".reply_link").hide();

                                var comment_reply_visible = 1;

                                comment_reply_link.click(function(e) {
                                    e.preventDefault();
                                    if($(this).children("span").html()=="редактировать") {
                                        $(this).children("span").html("отмена");
                                        $(this).parent().prev().parent().prev(".comment_desc").hide();
                                        $(this).parent().prev(".comment_desc_edit").show();

                                        $(this).parent().prev(".comment_desc_edit").children("textarea").keypress(function(e) {
                                            if((e.ctrlKey) && ((e.keyCode == 0xA)||(e.keyCode == 0xD))) {
                                                $(".comment_desc_edit_sbmt").click();
                                            }
                                        });

                                        tx_focus = true;

                                    }
                                    else {
                                        if(comment_reply_visible==1) {
                                            $(this).children("span").html("редактировать");
                                        }
                                        else {
                                            $(this).hide();
                                            $(this).prev().prev(".reply_link").show();
                                        }
                                        $(this).parent().prev().parent().prev("div.comment_desc").show();
                                        $(this).parent().prev("div.comment_desc_edit").hide();

                                        tx_focus = false;
                                    }

                                    setTimeout("redraw_footer()", 100);
                                  }
                                );

                                $(".comment_desc_edit_sbmt").click(function() {
                                     comment_textarea = comment_reply_link.parent().prev(".comment_desc_edit").children("textarea");

                                     if(comment_textarea.val()) {

                                          loading_image = comment_reply_link.parent().prev(".comment_desc_edit").children("img");
                                          loading_image.show();

                                          //alert(comment_textarea.val());

                                         $.ajax({
                                            type: "POST",
                        	  		 	 	data: "act=editcom&comment="+encodeURIComponent(comment_textarea.val()),
                        					dataType: "json",
                        					url: "/ajax.php",
                                            success: function(data) {
                                              if(data.type=="error") {
                                                alert(data.text);
                                                comment_reply_link.hide();
                                                comment_reply_link.prev().prev(".reply_link").show();
                                              }
                                              else {
                                                data.text = data.text.replace(/\\"/g, '"');
                                                comment_reply_link.parent().prev().parent().prev("div.comment_desc").html(data.text);
                                                comment_reply_link.children("span").html("редактировать");
                                              }
                                            },
                                            complete: function(data) {
                                                loading_image.hide();
                                                comment_reply_link.parent().prev().parent().prev("div.comment_desc").show();
                                                comment_reply_link.parent().prev("div.comment_desc_edit").hide();

                                                tx_focus = false;  
                                            }
                                         });
                                     }
                                     else {
                                       alert("Введите сообщение!");
                                     }
                                 });

                                setTimeout(
                                    function(){
                                        if(comment_reply_link.html()=="отмена") {
                                          comment_reply_visible = 0;
                                          alert("С момента добавления комментария прошло 3 минуты. Вы больше не можете его отредактировать.");
                                          comment_reply_link.parent().prev().parent().prev("div.comment_desc").show();
                                          comment_reply_link.parent().prev("div.comment_desc_edit").hide();
                                        }
                                        comment_reply_link.fadeOut("normal", function(){comment_reply_link.prev().prev(".reply_link").show();});
                                      },
                                      180000
                                );

					        	$('#storyComments').html( $('#storyComments').html()*1 + 1 );				        	
								$('.comment_textarea_' + parentid).val('Введите текст сообщения...');
								$('.comment_textarea_' + parentid).attr('lang', '1');
							    //$(document).scrollTo('#com'+data.comment_id,400);
								if(outlink != '') {
                                    $('#reply_message_' + parentid).show();
									//document.location.href = outlink;
								}

                                tx_focus = false;
                                setTimeout("redraw_footer()", 100);
					        	break;
					        default:
					            alert("Извините, произошла ошибка");
					            break;
					    }
						add_story_process = false;
						//$('#submitBut').attr('disabled',false);

                        $("#comm_loading_img").hide();
	  		 	 	},
                    complete: function() {
                        if(parentid) {
                      		CancelReply(parentid);
                            if(outlink != '') {
                              $('#reply_link_' + parentid).hide();
                            }
                      	}
                      	else {
                      		$('#comment_textarea_0').val('Введите текст сообщения...');
                      	}
                    }
		   	});




		}
}


//Saved Stories
function SaveStory(storyid, act){
		var actPage = '';
		if(act == 'save')
		{
			actPage = 'SaveStory';
			$('#delete_saved_' + storyid).show();
			$('#save_' + storyid).hide();			
		}
		else if(act == 'delete')
		{
			actPage = 'DelSavedStory';
			$('#delete_saved_' + storyid).hide();
			$('#save_' + storyid).show();			
		}
		if(storyid > 0 && actPage != '')
		{
			$.ajax({type: "POST", 
	  		 	 	data: "act=" + actPage + "&story_id=" + storyid,
					dataType: "json",	
					url: "/ajax.php",
	  		 	    success: function(data)
	  		 	 	{			
						return true;
	  		 	 	}
		   	});
		}
}
