/**
 * @author Fiedrich
 */


function previewPDF() {
	
	var allAnchors = $$('div.pageContent a');
	
	allAnchors.each(function(anchor, index) {
		var anchorElement = anchor;
		var anchorHref = anchor.getProperty('href');
		
		var regEx = /(.)(pdf)$/;
		
		if(regEx.exec(anchorHref)) {
			//console.log(anchor);
			
			function showThumbnail() {
				
				var pathPDF = this.getProperty('href');
				var anchorEl = this;
				var previewElement = new Element('img',{
					'styles': {
						'position': 'absolute',
						'left': '-130px',
						'border': '1px solid #000000'
					}
				});
				var thumbnailRequest = new Request({
					url:'http://www.dirkschmidt.com/',
					method: 'get',
					data: 'type=287&filePDF='+pathPDF,
					async: true,
					wait: false,
					noCache: true,
					onRequest: function() {
						anchorEl.setProperty('style', 'position: relative;');
						previewElement.inject(anchorEl);
					},
					onSuccess: function(responseText) {
						previewElement.setProperty('src', responseText);
					}
				}).send();
				
			}
			
			var thumbnailTimer;
			anchor.addEvents({
				'mouseenter': function(){
					if(anchor.getElement('img') != null) {
						anchor.getElement('img').setStyle('display', 'block');
					} else {
						thumbnailTimer = showThumbnail.delay(40, anchorElement);
					}
				},
				'mouseleave': function(){
					$clear(thumbnailTimer);
					if(anchor.getElement('img') != null) {
						anchor.getElement('img').setStyle('display', 'none');
					}
				}
			});
		}
	});
	
}

window.addEvent('domready', function() {
	previewPDF();
});
