
var link = Class.create({
  initialize: function(){
    this.Events = 'click';    
  },
  toAjax: function(elements, Events){
    Events = (typeof(Events) == 'undefined') ? this.Events : Events;
    observeFunction = new Array();
    elements.collect(
      function(el){
        var obj = {
          obs: function(e){      
            new Ajax.Request(el.href, {
              method: 'post',
              parameters: { ajax: true },
              onComplete: function(transport) {
                $('adminContent').update(transport.responseText);
                $('fishmenu').update("<div id='fishEye'>"+$('adminContent').down("#fishEye").innerHTML+"</div>");
                $('adminContent').down("#fishEye").remove();
              }
            });
            el.writeAttribute('oldHref', el.readAttribute('href'));
            el.writeAttribute('href', 'javascript:void(0);');
          },
          
          element: el
        };
                
        observeFunction.push(obj);
        if(typeof(el.onclick) == 'undefined'){
          Event.observe(el, Events, obj.obs);           
        }
      }
    );
  },
  toHtml: function(){
    observeFunction.collect(
      function(el){
        if(typeof(el.element.readAttribute('oldHref')) != 'undefined'){
          el.element.writeAttribute('href', el.element.readAttribute('oldHref'));
          el.element.writeAttribute('oldHref', '');
        }
        Event.stopObserving(el.element, Events, el.obs);
      }    
    );
  },
  hrefControl: function(status){
    if(typeof(status) == 'undefined' || status == 'on'){
      $$('a[href]').collect(function(el){
        el.writeAttribute('oldHref', el.readAttribute('href'));
        el.writeAttribute('href', 'javascript:void(0);');
      });      
    }else{      
      if(typeof(status) != 'undefined' || status == 'off'){
        $$('a[oldHref]').collect(function(el){
          el.writeAttribute('href', el.readAttribute('oldHref'));
          el.writeAttribute('oldHref', '');
        });      
      }
    }
  }
});

