  // Png-fix
  $(document).ready(function(){$(document).pngFix();});
  // Image-preloader function 
  $.preload = function(){jQuery.each (arguments,function (e) {jQuery("<img>").attr("src", this);});}
  
  // Authorization form
  $(document).ready(function()
    {
      $(".login_show").click(function()
        {
          var divHeight = $("table:first").height();
          var loginWidth = $("table:first").width();
          var background = $('<div id="overlayBackground"></div>');
          $(background).css({'width:' : '100%', 'height': divHeight, 'opacity' : '0.1'});
          $("#login_form").width(320);                                
          $(background).animate({'opacity' : '.6'}, 500);                                
          $("body").append(background);
          $("#login_form").show();
          
          var login = document.getElementById('login');
          login.focus();
          
          $("#overlayBackground").bind('click' ,function()
            {
      	      $(this).fadeOut(1000);
              $(this).remove();
              $("#login_form").hide();
            });
        });
    });
  
  function setCookie (name, value, expires, path, domain, secure) 
    {
      document.cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    }

  function getCookie(name) 
    {
	  var cookie = " " + document.cookie;
	  var search = " " + name + "=";
	  var setStr = null;
	  var offset = 0;
	  var end = 0;
	  if (cookie.length > 0) 
	    {
	      offset = cookie.indexOf(search);
		  if (offset != -1) 
		    {
		      offset += search.length;
			  end = cookie.indexOf(";", offset)
			  if (end == -1) 
			    {
			      end = cookie.length;
			    }
			  setStr = unescape(cookie.substring(offset, end));
		    }
	    }
	  return(setStr);
    } 
    
      $(document).ready(function(){
          $("#add_comm_topic").click(function(){
             $("#add_comm_end").hide();
             $(".comment_form").remove();
             $("#comment_to").val('');
             $("#add_comm_end").before('<div class="comment_form">'+$('.comment_form_tpl').html()+'</div>');
          });
	  
          $(".comment:odd").css("background-color","#f4ffde");	
          $(".answer").click(function()
            {
              $("#add_comm_end").show();
              $(".comment_form").remove();
              $("#c"+this.id.slice(1)).append('<div class="comment_form">'+$('.comment_form_tpl').html()+'</div>');
              $('#comment_to').val(this.id.slice(1)); 
              $('#comment_form_tpl').show();            
          });
      });
      
      function delclick()
        {
          $(".delete_comment").click(function(){
            level = (parseInt($('#c'+this.id.slice(1)).css("margin-left"))-10)/30;
            k = 1; 
            list_id = this.id.slice(1); 
            while (level < ((parseInt($('#c'+this.id.slice(1)).next("div").css("margin-left"))-10)/30)){
              list_id = list_id + "|" + $('#c'+this.id.slice(1)).next("div").attr("id").slice(1);
              $('#c'+this.id.slice(1)).next("div").remove();
              k++;
            }

            $('#c'+this.id.slice(1)).remove();
            
            $("#comment_count").html(parseInt($("#comment_count").html()) - k);
                
            $(this).after('<img style="position: absolute; margin-top: -2px;" class="ajaxPreloader" src="/img/ajax-loader.gif">');
            $.post("/comments/delete/", {comid:list_id, obj:$("#comm_obj").val()}, function(data){
                $(".ajaxPreloader").remove();        
              }); 	          
          });
        }
   
      $(document).ready(function(){
          $(".moderate_comment").click(function(){ 
            $(this).after('<img style="position: absolute; margin-top: -2px;" class="ajaxPreloader" src="/img/ajax-loader.gif">');
            $('#c'+this.id.slice(1)).html('<div style="height: 25px;"><img src="/data/users/avatar/comment_moder.png" style="float: left; margin-right: 10px;" /><div style="width: 100%; text-align: cwnter; font-size: 16px; padding-top: 3px;">Пришел добрый доктор Айболит и вылечил плохой коментарий <a href="javascript: void(0);" id="d'+this.id.slice(1)+'" class="delete_comment" style="text-decoration: underline; color: red">удалить</a></div></div>');  
            delclick();
            $.post("/comments/moderate/", {comid:this.id.slice(1), obj:$("#comm_obj").val()}, function(data){
                $(".ajaxPreloader").remove();        
              }); 	          
          });
      });   
      
      $(document).ready(function(){
        delclick();
      });        


  // ---------------------------- Language select
  $(document).ready(function()
    {
      var e = window.event; 
      $(document.body).click(function (e) 
        {
          if (e.target.className == "selector" || e.target.className == "select_flag" || e.target.className == "select_button" || e.target.className == "select_box")
            {
              $(".select_box").fadeIn("slow");
	        }
	      else
	        {
	          $(".select_box").hide(); 
	        }
        });
      
      // Top buttons hover
      $.preload("/img/login_a.gif", "/img/singup_a.gif", "/img/ru_flag.gif", "/img/quickloader.gif", "/img/enter_button_a.png", "/img/logout_a.png");
      
      $(".opt_highlight").mouseover(function(){$(this).css("background", "#ffb98e");});
      $(".opt_highlight").mouseout(function(){$(this).css("background", "white");});
        
      $(".login_button").mouseover(function(){$(".login_button").css("background-image", "url('/img/login_a.gif')");});     
      $(".login_button").mouseout(function(){$(".login_button").css("background-image", "url('/img/login.gif')");});
        
      $(".logout_button").mouseover(function(){$(".logout_button").css("background-image", "url('/img/logout_a.png')");});     
      $(".logout_button").mouseout(function(){$(".logout_button").css("background-image", "url('/img/logout.png')");}); 
        
      $(".enter_button").mouseover(function(){$(".enter_button").attr({"src":"/img/enter_button_a.png"});});
      $(".enter_button").mouseout(function(){$(".enter_button").attr({"src":"/img/enter_button.png"});});
        
      $(".singup_top_button").mouseover(function(){$(".singup_top_button").css("background-image", "url('/img/singup_a.gif')");});
      $(".singup_top_button").mouseout(function(){$(".singup_top_button").css("background-image", "url('/img/singup.gif')");});
      $(".singup_top_button").click(function(){document.location = 'http://brainfuckr.ru/signup/';});
      $(".logout_button").click(function(){document.location = 'http://brainfuckr.ru/logout/';});
    });