/***
 * akce provedene bezprostredne po nacteni
 */
window.onload=function() {
  Nifty("div.label","small");  
  onclickclear('text-search-id');
}
 

/**
 *  aktivace zalozek s akcemi
 */ 
$(document).ready(function() {
	 $(".but-show").click(function () {
	   $(this).hide();
	   $(this).parent().parent().find('.tabedit').show();
	   return false;
  });
  
  $(".tabedit").tabs();
});


/**
 *  aktivace zavirani zalozek
 */ 
$(document).ready(function() {

		var tlacitko = $("#tabsGallery").find('a.but-close');
		$('.but-close').click(function () {
		  var parent = $(this).parent();
		  parent.hide();
		  $(parent).parent().find('.but-show').show();
		  return false;
    });
		
});

/**
 * oznaceni vsech checkboxu
 */ 
$(document).ready(function() {
    $("a.checkAll").click(function () {
		  $(".checkbox").attr("checked",true);
		  return false;
    });

    $("a.uncheckAll").click(function () {
		  $(".checkbox").attr("checked",false);
		  return false;
    });
});


/**
 * okna
 */ 
$(document).ready(function() {
  $('#clicktosubmit').click( function() {
	   Boxy.confirm("Opravdu si přejete provést smazání?", function() { var el = $("#autosubmit"); el.trigger('click'); }, {title: 'Smazat'});	   
     return false;
  });


	$("a.slidevideoshow").click(function () { 
    var url = $(this).attr('href');
    
    mycontent='<iframe border=0 src="'+url+'"></iframe>'; 
    new Boxy( mycontent, { unloadOnHide: true, draggable: false, 
                           show: true, modal: true, 
                           title: "Video", closeText:"<strong>[ Zavřít ]</strong>", 
                           afterShow: function() { this.resize(450,420); } } );
    return false;
  });


	$("a.slideshow").click(function () { //slideshow window 
    var url = $(this).attr('href');
    
    mycontent='<iframe border=0 src="'+url+'"></iframe>'; 
    new Boxy( mycontent, { unloadOnHide: true, draggable: false, y: 10, 
                           show: true, modal: true, fixed: false, clickToFront: false,
                           title: "<span style='font-size: 1.5em'>Slideshow</span>", closeText:"<strong>[ Zavřít ]</strong>", 
                           afterShow: function() { this.resize(1030,775); } } );
    return false;
  });


	$("a.spam").click(function () {  //spam window 
    var url = $(this).attr('href');
    
    mycontent='<iframe border=0 src="'+url+'"></iframe>'; 
    new Boxy( mycontent, { unloadOnHide: true, draggable: false, 
                           show: true, modal: true, 
                           title: "SPAM", closeText:"<strong>[ Zavřít ]</strong>", 
                           afterShow: function() { this.resize(350,400); } } );
    return false;
  });


	$("a.improve").click(function () { //improve window 
    var url = $(this).attr('href');
    
    mycontent='<iframe border=0 src="'+url+'"></iframe>'; 
    new Boxy( mycontent, { unloadOnHide: true, draggable: false, 
                           show: true, modal: true, 
                           title: "Ohlášení", closeText:"<strong>[ Zavřít ]</strong>", 
                           afterShow: function() { this.resize(340,360); } } );
    return false;
  });

  $("a.cropimage").click(function () { //improve window 
    var url = $(this).attr('href');
    
    mycontent='<iframe border=0 src="'+url+'"></iframe>'; 
    new Boxy( mycontent, { unloadOnHide: true, draggable: false, 
                           show: true, modal: true, center: true,
                           actuator: $(this),
                           title: "Vyřezání obrázku", closeText:"<strong>[ Zavřít ]</strong>", 
                           afterShow: function() { this.resize(700,550); } } );
    return false;
  });


  $("a.remove").click(function () {
      var t = $(this);  
      var url = t.attr("href");
      
      Boxy.confirm("Opravdu si přejete provést smazání?", function() { location.replace(url); }, {title: 'Smazat'});
      return false;
  });

    
  $("a.leaveMessage").click(function () {
      var t = $(this);  
      var url = t.attr("href");
      
      Boxy.confirm("Opravdu si přejete odejít ze skupiny?", function() { location.replace(url); }, {title: 'Odejít?'});
      return false;
  });
});

