$.fn.unwrap = function()
{
	this.parent(':not(body)')
		.each(function(){
			$(this).replaceWith( this.childNodes );
		});
	
	return this;
};

$(function() {

	/*	### Flashanimation ### */
	if ($("#flash").length > 0) {
		flashembed("flash", { 
			src: "flash/intro.swf",
			version: [9, 0]
		});
	}

	/*	### Slideshow unter Kollektion ### */
   if ($("#slideshow").length > 0) {
	
		$("#slideshow").scrollable({
			activeClass: 'aktiv',
			disabledClass: 'inaktiv',
			speed: 750,
			size: 6
		});
	}
		
	/*	### Overlay fuer Newsbilder ### */
	if ($("#overlay").length > 0 && $("a.overlay").length > 0) {
		$("a.overlay").overlay({ 
			target: '#overlay', 
			expose: '#f1f1f1',
			preload:true
		}).gallery({
			speed: 800 
		});	
	}

    if($('a[rel=epaper]').length > 0)
    {
        /*if($('#overlay_epaper').length == 0)
        {
            $('body').append($('<div class="simple_overlay" id="overlay_epaper"><iframe frameborder="0" scrolling="no" height="200px" width="100%" src=""></iframe></div>'));
        }    */
        
        $('a[rel=epaper]').each(function()
		{
		
			klasse = $(this).attr('class');
			if ($(this).hasClass("external_epaper")) {
				var link = $(this).attr('href');
				link = link.replace(/\.pdf$/, '\/');	
				//console.log(link);
			} else {
				var link = $(this).attr('href');
				link = link.replace(/pdf\//, 'pdf-view/');			
			}
			
			/* -----------------------------------------------------------------
			 * E-Paper-Version nur verlinken, wenn Datei vorhanden ist
			 */
			
			klasse = $(this).attr('class');
			if ($(this).hasClass("external_epaper")) {
				epaper_url = link.replace(/\.pdf$/, '\/');
			} else {
				epaper_url = link.replace(/\.pdf$/, '_e-paper.pdf');
			}
			//console.log(klasse);
			var file_exists = false;
			
			if ($.browser.msie && window.XDomainRequest) {

				var xdr = new XDomainRequest();
				xdr.open("get", epaper_url);
				xdr.onload = function() {
					file_exists = true;
				};

				xdr.send();

			} else {

				$.ajax(
				{
					async: false,
					url: epaper_url,
					type:'HEAD',
					success: function()
					{
						file_exists = true;
					}
				});

			}
			//console.log(file_exists);
			
			if ( ! file_exists )
			{
				//console.log($(this).find('img').length);
				if ( $(this).find('img').length === 1 )
				{
					$(this).find('img').unwrap();
				}
			}
			klasse = $(this).attr('class');
			if ($(this).hasClass("external_epaper")) {
				link = link.replace(/\.pdf$/, '\/');
				//console.log(klasse);
			} else {
				link = link.replace(/\.pdf$/, '_e-paper.pdf');
			}
			$(this).attr('href', link);
			$(this).attr('title', 'PDF als E-Paper direkt im Browser betrachten');
			$(this).css('display', 'block');
			/* -------------------------------------------------------------- */
			
        });
		
		$('a[rel=epaper]').overlay({ 
			target: '#overlay_epaper', 
			expose: '#f1f1f1',
			top: "center",
			onBeforeLoad: function() {

			    var iframe = this.getOverlay().find("iframe");

			    iframe.attr('src', this.getTrigger().attr("href"));
		    }
		});   
    }
});

