jQuery(document).ready(function($){

	$('.footer-inner > ul:not(".links")').wrap('<div class="block four-col cut-right" id="footer-service"></div>');
	$('#footer-service').detach().insertAfter('#footer-payment').prepend('<h4>Service</h4>');
	$('.footer-inner > ul.links').addClass('bottomlinks').removeClass('links').css({'clear' : 'both'}).detach().insertAfter('.page');
	$('.bottomlinks a').css({'color' : 'white'});
	$('.bottomlinks').css({'padding-bottom' : '10px'});
	$('.bottomlinks li').css({'display' : 'inline'});

	if (typeof(productHasOptions) != 'undefined')
	{
		if(productHasOptions && !$('body').hasClass('product-view-has-options'))
		{
			$('body').addClass('product-view-has-options');
			$('.regular-price .price-label, .special-price .price-label').remove();
			$('.old-price').remove();
			$('.special-price').addClass('regular-price');
			diffpriceContainer = $('<span>Zubehörpreis: </span>').addClass('diffprice').append($('<span>').addClass('price'));
			$('span.product-only-price').after(diffpriceContainer);
			$('.diffprice').after($('<span>Gesamtpreis: </span>').addClass('total-price-label'));
	//		$('.regular-price').html('Gesamtpreis: ' + $('.regular-price').html();
			
			$('.diffprice .price').calcDiffInPrice();
			priceChecker = setInterval('jQuery(".diffprice .price").calcDiffInPrice()', 200);
		}
	}
	
/*	$('.more-views a').each(function(i,e){
		$(e).attr('href', $(e).attr('onclick').toString().split("('")[1].split("'")[0]);
		$(e).attr('onclick', '');
	}); */
	
	$('.more-views a').fancybox({'autoDimensions' : true});
	
	$(".fancybox").live('click', function(){
		$.fancybox({
			'speedIn': 600, 
			'overlayOpacity': 0.7,
			'speedOut': 200,
			'href': $(this).attr('href')
		});
		return false;
	});
	
	$(".fancybox-std").live('click', function(e){
		e.preventDefault();
		$.get($(this).attr('href'), function(data){
			$.fancybox({
				'speedIn': 600, 
				'overlayOpacity': 0.7,
				'speedOut': 200,
				'content': $('.std', data)
			});
		});
	});
	
	$('.truncated img').die().live('mouseover mouseout', function(){
		return false;
	});
	
	$('.pdf-quote-request a').click(function(e){
		e.preventDefault();
//		if(productAddToCartForm.validator.validate()) // removed because of usability issues
//		{
			productOptions	= $('#product_addtocart_form').serialize();
			
			$.ajax({
				url: '/pdf-angebot.php',
				data: productOptions,
				success: function(data) {
					jQuery.fancybox({
						content : data,
						overlayOpacity : 0.3
					});
				}
			});
//		}
	});
	
	cancelObservation();
	setInterval('cancelObservation();', 500);

	$('.product-options .truncated').click(function(e){
		e.preventDefault();
		$.fancybox({
			content: $('.truncated_full_value', this).clone(),
			overlayOpacity : 0.3,
			width : 560,
			height : 560,
			autoDimensions : false
		});
	});


	rotate_stuff = function(){
		el = jQuery('.cms-index-index .rotator a:last-child');
		
		jQuery(el).fadeOut(1000, function(){
			xx = jQuery(this).detach();
			jQuery('.cms-index-index .rotator').prepend(xx);
			jQuery(xx).show();
		});
	}

	setInterval('rotate_stuff()', 6000);

});

(function($){
	$.fn.calcDiffInPrice = function() {
		rawPrice			= parseFloat($('.product-only-price .price').html().replace('.','').replace(',','.'), 10);
		priceAsConfigured	= parseFloat($('.regular-price .price').html().replace('.','').replace(',','.'), 10);
		priceDiff			= (Math.round((priceAsConfigured - rawPrice)*100)/100).toString().replace('.', ',');
		priceDiffChunks 	= priceDiff.split(',');
		
		if (priceDiffChunks[1] == undefined) priceDiffChunks[1] = '00';
		if (priceDiffChunks[1].length == 1) priceDiffChunks[1] += '0';
		if (priceDiffChunks[0].length >= 4) {
			lastThreeDigits = priceDiffChunks[0].substr(priceDiffChunks[0].length - 3);
			priceDiffChunks[0] = priceDiffChunks[0].substring(0,priceDiffChunks[0].length - 3);
			priceDiffChunks[0] += '.';
			priceDiffChunks[0] += lastThreeDigits;
		}
		
		this.html(priceDiffChunks[0] + ',' + priceDiffChunks[1] + ' €');
//		alert(priceDiff);
	}
})(jQuery);


function cancelObservation() {
	$$('.product-options .truncated').each(function(e){e.stopObserving()});
}
