    function resetOverlays()
    {
      var dialogs = $("div.ui-dialog");
      if(dialogs.length == 0)
      {
        $(".ui-widget-overlay").remove();
      }
    }

// ###########################################################################  
  function sdl_alert(text){
    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: "index.php",
      data: {ajax:'true', msg:'alert', text: text},
      success: function(data){
        if($('#sdl_alert').length) $('#sdl_alert').remove();
        sdl_odpoved(data);
      },
      error:function (xhr, ajaxOptions, thrownError){
      	alert('AJAX error:\n'+xhr.status+'\n'+thrownError+'\n'+ajaxOptions);
      }   
    });     
  }
// ###########################################################################
  function sdl_confirm(text, req, rel, win, special){
   //alert('req=['+req+'], rel=['+rel+'], win=['+win+'], special=['+special+']');
  //return false;
    $.ajax({
      type: 'POST',
      dataType: 'json',
      url: "index.php",
      data: {ajax:'true', msg:'confirm', req: req, rel: rel, win: win, text: text, special: special},
      success: function(data){
        if($('#sdl_confirm').length) $('#sdl_confirm').remove();
        sdl_odpoved(data);
      },
      error:function (xhr, ajaxOptions, thrownError){
      	alert('AJAX error:\n'+xhr.status+'\n'+thrownError+'\n'+ajaxOptions);
      }   
    });     
  }
// ###########################################################################
    function  click(req, rel, win, special) {
     // alert('req=['+req+'], rel=['+rel+'], win=['+win+'], special=['+special+']');
      $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "index.php",
        data: {ajax:'true', msg:'form', req: req, md5id: rel, win: win, special: special},
        success: function(data){
          sdl_odpoved(data);
        },
        error:function (xhr, ajaxOptions, thrownError){
        	alert('AJAX error:\n'+xhr.status+'\n'+thrownError+'\n'+ajaxOptions);
        }   
     });    
    }
// ###########################################################################
    function  click_popup(req, rel, win, special) {
     // alert('req=['+req+'], rel=['+rel+'], win=['+win+']');
      $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "index.php",
        data: {ajax:'true', msg:'popup', req: req, md5id: rel, win: win, special: special},
        success: function(data){
          sdl_odpoved(data);
        },
        error:function (xhr, ajaxOptions, thrownError){
        	alert('AJAX error:\n'+xhr.status+'\n'+thrownError+'\n'+ajaxOptions);
        }   
     });    
    }
// ###########################################################################


  function sdl_js_init(){
    //$(".video").colorbox({transition:"fade", iframe:true, innerWidth:800, innerHeight:600});
    $(".video").colorbox({transition:"fade", iframe:true, innerWidth:600, innerHeight:450});
    $("a[rel='produkt']").colorbox({transition:"fade",width:"75%", height:"50%"}); 
    $("a[rel='lightview']").colorbox({transition:"fade",width:"75%", height:"50%"}); 
  
    $(".button").button({
      //icons: { primary: 'ui-icon-locked'}
      
    });
    $(".icobutton").button({
      text: false
    });

    
    $( ".icon-login" ).button( "option", "icons", {primary:'ui-icon-locked',secondary:''} );
    $( ".icon-logout" ).button( "option", "icons", {primary:'ui-icon-unlocked',secondary:''} );
    $( ".icon-edit" ).button( "option", "icons", {primary:'ui-icon-wrench',secondary:''} );
    $( ".icon-add" ).button( "option", "icons", {primary:'ui-icon-plusthick',secondary:''} );
    $( ".icon-del" ).button( "option", "icons", {primary:'ui-icon-closethick',secondary:''} );
    $( ".icon-ok" ).button( "option", "icons", {primary:'ui-icon-check',secondary:''} );
    $( ".icon-view" ).button( "option", "icons", {primary:'ui-icon-document',secondary:''} );
    $( ".icon-kosik" ).button( "option", "icons", {primary:'ui-icon-cart',secondary:''} );
    $( ".icon-bulb" ).button( "option", "icons", {primary:'ui-icon-lightbulb',secondary:''} );
    $( ".icon-pick" ).button( "option", "icons", {primary:'ui-icon-pin-s',secondary:''} );
    $( ".icon-sub" ).button( "option", "icons", {primary:'ui-icon-arrowthickstop-1-s',secondary:''} );
    $( ".icon-pis" ).button( "option", "icons", {primary:'ui-icon-folder-open',secondary:''} );
    $( ".icon-new" ).button( "option", "icons", {primary:'ui-icon-document',secondary:''} );
    $( ".icon-save" ).button( "option", "icons", {primary:'ui-icon-disk',secondary:''} );
    //$( ".disabled" ).button( "option", "disabled", true );

    $(':text').addClass('ui-widget-content');  
    $(':password').addClass('ui-widget-content');
    //$(':text').addClass('text ui-widget-content ui-corner-all');  
    //$(':password').addClass('text ui-widget-content ui-corner-all');
    //$('select').addClass('text ui-widget-content ui-corner-all');
    //$('#popup-'+id+' select').selectmenu({style:'dropdown'});
    //$('#popup-btn_user_add select').selectmenu({style:'dropdown'});    

    //alert('sdl_js_init:'+id+'\nid: '+'select'+' '+id);
    $("#header").click(function(){ window.location='uvod.html';return false; });
  }
