$(document).ready(function(){
	
	var img = $('#i0').attr('src').replace(/info_images/g, "original_images");
	
	$('#i0').addpowerzoom({
		largeimage: img,
		magnifiersize: [200,200] //<--no comma following last option!
	});
	
	$('table img[src*="small."]').each(function() {
		
		var id = $(this).attr('src').split(".");
		
		$(this).wrap('<a href="'+$(this).attr('src').replace(/small\./g, "")+'" rel="mpopup" title="Farbnummer: '+id[id.length - 2]+'" />');
		
	})
	
	$('table img[src*="small."]').click(function() {
		
		$("a[rel='mpopup']").colorbox({rel:'group2',transition:"elastic",speed: 500,maxHeight: "80%",open: true, rel: 'group2'});
		
	} ); 
	
	// Change image
	$("a.changeImage").click(function(){
			
		$("#i0").attr('src', $(this).attr('href'));
		$("#a0").attr('href', $(this).attr('href').replace(/info_images/g, "original_images"));
		$("#l0").attr('src', $(this).attr('href').replace(/info_images/g, "original_images"));
		
		$('#dio-lens,#dio-sensor').remove();
		
		/*$("#dio-lens img").replaceWith('<img src="'+$(this).attr('href').replace(/info_images/g, "original_images")+'" />');
		$("#dio-lens img").attr('width', $("#dio-lens img").width());
		$("#dio-lens img").attr('height', $("#dio-lens img").height());*/
		
		img = $('#i0').attr('src').replace(/info_images/g, "original_images");
		
		$('#i0').addpowerzoom({
			largeimage: img,
			magnifiersize: [200,200] //<--no comma following last option!
		});
		
		return false;
		
	});
	
	$(".optionValue").change(function() {

		freeShipping();
		
	});
	
});

$(document).ready(function() {
	
	freeShipping();
	
});

function freeShipping() {
	
	price = parseFloat(price);
	orgprice = parseFloat(orgprice);
	percent = parseFloat(percent);

	if ((getSelected() + orgprice) >= discount) {
		
		if (orgprice < discount) {
			
			$('#oldArea').html(old);
			
		}

		var preprice = orgprice + getSelected();
		
		calcprice = preprice - (preprice * percent / 100);
		//calcprice = ((getSelected() + price) * ((100 - percent) / 100));
		selected = getSelected() * ((100 - percent) / 100);
		
		$('#discount_percent').text(percent);
		$('#oldPrice').text(String(((getSelected() + orgprice)).toFixed(2)).replace(/\./, ','));
		$('.discount_less').css('display', 'none');
		$('#discountImage').slideUp('slow');
		
		if (calcprice >= 100) {

			$('.nonfree_shipping').fadeOut('slow', function() {
				
				$('.free_shipping').fadeIn('slow');
				
			} );
		
		} else {
			
			$('.free_shipping').fadeOut('slow', function() {
				
				$('.nonfree_shipping').fadeIn('slow');
				
			} );
			
		}
		
		$("#infoPrice").text(String(calcprice.toFixed(2)).replace(/\./, ',') + ' EUR');
		
	} else {
		
		subprice = 0;
		subprice = getSelected() + orgprice;
		diff = discount - subprice;
		
		$('#oldArea').html('<br />');
		$('.discount_less').css('display', 'block');
		$('#discountImage').slideDown('slow');
		
		if (subprice < 100) {
		
			$('.free_shipping').fadeOut('slow', function() {
				
				$('.nonfree_shipping').fadeIn('slow');
				
			} );
		
		} else {
			
			$('.nonfree_shipping').fadeOut('slow', function() {
				
				$('.free_shipping').fadeIn('slow');
				
			} );
			
		}
		
		$('#discountDiff').html(diff);
	
		$("#infoPrice").text(String(subprice.toFixed(2)).replace(/\./, ',') + ' EUR');
		
	}
	
}

function lightbox() {
	
	$('#a0, .morePics > a').colorbox({
		transition:"elastic",
		speed: 500,maxHeight: "80%",
		open: true,
		rel: 'popup_gal'
	});
	
}

function getSelected() {
	
	//number = parseFloat(orgprice.replace(/\,/, '.'));
	var number = 0;
	
	$(".optionValue option:selected").each(function() {
		
		var nNumber = parseFloat($(this).attr("val").replace(/\,/, '.'));
		
		if (nNumber > 0) {
			
			number += nNumber;
		
		}
		
	});
	
	var newPrice = number.toFixed(2);

	return parseFloat(newPrice);
	
}
