/**
* 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) {
            //new Effect.Appear('message');
            //new Effect.SlideUp('message');

         // kvuli framum
         parent.base.addMessage(t.responseText, 2);
      },
        // Handle 404
      on404: function(t) {
//          alert('Error 404: location "' + t.statusText + '" was not found.');
         parent.base.addMessage('Error 404: location "' + t.statusText + '" was not found.', 5);
      },
       // Handle other errors
      onFailure: function(t) {
//              alert('Error ' + t.status + ' -- ' + t.statusText);
         parent.base.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 + 'administrace/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 + ')');
           a = /position-container-(.+)-(\d+)/g.exec(dropon.id);
           htmlId = 'content-' + a[1] + '-' + a[2];
//           alert(htmlId);
           newItem2 = '<li id="position-id_' + data.id + 
                     '" class="position-content" style="border: 1px dashed lime; margin: 10px">' + "\n" + '<div class="position-content-label">' + data.moduleName + 
                     '</div>' + "\n" + '<div class="position-content-module-params"><a href="#" onclick="editContent(\'/' + baseUrl + 
                     'administrace/moduleconfig/' + data.id + 
                     '/\');return false;">Upravit parametry</a></div>' + "\n" + ' <div class="position-content-view-type">' + data.viewType + 
                     '</div>' + "\n" + ' <div class="position-content-view-params"><a href="#" onclick="editContent(\'/' + baseUrl +
                     'administrace/viewconfig/' + data.id + '/\');return false;">Upravit parametry</a></div>' + "\n" + ' <div class="handle" style="width:100px; height:30px; background: green">Presun</div> <a href="#" onclick="if (confirm(\'Opravdu chcete tento obsah vymazat?\')) { ajaxRequest(\'/' + baseUrl + 'administrace/ajax/deletecontent/\', \'position=' + data.id + '\'); new Effect.SlideUp(\'position-id_' + data.id + '\'); } " style="width: 80px; background: red">Smazat</a> </li>';

            newItem = document.createElement('li');
            newItem.id = 'position-id_' + data.id;
            newItem.addClassName('position-content');
            newItem.style = 'border: 1px dashed lime; margin: 10px';

            tmp = document.createElement('div');
            tmp.addClassName('position-content-label');
            tmp.innerHTML = data.moduleName;

            newItem.appendChild(tmp);

            tmp = document.createElement('div');
            tmp.addClassName('position-content-module-params');

            tmp2 = document.createElement('a');
            tmp2.href = '#';
            tmp2.onclick = "editContent('/" + baseUrl + "administrace/moduleconfig/" + data.id + "'); return false;";
            tmp2.innerHTML = 'Upravit parametry';

            tmp.appendChild(tmp2);
            newItem.appendChild(tmp);

            tmp = document.createElement('div');
            tmp.addClassName('position-content-view-type');
            tmp.innerHTML = data.viewType;

            newItem.appendChild(tmp);

            tmp = document.createElement('div');
            tmp.addClassName('position-content-view-params');

            tmp2 = document.createElement('a');
            tmp2.href = '#';
            tmp2.onclick = "editContent('/" + baseUrl + "administrace/viewconfig/" + data.id + "'); return false;";
            tmp2.innerHTML = 'Upravit parametry';

            tmp.appendChild(tmp2);
            newItem.appendChild(tmp);

            tmp = document.createElement('div');
            tmp.addClassName('handle');
            tmp.style = 'width:100px; height:30px; background: green';
            tmp.innerHTML = 'Presun';

            newItem.appendChild(tmp);

            tmp = document.createElement('a');
            tmp.href = '#';
            tmp.onclick = "if (confirm('Opravdu chcete tento obsah vymazat?')) { ajaxRequest('/" + baseUrl + "administrace/ajax/deletecontent/', 'position=" + data.id + "'); new Effect.SlideUp('position-id_" + data.id + "'); } ";
            tmp.innerHTML = 'Smazat';
            tmp.style = 'width: 80px; background: red';

            newItem.appendChild(tmp);

            position = document.getElementById(htmlId);
           position.appendChild(newItem);
            
            addMessage('Obsah byl přidán', 2);
           // position.innerHTML = newItem2 + position.innerHTML;

          //new Effect.SlideDown('position-id_' + data.id);
          //alert(dropon.id);
        },
        // 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 + 'administrace/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 = $('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 + 'administrace/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 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 editSelectTable(clas, show) {
  var inputs = document.getElementsByClassName(clas);
  
  for (var item in inputs) {
      if (show == 0) {
        inputs[item].checked = '';
      } else {
        inputs[item].checked = 'checked';
      }
  }
  //alert(inputs);
}


function activateTab(tab) {

 var activeTab = document.getElementById('tab-' + tab);
 var tabs = document.getElementsByClassName('tabs');
 

  for (var i = 0;i < tabs.length; i ++) {
      
      var tabElement = tabs[i];
      
      
      if (tabElement == activeTab) {
        tabElement.className = 'tabs activeTab';
        window.location.hash = "#activeTab-"+i;
      } else {
        tabElement.className = 'tabs';
      } 
  }
  
  var activeMenuLi = document.getElementById('tab-switch-' + tab); 
  var menuLis = document.getElementsByClassName('tabLi');
  
  for (var i = 0;i < menuLis.length; i ++) {
    var tabLi = menuLis[i];
   
    if (tabLi == activeMenuLi) {
        tabLi.className = 'tabLi profile-tab-active';
      } else {
        tabLi.className = 'tabLi';
      } 
  }
 
//
}


