$(document).ready(function() {
	var minFixationHeight = 650;
	var useFixation = true;
	
	if ($(window).height() > minFixationHeight && useFixation) {
		var startFixationAt = $('#header_sticky').offset().top;
		var isFixed = false;
	
		var url = window.location.href;
		var idx = url.indexOf("#")
		var hash = idx != -1 ? url.substring(idx + 1) : false;

		$.event.add(window, "scroll", function() {
			var p = $(window).scrollTop();

			if (p > startFixationAt && !hash) {
				if (!isFixed) {
					$('.sticky').each(function() {
						$('<div class="stickyPlaceholder">&nbsp;</div>').css('height', $(this).outerHeight(true) + 'px').insertAfter($(this));
					});
					$('.sticky').addClass('fixed');

					isFixed = true;
				}
			} else {
				isFixed = false;
				$('.sticky').removeClass('fixed');
				$('.stickyPlaceholder').remove();
			}
		});
	}
	
	
	/* Family structure list
	 -----------------------------------------------------*/
	$('.content_box_structure_family_list .trigger_opening').bind('click', function() {
		var papa = $(this).parent('.content_box_structure_family_list');
		
		if ($(this).hasClass('isOpen')) {
			$('.family_list_content', papa).stop().animate({
				'height': '170px'
			});

			$(this).removeClass('isOpen');
		} else {
			var container = $('.family_list_content', papa);
			var oldContainerHeight = container.outerHeight();

			container.css({
				'overflow': 'visible',
				'height': 'auto'
			});

			var height = container.height();

			container.css({
				'overflow': 'hidden',
				'height': oldContainerHeight + 'px'
			});
			
			
			$('.family_list_content', papa).stop().animate({
				'height': height + 'px'
			});
			
			$(this).addClass('isOpen');
		}
	});
	
	
	/* Item details
	 -----------------------------------------------------*/
	/* Replace manufacturer logo */
	var itemManuLogoContainer = $('.content_item #manufacturer_wrapper .manufacturer_logo');
	var curItemManuLogo = itemManuLogoContainer.css('backgroundImage');
	curItemManuLogo = /^url\((['"]?)(.*)\1\)$/.exec(curItemManuLogo);
	curItemManuLogo = curItemManuLogo ? curItemManuLogo[2] : false;

	if (curItemManuLogo) {
		var maxwidth = itemManuLogoContainer.innerWidth();
		var maxheight = itemManuLogoContainer.innerHeight();
		
		var newItemManuLogo = $('<img />').attr({
			'src': curItemManuLogo
		}).appendTo(itemManuLogoContainer);
		
		$(newItemManuLogo).load(function() {
			itemManuLogoContainer.css({
				'backgroundImage': 'none'
			});

			var width = newItemManuLogo.width();
			var height = newItemManuLogo.height();

			if (width > height) {
				width = maxwidth - 10;

				newItemManuLogo.css({
					'width': width + 'px'
				});
			} else if (height > width) {
				height = maxheight - 10;

				newItemManuLogo.css({
					'height': height + 'px'
				});
			}

			var toppos = (maxheight - newItemManuLogo.height()) / 2;
			newItemManuLogo.css({
				'marginTop': toppos + 'px'
			});
		});
	}
	
	/* Variant box action */
	$('.content_item .item_variant_box').unbind('click').bind('click', function(e) {
		e.preventDefault();

		$(this).next('.variant_selection_box').slideToggle({
			'duration': 100,
			'easing': 'linear'
		});
		if ($(this).next('.item_variant_box').hasClass('item_variant_box_active')) {
			$(this).next('.item_variant_box').removeClass('item_variant_box_active');
		}
		else {
			$(this).next('.item_variant_box').addClass('item_variant_box_active');
		}
	});
	
	/* Graduated prices box */
	$('#graduated_price_title').unbind('click').bind('click', function() {
		$('#graduated_price_content').slideToggle({
			'duration': 100,
			'easing': 'linear'
		});

		if ($('.graduated_price_title_arrow').hasClass('graduated_price_title_arrow_open')) {
			$('.graduated_price_title_arrow').removeClass('graduated_price_title_arrow_open');
			$('.graduated_price_title_wrapper').removeClass('graduated_price_title_wrapper_open');
		} else {
			$('.graduated_price_title_arrow').addClass('graduated_price_title_arrow_open');
			$('.graduated_price_title_wrapper').addClass('graduated_price_title_wrapper_open');
		}
	});
	
	/* QR Code */ 
	$('.content_item #qrcode').qrcode({
		width: 95,
		height: 95,
		text: window.location.href
	});
	
	/* Image slider */
//	clearInterval(slideImagesInterval);
	
	
	/* Cashdesk
	 -----------------------------------------------------*/
	/* Trigger click on input-radio on click at payment method container */
	$('.cashdesk .payment_method_line, .cashdesk .shipping_method_line').unbind('click').bind('click', function(e) {
		$('input', this).prop('checked', true).trigger('change');
	});
	
	
	
	/* Customer center
	 -----------------------------------------------------*/
	/* hover address-boxes START */
//	$('.address').unbind('mouseenter').bind('mouseenter', function() {
//		$(this).stop().animate({
//			'backgroundColor': '#FFE8D7'
//		}, 100);
//	}).bind('mouseleave', function() {
//		$(this).stop().animate({
//			'backgroundColor': '#ffffff'
//		}, 100);
//	});
	/* hover address-boxes START END */
});