// ###########################################################################
  function sdl_odpoved(data, id){
    for(i=0; i<data.length; i++){
      if(data[i].close=='true'){
        if($(id).length) $(id).dialog('close');
      }else if(data[i].close!='false'){
        if($('#'+data[i].close).length) $('#'+data[i].close).dialog('close');
      }
      //alert('action: '+data[i].action);
      switch (data[i].action){
        case 'replace':
          if($('#'+data[i].target).length) $('#'+data[i].target).replaceWith(data[i].html);
          //else alert('ERROR:\nprvek ['+data[i].target+'] neexistuje.');
          sdl_js_init();
          break;
        case 'append':
          $('#'+data[i].target).append(data[i].html);
          sdl_js_init();
          break;
        case 'value':
          $(''+data[i].target).val(data[i].html);
          break;
        case 'jqgrid':
          //alert('jqgrid:'+data[i].target);
          $('#grid_'+data[i].target).trigger('reloadGrid');
          //$('#'+data[i].target).val(data[i].html);
          break;
        case 'hide':
          //if($('#'+data[i].target).length) $('#'+data[i].target).hide();
          //if($('#'+data[i].target).length) $('#'+data[i].target).dialog( "option", "hide", 'slide' );
          break;
        case 'tinymce':
          //$(data[i].target).tinymce().execCommand('mceInsertContent',false,data[i].html);
          tinyMCE.activeEditor.execCommand('mceInsertContent',false,data[i].html);
          break;

      }               
    }
    return;
  }
// ###########################################################################
// Global init
// ###########################################################################
	$(function() {

    sdl_js_init();

    //$("div.zbozi_show").click(function(){ window.location=$(this).find("a").attr("href");return false; });

    //KOREKCE
    $(function () { //chceckobox change
      if ($.browser.msie) {
          $('input:checkbox').click(function () {
              this.blur();
              this.focus();
          });
      }
    });
//------------------------------------------------------------------------------    
    $('.popup').live('click', function () {
    //alert('sdlclick '+$(this).attr("id"));
      //id=$(this).attr("id");
      if($(this).attr("req")==undefined) var req=$(this).attr("id"); else var req=$(this).attr("req");
      if($(this).attr("rel")==undefined){ 
        if($(this).attr("grid")!=undefined){
          var grid=$(this).attr("grid"); var idrow = jQuery("#"+grid).jqGrid('getGridParam','selrow');
          if (idrow){var rel = jQuery("#"+grid).jqGrid('getCell',idrow,'md5id');
          }else{sdl_alert('Nemáte nic vybráno.'); return;}
        }
      }else{ var rel=$(this).attr("rel");}
      
      if($(this).attr("special")!=undefined){
        var special=$(this).attr("special");
        special=eval(special);
      }

      win=$(this).attr("win");
      //alert('rel: '+rel);
      $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "index.php",
        data: {ajax:'true', msg:'popup', req: req, md5id: rel, win: win, special: special},
        success: function(data){
          //if($('#popup-'+req).length) $('#popup-'+id).remove();
          sdl_odpoved(data, req);
        },
        error:function (xhr, ajaxOptions, thrownError){
        	alert('AJAX error:\n'+xhr.status+'\n'+thrownError+'\n'+ajaxOptions);
        }   
     });    
    }
    );

