function PositionProduct (productObject, viewPortHeight, srollFromTop) {
  var minimumFromTop = 145;
  var hiddenHeight = 70;
  var deviation = 80;
  var productImage = productObject.children('img');
  var productHeight = productImage.height() - hiddenHeight;
  var contentHeight = $('#content').outerHeight() - minimumFromTop - deviation;
  var topPosition = srollFromTop + viewPortHeight - productHeight;
  var maximumFromTop = contentHeight - minimumFromTop;
  var isBottom = topPosition > maximumFromTop;
  if (isBottom) {
    topPosition = maximumFromTop;
  }
  var isViewPortHigher = (minimumFromTop + productHeight) < viewPortHeight;
  var isNotOnTop = (srollFromTop - minimumFromTop) > 0;
  if ( isNotOnTop || isViewPortHigher) {
    productObject.css('top', topPosition);
  } else {
    productObject.css('top', minimumFromTop);
  }
}
function ResizeHomepageCarousel (viewPortHeight) {
  if ($('.carousel').length) {
    var carouselMinimalHeight = 484;
    var carouselItemHeight = 121;
    var contentMinimalHeight = 569;
    var contentOuterHeight = $('#content').outerHeight();
    var contentHeight = $('#content').height();
    var headerWrapperHeight = $('.headerWrapper').outerHeight();
    var carouselHeight = $('.carousel').height();
    var heightDivision = viewPortHeight - (contentOuterHeight + headerWrapperHeight);
    var newCarouselHeight = Math.floor((carouselHeight + heightDivision) / carouselItemHeight) * carouselItemHeight;
    var newContentHeight = contentHeight + heightDivision;
    var isContentHeightMinimal = newContentHeight <= contentMinimalHeight;
    var isCarouselHeightMinimal = newCarouselHeight <= carouselMinimalHeight;
    var carouselListHeight = $('.jcarousel-list li').length * carouselItemHeight;
    if (!isCarouselHeightMinimal && !isContentHeightMinimal) {
      if (carouselListHeight <= newCarouselHeight) {
        $('.jcarousel-next-vertical').addClass('jcarousel-next-disabled');
        $('.jcarousel-prev-vertical').addClass('jcarousel-prev-disabled');
      }
      $('.jcarousel-clip-vertical, .carousel').height(newCarouselHeight);
      $('#content').height(newContentHeight);
    } else {
      $('.jcarousel-clip-vertical, .carousel').height(carouselMinimalHeight);
      $('#content').height(contentMinimalHeight);
    }
  }
}
function CheckProperSpanRadio () {
  $('input[type="radio"]').parent('.radio').removeClass('checked').css('textIndent', '-200px');
  $('input[type="radio"]:checked').parent('.radio').addClass('checked');
}
function CheckProperSpanCheckbox () {
  $('input[type="checkbox"]').parent('.checkbox').removeClass('checked').css('textIndent', '-200px');
  $('input[type="checkbox"]:checked').parent('.checkbox').addClass('checked');
}
function CheckRadioButton (radioButtonObject) {
  var radioName = radioButtonObject.attr('name');
  $('input[type="radio"][name="' + radioName + '"]').removeAttr('checked');
  radioButtonObject.attr('checked', 'checked');
  CheckProperSpanRadio();
}
function CheckCheckboxButton (checkboxButtonObject) {
  if (checkboxButtonObject.is(':checked')) {
    checkboxButtonObject.removeAttr('checked');
  } else {
    checkboxButtonObject.attr('checked', 'checked');
  }
  CheckProperSpanCheckbox();
}

function ShowHideInputHint () {
  var inputObject = $(this);
  var objectValue = inputObject.val();
  var objectTitle = inputObject.attr('title');
  if (objectValue.length == 0) {
    inputObject.val(objectTitle);
    inputObject.addClass('hint');
  }
  if (objectValue == objectTitle) {
    inputObject.val('');
    inputObject.removeClass('hint');
  }
}

