/***************************************************************************************************************/

function MakeElementDraggable(el_id, opt)
{
  new Draggable(el_id, opt);
}

function MakeElementSortable(el_id, opt)
{
  Sortable.create(el_id, opt);
}

/***************************************************************************************************************/

function ManagerContextSearch(type, list_id, search, param)
{
  var sdd_id = list_id + 'sdd';
  var sdd = $(sdd_id);

  if (!$(sdd_id))
  {
    $(list_id + 'sinp').insert({ after: '<div id="' + sdd_id + '"></div>' });
    sdd = $(sdd_id);
    sdd.hide();
  }
  
  new Ajax.Updater(sdd_id, '/admin/inc/context_search.php',
          { parameters: 'type=' + type + '&listid=' + list_id + '&search=' + search + (param ? '&param=' + param : ''),
            onComplete: function () { sdd.show(); },
            evalScripts: true
          }
        );
}

/***************************************************************************************************************/

function ManagerInsertItem(list_id, obj_id, obj_type, title)
{
  var list_items = [];
  var list = $(list_id + 'list');

  if (list)
  {
    var item_id = list_id + 'i' + obj_id;
    var single = list.getAttribute('single');

    var items = list.childElements();
    for(var i = 0; i < items.length; i++) list_items[items[i].id] = 1;

    if(list_items[item_id] == undefined)
    {
      if (single == 1) list.innerHTML = '';
      
      var item_html = '';

      item_html += '<div id="' + item_id + '" class="set-item ' + obj_type + '" objid="' + obj_id + '" onmousedown="wm.front($(\'' + item_id + '\').up(\'.float-win\').id)">' + "\r\n";
      item_html += '<input type="hidden" name="' + list.getAttribute('inpname') + '[]" value="' + obj_id + '" />' + "\r\n";
      if (list.getAttribute('removable') == 1)
      {
        item_html += '<img src="/admin/images/li_rem.png" onclick="ManagerRemoveItem(\'' + list_id + '\', \'' + obj_id + '\')" class="man-small-icon" border="0" alt="remove" title="Убрать из списка" />';
      }
      item_html += '<img src="/admin/images/edit.png" onclick="wm.open(\'' + obj_type + '.php\', \'win' + obj_id + obj_type + '\', {type : \'' + obj_type + '\', action : \'edit\', target : \'' + obj_id + '\', opener : \'' + list_id + '\'})" class="man-small-icon" border="0" alt="edit" title="Редактировать" />' + "\r\n";
      item_html += '<u>' + title + '</u> (' + obj_id + ")\r\n";
      item_html += '</div>' + "\r\n";
  
      list.insert({ bottom: item_html });
  
      if ($(list_id + 'cntr') && single == 0) $(list_id + 'cntr').innerHTML = (items.length + 1) + '';
      
      MakeElementSortable(list_id + 'list', {
        tag: 'div',
        ghosting: false,
        overlap: 'vertical',
        constraint: false
      });
      
      return true;
    }
  }
  
  return false;
}

function ManagerRemoveItem(list_id, obj_id)
{
  var list = $(list_id + 'list');
  var items = list.childElements();

  if (list)
  {
    var item_id = list_id + 'i' + obj_id;

    new Element.remove(item_id);

    if ($(list_id + 'cntr')) $(list_id + 'cntr').innerHTML = (items.length - 1) + '';
    
    if (items.length - 1 == 0) list.innerHTML = '';
  }
}

/***************************************************************************************************************/

function SetupTynyMce(el)
{
  return tinyMCE.init({
    mode : 'exact',
    elements : el,
    theme : 'advanced',
    skin : 'o2k7',
    language : 'ru',
    plugins : 'imagemanager,safari,spellchecker,advimage,advlink,pagebreak,style,layer,table,save,advhr,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',
    theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
    theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
    theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,|,insertfile,insertimage",
    theme_advanced_toolbar_location : 'top',
    theme_advanced_toolbar_align : 'left',
    theme_advanced_statusbar_location : 'bottom',
    theme_advanced_resizing : true,
    convert_urls : false,
    button_tile_map : true,
    content_css: '/admin/css/editor.css'
  });
}

/***************************************************************************************************************/

function GetBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function GetBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}

function GetDocumentHeight()
{
  return (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
}

function GetDocumentWidth()
{
  return (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
}

function GetWinInnerHeight()
{
  return self.innerHeight || (document.documentElement && document.documentElement.clientHeight) || (document.body && document.body.clientHeight); 
}

function GetWinInnerWidth()
{
  return self.innerWidth || (document.documentElement && document.documentElement.clientWidth) || (document.body && document.body.clientWidth); 
}

/***************************************************************************************************************/