(function () {
  var $ = jQuery;
  var lib = fff;

  // checks if the link is a local link
  // this must begin with /
  // if the given href begins with the protocol and our own domain, these are stripped of,
  // so that we always get back only the local link
  // links with a query are not treated as local
  lib.getlocallink = function (href) {
    // check for query 
    var r = href.indexOf('?');
    if (r >= 0) {
      // the link contains a query
      // these links should not be handled by ajax
      return undefined;
    }
    if (!href) return undefined;
    // matches everything beginning with /
    if (href.match(/^\/(.*)?$/)) {
      return href;
    }
    var start = window.location.protocol + '//' + window.location.host;
    r = href.indexOf(start);
    if (r < 0) {
      return undefined;
    }
    href = href.slice(r+start.length);
    return href;
  }
  
  // transform blog images in a form suitable for the gallery plug in
  lib.handleBlogImages = function (callback) {
    var $p = $('#cu .gallery-placeholder');
    if (!$p) {
      // this means, this is not a blog page
      callback();
      return;
    }
    var imgs = $('#cu .blog-post img');
    if (imgs.length < 1) {
      // we make a gallery only if there are more than 2 pics
      callback();
      return;
    }
    if (imgs.length < 2) {
      // we have exactly 1 image
      var i = $(imgs[0]);
      $p.append(i);
      i.wrap('<div class="bildrahmen"></div>');
      callback();
      return;
    }
    // construct the parts needed for the ad gallery
    var adgal = $('<div class="ad-gallery"></div>');
    adgal.append('<div class="ad-image-wrapper"></div><div class="ad-controls"></div>');
    var ul = $('<ul class="ad-thumb-list"></ul>');
    imgs.each(function () {
      var i = $(this)
      ul.append(i);
      i.wrap('<li><a href="#"></a></li>');
      i.parent('a').attr('href',i.attr('src'));
      i.removeAttr('height');
      i.removeAttr('width');
      i.removeAttr('title');
      i.removeAttr('alt');
    });
    adgal.append(ul);
    ul.wrap('<div class="ad-nav"><div class="ad-thumbs"></div></div>');
    $p.append(adgal);
    callback();
  }

  
  lib.loadContent = function(param, successcallback, cancelcallback) {
    var dataParam = $('body').attr('data-path');
    //    console.log('data-path:'+dataParam);
    if (dataParam == param) {
      //      console.log('loadContent cancelled because content already loaded');
      // but we have to restore the title
      var title = $('#cu').attr('data-title');
      if (title) {
        document.title = title;
      }
      if (cancelcallback) {
        cancelcallback();
      }
      return;
    } else {
    //      console.log('now loading content:'+param);
    }
    // now load the content via ajax
    var successHandler =function(html) {
      // Each content page should start with a special comment (see: docs/readme.txt)
      // We extract the metainfo in this comment here
      //      console.log('ajax success');
      var re=/^\s*<!--([\s\S]*?)-->/;
      var a = re.exec(html);
      if (a) {
        var meta = a[1]
        // In javascript, the dot never matches newlines, so we get the rest of the line,
        // where the line started with withespace - tit - more letters or whitespace - :
        var titleA = /[\s\r\n]*tit(?:[\w\s]*)?:(.*)/.exec(meta);
        //            var descA = /[\s\r\n]*des(?:[\w\s]*)?:(.*)/.exec(meta);
        //            var keywA = /[\s\r\n]*key(?:[\w\s]*)?:(.*)/.exec(meta);
        if (titleA) {
          var title = titleA[1];
          document.title = title;
        }
        html = html.replace(re,'');
      }
      else {
        lib.warn('missing metadata for content: '+param);
      }
      // save to data-path
      $('body').attr('data-path', param);
      if (successcallback) {
        // give back the result in the callback
        successcallback(html);
      }
    }

    var errorHandler =function (request, status, error) {
      lib.warn('ajax error');
      loadContent('error.html');
      if (successcallback) {
        successcallback();
      }
    }

    $.ajax({
      dataType: 'html',
      url: '/ajax'+param,
      success: successHandler,
      error: errorHandler
    });
  }
  
  

  
})(); // end of scope fn


