(function($){  
 $.fn.toggleable= function(options) {      
  var defaults = {
    moreText : 'ShowFullSummary'
   ,hideText : 'HideSummary'
  };      
  var options = $.extend(defaults, options);
  return this.each(function() {    
    var obj = $(this)
   
    var elink= $('<a href="" class="extenderLink">'+options.moreText+'</a>');
    elink.toggle(function(){
       $(this).addClass('extOpen').text(options.hideText);      
       obj.show();
    }
    ,function(){
       $(this).removeClass('extOpen').text(options.moreText);
       obj.hide();
    });
    obj.after(elink);
    
  });  
 };  
})(jQuery);  

(function($){  
 $.fn.toggler= function(options) {      
  var defaults = {
     toggles: 'next'
  };      
  var options = $.extend(defaults, options);
  return this.each(function() {    
    var obj = $(this);
    var toggles = null;
    obj.next().hide();
    obj.addClass("collapsed")
    if (options.toggles == 'next'){
       toggles = obj.next()
    }  
    else{
       toggles = obj.prev()    
    } 
    obj.toggle(function(){
       obj.addClass('expanded'); 
       toggles.show().css({'margin-left':'15px'});       
    }
    ,function(){
       obj.removeClass('expanded');
       toggles.hide();       
    });
  });  
 };  
})(jQuery); 

(function($){  
 $.fn.infobox= function(options) {      
  var defaults = {};      
  var options = $.extend(defaults, options);
  var $termContainer = $("<div id='termContainer'><p></p></div>");  
  $('body').append($termContainer);
  $termContainer.hide();
  return this.each(function() {    
    var $this = $(this);
    var title = $this.attr('title');    
    //remove contents of title tag so the browser won't display it by default    
    $this.attr('title','');
    var visible = false;
    var timer = null
    $this.css({'cursor':'help'});
    $this.hover( 
      function(e){
        //create and display term container        
        timer = setTimeout(function(){
        if(!visible){
          visible = true;
          offset = $this.offset()          
          offset['left'] = offset['left'] + $this.width();          
          $termContainer
            .css(offset)
            .find('p').text(title)
            .end()
            .fadeIn(150, function(){});          
            }
        } , 250);          
      },
      function(e){
        clearTimeout(timer);        
        if(visible){
          $termContainer.fadeOut(150, function(){
              visible = false;
          });
        }        
      });    
  });  
 };  
})(jQuery); 