function triggerClose() {  
  var el = $(".boxy-inner").find('a.close');
  el.trigger('click');
}
function autoClose(){
    setTimeout("triggerClose()",2000);
}


/*
function showboxy(myactuator) {
  mycontent='<iframe border=0 src="mycontentfortheiframe.php">You need a Browser which can display iframes</iframe>';
  new Boxy( mycontent, { unloadOnHide: true, draggable: false, show: true, modal: true, title:  "Replaceme!", closeText:"[ Close ]", actuator: $('#'+myactuator)[0]} );
}
*/
                            /*
function cropWindow(elem) {
    var url = $(elem).attr('href');
    var mycontent='<iframe border=0 src="'+url+'"></iframe>';
     
    new Boxy( mycontent, { unloadOnHide: true, draggable: false, 
                           show: true, modal: true, 
                           title: "", closeText:"<strong>[ Zavřít ]</strong>", 
                           afterShow: function() { this.resize(700,550); } } );
   return false;
};
*/

/**
 * zobrazeni pouze viditelnych skupin
 */ 
$(document).ready(function()	{
  var actual = $('#actualgrouplist').html();
  $(".page-"+actual).show();
});


$(document).ready(function()	{
	
	$('#showLeftGroup').click(function () {
  	   var actual = $('#actualgrouplist').html();
  	   actual--;
  	   
       if(!actual) return false;
  	   
  	   $("#actualgrouplist").html(actual);
  	   $(".hideGroup").hide();
  	   $(".page-"+actual).show();
  	   
  	   return false;
  });
	
	$('#showRightGroup').click(function () {
  	   var actual = $('#actualgrouplist').html();
  	   var total = $('#totalgrouplist').html();
       actual++;
  	   
       if(actual>total) return false;
  	   
       $("#actualgrouplist").html(actual);
  	   $(".hideGroup").hide();
       $(".page-"+actual).show();
  	   
       return false;
  });
});




/**
 * nastaveni smazani pole
 */ 
function onclickclear(identif) {
   var ele = document.getElementById(identif);
   if(ele!=null)
      ele.onclick = function() { this.value=""; }; 
}

function gohref(checkbox,odkaz) {
  if(checkbox.checked) {
    location.replace(odkaz+'&value=1');
  }
  else {
    location.replace(odkaz+'&value=0');
  }
}


$(document).ready(function () {
    $('#username').keyup(function(){ $('#result').html( passwordStrength($('#password').val(),$('#username').val()) )} )
		$('#password').keyup(function(){ $('#result').html( passwordStrength($('#password').val(),$('#username').val()) )} )
			
});
	
$(document).ready(function () {
        // Username validation logic
        var validateUsername = $('#validateUsername');
        $('#username').keyup(function () {
            var t = this; 
            
            if (this.value != this.lastValue) {
                                
                if (this.timer) clearTimeout(this.timer);
                
                if ((t.value).length<3) {
                  validateUsername.removeClass("good");
                  validateUsername.removeClass("wrong");
                            
                  validateUsername.addClass('wrong').html('přezdívka je příliš krátká');
                  return;
                }
                
                validateUsername.removeClass('error').html('<img src="/img/ajax-loader.gif" height="16" width="16" /> kontroluji dostupnost...');
                
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'validation.php',
                        data: 'action=check_username&username=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            validateUsername.removeClass("good");
                            validateUsername.removeClass("wrong");
                            
                            validateUsername.addClass(j.state);
                            validateUsername.html(j.msg);
                        }
                    });
                }, 200);
                this.lastValue = this.value;
            }
        });
});



  $(document).ready(function () {
        // Username validation logic
        var validateUsername = $('#saveRubrika');
        $('#saveRubrika').click(function () {
            var t = this; 
            if (this.value != this.lastValue) {
                
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'validation.php',
                        data: 'action=check_username&username=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            validateUsername.html(j.msg);
                        }
                    });
                }, 200);
                this.lastValue = this.value;
            }
        });
  });
    



