// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var folded_status = new Array();
function Folder(x, update_url) { 
   element = $(x);
   // adding folder to element
   element.folder = this;
   
   // could be usefull
   this.element = element;
   if(update_url == '') { 
      update_url = "/admin/content/change_fold_status/"
   }
   this.update_url = update_url
   this.child = $(this.element.id.replace('node','child'));
   this.efect_defaults = { duration:0.5 }
   // status 
   //this.folded = is_folded
   this.folded = this.child.style.display=="none";
   // methods  
   this.change =  function () { 
      if(this.folded) { 
         this.unfold();
      } else {
         this.fold();
      }
      this.folded = !this.folded;
   }
   this.send_fold_status_to_server = function (s) { 
      id = this.element.id.replace('node','');
      id = id.replace('child','');
      if( this.update_url != 'none')  { 
      new Ajax.Request(update_url + id, 
         { 
            method: 'get',
            parameters: 'fold=' + s
         }
      )
      }
   }
   this.unfold = function () { 
      this.send_fold_status_to_server('unfold')   
      Effect.BlindDown(this.child, this.efect_defaults);
   }
   this.fold = function () { 
      this.send_fold_status_to_server('fold')
      Effect.BlindUp(this.child, this.efect_defaults);
   }

} // End of folder class
var InsertHelperFireObject;
function InsertHelper(x) { 
   element = $(x);
   if(element.inserthelper) { 
      window.alert("InsertHelper already added for element " + $(x).id);
   }
   element.inserthelper = this;
   
   this.element = element;



   // function lunch_insert()
   this.lunch_insert= function() { 
      InsertHelperFireObject = this.element;
      this.litbox = new LITBox('/admin/asset/ajax_list', {width: 300, height: 500, opacity:0.9, overlay: true, top: -1});
      //window.open('/admin/asset/ajax_list','asset_window',"width=400,height=400");
   }
   this.lunch_help = function() { 
      new LITBox('/textile.utf8.html', {width: 700, height: 500, opacity: 1, overlay: true, top: -1});
   }
   // function html_insert_link
   this.html_insert_link = function() { 
      return "<a href=\"#\" onClick=\"$('"+ this.element.id + "').inserthelper.lunch_insert(); return false;\">Wstaw plik</a> <a href=\"#\" onClick=\"$('"+ this.element.id + "').inserthelper.lunch_help(); return false;\">Pomoc</a>";
   }

   this.insertText = function(val) { 
      this.insertAtCursor(val);
      this.litbox.remove();
   }
   this.insertAtCursor = function(myValue) { 
      //IE support
      if (document.selection) {
         this.element.focus();
         sel = document.selection.createRange();
         sel.text = myValue;
      }
      //MOZILLA/NETSCAPE support
      else if (this.element.selectionStart || this.element.selectionStart == '0') {
         var startPos = this.element.selectionStart;
         var endPos = this.element.selectionEnd;
         this.element.value = this.element.value.substring(0, startPos)
            + myValue
            + this.element.value.substring(endPos, this.element.value.length);
      } else {
         this.element.value += myValue;
      }
   }

   new Insertion.Before(this.element,this.html_insert_link());
}

function insertAtCursor(myValue) {
   //IE support
   if (document.selection) {
      this.element.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
   }
   //MOZILLA/NETSCAPE support
   else if (this.element.selectionStart || myField.selectionStart == '0') {
      var startPos = this.element.selectionStart;
      var endPos = this.element.selectionEnd;
      this.element.value = myField.value.substring(0, startPos)
         + myValue
         + this.element.value.substring(endPos, myField.value.length);
   } else {
      this.element.value += myValue;
   }
}
