/* konstruktor */
function psyo()
{
  this.panelWidth = Cookie.get('psyo_admin_panel_width');
  if (!this.panelWidth) this.panelWidth = 200;
  this.panelIsMinimized = false;
  this.panelIsResizable = null;
  this.panelIsResizableStatus = false;
  this.panelId = 'psyo_admin_panel';
  this.window = null; 
}
psyo.prototype = {
  initialize : function()
  {
    // nastaveni vysky sf_admin_container
    if ($('psyo_content_holder'))
      $('psyo_content_holder').style.height = document.viewport.getHeight()-95+'px';
  
    // nastaveni wysiwygu
    if ($('wysiwyg_frame'))
      $('wysiwyg_frame').style.height = $('psyo_admin_content').getHeight()-5+'px';

    // schovavani panelu
    Event.observe('psyo_admin_panel_off', 'click', this.togglePanel.bindAsEventListener(this));
    Event.observe('psyo_admin_panel_on', 'click', this.togglePanel.bindAsEventListener(this));
    Event.observe('psyo_admin_panel_hide', 'dblclick', this.togglePanel.bindAsEventListener(this));
    Event.observe('psyo_admin_panel_controls', 'dblclick', this.togglePanel.bindAsEventListener(this));
    
    // mass akce
    if ($('mass_select_all'))
      Event.observe('mass_select_all', 'click', this.massToggleAll.bindAsEventListener(this));
    
    // resize panelu
    this.panelIsResizable = new Resizable(this.panelId, {handle:'psyo_admin_panel_resize',constraint:'horizontal', onEnd: this.savePanelWidth });
    
    // nastavime zavirani panelu
    $$('div.psyo_admin_panel_vtab.collapse').each(function(tab){
      var button = new Element('img', {src: '/images/admin/v33/panel_toggle.png'}).addClassName('fr').addClassName('collapse_button').observe('click', function() {
        if (tab.style.display=='none')
          Cookie.set(tab.id, 1);
        else
          Cookie.set(tab.id, -1);
          
        tab.toggle();          
      });
      tab.previous('.psyo_admin_panel_vtab_header').insert({top:button});
      // zavirani po doubleclicku na zahlavi tabu - bohuzel pri tom oznacuje text
//      tab.previous('.psyo_admin_panel_vtab_header').observe('dblclick', function() {
//        tab.toggle();
//      });
    });
    if (PsyoTinyMCEInitQueue)
    {
      PsyoTinyMCEInitQueue.each(function(id){
        tinyMCE.execCommand('mceAddControl', false, id);
      });
    }
  },
  savePanelWidth : function(e)
  {
    Cookie.set('psyo_admin_panel_width', $('psyo_admin_panel').getWidth()-2);
  },
  togglePanel : function(e)
  {
    if ($('psyo_admin_panel_hide').style.display!='none')
    { // zobrazime panel
      $('psyo_admin_panel_hide').hide();
      $('psyo_admin_panel').show();
      $('psyo_admin_panel_resize').show();
      Cookie.set('psyo_admin_panel_show', 1);
    }
    else
    { // schovame panel
      $('psyo_admin_panel').hide();   
      $('psyo_admin_panel_resize').hide();
      $('psyo_admin_panel_hide').show();
      Cookie.set('psyo_admin_panel_show', 0);
    }
  },
  massToggleAll : function(e)
  {
      $$('#sf_admin_content input.mass').each(function(el){ 
        el.checked = $('mass_select_all').checked;
      });    
  },
  loadPsyoPage : function(slug)
  {
    $('psyo_page_edit').update('Načítám informace o aktuální stránce ...');
    var url = '/admin.php/dashboard/psyoPage/slug/'+slug;
    new Ajax.Updater('psyo_page_edit', url, {
      evalScripts: true
    });
  },
  unloadPsyoPage : function()
  {
    $('psyo_page_edit').update('Nahrávám údaje o stránce ...');
  }   
}
  
function panelTreeFormToggle(name, elName, elValue, caption)
{
    holder = $(name+'Holder');
    form = $(name+'Form');
    Position.absolutize(form);
    var offsets = Position.cumulativeOffset(holder);
    /* nastavit hodnotu, pokud je predana */
    if ((elName) && ($(elName))) $(elName).value = elValue;
    /* posunout zacatek bloku na zadany node (pokud je node hodne dole, nechceme zobrazit form na zacatku UL) */
    if ((elValue) && ($('node'+elValue)))
    {
        var parent_node_offsets = Position.cumulativeOffset($('node'+elValue));
        var top_shift = parent_node_offsets[1];
        

    }        
    else {var top_shift = offsets[1];}
    
    form.style.top    = top_shift + 'px';
    form.style.left   = offsets[0] + 'px';
    form.style.width  = holder.offsetWidth - 15 + 'px';
    form.style.height = 50 + 'px';
    /* nastavime caption */
    if (caption)
    {
        $(name+'FormCaption').innerHTML = caption;
    }                
    Element.toggle(form);
}
  function setFrameLayout(i)
  {
      if (i==1)
      {
          parent.document.body.rows = '100%,0';
      }
      if (i==2)
      {
          parent.document.body.rows = '75,*';
          $('content').innerHTML = '';
          $('header').style.height = '75px';
      }
  }
  function getSelectedText()
  {
      if (window.getSelection)
      {
          return window.getSelection();
      }
      else if (document.getSelection)
      {
          return document.getSelection();
      }
      else if (document.selection)
      {
          return document.selection.createRange().text;
      }
      else return;
  }  
function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}  
var PsyoTinyMCEInitQueue = new Array();  
// pole pro ukladani vsech stromu
var psyoMenus = new Array;
