// custom autocompleter

$.widget("custom.headercomplete", $.ui.autocomplete, {
    _renderMenu: function(ul, items) {
        var self = this;
        ul.append("<li class='ui-category'>Forslag til søgning</li>");
        $.each(items, function(index, item) {
            self._renderItem(ul, item);
        });
    }
});

(function ($) {
  //fix collapsing menus (currently done in script gadget)
  $("#navside ul.level_1 > li .box-inner").hide(); 
  $("#navside ul.level_1 > li.inPath .box-inner").show();
  
  //autocompletion
  $.getScript('/modules/virk/javascript/autocomplete.js', function(data, textStatus){
    $( "input.auto" ).headercomplete({ minLength: 4,source: ordliste });
    //submit instantly
    $( "input.auto" ).bind( "headercompleteselect", 
      function(event, ui) { 
        if(ui.item.value != '') {
          $("input.auto").val(ui.item.value);
          this.form.submit(); 
        } else {
          return false;
        }
      });
  });
  
  $.fn.collapse = function(maxheight, text) {
    return this.each(function() {
      var ul = $(this);
      var height = ul.outerHeight();
      ul.attr('data-orgheight', height);
      if(height > maxheight) {
          ul.css({'height' : maxheight+'px', 'overflow' : 'hidden'});
          ul.after($("<span class='more'>" + text + "</span>").click(function(e) {
              $(this).hide().prev().animate({'height' : ul.attr('data-orgheight'), 'overflow' : ''});
          }));
      }
    });
  };
})(jQuery);

function checkAdaptiveCookie(node, name, values, show) {
  if(name != null) {
    var c =  $.cookie(name);
    if(values == '') {
      node.toggle((c != null) == show); 
      return;
    } else {
      var va = values.split(',');
      for(var i=0;i<va.length;i++) {
          if(va[i] == c) {                            
              node.toggle(show);
              return;
          }
      }
      node.toggle(!show);
    }
  }
}

function handleAdaptiveCookie(name,value,expiry,clear) {    
  var options = {path: '/'};    
  if(clear) { 
    $.cookie(name, null, options); 
  } else {  
    if(expiry > 0) { 
      options['expires'] = expiry; 
    }
    $.cookie(name, value, options); 
  }
}

/*
Example url:
http://eogsdevweb01/cms/render/live/da/sites/virk/mitvirk/rediger-genveje.html?bookmark_title=gourmet+ministeriet&bookmark_type=search&bookmark_url=www.dmi.dk&bookmark_source=gourmet_ministeriet
*/

function bookmark_this() {
  var title = $('title').text();
  var url = window.location.pathname + window.location.search;
  var atype = $('meta[name=atype]').attr('content');
  var urltype = "information";  
  var source = "";
  if(atype == "indberetning") {
    urltype = "indberetning";
    source = $('meta[name=myndighed]').attr('content');
  } else if(atype == "myndighed") {
    source = $('meta[name=myndighed]').attr('content');
  } else if(url.indexOf("x=") >= 0) {
    urltype = "search";
  }
  var url = "/cms/render/live/da/sites/virk/system/mitvirk/rediger-genveje.html?bookmark_title=" + title + 
    "&bookmark_type=" + urltype + "&bookmark_url=" + url + "&bookmark_source=" + source + "&redirectUrl=" +url;
  //alert(url);
  window.location = url;
}

function magic_search() {
  if($("div.cvr").length != 1) { return; }
  if($("div.cvr span:contains('ophørt')").length > 0) {
    var name = $("div.cvr strong").text();
    if(name && window.location.href.match(/x=[0-9]{8}/)) {
      window.location = window.location.href.replace(/x=[0-9]{8}/, 'x='+name)      
    }
  }
}

$(function() {
  magic_search();
});


