window.addEvent('domready', function() {
	// Close-Link on SkyScraper Ad
    if( $( 'skyscraperclose' ) ) {
		$( 'skyscraperclose' ).addEvent( 'click', function() { $( 'skyscraper' ).destroy(); } );
	};

	// Text-Images
	$$( '.textimage' ).each( function( el ) {
		createTextImage( el );
  	} );

	function createTextImage( el ) {
		var elText = el.get( 'text' );
		el.empty();
		var tagname = el.tagName.toLowerCase();
		var classRegex = /textimage-([a-z0-9-]+)/;
		var classMatch = classRegex.exec( el.className );
		if ( classMatch ) tagname = classMatch[1];
		new Element( 'img', { 'src': THEME_IMAGE_DIR + '/dynamic/' + tagname + '/' + Base64.encode( elText ) + '.png', 'alt': elText } ).inject( el );
	}

	// Youtube Videos
	var youtubeVideos = 0;
	var youtubeVideoWidth = 0;
	var youtubeVideoHeight = 0;
	$$( '#post-content a' ).each( function ( link ) {
		if ( link.href.search( /youtube/i ) > 0 ) {
			link.addClass( 'icon' );
			link.addClass( 'iyoutube' );
			var vUrlRegex = /v=([^&]+)/;
			var match = vUrlRegex.exec( link.href );
   			if (!match) {
   				// New style youtube links
    			var vUrlRegex = /v\/([^&]+)/;
				var match = vUrlRegex.exec( link.href );
			}
			if ( match ) {
				var vidH3 = new Element( 'h3', { 'html': link.get( 'text' ) } );
				createTextImage( vidH3 );
				vidH3.inject( $( 'post-content' ) );
				youtubeVideos++;
				var youtubeVideoElement = new Element( 'div', { 'id': 'youtubeVideo' + youtubeVideos, 'class': 'youtubeVideo' } );
				youtubeVideoElement.inject( $( 'post-content' ) );
				youtubeVideoWidth = youtubeVideoElement.getWidth();
				youtubeVideoHeight = youtubeVideoWidth * 0.80;
				new Element( 'div', { 'id': 'youtubeVideo' + youtubeVideos + 'Target' } ).inject( youtubeVideoElement );
				swfobject.embedSWF( 'http://www.youtube.com/v/' + match[1] + '&hl=de&fs=1', 'youtubeVideo' + youtubeVideos +  'Target', youtubeVideoWidth, youtubeVideoHeight, '9.0.0' );
			}
		} else if ( link.href.search( /wikipedia/i ) > 0 ) {
			link.addClass( 'icon' );
			link.addClass( 'iwikipedia' );
		}
	} );
});