$(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").live("click",function(){
    var id=$(this).attr("id");
    var tbl=$(this).attr("rel");
    $.get('tbl/scripts/copy.php', { tbl: tbl, id: id }, function(data){
      if(data=="1"){
        $("#"+id).append("<b class=\"copy_ok\">&nbsp;&nbsp;OK</b>");
        $(".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";
        $.get("load.php"+id+"/count&pocet=1", function(vystup){
          if(vystup!=0){
            $(pct).html(" "+vystup+"");
          } else {
            $(pct).text("");
          }
          return vystup;
        });
      }
    }
    
    countr("lide");
    countr("user/online");
    countr("vzkazy");
    countr("upozorneni/aktivity");
    
    $(".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').livequery(function(){ 
    $(this).hover(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() + 5;
    	var topVal = offset.top + $(this).height() + 5;
    	if($(this).hasClass("lft")){
    		var leftVal = offset.left - $(".box").width() - 5;
    		var topVal = offset.top + $(this).height() + 5;
    	} 
      if($(this).hasClass("cntr")) {
    		var leftVal = offset.left - Math.round((($(".box").width()-$(this).width())/2));
    	}
    	if(leftVal + $(".box").width() + 5 - $(document).scrollLeft() > $(window).width()){
    		var leftVal=offset.left - 5 - $(".box").width();
    	}
    	if(topVal + $('.box').height() + 5 - $(document).scrollTop() > $(window).height()){
    		var topVal=offset.top - 5 - $('.box').height();

    	}
    	if(leftVal < 5){var leftVal = 5 + $(document).scrollLeft();}
    	if(topVal < 5){var topVal = 5 + $(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";
	
        $(".box_in").load(url,function(){

    		var offset = $(".openerbox").offset();
    		var leftVal = offset.left + $(".openerbox").width() + 5;
    		var topVal = offset.top + $(".openerbox").height() + 5;
    		if($(".openerbox").hasClass("lft")){
    			var leftVal = offset.left - $(".box").width() + 5;
    			var topVal = offset.top + $(".openerbox").height() + 5;
    		} 
		    if($(".openerbox").hasClass("cntr")) {
	      		var leftVal = offset.left - Math.round((($(".box").width()-$(".openerbox").width())/2));
	      }
    		if(leftVal + $(".box").width() + 5 - $(document).scrollLeft() > $(window).width()){
    			var leftVal=offset.left - 5 - $(".box").width();
    		}
    		if(topVal + $('.box').height() + 5 - $(document).scrollTop() > $(window).height()){
    			var topVal=offset.top - 5 - $('.box').height();
    		}
    		if(leftVal < 5){var leftVal = 5 + $(document).scrollLeft();}
    	  	if(topVal < 5){var topVal = 5 + $(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);
	   
	   
    }, function(){
      
        clearTimeout(otevreni);
        zavreni=setTimeout('$(".box").hide()',1000);

    });
  }, function() { 
    // unbind the mouseover and mouseout events 
    $(this) 
      .unbind('mouseover') 
      .unbind('mouseout'); 
  }); 
  
      
    //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('upozorneni/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);
  	});
  	
 	

  	$('.blog_text img, .contentLeft img, .contentLeft object, .contentLeft embed').each(function() {
      var maxWidth = 600; // 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
      }*/
    });
    
});

