

/**
 * ajaxRequest 
 * 
 * @param url $url 
 * @param params $params 
 * @param type = 'updater' $type = 'updater' 
 * @access public
 * @return boolean
 */
function ajaxRequest(url, params, updateId)
{
   var opt = {
      // Use POST
      method: 'post',
        // Send this lovely data
      postBody: params,
        // Handle successful response
        
      onSuccess: function(t) {
         if (updateId == undefined) {
            addMessage(t.responseText, 2);
         } else {
            $(updateId).value = t.responseText;
         }
      },
        // Handle 404
      on404: function(t) {
//          alert('Error 404: location "' + t.statusText + '" was not found.');
         addMessage('Error 404: location "' + t.statusText + '" was not found.', 5);
      },
       // Handle other errors
      onFailure: function(t) {
//              alert('Error ' + t.status + ' -- ' + t.statusText);
         addMessage('Error ' + t.status + ' -- ' + t.statusText, 4);
      }
   }
   /*
   if (undefined == updateId) {
      updateId = 'message';
   }*/

   new Ajax.Request(url, opt);
   //new Ajax.Updater(updateId, url, opt);

}

/**
 * ajaxRequestReturn
 * prijima odkaz na funkci, ktera se ma spustit na onsuccess
 * 
 * @param url $url 
 * @param params $params 
 * @param success $success 
 * @return boolean
 */
function ajaxRequestReturn(url, params, success)
{
   var opt = {
      // Use POST
      method: 'post',
        // Send this lovely data
      postBody: params,
        // Handle successful response
        
      onSuccess: function(t) {
        // alert(t.responseText);
           success(t.responseText);
      },
        // Handle 404
      on404: function(t) {
//          alert('Error 404: location "' + t.statusText + '" was not found.');
         addMessage('Error 404: location "' + t.statusText + '" was not found.', 5);
      },
       // Handle other errors
      onFailure: function(t) {
//              alert('Error ' + t.status + ' -- ' + t.statusText);
         addMessage('Error ' + t.status + ' -- ' + t.statusText, 4);
      }
   }
      /*
    if (undefined == updateId) {
      updateId = 'message';
   }*/
   new Ajax.Request(url, opt);
   //new Ajax.Updater(updateId, url, opt);

}


function addNewPositionContent (element, dropon, event, url)
{
   params = 'module='+ element.id + '&position=' + dropon.id;
   baseUrl = url;
   url = '/' + url + 'admin/ajax/addcontent/';
   var opt = {
      // Use POST
     method: 'post',
        // Send this lovely data
     postBody: params,
        // Handle successful response
        
     onSuccess: function(t) {
            //new Effect.Appear('message');
            //new Effect.SlideUp('message');

// ****           var data = eval('(' + t.responseText + ')');

             wreload(t.responseText);

        },
        // Handle 404
     on404: function(t) {
         addMessage('Error 404: location "' + t.statusText + '" was not found.', 5);
         // alert('Error 404: location "' + t.statusText + '" was not found.');
        },
       // Handle other errors
     onFailure: function(t) {
         addMessage('Error ' + t.status + ' -- ' + t.statusText, 4);
              //alert('Error ' + t.status + ' -- ' + t.statusText);
        }
   }

   new Ajax.Request(url, opt);
}

function getGroupOrder(url) {
   var sections = document.getElementsByClassName('section');
   var params = '';
   sections.each(function(section) {
         var sectionID = section.id;
         var order = Sortable.serialize(sectionID);
         if (order != '') {
            if (params == '') {
               params = order;
            } else {
               params = params + '&' + order;
            }
         }
         });
//   alert(params);
   new ajaxRequest(url + 'admin/ajax/savepositioncontent/', params);
   return false;
}


var nodiac = { 'á': 'a', 'č': 'c', 'ď': 'd', 'é': 'e', 'ě': 'e', 'í': 'i', 'ň': 'n', 'ó': 'o', 'ř': 'r', 'š': 's', 'ť': 't', 'ú': 'u', 'ů': 'u', 'ý': 'y', 'ž': 'z' };
/** Vytvoření přátelského URL
* @param string s řetězec, ze kterého se má vytvořit URL
* @return string řetězec obsahující pouze čísla, znaky bez diakritiky, podtržítko a pomlčku
* @copyright Jakub Vrána, http://php.vrana.cz
*/
function make_url(s)
{
    s = s.toLowerCase();
    var s2 = '';
    for (var i=0; i < s.length; i++) {
        s2 += (typeof nodiac[s.charAt(i)] != 'undefined' ? nodiac[s.charAt(i)] : s.charAt(i));
    }
    return s2.replace(/[^a-z0-9_]+/g, '-').replace(/^-|-$/g, '');
}

/**
 * selectItem 
 * vyber polozek v menu
 *
 * @param obj $obj 
 * @return void
 */
function selectItem(obj)
{
   active = $('page-tree-root').getElementsByClassName('active-page');
   for (i = 0; i < active.size(); i++) {
      active[i].removeClassName('active-page');
   }
   obj.addClassName('active-page');
}

/**
 * addPage
 * pridani polozky do menu a stranky
 * 
 * @param notChildren $notChildren 
 * @return boolean
 */
function addPage(notChildren)
{
   id = getActiveMenuItemId();
}

/**
 * getActiveMenuItemId
 * vrati id aktivni polozky v menu
 * 
 * @return mixed
 */
function getActiveMenuItemId(whole)
{
   //var active = $('block-content-menu').getElementsByClassName('active-page');
   var active = $('page-tree-root').getElementsByClassName('active-page');
   if (1 == active.size()) {
      parents = active[0].ancestors();
      if (1 <= parents.size()) {
         if (whole) {
            return parents[0];
         } else {
            id = parents[0].id.split('_').last();
            return id;
         }
      }
   }
   return false;
}

function deletePage(url)
{
   var id = getActiveMenuItemId();
   ajaxRequest(url + 'admin/ajax/deletemenuitem/' + id + '/');
   getActiveMenuItemId(true).remove();
}

/**
 * calcHeight
 * zmena velikosti iframu podle jeho obsahu
 * 
 * @param obj $obj 
 * @return void
 */
function calcHeight(obj)
{
   var size = windowSize();
   obj.height = size.height - 191;
}

function resizeWindow()
{
   var size = windowSize();
//   alert(Object.values(size));
   window.width  = size.width;
   window.height = size.height;
}


function windowSize() {
   var result = {height:0, width:0};
   if(typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      result.width = window.innerWidth;
      result.height = window.innerHeight;
   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      result.width = document.documentElement.clientWidth;
      result.height = document.documentElement.clientHeight;
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      result.width = document.body.clientWidth;
      result.height = document.body.clientHeight;
   }
   return result
}

function loadTemplate(t)
{
   var data = eval('(' + t + ')');
   $('template-name').value = data.name;
   $('template-description').value = data.description;
   $('detail').value = data.detail;
}

function wreload(t)
{
   addMessage(t);
   window.location.reload();
}

function fillEditArea(t)
{
   editAreaLoader.setValue('tpt-content', t);
}

