// Globalise the downloadFile variable
var downloadFile;

document.observe("dom:loaded", function() {

	//////////////////////////////////////////
	// Training area download catcher
	//////////////////////////////////////////

	// If the current URL contains '/training/' then search for A tags with the correct file extension
	if ( window.location.toString().include('/training/') ){
		
		$$('a').each( function(element){
			
			if ( element.readAttribute('href') != null ) {
				
				var b_is_download = false;

				if ( element.readAttribute('href').endsWith(".pdf") ) { b_is_download = true; }
				if ( element.readAttribute('href').endsWith(".ppt") ) { b_is_download = true; }
				if ( element.readAttribute('href').endsWith(".wmv") ) { b_is_download = true; }
	
				if ( element.readAttribute('href') != null && b_is_download ){
	
					// Clear existing onclick script
					element.writeAttribute('onclick', '');
					
					element.observe('click', function(event){

						downloadFile = element.getAttribute("href");

						new Ajax.Request('/data/energize/scripts/ajax_download_catcher.php', {
							parameters: { filename_download: element.readAttribute('href') },
							method: 'get',
							onComplete: function(response){
								window.location = downloadFile;
							}
						});
			
						Event.stop(event);
					});
		
				}

			}
	
		});

	}


	/////////////////////////////////////////////////////////
	// LOGGED OUT
	/////////////////////////////////////////////////////////
 
	if ( $('article_options_off') ){
	
		elementFollowWindow( $('article_options_off') );

		
		$('article_options_off').select('a').each( function(element){
			element.observe("click", function(event){
	
				alert(commonSubTexts_json.msg_featureforsubscribersonly);

				Event.stop(event);
			});
		});

	}

	if ( $('article_options_and_bookmarks') ){	

		elementFollowWindow( $('article_options_and_bookmarks') );

	}
});
