$.fn.carousel = function (maxHeight) {
  // for multiple carousels we need $(this).each!

  // debug
  value = true;
  //maxWidth = 150;

  // count items
  itemAmount = $(this).find('a img').size();

  // measure items for biggest height, width
  //maxHeight = 0;
  //$(this).find('a img').each(function() {
  //	if ($(this).width() > maxWidth) { maxWidth = $(this).width() }
  //	if ($(this).height() > maxHeight) { maxHeight = $(this).height() }
  //});

  // scale images
  $(this).find('a img').each(function() {
    // prevent height settings from working
    // $(this).attr('height', '');

    quotient = $(this).width() / $(this).height(); // width / quot = height, height * quot = width

    if ($(this).attr('width')) {
      if (($(this).width() / quotient) > maxHeight) {
        $(this).attr('width', '');
        if ($(this).height() > maxHeight) {
          $(this).height(maxHeight)
        }
        // how to catch a width > natural width problem?
      }
      // ignore heights
    } else {
      if ($(this).height() > maxHeight) {
        $(this).height(maxHeight)
      }
    }
    // padding-top = parseInt(maxHeight - height /2)
    var myVal = parseInt((maxHeight - $(this).height()) / 2);
    myVal += 'px'
    $(this).css('padding-top', myVal);
  });

  // calculate total width of all images, resize wrap to fit
  totalWidth = 0;
  $(this).find('a').each(function() {
    totalWidth += $(this).outerWidth(true);
  });
  $(this).attr('totalWidth', totalWidth);
  $(this).find('.scroll-box').width(totalWidth);

  // set height of carousel
  $(this).css('height', $(this).height());


  // duplicate scroll-box
  //$(this).find('.scroll-box').clone().appendTo(this);

  // change scroll-box styling
  $(this).find('.scroll-box').addClass('animated');
  $(this).find('.scroll-box').eq(1).css('left', totalWidth);
  return value;
};

function initCarousel() {
  $('#panorama ul li').css('width', $('#panorama').width() + 'px');
  //$('.row.customer-reel .carousel').css('width', $('#panorama').width() + 'px');

  $('#panorama ul').trigger("destroy", true);
  $('#panorama ul').carouFredSel({
            circular: true,
            infinite: true,
            width: 'auto',
            auto   : {
              play: true,
              pauseDuration: 5000
            },
            scroll  : {
              items  : 1,
              pauseOnHover: true
            },
            prev  : {
              button  : "#prev-1",
              key    : "left"
            },
            next  : {
              button  : "#next-1",
              key    : "right"
            },
            pagination  : "#pager-1"
          });
}

$.fn.fitImage = function () {
	$(this).each(function() {
		$(this).wrap('<div class="img-fitter"></div>');
		myHeight = $(this).height();
		if (myHeight < 10) { myHeight = 200 }
		$(this).parent().height(myHeight);
		var myBG = 'url(' + $(this).attr('src') + ') center center no-repeat';
		$(this).parent().css('background', myBG);
		$(this).hide();
	});
	return true;
};

$(window).resize(function() {
  //$('#panorama ul').trigger("updateSizes");
  if (jQuery(window).width() > 1150) {
    jQuery('.container').addClass('shadow-ie');
  } else {
    jQuery('.container').removeClass('shadow-ie');
  }
  initCarousel();

});


// doc ready for chrome
$(window).load(function() { 

  $('.carousel').carousel(70);
  $('.carousel').height('70px');

  // ------ carouFredSel init & controls ------
  initCarousel();

  // img fitter
  $('img.cut-this').fitImage();


  $('#panorama ul li').css('width', $('#panorama').width() + 'px');
  $('.row.customer-reel .carousel').css('width', $('#panorama').width() + 'px');

});

// doc ready for everyone else
$('document').ready(function() {

  if (jQuery(window).width() > 1150) {
    jQuery('.container').addClass('shadow-ie');
  } else {
    jQuery('.container').removeClass('shadow-ie');
  }
  /*$('#panorama ul li').css('width', '2000px');
   $('.row.customer-reel .carousel').css('width', '2000px');
   */
  $('.carousel').carousel(70);

  // ------ carouFredSel init & controls ------
  initCarousel();
  
  // img fitter
  $('img.cut-this').fitImage();


//  $("#foo1").carouFredSel({
//            width   : 1108,
//            auto  : {
//              items       : 5,
//              duration    : 5000,
//             easing      : "linear",
//              pauseDuration  : 0,
//              pauseOnHover  : "immediate"
//            }
//          });

  $('#panorama ul li').css('width', $('#panorama').width() + 'px');
  $('.row.customer-reel .carousel').css('width', $('#panorama').width() + 'px');



	//side-menu logic
if ($('span.vnav')) {
	//check if submenu is active
	if($('.vnav h2 span').hasClass('submenu')) {
		var branchCount = 0;
		$('span.vnav li').each(function() {
			if ($(this).hasClass('weceem-menu-level1')) {branchCount ++;}
			$(this).attr('branch', branchCount);
			$(this).hide();
		});
		
		which = $('span.vnav .weceem-menu-active').attr('branch');
		$('span.vnav li').each(function() {
			if ($(this).attr('branch') == which) { $(this).show();};
		});
		$(this).hide();
		$('span.vnav .weceem-menu-level1').hide();
		$('span.vnav .weceem-menu-level2').removeClass('weceem-menu-level2').addClass('weceem-menu-level1')
		$('span.vnav .weceem-menu-level3').removeClass('weceem-menu-level3').addClass('weceem-menu-level2')
	};
	
	
	
	
	
	
	
};



	// Tabs
	// hide all content-divs but first
	$('div.tabs div').hide().eq(0).show();
               
	//set first tab to active
	$('div.tabs ul li:first').addClass('active');
               
	$('div.tabs ul.nav li a').click(function() {
		// hide all content-divs but the clicked
		$('div.tabs div').hide();
                $('div#'+$(this).attr('href')).show();
                   
                // set the clicked tab to active, all others inactive
                $(this).parent().addClass('active').siblings().removeClass('active');
                return false;
        });
        
        $('#lang-select').change(function(){
		window.location = $(this).val();
	});

});

