	$(document).ready(function()
	{
		setProductTabs();
		
		/**
		 * Add image handlers
		 */
		if ($('div#productHighResPhotos img').size() > 1)
		{
			$('div#productHighResPhotos img')
				.click(function(){
					var id = parseInt($(this).attr('id').match(/\d+/gi));
					swapImage('#swap' + id);
					return false;
				})
				.mouseover(function() {
					var id = parseInt($(this).attr('id').match(/\d+/gi));
					swapImage('#swap' + id);
					return false;
				});
		}
		
		/**
		 * More information link
		 */
		if ($('a#fancy-productMoreInformation').size() == 1)
		{
			$('a#fancy-productMoreInformation').fancybox({
				'zoomSpeedIn'		:	200, 
				'zoomSpeedOut'		:	400, 
				'overlayShow'		:	true,				
				'overlayOpacity'	:	0.4,
				'centerOnScroll'	:	true,
				'frameWidth'        :   450,
				'frameHeight'       :   350,
				'hideOnContentClick':	true				
			});
		}
	
		/**
		 * Find all elements with class "manual_flexcroll" and attach an id if not done so.
		 * Those elements are probably hidden and will be flexcrolled when they are made visible (ie. on activating a producttab)
		 */
		if($('.manual_flexcroll').size() > 0)
		{
			var numManualFlexcroll = 0;
			$('.manual_flexcroll').each(function(){
				if ($(this).attr('id').length <= 0)
				{
					$(this).attr('id', 'manualFlexCroller_' + numManualFlexcroll);
					numManualFlexcroll++;
				}
			});
			
			/**
			 * Simuleer click op het eerst zichtbare tabblad (maar niet drivers!)
			 */
			if($('p.tab a.selectedTab').size() == 1)
			{
				$('p.tab a.selectedTab').click();
			}
		}
	});
	
	function setProductTabs()
	{
		/**
		 * Tabs
		 */
		if ($('div.productTabsContent').size() > 0)
		{
			/**
			 * Stel eerste tabblad in als geselecteerde tab
			 */
			$('div.productTabsContent')
				.not(':first')
				.hide();
				
			$('div#productTabs p.tab a:first-child')
				.addClass('selectedTab');
			
			/**
			 * Registreer de onclick functionaliteit voor iedere a-tag
			 */
			$('div#productTabs p.tab a').click(function() {
				$('div.productTabsContent')
					.hide();

				$(this)
					.addClass('selectedTab')
					.siblings()
					.removeClass('selectedTab');
					
				if ($(this).attr('id') !== '')
				{
					$('#tab_' + $(this).attr('id'))
						.show();
					/**
					 * Apply flexcroller manually to each tab
					 */
					if ($('#tab_' + $(this).attr('id') + ' .manual_flexcroll').size() > 0)
					{
						$('#tab_' + $(this).attr('id') + ' .manual_flexcroll').each(function(){
							CSBfleXcroll($(this).attr('id'));
							$(this).removeClass('manual_flexcroll');
						});
					}
				}
				else if ($(this).attr('href') !== '')
				{
					/* follow the url provided */
					window.location.href = '' + $(this).attr('href') + '';
					return;
				}
				
				return false;
			});
		}
	}
	
	function swapImage(objname)
	{
		$("#productImage").attr("src", $(objname).attr("lowsrc"));
	}
	
	function openHighResImage(iid,iname)
	{
		window.location = '/image/productimagelarge/'+iid+'/'+iname+'.jpg'; // 3934
		return false;
	}
	
	function showReview(reviewId, reviewTitle)
	{
		window.location = '/awards-productreview/id/' + reviewId;
		return false;
	}
	
	function downloadDataSheet(productId)
	{
		typeId = 4 /* ProductSheet */;
		
		$.get('/include/download.php'
				,{ id: productId, type: typeId }
				,function(data){ 
					openPDF('/showdownload.php?id=' + data + '&hasacr=1');
				});
		
		return false;
	}