$(document).ready(function(){
	
	var tRows='';
	window.products={
			zoomplus_img:new Image(),
			zoomminus_img:new Image(),
			preload_img:new Image(),
			zoomSpeed:500,
			maxZ:500,
			zoomImage:function(link){
				products.maxZ+=2;
				$($(link).data('parent')).css('zIndex', products.maxZ);
				var w=$(link).data('zoomImage').width;
				var h=$(link).data('zoomImage').height;
				$(link).css({backgroundImage: 'url('+products.zoomminus_img.src+')', zIndex:products.maxZ});
				$($(link).data('zoomImage').image).css('zIndex', products.maxZ-1).animate({width:w, height:h, opacity:1},{duration:products.zoomSpeed});
				$(link).data('zoomImage').zoomed=true;
			},
			hideImage:function(link){
				$(link).css('backgroundImage', 'url('+products.zoomplus_img.src+')');
				$($(link).data('zoomImage').image).animate({width:$(link).data('relImg').width+'px', height:$(link).data('relImg').height+'px', opacity:0},{duration:products.zoomSpeed});
				$(link).data('zoomImage').zoomed=false;
			},
			zoom:function(link){
				 if(!$(link).data('zoomImage')){
					 var imgW=$(link).next().width();
					var imgH=$(link).next().height();
					$(link).css({zIndex:products.maxZ, backgroundImage:'url('+products.preload_img.src+')'}).data('relImg', {width:imgW, height:imgH});
					var img=new Image();			
					img.src=link.href;
					var init=function(event, img){
						if(!img) img=this;
						products.maxZ+=2;
						$(link).data('zoomImage',{image:img, width:img.width, height:img.height, 'zoomed':false});;
						$(img).css({
							width:$(link).data('relImg').width+'px',
							height:$(link).data('relImg').height+'px',
							position:'absolute',
							top:'-1px',
							right:'-1px',
							zIndex:products.maxZ-1,
							border:'solid #D2D3DC 1px',
							opacity:0
						}).insertBefore(link);
						products.zoomImage(link);
					};
					if(img.complete) init('e', img);
					else img.onload=init;
				}else{
					var zoomed=$(link).data('zoomImage').zoomed;
					if(zoomed){
						products.hideImage(link);
					}else{
						products.zoomImage(link);
					}
				}
			}
	}
	products.zoomplus_img.src='fileadmin/template/img/zoom-plus.gif';
	products.zoomminus_img.src='fileadmin/template/img/zoom-minus.gif';
	products.preload_img.src='fileadmin/template/img/preloader.gif';
	$('.product').each(function(){	
		$(this).find('a.zoomlink').each(function(){
			if(this.href=="" || this.href.charAt(this.href.length-1) == '/') $(this).remove();
			else{
				var link=this;
				$(this).css('display', 'block').focus(function(){this.blur();}).parent().click(function(event){
					event.preventDefault( );
					$(link).data('parent', this);
					
					products.zoom(link);
				});
			}
		});
		var linkID=$(this).prev().attr('id');
		var pdflink=$(this).find('.pdf_datasheets a:first');
		if(linkID && pdflink.length==1){
			var href=pdflink.attr('href');
			var linktext=pdflink.text();
			pdflink='<a href="'+href+'" target="_blank" class="pdf_link">'+linktext+'</a>';
			tRows+='<tr><td><a href="'+location.href.replace(/#.*/,'')+'#'+linkID+'" class="downlink">'+$(this).find('.product_text >h2').text()+'</a></td><td>'+pdflink+'</td></tr>';
		}
	});
	if(tRows != '') {
		
		$('.product:first').prev().before('<div class="contentbox product_overview"><h2>Overview</h2><table><tbody>'+tRows+'</tbody></table><hr /></div>');
		$('.product_overview a').focus(function(){this.blur();});
	}
	$.localScroll();
});