(function($){
    
    $.fn.sjekkliste_edit = function(options) {      
    var defaults = {};        
    var options = $.extend(defaults, options);
    
    return this.each(function() {            
  
      var $this = $(this);
      var currentFormField = null;        
      var $list = $('select', $this);             
      var action = $this.attr('action');
      var $editForm = $("<div class='edit'><input type='hidden' name='tm_serial'><input class='title' type='text'><textarea class='text' cols='50' rows='7'></textarea><input class='save' type='submit' value='Lagre endringer'><a class='cancel' href='#'>Avbryt</a></div>")
                        .appendTo($this);
      
      var $title = $('.title', $editForm);       
      var $text =  $('.text', $editForm);      
      var $save = $('.save', $editForm);      
      var $tm_serial = $('[name="tm_serial"]', $editForm)     
      var $newElement = null;
      
      var $action_add = $(".action_add", $this);
      var $action_up = $(".action_up", $this);
      var $action_down = $(".action_down", $this);
      var $action_delete = $(".action_delete", $this);      

      var $cancel = $(".cancel", $this).hide();     
      
      var toolbarStateEnabled = function(state){        
        $action_add.attr('disabled', state);
        $action_up.attr('disabled', state);
        $action_down.attr('disabled', state);
        $action_delete.attr('disabled', state);        
      }    
      
      
      
      $list.bind('change', function(){
          //retrieve data from the occurrence          
          var $_this = $(this); 
          $save.attr('disabled', false);
          $title.attr('disabled', false);
          $text.attr('disabled', false);
          toolbarStateEnabled(false);
          if($newElement){
            $newElement.remove();
            toolbarStateEnabled(true);
          }          
          data = {
                    'tm_serial' : $_this.attr('value')
                   ,'action' :'info'                    
                 }
          $list.attr("disabled", true)
          $.getJSON( action, data , function(data){              
              $title.attr('value', data.title);
              $text.val(data.text);
              $tm_serial.attr('value', data.tm_serial)
              $list.attr("disabled", false);
          })
          //display it for the user
          return false;
      });
      
      if($list.children().length == 0){
        toolbarStateEnabled(true);
        $action_add.attr('disabled', false);
        $title.attr('disabled', true);
        $text.attr('disabled', true);
        $save.attr('disabled',true);
      }
      else{
        $list.children().eq(0).attr('selected', true);
        $list.trigger('change');      
      }      
      
      $save.bind('click', function(e){
        $list.attr("disabled", true);
        $_this = $(this)
	$_this.attr("disabled",true);
        var $selected = $(':selected', $list);
        var position = $list.children().index($selected);                    
        var data = {
            'action':'update'
           ,'position':position
           ,'title':$title.attr("value")
           ,'text':$text.val()
           ,'tm_serial':$tm_serial.attr("value")             
        };
        
        
        $.post(action, data, function(data){
            $list.attr("disabled", false);
            toolbarStateEnabled(false);
            $cancel.hide();              
            $selected
              .attr('value', data.tm_serial)
              .text(data.title);
            $tm_serial.attr('value', data.tm_serial);
            $_this.attr("disabled", false);
        }
        , "json");
        
        return false;
      });
      
      $action_add.bind('click', function(e){
          $_this = $(this);
          $title.attr('disabled', false);
          $text.attr('disabled', false);
          $save.attr('disabled',false);
          $cancel.show();
          toolbarStateEnabled(true);
          $list.attr('disabled', true);
          if($list.children().length == 0){
            $newElement = $("<option value=''>[Nytt element]</option>").appendTo($list);
            $newElement.attr('selected',true);
          }
          else{
            $newElement = $("<option value=''>[Nytt element]</option>").insertAfter($(':selected', $list));
            $newElement.attr('selected',true);
          }                        
          $tm_serial.val('');
          $title.val('');
          $text.val('');
          return false;
      });
      
      $cancel.bind('click', function(e){
          $(this).hide();          
          $list.attr('disabled', false);
          $newElement.prev().attr('selected', true);
          $list.trigger('change');
          $newElement.remove();
          $newElement = null;
          toolbarStateEnabled(false);
          return false;
      });        
        
        $(".action_up", $this).bind('click',function(){           
           var data = {               
               action:"move_up"
             , tm_serial: $tm_serial.attr("value") 
           };
           $_this = $(this);
           var $selected = $(':selected', $list);
           
           $list.attr("disabled", true);
           toolbarStateEnabled(true);
           $.post(action, data, function(){
             $list.attr("disabled", false);
             toolbarStateEnabled(false);
             $selected.insertBefore($selected.prev());
          }
          , "json");
           
           return false;             
         });
        
         $(".action_down", $this).bind('click',function(){
           var data = {               
               action:"move_down"
             , tm_serial: $tm_serial.attr("value") 
           };
           $_this = $(this);
           var $selected = $(':selected', $list);
           
           $list.attr("disabled", true);
           toolbarStateEnabled(true);
           $.post(action, data, function(){
             $list.attr("disabled", false);
             toolbarStateEnabled(false);
             $selected.insertAfter($selected.next());             
          }
          , "json");
           
           return false;
         });
         
         $(".action_delete", $this).bind('click',function(){             
             var data = {               
               action:"delete"
             , tm_serial: $tm_serial.attr("value") 
           };
           var $selected = $(':selected', $list);
           $list.attr("disabled", true);
           toolbarStateEnabled(true);
           $.post(action, data, function(){
             $list.attr("disabled", false);
             toolbarStateEnabled(false);
             $selected.next().attr('selected',true);
             $list.trigger('change');
             $selected.remove();
          }
          , "json");
           return false;
         });        
      })};
   

   
})(jQuery);





$(document).ready(function(){  
  $('.toggles.next').toggler();
  $('.toggles.prev').toggler({toggles:'prev'});
  $('a.term').infobox();
});













