// funktionen
(function($){

  $.fn.defaultValue = function(){
    return this.each(function(){
      if ($(this).val() == ''){
        $(this).val($(this).attr('replace_value'));
      }
      $(this).bind('focus', function(){
        if ($(this).val()  == $(this).attr('replace_value')){
          $(this).val('');
        }
      });
      $(this).bind('blur', function(){
        if ($(this).val() == ''){
          $(this).val($(this).attr('replace_value'));
        }
      });
    })
  }
  $.fn.vAlignBottom = function(){
    return this.each(function(){
      var placeHolder  = $(this).clone(false);
      placeHolder.insertBefore($(this));
      placeHolder.css('visibility', 'hidden');
      placeHolder.removeClass('valign');
      
      var parent = $(this).parent();
      parent.css('position', 'relative');
      $(this).css('position', 'absolute');
      $(this).css('bottom', '0px');
      if ($(this).hasClass('moreInfo')){
        $(this).css('right', '0px');
        $(this).css('marginRight', '0px');
      }
    })
  }
    $.fn.sameHeight2 = function(el, childEl, reset){
      var that = this;
      if (reset == null){
        $('#corpus').bind('changeFontSize', function(){$(that).sameHeight2(el, childEl, 1);});
      }
      var maxHeight = 0;
      var curHeight = 0;
      var getData = function (parentEl, el, childEl){
        var data = {};
        data.maxHeight = 0;
        //data.curHeight = 0;
        data.els = new Array();
        data.ignoreEls = new Array();
        $(parentEl).find(el).each(function(){
          if (childEl) {
            var dataChild = getData(this, childEl);
            data.els = data.els.concat(dataChild.els);
            if (dataChild.maxHeight > data.maxHeight) {
              data.maxHeight = dataChild.maxHeight;
            }
          } else {
            if ($(this).css('display') != 'none') {  
              if ($(this).data('curHeight') == null) {
                $(this).data('curHeight', $(this).height());
              }
              curHeight = $(this).height();
              //console.log(this);
              //console.log(curHeight);
              if (curHeight > data.maxHeight) data.maxHeight = curHeight;
              data.els.push($(this));
            } else {
              data.ignoreEls.push($(this));
            }
          }
        });
        return data;
      };
      var resizeNow = function(data){
        var newHeight = data.maxHeight;
        //alert ('resizeTo::'+newHeight);
        $.each(data.els, function(index, value) { 
          $(value).height(newHeight+'px');
          $(value).addClass('resize2');
          //$(value).css('background', 'green');
          //console.log(value); 
        });      
      }
      return this.each(function(){
        var data  = getData(this, el, childEl);
        resizeNow(data);
      });
    
    }
  $.fn.sameHeight = function(el, childEl, reset){
    var that = this;
    if (reset == null)
    {
      $('#corpus').bind('changeFontSize', function(){
        $(that).sameHeight(el, childEl, 1);
      });
    }
    var maxHeight = 0;
    var curHeight = 0;
    return this.each(function(){
      maxHeight = 0;
      curHeight = 0;
      $(this).find(el).each(function()
      {
        if (reset != null) $(this).height('auto');
        
        if (childEl != null){
          $(this).find(childEl).each(function(){
            if ($(this).css('display') != 'none'){
              if ($(this).data('curHeight') == null) {
                $(this).data('curHeight', $(this).height());
              }
              //console.log($(this));
              curHeight = $(this).height();
              if (curHeight > maxHeight) maxHeight = curHeight;
              //console.log(maxHeight);
            }
          });
        } else {
          if ($(this).data('curHeight') == null) {
            $(this).data('curHeight', $(this).height());
          }
          curHeight = $(this).height();
          if (curHeight > maxHeight) maxHeight = curHeight;
        }
      })
      
      if (maxHeight >0){
          if (childEl != null){   
            $(this).find(el).each(function(){
              $(this).find(childEl).each(function(){
                //console.log('setze hoehe von'+$(this).html()+' auf '+maxHeight);
                $(this).height(maxHeight+'px');
              });
            });
          } else { 
            $(this).find(el).each(function(){
              // alert('set Height from '+$(this).height() + '('+$(this).data('curHeight')+') to '+maxHeight);
              if ($(this).height() < maxHeight) {
                $(this).height(maxHeight+'px');
              } 
              //  console.log('setze hoehe von'+$(this).html()+' auf '+maxHeight);
              //$(this).find(el).height(maxHeight+'px');
            });
          }
      }
    })
  }
  
  var nonverblasterLoaded = false;
  $.fn.videobox = function(){
    if (!nonverblasterLoaded){
          $.getScript("/fileadmin/templates/v1/js/jquery.swfobject.1-1-1.js");
          $.getScript("/fileadmin/templates/v1/js/nonverblaster.js");
      nonverblasterLoaded = true;     
    }
    // wrapper fuer singlePlayer
    var singlePlayer;
    function getSinglePlayer(){
      if (singlePlayer == null){
        var Wrapper = $('<div id="singlePlayerWrapper" style="display:none;" />');
        $('h1:first').after(Wrapper);
        Wrapper.append('<div id="singlePlayer"/>');
        singlePlayer= $('#singlePlayer');
        singlePlayer.width(640);
        singlePlayer.height(360);
      }
      return singlePlayer;
    };
    
    var lastPlayed =   'empty';
    var autoPlay = 'true';
    function playInWrapper(wrapper, videoSrc, teaserImg){
      var playerStatus = wrapper.attr('playerStatus');
      var flashId = wrapper.attr('id')+'_flash';
      
      wrapper.empty();
      lastPlayed = videoSrc;
      //var videoSize = {'x':wrapper.width(), 'y':wrapper.height()};
      var videoSize = {'x':640, 'y':360};
      wrapper.flash({
          swf: '/fileadmin/templates/v1/js/NonverBlaster.swf',
          id: flashId,
          width: videoSize.x,
          height: videoSize.y,
          bgcolor: '#ffffff',
          wmode: 'transparent',        
          allowFullScreen:'true',
          allowScriptAccess:'always',

          // these arguments will be passed into the flash document
          flashvars: {
            videoURL: videoSrc,
            teaserurl: teaserImg,
            allowSmoothing: 'true',
            autoPlay: autoPlay,
            buffer: '3',
            showTimeCode: 'true',
            loop: 'false',
            controlColor: '0xdddddd',    
            //controlColour: '0xff0000',
            scaleIfFullscreen: 'true',
            showScalingButton: 'true',
            crop: 'true'
          }
        });
      if (autoPlay == 'false'){
        wrapper.attr('playerStatus', 'pause');
        autoPlay = 'true';
      }
    };
    return this.each(function(index, el){
      var data = {    
        'autoPlay': true,
        'type': 'modal',
        'src' : 'test',
        'size': {
          'x':640,
          'y':360
        },
        'preview': 'test',
        'desc':'test'
      };
      var imgWrapper = $(el).find('.image').first();
      var image = $(imgWrapper).find('img').first();
      var desc = $(el).find('.desc').first();
      data.src = imgWrapper.attr('src');
      
      var testSrc = data.src.substr(7);
      if (testSrc != 'http://') {
        data.src = '/'+data.src;
      }
      data.preview = image.attr('src');
      data.desc = desc.text();
      //data.size.x = image.attr('width');
      //data.size.y = image.attr('height');
      //data.size.y = image.attr('height');
      $(el).data('video',  data);
      $(el).addClass('playVideo');
      $(el).css('cursor', 'pointer');
      $(el).bind('click', function(e){
        data.wrapper = '#singlePlayerWrapper'; //getSinglePlayer();
        playInWrapper(getSinglePlayer(), data.src, data.preview);
        $(el).trigger('playVideo', data);
        //console.log($(this).data('video'));      
      });
    });
  }
    
  //var nonverblasterLoaded = false;
  $.fn.videoplayer = function(){
    if (!nonverblasterLoaded){
          $.getScript("/fileadmin/templates/v1/js/jquery.swfobject.1-1-1.js");
          $.getScript("/fileadmin/templates/v1/js/nonverblaster.js");
      nonverblasterLoaded = true;     
    }
    var loadFlash = function(){
      
    };
    var opacityValueStart = 0.6;
    var singlePlayer;
    function getSinglePlayer(){
      if (singlePlayer == null){
        var Wrapper = $('<div class="csc-default" style="margin:0 10px;" />');
        $('h1:first').after(Wrapper);
        Wrapper.append('<div id="singlePlayer"/>');
        singlePlayer= $('#singlePlayer');
        singlePlayer.width(640);
        singlePlayer.height(360);
      }
      return singlePlayer;
    };
    var lastPlayed =   'empty';
    var autoPlay = 'false';
    
    
    function playInWrapper(wrapper, videoSrc, teaserImg){
      var playerStatus = wrapper.attr('playerStatus');
      var flashId = wrapper.attr('id')+'_flash';
      if (lastPlayed == videoSrc) {
        registerForJavaScriptCommunication(flashId);
        if (playerStatus != 'pause'){
          sendToNonverBlaster('pause');
          wrapper.attr('playerStatus', 'pause');
        } else {
          sendToNonverBlaster('play');
          wrapper.attr('playerStatus', 'play');
        }
        return false;
      }
      
      wrapper.empty();
      $('div [src='+lastPlayed+']').fadeTo('fast', opacityValueStart).attr('opacityValue' , opacityValueStart);
      $('div [src='+videoSrc+']').fadeTo('fast', 1).attr('opacityValue' , 1);
      lastPlayed = videoSrc;
      var videoSize = {'x':wrapper.width(), 'y':wrapper.height()};
      wrapper.flash({
          swf: '/fileadmin/templates/v1/js/NonverBlaster.swf',
          id: flashId,
          width: videoSize.x,
          height: videoSize.y,
          bgcolor: '#ffffff',
          wmode: 'transparent',
          // these arguments will be passed into the flash document
          flashvars: {
            videoURL: videoSrc,
            teaserurl: teaserImg,
            allowSmoothing: 'true',
            autoPlay: autoPlay,
            buffer: '3',
            showTimeCode: 'true',
            loop: 'false',
            controlColor: '0xdddddd',
            //controlColour: '0xff0000',
            scaleIfFullscreen: 'true',
            showScalingButton: 'true',
            crop: 'true'
          }
        });
      if (autoPlay == 'false'){
        wrapper.attr('playerStatus', 'pause');
        autoPlay = 'true';
      }
    };
    var firstEl = true;
    return this.each(function(index, el){
      var wrapper = $(el);
      var videoSrc = $(el).attr('src');
      var testSrc = videoSrc.substr(7);
      if (testSrc != 'http://') {
        videoSrc = '/'+videoSrc;
        $(el).attr('src',  videoSrc);
      }
      
      var videoWidth = $(el).width();
      var videoHeight = $(el).height();
      var teaserImg = $(el).find('img');
      if (teaserImg != null) teaserImg = teaserImg.attr('src');
      if (videoWidth == '640') videoHeight = 360;
      $(el).fadeTo('fast', opacityValueStart);
      $(el).attr('opacityValue', opacityValueStart);
      $(el)
        .mouseenter(function(e){
          e.stopPropagation();
          $(this).fadeTo('fast', 1)})
        .mouseleave(function(e){
          e.stopPropagation();
          var opacValue = $(this).attr('opacityValue');
          $(this).fadeTo('fast', opacValue)
         });
      
      if (firstEl){
        if ($(el).attr('playinside') != '1'){
          firstEl = false;
          playInWrapper(getSinglePlayer(), videoSrc, teaserImg);
        }
      }
      $(el).css('cursor', 'pointer');
      $(el).bind('click', function(e){
        if ($(el).attr('playinside') == '1'){
          videoWrapper = $(el);
          $(el).unbind('click');
        }else {
          videoWrapper = getSinglePlayer();
        }
        playInWrapper(videoWrapper, videoSrc, teaserImg);
        /*
        $(el).flash({
          swf: '/fileadmin/templates/v1/js/NonverBlaster.swf',
          width: videoWidth,
          height: videoHeight,
          bgcolor: '#ffffff',
          wmode: 'transparent',
          // these arguments will be passed into the flash document
          flashvars: {
            videoURL: videoSrc,
            allowSmoothing: 'true',
            autoPlay: 'true',
            buffer: '3',
            showTimeCode: 'true',
            loop: 'false',
            controlColor: '0xdddddd',
            //controlColour: '0xff0000',
            scaleIfFullscreen: 'true',
            showScalingButton: 'true',
            crop: 'true'
          }
        });
        */
      });

      //console.log(this);
    })
  };
  
  $.fn.showhideContent = function(){
    
    function toggleContent(useContent, start){
      var id = $(useContent).attr('id');
      var toggler = $('[togglecontent='+id+']'); 
      
      if ($.cookie(id+'_toggle') == null){
          $.cookie(id+'_toggle', 'show', { expires: 7, path: '/'});
      } 
      if (start != null)
      {
        if ($.cookie(id+'_toggle') == 'hide'){
          useContent.hide();
          toggler.addClass('show').removeClass('hide');
        } else {
          toggler.removeClass('show').addClass('hide');
        }
      } else {
        useContent.slideToggle('fast', function() { 
          //if ($(this).is(':hidden')){
          if ($.cookie(id+'_toggle') == 'show'){
            toggler.addClass('show').removeClass('hide');
            $.cookie(id+'_toggle', 'hide', { expires: 7, path: '/'});
          } else {
            toggler.removeClass('show').addClass('hide');
            $.cookie(id+'_toggle', 'show', { expires: 7, path: '/'});
          }
        });
      }
    };
    
    
    return this.each(function(){
      var useContent = $(this).attr('togglecontent');
      useContent = $('#'+useContent);
      
      if (useContent){
        toggleContent(useContent, 1);
      }
      
      $(this).bind('click', function(e){
        e.stopPropagation();
        if (useContent){
          toggleContent(useContent);
        }
         
         return false;
      });
    
    });
    
  }
  
  $.fn.fontResizer = function(options){
    var defaults = {
      controlBox: '.resizeControls',
      minRatio : 0.6,
      maxRatio : 2.5,
      //elements: '.bodytext, .boxContent'
      elements: '.bodytext, td.item, .align-justify, .align-right, .align-left, .csc-bulletlist, .boxContent li, .news-single-item p, .news-list-item p'
    };
    var options = $.extend(defaults, options);    
    var fontRatio = 1;
    
    return this.each(function(){ 
      if ($('body').attr('id') == 'print') return;
      var that  =this;
      var decreaseFont = function(){
        fontRatio = (parseFloat(fontRatio*10) - 1) /10;
        if (fontRatio <= options.minRatio) fontRatio = options.minRatio;
      }
      var resetFont = function(){
        fontRatio = 1;
      }
      var increaseFont = function(){
        fontRatio = (parseFloat(fontRatio*10) + 1) /10;
        if (fontRatio >= options.maxRatio) fontRatio = options.maxRatio;
      }
      var changeFontSize = function(){
        $.cookie('fontRatio', fontRatio, { expires: 7, path: '/'}); 
        $(that).find('.resizeable').each(function(){
          if ($(this).data('originalFontSize') == null)
          {
            $(this).data('originalFontSize', $(this).css('font-size'));
          }
          
          if ($(this).data('originalLineHeight') == null)
          {
            $(this).data('originalLineHeight', $(this).css('line-height'));
          }
          var newFontSize = Math.round(parseFloat($(this).data('originalFontSize'), 10) * fontRatio);
          var newLineHeight = Math.round(parseFloat($(this).data('originalLineHeight'), 10) * fontRatio);
          $(this).css('font-size', newFontSize);
          $(this).css('line-height', newLineHeight+'px');
        });
        $(that).trigger('changeFontSize'); 

      }
          
          
      $(this).find(options.elements).addClass('resizeable');
      var controlBox = $(options.controlBox);
      var decreaser = $('<a href="#decrease"><span class="icons icons_aminus">-</span></a>');
      var resetter  = $('<a href="#reset"><span class="icons icons_areset">o</span></a>');
      var increaser = $('<a href="#increase"><span class="icons icons_aplus">+</span></a>');
      controlBox.append(decreaser).append(resetter).append(increaser);
      controlBox.children('a').click(function(e, el){
        switch (e.currentTarget.hash){
          case '#increase': increaseFont();break;
          case '#reset': resetFont();break;
          case '#decrease': decreaseFont();break;
        }
        changeFontSize();
        return false;
      })
        
      if ($.cookie('fontRatio') == null){
        $.cookie('fontRatio', fontRatio, { expires: 7, path: '/'}); 
      } else {
        var tmpFontRatio = $.cookie('fontRatio');
        if (tmpFontRatio != fontRatio)
        {
          fontRatio = tmpFontRatio;
          changeFontSize();
        }
      }
      
    });
  }
})(jQuery);




     