/**
 * count commentlimit
 */ 
$(document).ready(function()	{
  $('.commentlimit').keyup(function(){
    countcomentlimit( $(this) );
  })

  $('.textlimit').keyup(function(){
   	counttextlimit( $(this) );
  })
});

function countcomentlimit(elem) {
   	var commentlimitinfo = $(elem).parent().parent().parent().parent().find('.commentlimitinfo');
   	limitChars($(elem), 200, commentlimitinfo);
}

function counttextlimit(elem) {
    var textlimitinfo = $(elem).parent().parent().parent().parent().find('.textlimitinfo');
    limitChars( $(elem) , 3000, textlimitinfo );  
}

function limitChars(textid, limit, infodiv) {
  	var text = $(textid).val();	
  	var textlength = text.length;
  	
  	if(textlength > limit) {
  		$(infodiv).html('Překročen rozsah '+limit+' znaků!');
  		$(textid).val(text.substr(0,limit));
  		return false;
  	}
  	else {
  		setznaku(infodiv,textlength,limit);
  		return true;
  	}
}

function setznaku(obj, len, limit) {
  $(obj).html('znaků: '+ len +' / '+limit);
}
/******************************************************/



function markup(element,type) {
    if(type=='')
      type = 'marktop';
    $(element).markItUp(mySettings,type);
}

  
$(document).ready(function()	{
	// Add markItUp! to your textarea in one line
	// $('textarea').markItUp( { Settings }, { OptionalExtraSettings } );
  markup( $('.topmarkup') , 'markuptop' );
  markup( $('.downmarkup') , 'markupdown' );
	
	// You can add content from anywhere in your page
	// $.markItUp( { Settings } );	
	$('.add').click(function() {
 		$.markItUp( { 	openWith:'<opening tag>',
						closeWith:'<\/closing tag>',
						placeHolder:"New content"
					}
				);
 		return false;
	});
	
	// And you can add/remove markItUp! whenever you want
	// $(textarea).markItUpRemove();
	$('.toggle').click(function() {
		if ($(".texteditor.markItUpEditor").length === 1) {
 			$(".texteditor").markItUpRemove();
			$("span", this).text("get markItUp! back");
		} else {
			$('.texteditor').markItUp(mySettings);
			$("span", this).text("remove markItUp!");
		}
 		return false;
	});
	
});  


/**
 * source 
 * http://www.somacon.com/p355.php
 */ 
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

/**
 * dialog box
 */ 
$(document).ready(function() {
		$("#dialog").dialog({
			bgiframe: true,
			draggable: false,
			width: 500,
			modal: true,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});    
    
    setTimeout("keepConnection()",keepInterval);
});

/**
 * udrzovani spojeni
 */ 
var keepInterval = 500000;  
function keepConnection() {
  $.post("/?I=keepconnection", 1, function(){ });
  setTimeout("keepConnection()",keepInterval);
}

$(document).ready(function() {
  $('.remove-avatar').click( function() {
    removeAvatar($(this));
    return false;
  });
});

function removeAvatar(elem) {
  Boxy.confirm("Opravdu si přejete provést smazání?", function() { $(elem).parent().html('<small class="bold">Aby byl avatar úplně smazán, musíte dole kliknout na uložit&nbsp;změny!</small><input type="hidden" name="delete_avatar" value="1" />'); }, {title: 'Smazat'});
  return false;
}  
