

$(document).ready(function () {
    $("#recipient").tokenInput("/?B=checkSender", {
            hintText: "Vložte jméno příjemce",
            noResultsText: "Žádné výsledky",
            searchingText: "Hledám...",
            prePopulate: getPrepopulate(),
            classes: {
                tokenList: "token-input-list-facebook",
                token: "token-input-token-facebook",
                tokenDelete: "token-input-delete-token-facebook",
                selectedToken: "token-input-selected-token-facebook",
                highlightedToken: "token-input-highlighted-token-facebook",
                dropdown: "token-input-dropdown-facebook",
                dropdownItem: "token-input-dropdown-item-facebook",
                dropdownItem2: "token-input-dropdown-item2-facebook",
                selectedDropdownItem: "token-input-selected-dropdown-item-facebook",
                inputToken: "token-input-input-token-facebook"
            }
        });
});

function getPrepopulate() {
  var senders = $('#senders').find(':hidden');
  var celkem = senders.length;
  if(celkem) {
    var output = new Array();
    var hodnota,nazev;
    for(var i=0;i<celkem;i++) {
      output.push({id: $(senders[i]).attr("value"), name: $(senders[i]).attr("name")}); 
    }
  }

  return output;
}

/*
$(document).ready(function() {
		
		var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}
		
		$("#dialogAddToPost").dialog({
		
			bgiframe: true,
			autoOpen: false,
			height: 370,
			width: 320,
      modal: true,
			buttons: {
				'Přidat příjemce': function() {
				  
				
					var hodnota = $('#senders').html();
		      var type = $('#addTypeFind').val();
          
          var foo = [];
          var hodnota = $('#names :selected').each(function(i, selected){ foo[i] = $(selected).text();});
          var old = $('#senders').html();
          var remove = '<a href="#" onClick="javascript: smaz(this)" class="ico-delete">,</a>';
          
          var infotext = hodnota.text().split('(')[0];
          var identif = parseInt(hodnota.val());
          
          //pokud neni nic oznaceno
          if(!identif ) return;
          
          switch(type) {
            case "profil":
              type='profil'; break;
            default:
              type='group'; break;
          }
          
          if(existSender(identif,type)) 
            var vysledek = window.confirm('Tento příjemce již byl vložen, přejete si jej vložit znovu?'); 
          
          if(vysledek==0)
            return;
          
          switch(type) {
            case "profil":
              $('#senders').html(old+'<span><input type="hidden" id="profil'+identif+'" name="profil[]" value="'+identif+'" /> '+infotext+remove+'</span>' );
            break;
            default:
              $('#senders').html(old+'<span><input type="hidden" id="group'+identif+'" name="group[]" value="'+identif+'" /><strong>'+infotext+'</strong>'+remove+'</span>' );
					}
					
					
				},
				'Zavřít': function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		$('#create-user').click(function() {
			$('#dialogAddToPost').dialog('open');
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});

});

function existSender(hodnota,type) {
  var element = $('#senders').find('#'+type+'_'+hodnota).size(); // .attr('name',type+'_'+hodnota)
  return element;
}


function smaz(element){
  var hodnota = element.parentNode;
  hodnota.parentNode.removeChild(hodnota);
}
*/

/**
 * hledani mist
 */ 
/*
$(document).ready(function () {        
        var validateGroupname = $('#validateName');
        var misto = $('#names');

        $('#searchSenderName').keyup(function () {
            var t = this; 
            var type = $('#addTypeFind').val(); //typ prijemce (profil/skupina)

            
            if (this.value != this.lastValue) {
                               
                if (this.timer) clearTimeout(this.timer);
                if ((t.value).length<3) { return;}
                
                validateGroupname.removeClass('error').html('<img src="/img/ajax-loader.gif" height="16" width="16" />hled');
                
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: '/?B=checkSender',
                        data: 'name=' + t.value+'&type='+type,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {

                            validateGroupname.removeClass('error').html('');
                            misto.find('option').remove();

                            for (i=0; i<j.msg.length ; i++)  {
                               misto.append('<option value="'+j.msg[i].id+'">'+j.msg[i].name+'</option>');
                            }
                        }
                    });
                }, 200);
                this.lastValue = this.value;   
            }
        });
});
*/

/*

    //prevod zdorjoveho kodu nahledu
    //do vysledne pozice
    $(document).ready(function () {
        $('.sourceNahled').keyup(function () {
            
            var t = this; 
            
            if (this.value != this.lastValue ) {
                                
                if (this.timer) clearTimeout(this.timer);
                
                this.timer = setTimeout(function () {
                    
                   $.ajax({
                        url: '?B=convertEditorToHTML',
                        data: 'value='+t.value,
                        dataType: 'text',
                        type: 'post',
                        success: function (j) {
                            $('#nahlededitor').html(j);
                        },
                        error: function(j) {
                            $('#nahlededitor').html(error);
                        }
                    });
                }, 200);
                this.lastValue = this.value;
            }
        });
    });
 */

