$(document).ready(function(){

 
  //$(".content_overflow").css({"overflow": "hidden"});

  $(".openpage").live("click",function(){
    $(".content_in").load("load.php"+$(this).attr("href"));
    return false;
  });

  $(".datepicker").datepicker({
    dateFormat: 'yy-mm-dd',
    showAnim: "",
    onSelect: function() {this.form.submit();}
  });
  
  
  //přepis časů ve formuláři do formátu
  $('.form_time').keyup(function(){
    var val=$(this).attr('id');
    var h=$('#'+val+' .h').val()*1;
    var m=$('#'+val+' .m').val()*1;
    var s=$('#'+val+' .s').val()*1;
    $('.'+val).val(h+':'+m+':'+s);
  });
  
  //přepis časů ve formuláři na minuty
  $('.form_min').keyup(function(){
    var val=$(this).attr('id');
    var h=$('#'+val+' .h').val()*1*60;
    var m=$('#'+val+' .m').val()*1;
    var s=$('#'+val+' .s').val()*1/60;
    var hodn=Number(h+m+s);
    $('.'+val).val(hodn);
  });
  
  //kopírování řádku tabulky do "schránky"
  $(".copy_tbl").livequery(function(){
    $(this).click(function(){
      var id=$(this).attr("id");
      var tbl=$(this).attr("rel");
      $(this).addClass("copy");
      $.get('tbl/scripts/copy.php', { tbl: tbl, id: id }, function(data){
        if(data=="1"){
          $(".copy").append("<b class=\"copy_ok\">&nbsp;&nbsp;OK</b>");
          $(".copy").removeClass("copy");
          $(".copy_ok").fadeOut("slow");
          $(".paste_tbl").fadeIn("slow");
        }
      });
      return false;
    });
  });
  
  $(".copytbl").livequery(function(){
    $(this).click(function(){
      var id=$(this).attr("id");
      var tbl=$(this).attr("rel");
      var datum=$(this).attr("name");
      $(this).addClass("copy");
      $.get('tbl/scripts/kopirovat.php', { tbl: tbl, id: id, datum: datum }, function(data){
        if(data=="1"){
          $(".copy").append("<b class=\"copy_ok\">&nbsp;&nbsp;OK</b>");
          $(".copy").removeClass("copy");
          $(".copy_ok").fadeOut("slow");
          $(".paste_tbl").fadeIn("slow");
        }
      });
      return false;
    });
  });
  
  
  
  
  $(".ookno").live("click",function(){
        var cl=$(this).attr("class");
        var param=cl.split(" ");
        var size=param[1].split("x");
        var href=$(this).attr("href");
        var nick=$(this).attr("name");
        var okno=window.open(href, nick, 'width='+size[0]+', height='+size[1]+', resizable=yes, scrollbars=yes');
        vyska = screen.height;
        sirka = screen.width;
        var levy = (sirka - size[0])/2;
        var horni = (vyska - size[1])/2;
        okno.moveTo(levy, horni);
        okno.focus();
        return false;
  });


  if($('.denik').width()>$(".content_overflow").width()){
    
    //$(".content_overflow").scrollLeft($.cookie("left"));
    
    $(".denik th, .tr_filtr td").livequery(function(){
	    $(this).css({
		   "cursor": "move"
	    });
    });
   
    
    $('.denik')
    .bind('dragstart',function(e){
            $.cookie("left",e.pageX+$(".content_overflow").scrollLeft());
            return $(e.target).is('.denik th, .tr_filtr td');
    })
    .bind('drag', function(e){
      $(".box").text("");
      var left=$.cookie("left")-e.pageX;
      $(".content_overflow").scrollLeft(left);
    });
    
    
  };
  
  
  
  $(".hover").hover(function(){
    var id="#bub_"+$(this).attr("id");
    var str=$(id).html().replace(/<h6>/g,'').replace(/<\/h6>/g,'\n');
    $(this).attr("title",str);
  });


    function countr(name){
      var n="a[name='"+name+"']";
      if($(n).text()!=""){
        var id=$(n).attr("id");
        var pct=n+" > span";
        var res=id.split('?');
    		if(res[0]==""){
    			var url="l.php"+id+"/count&pocet=1";
    		} else {
    			var url=res[0]+"?"+res[1]+"/count&pocet=1";
    		}
        $.get(url, function(vystup){
          if(vystup!=0){
            $(pct).html(vystup);
          } else {
            $(pct).text("");
          }
          return vystup;
        });
      }
    }
    
    countr("lide");
    countr("vzkazy");
    countr("aktivity");
    countr("user/online");
    //countr("plan/klienti");
    
    
    
    $(".top, .content, .pata, .reklamy").click(function(){
      $(".box").hide();
    });
       
    
    zavreni=setTimeout('$(".box").fadeOut()',1000);
   
    $(".box").hover(
      function(){
        clearTimeout(zavreni);
        //nic
      },
      function(){
        zavreni=setTimeout('$(".box").hide()',1000);
      }
    );
    
    
  //avatar
  $(".contentNickAvatar").prependTo($(".content")).show();
  //
  
  //box vše
  $('.openbox').live("mouseenter",function() {
      
      $(".openbox").removeClass("openerbox");
      $(this).addClass("openerbox");
      
      if($(this).attr("name")){
        	countr($(this).attr("name"));
      }
      
      $(".box").css({
          "margin": ""
      });
      $(".box").hide();
      $(".box").text("");
      $(this).attr("title","");
      
      
    	$(".box").html("<div class=\"box_in\">načítání...</div>");
   	
    	var offset = $(this).offset();
    	var leftVal = offset.left + $(this).width() + 0;
    	var topVal = offset.top + $(this).height() + 0;
    	if($(this).hasClass("lft")){
    		var leftVal = offset.left - $(".box").width() - 0;
    		var topVal = offset.top + $(this).height() + 0;
    	} 
      if($(this).hasClass("cntr")) {
    		var leftVal = offset.left - Math.round((($(".box").width()-$(this).width())/2));
    	}
    	if(leftVal + $(".box").width() + 0 - $(document).scrollLeft() > $(window).width()){
    		var leftVal=offset.left - 0 - $(".box").width();
    	}
    	if(topVal + $('.box').height() + 0 - $(document).scrollTop() > $(window).height()){
    		var topVal=offset.top - 0 - $('.box').height();

    	}
    	if(leftVal < 0){var leftVal = 0 + $(document).scrollLeft();}
    	if(topVal < 0){var topVal = 0 + $(document).scrollTop();}
    	var leftVal = leftVal + "px";
    	var topVal = topVal + "px";
    	$(".box").css({
    	  "left": leftVal, 
    	  "top": topVal
    	});
    	
    	if($(this).attr("id")){
    	
    		//var url="load.php"+$(this).attr("id")+"/pgl";
    	
    		var vysl=$(this).attr("id").split('?');
    		if(vysl[0]==""){
    			var url="load.php?"+vysl[1]+"/pgl";
    		} else {
    			var url=$(this).attr("id")+"/pgl";
    		}
    		
    		//$("body").prepend(url);       
	
        $(".box_in").load(url,function(){

    		var offset = $(".openerbox").offset();
    		var leftVal = offset.left + $(".openerbox").width() + 0;
    		var topVal = offset.top + $(".openerbox").height() + 0;
    		if($(".openerbox").hasClass("lft")){
    			var leftVal = offset.left - $(".box").width() + 0;
    			var topVal = offset.top + $(".openerbox").height() + 0;
    		} 
		    if($(".openerbox").hasClass("cntr")) {
	      		var leftVal = offset.left - Math.round((($(".box").width()-$(".openerbox").width())/2));
	      }
    		if(leftVal + $(".box").width() + 0 - $(document).scrollLeft() > $(window).width()){
    			var leftVal=offset.left - 0 - $(".box").width();
    		}
    		if(topVal + $('.box').height() + 0 - $(document).scrollTop() > $(window).height()){
    			var topVal=offset.top - 0 - $('.box').height();
    		}
    		if(leftVal < 0){var leftVal = 0 + $(document).scrollLeft();}
    	  	if(topVal < 0){var topVal = 0 + $(document).scrollTop();}
    		var leftVal = leftVal + "px";
    		var topVal = topVal + "px";
    		$(".box").css({
    		  "left": leftVal, 
    		  "top": topVal
    		});
    		$(".box *").removeClass("openbox");
    		
    	});
    	
    	otevreni=setTimeout('$(".box").show()',500);
    	
    	} else {
        var alt=$(this).attr("alt");
        //$("#"+alt).hide();
        $(".box_in").html("<ul>"+$("#pod_"+alt).html()+"</ul>").addClass("boxin").addClass("denik_nastroje");
        $(".box").css({
          "margin": "-7px 0px 0px -5px"
        });
        
        
        otevreni=setTimeout('$(".box").show()',0);
      
      }
	
    	clearTimeout(zavreni);
	   

  }); 
  
  $('.openbox').live("mouseleave",function() {
    		clearTimeout(otevreni);
        zavreni=setTimeout('$(".box").hide()',1000);
  });
  
      
    //přečteno vzkazy        
    $(".prct").live("click", function(){
      var id=$(this).attr("id");
      $.post("vzkazy/vzkazy/scripts/precteno.php",{id: id}, function(vystup){
        if(vystup==1){
          countr('udalosti');
          $(".vzkaz"+id).fadeOut();
          $(".prispevek"+id).css({"font-weight":"normal"});
        }
      });
      return false;
    });
    

  //vyčistit návštevy
  $(".vycistit_navstevy").live("click",function(){
      var rel=$(this).attr("rel");
      $.post("setting/scripts/set_time.php", {funkce: rel},function(){
        $(".chat .navstevy").fadeOut();
      });

      return false;
    });


    //vyčistit události
    $(".vycistit").live("click",function(){
      var name=$(this).attr("name");
      var rel=$(this).attr("rel");
      $.post("setting/scripts/set_time.php", {funkce: rel},function(){
        $("."+name).fadeOut();
        countr('aktivity');
      });
      return false;
    });
    
    
    //hledání
    $(".usersearch").live("click",function(){
  	      $(".divsearch").load("load.php?a=user/lide/search&aa="+aa);
  	});
  	$(".usersearch").live("keyup",function(){
  	      var s=$(this).val();
  	      $.cookie('s',s);
  	      $(".divsearch").load("load.php?a=user/lide/search&aa="+aa);
  	});
  	
  	
  	$(".search-user").livequery(function(){
			$(this).keyup(function(){
				$(".box").load("load.php"+$(this).attr('id')+"/pgl&search="+search_translate($(this).val()));
			});
			$(this).hover(function(){
				$(".box").load("load.php"+$(this).attr('id')+"/pgl&search="+search_translate($(this).val()));
			},
			function(){
				$(".box").text();
			});
		});
		
		
		//search
		  $("body").append("<div class='search-box'></div>");
      $("input[name='search-input'], .search-input").livequery(function(){
        $(this).click(function(){
          if($(this).val()!=""){
            $(".search-box").show();
          } else {
            $(".search-box").text("").hide();
          }
        });
        $(this).keyup(function(){
          if($(this).val()!=""){
            var position=$(this).offset();
            $(".search-box")
              .show()
              .load($(this).attr("rel")+"&search="+search_translate($(this).val())+"&href="+$(this).attr("id"))
              .css({
                top: position.top+$(this).height()+5, //5 - jeste kousek dolu
                left: position.left
              });
           } else {
             $(".search-box").hide();
           }
        });
      });
      $("*").click(function(){
        $(".search-box").hide();
      });
      
  	

 	

  	$('.blog_text img, .blog_text object, .blog_text embed').each(function() {
      var maxWidth = 570; // Max width for the image
      //var maxHeight = 400;    // Max height for the image
      var ratio = 0;  // Used for aspect ratio
      var width = $(this).width();    // Current image width
      var height = $(this).height();  // Current image height
  		
      // Check if the current width is larger than the max
      if(width > maxWidth){
          ratio = maxWidth / width;   // get ratio for scaling image
          $(this).css("width", maxWidth); // Set new width
          $(this).css("height", height * ratio);  // Scale height based on ratio
          height = height * ratio;    // Reset height to match scaled image
      }
  
      // Check if current height is larger than max
      /*if(height > maxHeight){
          ratio = maxHeight / height; // get ratio for scaling image
          $(this).css("height", maxHeight);   // Set new height
          $(this).css("width", width * ratio);    // Scale width based on ratio
          width = width * ratio;    // Reset width to match scaled image
      }*/
    });
    

			//$(".load_aktivity li a").livequery(function(){
				$(".load_aktivity li a").live("click",function(){
					var rel=$(this).attr('rel');
					var url="load.php"+$(this).attr('id');
					$(".aktivity_l:last").after("<div class=\"aktivity_l\"></div>");
					$(this).text('načítá se...');
					$(".aktivity_l:last").load(url, function(){
						$(".load_aktivity").hide();
						$(".load_aktivity:last").show();
					});
					return false;
				});
			//});
    
});