function CalculateFullscreenMapDimension (mapId) {
  if ($('#' + mapId).length) {
    var mapObject = $('#' + mapId);

    var rightBorderWidth = 43;
    var minimalWidth = $('#header').outerWidth() - rightBorderWidth;
    var viewPortWidth = $(window).width();
    var recalculateWidth = viewPortWidth - rightBorderWidth;
    mapObject.width(minimalWidth);
    if (recalculateWidth > minimalWidth) {
      mapObject.width(recalculateWidth);
    }

    var minimalHeight = 400;
    $('#content').height(minimalHeight);
    var footerHeight = $('.footerWrapper').outerHeight();
    minimalHeight = minimalHeight + footerHeight;
    var viewPortHeight = $(window).height();
    var recalculateHeight = viewPortHeight - footerHeight;
    mapObject.height(minimalHeight);
    if (recalculateHeight > minimalHeight) {
      mapObject.height(recalculateHeight);
    }
  }
}

function SelectLabeledInput () {
  var relativeId = $(this).attr('for');
  if (relativeId) {
    var relativeObject = $('#' + relativeId);
    var relativeObjectType = relativeObject.attr('type');
    switch (relativeObjectType) {
      case 'radio':
        CheckRadioButton(relativeObject);
      break;
      case 'checkbox':
        CheckCheckboxButton(relativeObject);
      break;
    }
  }
}

$(document).ready(function () {

  CheckProperSpanRadio();
  CheckProperSpanCheckbox();

  $('#text table tbody tr:nth-child(even)').addClass('even');
  $('input, textarea').bind('focus blur', ShowHideInputHint);
  $('label').bind('click', SelectLabeledInput);
  $('span.radio').bind('click', function(){
    var relativeObject = $(this).children('input[type="radio"]');
    CheckRadioButton(relativeObject);
  });
  $('span.checkbox').bind('click', function(){
    var relativeObject = $(this).children('input[type="checkbox"]');
    CheckCheckboxButton(relativeObject);
  });
  if ($('#text form select').length) {
    $('#text form select').selectbox();
  }
  $(".tabbedContent").tabs();
  PHOTOGALLERY_BUTTONWIDTH = 54;
  PHOTOGALLERY_IMAGEHEIGHT = 73;
 
  $('.select').parent('li').addClass('withSelectbox');

  /* homepage carousel */
  $('.carousel').jcarousel({
    vertical: true,
    scroll: 1
  });
  
  if ($('.homepage').size()) {
	  ResizeHomepageCarousel ($(window).height());
  }

  $('.carousel a').live('click', function() {
    $('.carouselContent .action').hide();
    var contentId = $(this).attr('href');
    var backgroundImageName = contentId.substring(1);
    $('#backgroundImage .background').fadeOut('slow');
    $('#backgroundImage .background[name="' + backgroundImageName + '"]').fadeIn('slow');
    $(contentId).show();
    $('.carousel li').removeClass('selected');
    $(this).parent('li').addClass('selected');

    return false;
  });

/*  PositionProduct ($('#content .product'), $(window).height(), $(window).scrollTop());*/
setInterval(function () {  PositionProduct ($('#content .product'), $(window).height(), $(window).scrollTop());}, 500);

  CalculateFullscreenMapDimension('fullscreenMap');
  $(window).resize(function(){
    var productObject = $('#content .product');
    var viewPortHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
    PositionProduct (productObject, viewPortHeight, scrollTop);
    ResizeHomepageCarousel (viewPortHeight);
    CalculateFullscreenMapDimension('fullscreenMap');
  });
  $(window).scroll(function(){
    var productObject = $('#content .product');
    var viewPortHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
    PositionProduct (productObject, viewPortHeight, scrollTop);
  });
  
  
  $('a[href="#action00"]').trigger('click');
  
          
});