//------------------------------------------------------------------------------
    $('.confirm').live('click', function () {
    //$('.confirm').click(function () {
    //alert('sdlclick '+$(this).attr("id"));
      if($(this).attr("req")==undefined) var req=$(this).attr("id"); else var req=$(this).attr("req");
      if($(this).attr("rel")==undefined){ 
        if($(this).attr("grid")!=undefined){
          var grid=$(this).attr("grid"); var idrow = jQuery("#"+grid).jqGrid('getGridParam','selrow');
          if (idrow){var rel = jQuery("#"+grid).jqGrid('getCell',idrow,'md5id');
          }else{sdl_alert('Nemáte nic vybráno.'); return;}
        }
      }else{var rel=$(this).attr("rel");}

      if($(this).attr("special")!=undefined){
        var special=$(this).attr("special");
        special=eval(special);
      }else{
        var special=false;
      }  
      
      var win=$(this).attr("win");
      var title=$(this).attr("title");
      sdl_confirm(title, req, rel, '', special);
      
      return false; 
    });
//------------------------------------------------------------------------------    
    $('.click').live('click', function () {
    //alert('sdlclick '+$(this).attr("id"));
      //id=$(this).attr("id");
      //req=$(this).attr("id");
      if($(this).attr("req")==undefined){ var req=$(this).attr("id");}else{var req=$(this).attr("req");}
      if($(this).attr("rel")==undefined){ 
        if($(this).attr("grid")!=undefined){
          var grid=$(this).attr("grid"); var idrow = jQuery("#"+grid).jqGrid('getGridParam','selrow');
          if (idrow){var rel = jQuery("#"+grid).jqGrid('getCell',idrow,'md5id');
          }else{sdl_alert('Nemáte nic vybráno.'); return;}
        }
      }else{var rel=$(this).attr("rel");}

      if($(this).attr("special")!=undefined){
        var special=$(this).attr("special");
        special=eval(special);
      }      

      win=$(this).attr("win");
      $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "index.php",
        data: {ajax:'true', msg:'form', req: req, md5id: rel, win: win, special: special},
        success: function(data){
          sdl_odpoved(data, req);
        },
        error:function (xhr, ajaxOptions, thrownError){
        	alert('AJAX error:\n'+xhr.status+'\n'+thrownError+'\n'+ajaxOptions);
        }   
     });
    });
    
    
	});

//------------------------------------------------------------------------------
function sdl_tinymce_init(id){
	//$('textarea.mceEditor').tinymce({
	//alert(id);
	//alert($(id).val());
  $(id).tinymce({
		// Location of TinyMCE script
		script_url : './js/jquery-tiny_mce/tiny_mce.js',

		// General options
		language : "cs",
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
 
		// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,"
					+"justifyleft,justifycenter,justifyright,justifyfull,|,"
					+"bullist,numlist,|,outdent,indent,|,sub,sup,|, formatselect,styleselect,fontselect,fontsizeselect,|,forecolor,backcolor,|,removeformat,|,code",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,selectall,|,search,replace,|,undo,redo,|,link,unlink,separator,hr,charmap,|,visualaid,tablecontrols,|,image, sdlimage",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",

		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",

  	theme_advanced_styles : "pop-up video=video;",


		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",
    height : "300px",
		width : "100%",
    
    setup : function(ed) {
        // Add a custom button
        ed.addButton('sdlimage', {
            title : 'Upload obrazku',
            image : 'img/sdlupload.png',
            onclick : function() {
             //function  click(req, rel, win, special) {
              click_popup('btn_upload', id, '', 'tinymce_image');
              //click('sdltest', id, '', 'tinymce_image');
              //sdl_alert('baf');
              
				// Add you own code to execute something on click
			//	ed.focus();
				    
				    //$(id).tinymce().execCommand('mceInsertContent',false,'<b>Hello world!!</b>');
                //ed.selection.setContent('<strong>Hello world!</strong>');
                //$('#content').tinymce().execCommand('mceInsertContent',false,'<b>Hello world!!</b>');
            }
        });
    }    
    		
	});
}	
// ###########################################################################