var Admin = Class.create({
  initialize: function(observeElements){
    this.stopArr = new Array();
    this.stopEdit = new Array();
    this.observeElements = observeElements;
    
    this.markElement = new Array();
    this.DeleteElement = new Array();
    document.observe('dom:loaded', function(){
      var div = new Element('div', { 'id': 'adminContent' });
      $('container').wrap(div);
      var div2 = new Element('div', { 'id': 'fishmenu' });
      $('fishEye').wrap(div2);
      $('adminContent').insert({
        before: "<img id='logoCMS' style='position:absolute; top:0; left:0;' src='images/ci/logoCMS.png'>"
      });
      $('fishmenu').setStyle({
        position: 'absolute',
        left: parseInt((document.viewport.getWidth()/2)-($('fishEye').getWidth()/2))+'px'
        
      });

      //alert(document.getElementsByTagName("body")[0].style);      
    });
  },
  
  
  markElementStart: function(){
    //this.observeElements = (typeof(obsElements) == 'undefined') ? this.observeElements : obsElements;    
    //$$('.li_menu', '.article', '.li_subMenu')
    obj = new Array();
    this.observeElements.collect(function(e){
      obj.push({    
        element: e,
      
        start: function(event){
          e.setStyle({
            border: '1px solid black'
          });
        },        
        stop: function(event){
          e.setStyle({
            border: '0px solid black'
          });
        }      
      });      
    });
    
    this.markElement = obj;
    
    this.markElement.collect(function(el){
      Event.observe(el.element, 'mouseover', el.start);
      Event.observe(el.element, 'mouseout', el.stop);
    });
  },
  
  markElementStop: function(){
    this.markElement.collect(function(el){
      Event.stopObserving(el.element, 'mouseover', el.start);
      Event.stopObserving(el.element, 'mouseout', el.stop);
    });
  },
  
  sortableList: function(id, sortClass){
    this.markElementStart();
    this.stopArr.push(id);
    Sortable.create(id,{
      ghosting:false,
      dropOnEmpty:true,
      constraint:false,
      hoverclass: 'hover2',
      onUpdate:function(el){
        save.sorting($(id), sortClass);
      }
    });    
  },
  
  stopSortable: function(){    
    this.stopArr.collect(function(ids){
      Sortable.destroy(ids);
    });    
    this.markElementStop();
  },
  
  sortableContent: function(newContainment, tag){    
    this.stopArr = this.stopArr.concat(newContainment);
    newContainment.collect(function(elementId){ 
      Sortable.create(elementId,
      {
        tag: tag,    
        constraint: true,
        dropOnEmpty: true,
        hoverclass: 'hover2',
        containment: newContainment,
        onUpdate:function(el){
          newContainment.collect(function(elementId){
            //sorting($(elementId), 'article');
            save.sorting($(elementId), 'article');
          });
        }
      });
    });    
  },
  
  editTinyMce: function(elements){
    this.markElementStart();
    arr = new Array();
    elements.collect(
    function(el){      
      arr.push(new Ajax.InPlaceRichEditor(el.id, 'administration/ajax/changeArt.php', {}, tinymce_advanced_options));   
    });
    this.stopEdit = this.stopEdit.concat(arr);
  },
  
  editList: function(elements){
    edit = new Array();
    elements.collect(function(el){
      edit.push(new Ajax.InPlaceEditor(el.id, 'administration/ajax/changeArt.php', {
          callback: function(form, value) {
            return {value: value, action: 'htmlentities'}
          }
      }));
    });
    this.stopEdit = this.stopEdit.concat(edit);
  },
  
  stopEditMode: function(){    
    this.stopEdit.invoke('dispose');
    this.markElementStop();
  },
  
  startDelete: function(elements){
    this.markElementStart();
    var obj = new Array();
    elements.collect(function(e){
      obj.push({    
        element: e,
      
        deletes: function(){
          Check = confirm('Wollen Sie den Artikel wirklich löschen?');
          if (Check == true){
            new Ajax.Request('administration/ajax/delete.php', {
              parameters: {artId: e.id.split('_')[2], table: e.id.split('_')[0]},
              onSuccess: function(transport){
                e.remove();
                if(transport.responseText == "reload"){
                  new Ajax.Request("index.php", {
                    method: 'post',
                    parameters: { ajax: true },
                    onComplete: function(transport) {
                      $('adminContent').update(transport.responseText);
                    }
                  });
                }
              }
            });
          }
        }  
      });           
    });
    this.DeleteElement = obj;
    this.DeleteElement.collect(function(el){
      Event.observe(el.element, 'click', el.deletes);      
    });
  },
  
  stopDelete: function(){
    this.DeleteElement.collect(function(el){
      Event.stopObserving(el.element, 'click', el.deletes);
    });
    this.markElementStop();
  },
  
  startAdd: function(contentId){
    $$('#menue', '.content_element', '.submenu').invoke('insert', {top: "<div class='added' style='background-color:yellow; font-weight: bolder; font-size:20px; text-align:center; width:100%; height:20px;'>+</div>"});    
    $$('.added').collect(function(el){
      Event.observe(el, 'click', function(event){
         new Ajax.Request('administration/ajax/newArticle.php', {
          parameters: {
            Class: el.up(0).className,
            cont_id: contentId, 
            position: el.up(0).id
          },
          onComplete: function(transport){
            new Ajax.Request(transport.responseText, {
              method: 'post',
              parameters: { ajax: true },
              onComplete: function(transport) {
                $('adminContent').update(transport.responseText);
              }
            });
          }
        });
        
      });
    });
    
    $$('.submenu').invoke('show'); 
  },
  
  stopAdd: function(){
    $$('.added').invoke('remove');    
    $$('.submenu').invoke('hide'); 
  },
  
  startChangeHeadPict: function(element){    
    Event.observe(element, 'dblclick', function(){
        Lightview.show({
         href: 'administration/ajax/changePicture.php',
         rel: 'iframe',
         title: 'Bild austauschen',
         caption: 'Bitte w&auml;hlen Sie das neue Bild aus!',
         options: {
           width: 600,
           height: 500,
           topclose: true,
           ajax: {
             method: 'get',
             onComplete: function(){ $('name').focus(); }
           }
         }
       });      
    });
          
  }
});

var saving = Class.create({
  sorting: function(el, searchTag){    
    var sortEl = el.getElementsByClassName(searchTag);
    for(i=0; i<sortEl.length; i++){
      var thisId = sortEl[i].id.split('_');
      new Ajax.Request('administration/ajax/changeArt.php', {
        parameters: {editorId: sortEl[i].id, value: i+1},
        onSuccess: function(transport){         
        }
      });
      new Ajax.Request('administration/ajax/sort.php', {
        parameters: {editorId: 'articlecontent_position_'+thisId[2], value: el.id},
        onSuccess: function(transport){                    
          
        }
      });
    }  
  } 
});




