$(document).ready(function() {

 // set height of ProductVariantsTab to 350 if it is greater than 350
  var setHeightOfProductVariantsTab = function($tab){
		var innerHeight = 0;
		$tab.children().each(function(){
			innerHeight = innerHeight + $(this).height();
		});
		if (innerHeight < 350){  
			$tab.height(innerHeight+15);
		} else {		
			$tab.height(350);
			$tab.jScrollPane({showArrows:true, scrollbarWidth:18});			
		}  	  
  }
  
  // TODO: it is better when this is not called always
	var $moduleContainer = $('#productVariantsTabNav').parents('.moduleContainer').eq(0);
	if ($moduleContainer.length == 1){
		var firstTab = '.' + $moduleContainer.find('ul:first li:first a').attr("class") + '.tab_content';
		var $contentObj = $moduleContainer.find(firstTab);		
		setHeightOfProductVariantsTab($contentObj);
	}

  $('.tabNavigation ul li').click(function() {
    $obj = $(this).find('a').attr('class');
    $parent = $(this).parents('.moduleContainer').eq(0);

    $parent.find('.tabNavigation ul li.active').removeClass('active');
    $parent.find('.tabNavigation ul li a.'+$obj).parent().addClass('active');

    $parent.find('.tab_content').jScrollPaneRemove();
    $parent.find('.tab_content').css('display', 'none');
    $parent.find('.tab_content.'+$obj).css('display', 'block');
    
    /* Rounded Corners */
		if (!$parent.hasClass('corporate_downloads')){
			$parent.find('.tab_content.'+$obj+' div.rounded').each(function(){
			  $(this).css('width', +$(this).find('img').width()+'px').css('height', +$(this).find('img').height()+'px');
			});
		}

		if ($(this).hasClass('form')) {
			initForms();		
			var $multi = $parent.find('.tab_content.'+$obj+' .multiselect');
			$multi.find('ul').jScrollPaneRemove();
			$multi.find('ul').css('height', $multi.height()+'px');
			$multi.find('ul').jScrollPane({showArrows:true, scrollbarWidth:18});	
			Cufon.replace(".moduleContainer .form .button", {"fontFamily": "TheSansOsF Plain", hover: false});	
			if (!($.browser.msie && $.browser.version.substr(0,1) == "7")) {
				Cufon.CSS.ready(function() {
					$(".button cufon").css("margin-top", "3px").css("margin-bottom", "4px");
				});
			} else {
				Cufon.CSS.ready(function() {
					$(".button cufon").css("margin-top", "2px").css("margin-bottom", "5px");
				});
			}
		}
	
		var $this = $(this);
    if ($this.parents('div').eq(0).attr('id') == 'productVariantsTabNav'){	
			var $contentObj = $parent.find('.tab_content.'+$this.find('a').attr('class'));		
			setHeightOfProductVariantsTab($contentObj);
   	}

    return false;
    
  });
  
   $('.jump2productVariantsTab, .jump2RaffleFormTab').click(function() {
    $obj = $(this).attr('id');
    $tab = $(this).attr('tab');
    $parent = $('#' + $tab).parents('.moduleContainer').eq(0);
    $parent.find('#' + $tab + ' ul li.active').removeClass('active');
    $parent.find('#' + $tab + ' ul li a.'+$obj).parent().addClass('active');

		$parent.find('.tab_content').jScrollPaneRemove();
    $parent.find('.tab_content').css('display', 'none');
    $parent.find('.tab_content.'+$obj).css('display', 'block');

		if ($(this).hasClass('jump2productVariantsTab')){	
			$contentObj = $parent.find('.tab_content.'+$obj);
			setHeightOfProductVariantsTab($contentObj);
			var new_position = $contentObj.offset();
			window.scrollTo($(document).scrollLeft(), new_position.top-70);		
		}

    /* Rounded Corners */
    $parent.find('.tab_content.'+$obj+' div.rounded').each(function(){
      $(this).css('width', +$(this).find('img').width()+'px').css('height', +$(this).find('img').height()+'px');
   	});

    return false;
    
  });


  $('.headerTabNavigation ul li').click(function() {
    $obj = $(this).find('a').attr('class');
    $parent = $(this).parents('.moduleContainer').eq(0);

    $parent.find('.headerTabNavigation ul li.active').removeClass('active');
    $parent.find('.headerTabNavigation ul li a.'+$obj).parent().addClass('active');

    $parent.find('.tab_content').css('display', 'none');
    $parent.find('.tab_content.'+$obj).css('display', 'block');
    
    $('.teaser_paper .paperbox .rounded').width($('.teaser_paper .paperbox .rounded > img').width()).height($('.teaser_paper .paperbox .rounded > img').height());

    return true;

  });
});

