// inicializace
$("#dialog").dialog("destroy");

$(document).ready(function() {	    
    // dialog pro zasilani SMS
    $('a.message-send').click(function(){
        if ($(this).attr('rel')) {
            // jednoducha zprava
            var tokens = $(this).attr('rel').split(';');
            $('strong#recipient').html(tokens[1]);
            $('span#phoneText').html(tokens[0]);
            $('input#phone').val(tokens[0]);
        }
        else {
            // hromadna zprava
            var phoneList = new Array();
            // ziskam zaskrtnute klienty (telefony)
            $('input[type=checkbox]:checked').each(function(){
                phoneList.push($(this).val());
            });
            if (phoneList) {
                $('strong#recipient').html('hromadná zpráva');
                $('span#phoneText').html('hromadná zpráva');
                $('input#phone').val(phoneList.join(';'));
            }
        }
        
    		$("#message-send").dialog({
    		  width: 450,
    			modal: true,
    			buttons: {
    				Zavrit: function() {
    					$(this).dialog('close');
    					$('strong#recipient').html('');
              $('input#phone').val('');
              $('#message-text').val('');
    				},
    				Odeslat: function() {
    				  var dialog = $(this);
    				  $('p#sending').css('visibility', '');
    					$.get('/ajax/sms-send', {recipient: $('input#phone').val(), text: $('#message-text').val()}, function(data) {
                  // schovam okno
                  dialog.dialog('close');
                  // potvrzovaci hlaska
                  alert('Zpráva pro klienta "'+$('strong#recipient').html()+'" byla odeslána.');
                  // nastavim defaultni popisky
                  $('strong#recipient').html('');
                  $('input#phone').val('');
                  $('#message-text').val('');
                  $('p#sending').css('visibility', 'hidden');
              });  
    				}
    			}
    		});
    });
    
    // zaskrtavani
    function highlightCheckbox(checkbox) {
        if (checkbox.is(':checked')) {
            checkbox.parent().parent().addClass("highlight");
        } else  {
            checkbox.parent().parent().removeClass("highlight");
        }
    }
    $('a.check-all').click(function(){
        $('input[type=checkbox]').each(function(){
            $(this).attr('checked', !$(this).is(':checked'));
            highlightCheckbox($(this));
        });
    });
    $('input[type=checkbox]').bind('change', function(){
        highlightCheckbox($(this));
    });
});

// ---

