/*
 * global.js
 * 
 * @author Thomas Monzel | Apparat-Hamburg
 * @description Scripte die auf jeder Seite genutzt werden können
 */

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

/* 
 * Erweiterung von jQuery Funktionen
 *
 */
$.fn.hasChildren = function() {
    return this.children().size() > 0 ? true : false;
}

$.fn.removeChildAt = function(index) {
    return this.find(':eq(' + index + ')').remove();
}

$.fn.tooltip = function(text, topOffset) {
    var tl = $('#tip-layer');
    topOffset = topOffset ? topOffset : 15;

    var scope = this;
    this.mouseover(function() {
        tl.text(text);
        var ww = $(window).width();
        var lo = ($(this).width()-tl.outerWidth())/2;
        var to = -(tl.height()+topOffset);
        var l = $(this).offset().left+lo;
        var t = $(this).offset().top+to;
        if(l < 0) {
            l = 0;
        }

        if((l+tl.outerWidth()) > ww) {
            l = ww-tl.outerWidth();
        }

        tl.css({visibility:'visible', left:l, top:t});
    });

    this[0].ontouchend = function() {
        scope.trigger('mouseout');
    }

    this.mouseout(function() {
        tl.css({visibility:'hidden'});
        tl.empty();
    });
}

var openedInfobox = null;

$.fn.applyInfobox = function() {
  var scope = this;
  var headline = $('h3', this);
  var background = $('.background', this);
  var arrow = $('<span class="arrow">&nbsp;</span>');
  headline.append(arrow);

  
  var content = $('.content', this);
  var text = $('p', content);
  var opened = false;
  var pxClosedHeight = 29;
  var pxOpenedHeight = 40;

  $(window).resize(function() {
    if(scope.opened) scope.close();
  });

  this.css({height:pxClosedHeight+'px'});
  content.css({height:pxClosedHeight+'px'});

  headline.click(function() {
    if(opened) {
      scope.close();
    } else {
      scope.open();
    }
  });


  this.opened = false;

  this.open = function() {
    if(openedInfobox) {
      openedInfobox.close();
    }

    this.opened = true;

    var newTextHeight = pxOpenedHeight+text.height();
    openedInfobox = this;
    this.animate({height:newTextHeight+'px', top:-(newTextHeight-pxClosedHeight)}, 250);
    background.animate({opacity:0.8}, 250);
    content.css({height:newTextHeight+'px'});
    arrow.css('background-position', 'bottom');
    opened = true;
  }

  this.close = function() {
    this.opened = false;
    this.animate({height:pxClosedHeight+'px', top:0}, 250);
    background.animate({opacity:0.3}, 250);
    arrow.css('background-position', 'top');
    opened = false;
  }
}

function XDocument(obj) {
    this.id = obj.id;
    this.label = obj.label;
    this.url = obj.url;
    this.alias = obj.alias;
    this.type = obj.type;
    this.content = obj.content;

    delete obj.id;
    delete obj.label;
    delete obj.url;
    delete obj.alias;

    this.attributes = [];
    for(prop in obj) {
        this.attributes[prop] = obj[prop];
    }

    this.attr = function(name) {
        return this.attributes[name];
    }
}

var App = (function() {
    var c = function() {}
    var config = [];

    c.config = function(name, value) {
        if(value === undefined) {
            return config[name];
        } else {
            config[name] = value;
            return true;
        }
    }

    c.anchor = location.href.split('#')[1];
    
    c.makeAnchor = function(string) {
        if(string) {
            return '#' + string.replace(/\s/g, '+');
        }

        return '';
    }

    c.navigateToURL = function(url) {
        window.location.href = url;
    }

    c.document = null;
    c.eldest = null;
    c.isIPad = navigator.userAgent.match(/iPad/i) != null;
    c.isIE = $.browser.msie;

    return c;
})();

function replaceFonts() {
    // Schriften werden in Bilder umgewandelt
    Cufon.replace('#nav .nav-points a', {fontFamily:'GillSansLight', hover:true});
    Cufon.replace(['h1', 'h2', 'h3'], {fontFamily:'GillSansLight'});
    //Cufon.replace('span[style*=Frutiger]', {fontFamily:'Frutiger'});
    Cufon.replace('span[style*=GillSansLight]', {fontFamily:'GillSansLight'});
    Cufon.replace('span[style*=AvantGardeStd-XLt]', {fontFamily:'AvantGardeStd-XLt'});
    Cufon.replace('span[style*=RNSCamelia]', {fontFamily:'RNSCamelia'});
    Cufon.replace('span[style*=Bauhaus]', {fontFamily:'Bauhaus'});
    Cufon.replace('span[style*=BickhamScriptPro]', {fontFamily:'BickhamScriptPro'});
    Cufon.replace('span[style*=Bodon]', {fontFamily:'Bodon'});
    Cufon.replace('span[style*=BodoniBE]', {fontFamily:'BodoniBE'});
    Cufon.replace('span[style*=DayRoman]', {fontFamily:'DayRoman'});
    Cufon.replace('span[style*=Doergon]', {fontFamily:'Doergon'});
    Cufon.replace('span[style*=Engebrechtre]', {fontFamily:'Engebrechtre'});
    Cufon.replace('span[style*=Exmouth]', {fontFamily:'Exmouth'});
    //Cufon.replace('*[style*=Helvetica]', {fontFamily:'Helvetica'});
    //Cufon.replace('*[style*=LucidaSans]', {fontFamily:'LucidaSans'});
    Cufon.replace('span[style*=MKorsair]', {fontFamily:'MKorsair'});
    Cufon.replace('span[style*=OptimusPrinceps]', {fontFamily:'OptimusPrinceps'});
}

/*
 * DOM - ready
 *
 */
$(function() {
    $(this.body).prepend($('<div/>').attr('id', 'tip-layer'));
    replaceFonts();
});
