// init documentready-function
jQuery(document).ready(function($) {

  if ($.browser.msie) {
    // add some ie-classes for css bugfixing
    if (parseInt($.browser.version, 10) === 6) $(document.body).addClass('ie6');
    if (parseInt($.browser.version, 10) === 7) $(document.body).addClass('ie7');
  }

  // Futura font. This is freaky in IE7, thus we dont replace the links in the
  // menu, crazy flicker-bug with canvas-links
  var replace = '#footer p, .startseite .address p, .startseite .teaser p, ' +
                '.startseite #content h2, .startseite #content h3';
  if (parseInt($.browser.version, 10) === 7) {
    Cufon.replace(replace, { fontFamily: 'Futura' });
    Cufon.now();
  } else {
    Cufon.replace('#nav > ul > li > a, ' + replace, { fontFamily: 'Futura' });
    Cufon.now();
  }
    
  // Special homepage-header-Content, zoom-in-movie
  if ($(document.body).hasClass('startseite')) {
    // create the tagline
    var tagline = $('<h3 class="tagline">Wir sind anders</h3>');
    $('#headerImages').append(tagline);

    if ($('#headerImages .images img').length) {
      // zoom in the image, shifting position with top/left
      $('#headerImages .images img').delay(1000).animate({
        width: 2293,
        height: 893,
        top: -380,
        left: -620
      }, 6000, function() {
        // then waiit 1 sec and zoom in the tagline
        Cufon.replace(tagline);
        Cufon.now();
        tagline.hide().delay(500).fadeIn(2000);
      });
    }
  }

  // newsletter accordion, yay
  if ($('#newsletter').length) {
    // add the corresponding links
    $('#newsletter h2').each(function(i) {
      var html = $(this).html();
      // wrap everything between the h2s in an extra div for accordion structure
      $(this).nextUntil('h2').wrap('<div class="accordionContent' + (i + 1) + '" />');
      $('.accordionContent' + (i + 1)).wrapAll('<div class="accordionWrapper" />');
      // and add a link to the h2
      $(this).html('<a href="#artikel-' + (i + 1) + '">' + html + '</a>');
      
    });
    
    // and apply the accordion with the current active tab
    var activeTab = 0;
    var url = document.location.href;
    var urlSplit = url.split('#artikel-');
    activeTab = parseInt(urlSplit[1], 10) - 1;
    
    $('#newsletter').accordion({
      autoHeight: false,
      active: activeTab,
      // and on each accordion change, change the url to the current tab
      changestart: function(event, ui) { 
        var currentActive = $('#newsletter').accordion('option', 'active');
        document.location.href = urlSplit[0] + '#artikel-' + (currentActive + 1);
      }
    });
  }

  // Add Colorbox to every csc-textpic element
  $('.csc-textpic').each(function(i) {
    $(this).find('a.colorbox').each(function() {
      var fTitle = $(this).attr('title');
      fTitle = fTitle.replace(/###/gi, '<br />');
      $(this).fancybox({
        titlePosition: 'over',
        title: fTitle
      });
    });
  });
    
  // hover-states for menu 1stchild
  // using js, because the ie-familiy sucks with hover on canvas/li
  $('#nav > ul > li > a').each(function() {
    var parent = $(this).parent();
    $(this).mouseenter(function() {
      parent.addClass('hoverOne');
      $(this).css('color', '#fff'); // manually set the color for ff-bugfixing
      if (parseInt($.browser.version, 10) !== 7) { // dont do this for ie7
        Cufon.replace($(this), { fontFamily: 'Futura' });
        Cufon.now(); 
      }
    });
    $(this).mouseleave(function() {
      parent.removeClass('hoverOne');
      $(this).css('color', '#58585a'); // manually set the color for ff-bugfixing
      if (parseInt($.browser.version, 10) !== 7) { // dont do this for ie7
        Cufon.replace($(this), { fontFamily: 'Futura' });
        Cufon.now(); 
      }
    });
  });
    
}); // end documentready

// Typo3-Functions for decrypting masked email-addresses, for spamprotection
function decryptCharcode(n, start, end, offset) {
    n = n + offset;
    if (offset > 0 && n > end) {
        n = start + (n - end - 1);
    } else if (offset < 0 && n < start) {
        n = end - (start - n - 1);
    }
    return String.fromCharCode(n);
}

function decryptString(enc, offset) {
    var dec = "";
    var len = enc.length;
    for (var i = 0; i < len; i++) {
        var n = enc.charCodeAt(i);
        if (n >= 0x2B && n <= 0x3A) {
            dec += decryptCharcode(n, 0x2B, 0x3A, offset);
        } else if (n >= 0x40 && n <= 0x5A) {
            dec += decryptCharcode(n, 0x40, 0x5A, offset);
        } else if (n >= 0x61 && n <= 0x7A) {
            dec += decryptCharcode(n, 0x61, 0x7A, offset);
        } else {
            dec += enc.charAt(i);
        }
    }
    return dec;
}

function linkTo_UnCryptMailto(s) {
    location.href = decryptString(s, -2);
}