// ausfuehren
(function($){
  $(document).ready(function() {
    $('#corpus img').removeAttr('height');
    $('#corpus img').removeAttr('width');
    $('#content .bg-shadow').each(function(){
      var newWidth = $(this).width() + 4;
      $(this).width(newWidth);
    });
    //$('.news-list-morelink').children('a').addClass('valign');
    $('#corpus').fontResizer();
    $('.videobox').videobox();
    
    function slideShowCallback(curr, next, opts) {
      var curr = this;
      var parent = $(this).parent();
      if (parent.height() > $(curr).height())
      {
        parent.animate({
          height: $(curr).height()
        }, 500, function() {});
      }
    }
    $('.slideshow').cycle({ 
      fx:      'fade', //'scrollLeft',
      direction: 'left', 
      //speedIn:  5000, 
      //speedOut: 1500, 
      //easeIn:  'bounceout', 
      //easeOut: 'backin', 
      speed: 5000,
      before: slideShowCallback
      //delay:   -2000 
    });
    
    $('[replace_value]').defaultValue();
    
    $('.stripes').children('div:odd').addClass('odd');
    $('.stripes').children('div:even').addClass('even bg-grey');
        
    $('table.downloads tbody').each(function(){
      $(this).children('tr.downloadItem:even').addClass('even');
      $(this).children('tr.downloadItem:odd').addClass('odd');
    });
    //$('tr.downloadItem:even').addClass('even');
    //$('tr.downloadItem:odd').addClass('odd');
    
    
    //Set default open/close settings
    
    $('.acc_container').hide().addClass('hide'); //Hide/close all containers
    $('.acc_trigger:first').addClass('hide').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

    //On Click
    $('.acc_trigger').click(function(){
      if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
        //$('.acc_trigger').removeClass('hide').next().slideUp(); //Remove all "active" state and slide up the immediate next container
        //$(this).toggleClass('hide').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
        $(this).toggleClass('hide').next().slideDown();
      } else {
        $(this).removeClass('hide').next().slideUp();
      }
      return false; //Prevent the browser jump to the link anchor
    });
    
  });
  $(window).load(function(){
      $('.valign').vAlignBottom();
      $('.toggler').showhideContent();
      $('#corpus .grid-row').sameHeight2('.csc-default', 'h2:first');
      $('#corpus .grid-row').sameHeight('.csc-default', 'h3:first');
      $('#corpus .grid-row').sameHeight2('.box', 'h2:first');
      $('#corpus .grid-row').sameHeight('.innerWrapLeftCorner');
      $('#corpus .grid-row').sameHeight('.box');
      $('#corpus .grid-row').sameHeight('.inner');
  });
})(jQuery);
  


