/*!
 * Bremer Modernisieren Landingpage
 * -----------------------------------------------------------------------------
 *
 * @version       $Id: Category.php 714 2011-12-21 07:38:02Z hieblmedia $
 * @author        Reinhard Hiebl
 * @copyright     Copyright (C) Reinhard Hiebl, http://www.hieblmedia.de
 * @license       Non-Public, Commercial
 *
 */

$(document).ready(function() {

  $('body').removeClass('nojs');

  var headerTop = $('#header_top');
  var cloudsEl = $('#clouds');
  headerTop.height(0).show();
  var setFullscreen = (function(){
    headerTop.height(0);

    var wh = $(window).height();
    var dh = $('body').height();

    if(dh < wh){
      cloudsEl.css('margin-top', (wh-dh));
      headerTop.height((wh-dh));
    } else {
      headerTop.height(0);
      cloudsEl.css('margin-top', '');
    }
  }); setFullscreen();
  $(window).resize(setFullscreen);


  var imageWidth = 1340;
  var scrollClouds = (function(){
    cloudsEl.animate({
      backgroundPosition: imageWidth+' 0'
    }, 80000, "linear", function() {
      cloudsEl.css("background-position", '0px 0px');
      scrollClouds();
    });
  }); scrollClouds();


  var items = $('.item');
  var currentItem = items.eq(0);
  items.hide().removeClass('hideme');
  items.eq(0).show();

  items.each(function(){
    $(this).width(currentItem.width());
    $(this).css('float', 'left');
    $(this).attr('tabindex', '-1');
  });

  $('.item_text_content').attr('tabindex', '-1');
  var contentEl = $('.item_text_content').eq(0);
  var contentElHeight = contentEl.height();
  $('.item_image_point a').click(function(){
    var el = $(this);
    var elParent = el.parent();

    var item = el.closest('.item');
    var contentEl = item.find('.item_text_content');
    var parentEl = contentEl.parent();

    var defaultText = item.data('default_text');
    if(!defaultText) {
      defaultText = contentEl.html();
      item.data('default_text', defaultText);
    }

    var textEl = el.find('span.middle');
    textElWidth = textEl.width();

    if(el.hasClass('active')) {
      el.removeClass('active');
      parentEl.stop().animate({height: 0}, 150, function(){
        contentEl.html(defaultText);
        elParent.css('z-index', '');
        parentEl.animate({height: contentElHeight}, 250, function(){
          //contentEl.css('overflow', 'visible'); // damit schatten nicht abgeschnitten werden
        });
      });
      return false;
    }

    $('.item_image_point a.active', item).each(function(){
      $(this).removeClass('active').parent().css('z-index', '');
    });
    textEl.css({width: 0, overflow: 'hidden'});
    textEltxt = textEl.find('.txt');
    textEltxt.css({height: textEltxt.height(), overflow: 'hidden'});
    textEl.stop().animate({width: textElWidth}, 250, function(){});

    var id = el.attr('href');
    id = id.substring(id.indexOf("#")); // ie <= 7 return full url argh :(
    var content = $(id).html();

    el.addClass('active');
    elParent.css('z-index', 1000);

    parentEl.stop().animate({height: 0}, 150, function(){
      contentEl.html(content);
      contentEl.css({'visibility': 'hidden', 'height': 'auto'});
      var newHeight = contentEl.height();
      contentEl.css({'visibility': 'visible', 'height': '0'});
      parentEl.animate({height: newHeight}, 1000, function(){
        //contentEl.css('overflow', 'visible'); // damit schatten nicht abgeschnitten werden
        contentEl.focus();
      });

    });
    return false;
  });

  var runningAnimation = false;
  $('.buttonset_next').show().find('a').click(function(event){
    if(runningAnimation) { return false; }
    var next = currentItem.next();
    if(!next.length) {
      next = $('.item').eq(0);
      next.insertAfter(currentItem);
    }

    var bodyInner = $('#body_inner');
    var pageMargins = $('#main_content').closest('.page_margins');

    var marginRight = ($(window).width() - (currentItem.position().left + currentItem.width()));
    if(marginRight < 0) { marginRight = 0; }
    currentItem.css('margin-right', marginRight);
    pageMargins.css('width', '100%');
    bodyInner.css('overflow', 'hidden');

    $('#items').width( (items.eq(0).width() * items.length) * 2);

    //currentItem.css('margin-left', -(currentItem.width()+marginRight));

    next.show();
    runningAnimation = true;
    $('.buttonset_next').css('opacity', 0.5);
    currentItem.animate({
      'margin-left': -(currentItem.width()+marginRight)
    }, 1500, function() {
      currentItem.hide();
      currentItem.css('margin-left', '');
      currentItem.css('margin-right', '');
      $('#items').css('width', '');

      pageMargins.css('width', '');
      bodyInner.css('overflow', '');

      currentItem = next;
      currentItem.focus();
      $('.buttonset_next').css('opacity', '');

      // pointer zurücksetzten ???
      //$('.item_image_point a.active:visible').eq(0).trigger('click');
      //$('.item_image_point a.active:visible').removeClass('active');

      runningAnimation = false;
    });
    $('#main').stop().animate({
      backgroundPosition: '-='+(currentItem.width()+marginRight)+' 100%'
      }, 700, function() {

    });
    cloudsEl.stop().animate({
      backgroundPosition: '-='+((currentItem.width()+marginRight)/3)+' 0'
    }, 1500, function() {
      scrollClouds();
    });

    return false;
  });

  $('a').click(function(e){
    e.stopPropagation();
  });
  $('body').click(function(e){
    if(!$(e.target).is('a')){
      $('.item_image_point a.active:visible').eq(0).trigger('click');
      $('.item_image_point a.active:visible').removeClass('active');
    }
    return true;
  });


  $('body').addClass('js');
});

