// jQuery elastic plugin
(function(g){g.fn.extend({elastic:function(){var h=["paddingTop","paddingRight","paddingBottom","paddingLeft","fontSize","lineHeight","fontFamily","width","fontWeight"];return this.each(function(){function i(c,j){curratedHeight=Math.floor(parseInt(c,10));a.height()!=curratedHeight&&a.css({height:curratedHeight+"px",overflow:j})}function k(){var c=a.val().replace(/&/g,"&amp;").replace(/ /g,"&nbsp;").replace(/<|>/g,"&gt;").replace(/\n/g,"<br />"),j=b.html().replace(/<br>/ig,"<br />");if(c+"&nbsp;"!= j){b.html(c+"&nbsp;");if(Math.abs(b.height()+l-a.height())>3){c=b.height()+l;if(c>=d)i(d,"auto");else c<=e?i(e,"hidden"):i(c,"hidden")}}}if(this.type!="textarea")return false;var a=g(this),b=g("<div />").css({position:"absolute",display:"none","word-wrap":"break-word"}),l=parseInt(a.css("line-height"),10)||parseInt(a.css("font-size"),"10"),e=parseInt(a.css("height"),10)||l*3,d=parseInt(a.css("max-height"),10)||Number.MAX_VALUE,f=0;if(d<0)d=Number.MAX_VALUE;b.appendTo(a.parent());for(f=h.length;f--;)b.css(h[f].toString(), a.css(h[f].toString()));a.css({overflow:"hidden"});a.bind("keyup change cut paste",function(){k()});a.bind("blur",function(){if(b.height()<d)b.height()>e?a.height(b.height()):a.height(e)});a.live("input paste",function(){setTimeout(k,250)});k()})}})})(jQuery);


// main menu background fix
function fix_main_menu_background() {
    var _menu_width = 0;
    $('#menu-main-menu li').each(function(a, b) { _menu_width += $(b).width(); });
    $('#menu-main-menu').css({
      'background-position': (_menu_width-5)+'px 0px'
    });
  }
  fix_main_menu_background();





  // startpage position fix
  function startpage_position_fix() {
    var _main_height = ($(window).height() - ($('.header').outerHeight() + $('.footer').outerHeight())) - $('.startpage-wrapper').height();
    if(_main_height < 300) _main_height = 300;
    var _top = _main_height - $('.startpage-wrapper').height();
    $('.startpage-wrapper').css({
      'margin-top': _top/2
    });
  }














$(document).ready(function() {
  
  
  
  
  // add label to comment textarea
  $('textarea#comment').elastic().after('<label for="comment">Comment (required)</label>');
  
  // add IDs to labels in comment form
  $('#email, #url, #author, #comment').each(function(a, b) {
    $(b).next('label').attr('id', 'label-'+$(b).attr('id'));
  });
  
  // jquery infieldlabels
  $('.header .search #s, .sidebar .newsletter #nl-email, #email, #url, #author, #comment').each(function(a, b) {
    var _label = $(b).prev('label');
    if(_label.length == 0) _label = $(b).next('label');
    _label.inFieldLabels();
  });
  
  
  // ie last-child compatibility
  $('ul.first li:last').addClass('last-child');
  
  
  
  // product/blog gallery
  $('.product-details .images .thumbs a, .category-blog .images .gallery-thumbs a').each(function(a, b) {
    
    var _preload = new Image();
    _preload.src = $(b).attr('href');
    
    if(a == 0) { $(b).addClass('current'); }
    
    $(b).click(function(e) {
      e.preventDefault();
      $(b).closest('.images').find('.large img').attr('src', $(b).attr('href')).attr('rel', $(b).find('img').attr('rel'));
      
      // blog galleries only
      if($(b).closest('.gallery-thumbs').length == 1) {
        $(b).closest('.gallery-thumbs').find('a').removeClass('current');
        $(b).addClass('current');
      }
    });
    
    // blog galleries only
    if($(b).closest('.gallery-thumbs').length == 1 && a == 0) {
      $(b).closest('.images').find('.large img').css({ 'cursor': 'pointer' }).click(function(e) {
        var _next = $(b).closest('.gallery-thumbs').find('.current').next();
        if(_next.length == 0) _next = $(b).closest('.gallery-thumbs').find('a').first();
        _next.click();
      });
    }
    
  });


  // image zoom
  $('.product-details .images .large img').click(function() {
    $('#productzoom').html('<img src="'+ $(this).attr('rel') +'"/>').fadeIn().click(function() {
      $(this).fadeOut();
    });
  });
  
  
  
  
  
  
  
  
  
  // startpage slideshow resizing
  $(window).resize(function() {
    
    var _vp_width = $(window).width();
    var _vp_height = $(document).height()-$('#footer').height()+10;
    
    if(_vp_width < 950) _vp_width = 950;
    
    $('.home .startpage-slideshow .startpage-slide').css({
      opacity: 1
    })
    
    $('.startpage-slideshow .startpage-slide img').each(function(a, b) {
      $(b).width(_vp_width);
      $(b).css({ 'height': 'auto', 'margin-left': 0 });
      if($(b).height() < _vp_height) {
        $(b).height(_vp_height);
        $(b).css({ 
          'width': 'auto'
        });
      }
      $(b).css({
        'margin-left': (_vp_width - $(b).width()) / 2
      });
      
    });
    
  });
  
  
  
  
  
  // startpage slideshow fading
  $('.startpage-slideshow').each(function(a, b) {
    
    var _slides_counter = $(b).find('.startpage-slide').length;

    var _initial_z = _slides_counter+1;
    var _global_z = _slides_counter+1;
    
    // start slideshow only when more than one slide is given
    if(_slides_counter > 1) {
    
      $(b).find('.startpage-slide').each(function(c, d) {
        $(d).css({ 'z-index': _initial_z-- });
        if(c == 0) $(d).addClass('current');
      });
    
      window.setInterval(function() {
        var _next = $(b).find('.startpage-slide.current').next();
        if(_next.length == 0) _next = $(b).find('.startpage-slide').first();
        _next.hide().css({ 'z-index': _global_z++ }).fadeIn(1500, function() {
          $(b).find('.startpage-slide').removeClass('current');
          _next.addClass('current');
        });
      }, 5000);
     
    }
    
  });
  
  
  
  
  
  // startpage position fix
  $(window).resize(function() {
    startpage_position_fix();
  });
  
  
  // dynamicly expand the comment box
  $('#comment').expandingTextArea();
    
    
    
});























$(window).load(function() {
  
  // run the main menu background fix again, because the menu has
  // different dimensions as soon as the webfont has loaded
  fix_main_menu_background();
  startpage_position_fix();
  
  // update slideshow when all images are being loaded
  $(window).resize();
  
});