var Preload = (function(images){
  if(typeof(images) == 'object') {
    jQuery.each(images, function(i, image){
      var tmp = new Image();
      tmp.src = image;
    });
  }
});

(function($) {
  if(!document.defaultView || !document.defaultView.getComputedStyle){
      var oldCurCSS = jQuery.curCSS;
      jQuery.curCSS = function(elem, name, force){
          if(name === 'background-position'){
              name = 'backgroundPosition';
          }
          if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
              return oldCurCSS.apply(this, arguments);
          }
          var style = elem.style;
          if ( !force && style && style[ name ] ){
              return style[ name ];
          }
          return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
      };
  }

  var oldAnim = $.fn.animate;
  $.fn.animate = function(prop){
      if('background-position' in prop){
          prop.backgroundPosition = prop['background-position'];
          delete prop['background-position'];
      }
      if('backgroundPosition' in prop){
          prop.backgroundPosition = '('+ prop.backgroundPosition + ')';
      }
      return oldAnim.apply(this, arguments);
  };

  function toArray(strg, start){
      strg = strg.replace(/left|top/g,'0px');
      strg = strg.replace(/right|bottom/g,'100%');
      strg = strg.replace(/([+\-]=)?(-?[0-9\.]+)(\s|\)|$)/g,"$1$2px$3");
      var res = strg.match(/([+\-]=)?(-?[0-9\.]+)(px|\%|em|pt)\s([+\-]=)?(-?[0-9\.]+)(px|\%|em|pt)/);

      if(typeof(start) == 'object' && start.length) {
        if(res[1] === '-=') { res[2] = start[0] - res[2]; }
        if(res[1] === '+=') { res[2] = start[0] + res[2]; }
        if(res[4] === '-=') { res[5] = start[2] - res[5]; }
        if(res[4] === '+=') { res[5] = start[2] + res[5]; }
      }

      return [parseFloat(res[2],10),res[3],parseFloat(res[5],10),res[6]];
  }

  $.fx.step.backgroundPosition = function(fx) {
      if (!fx.bgPosReady) {
          var start = $.curCSS(fx.elem,'backgroundPosition');

          if(!start){//FF2 no inline-style fallback
              start = '0px 0px';
          }
          start = toArray(start);

          fx.start = [start[0],start[2]];

          var end = toArray(fx.end, start);
          fx.end = [end[0],end[2]];

          fx.unit = [end[1],end[3]];
          fx.bgPosReady = true;
      }

      var nowPosX = [];
      nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
      nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
      fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
  };
  })(jQuery);
