$(document).ready(function(){
	
	//Readspeaker funktioniert ansonsten nicht
	$("#rsform").attr("enctype","");
	
	//Print Link
	$("a#printlink").click(function(index){
		window.print();
	});
	
	$("a.extern,a.pdf,a.file,area.extern").each(function(index){
		$(this).attr('target','_blank');
		
		//console.log($(this).children('img')[0]);
		if($(this).children('img')[0]){
			$(this).removeClass("pdf");
			$(this).removeClass("extern");
		}

		if(($.browser.msie && parseInt(jQuery.browser.version)<8) && !$(this).children('img')[0]){
			oldClass = $(this).attr('class').toString();
			$(this).removeClass(oldClass).addClass('js-' + oldClass);
			var icnLinkExtern = $("<span></span>").addClass('icon'); // span tag in link einbauen
			$(this).prepend(icnLinkExtern);
		}	
	});

// ************************************************************************
// AJAX für Broschüren-Bestellungen
// ************************************************************************
  // der DIV, in dem der Warenkorb liegt (in der rechten Spalte) 
  var cart = $('#warenkorb');
	
	
	
//Beim onclick auf 'Bestellen' gehts los
	$("a.addCart").click(function(){
	// Anstelle des Bestellen-Links, soll fett formatiert `Bestellt` erscheinen
		// Hier wird der Strong-Tag vorbereitet  
		var ordered = $('<strong></strong>').html("Im <a href=\"/publikationen/?bestellen=show&amp;backUrl="+window.location.pathname+"\">Warenkorb</a>");
		//Die Parameter aus dem Bestellen-Link werden herausgefiltert...
    var getVariables = $(this).attr("href").split("?")[1];
    
    //...und an den AXAX-Link ran gehängt
    var url = "http://" + window.location.hostname + ":"+window.location.port+"/fileadmin/_Templates/php/ajax_cartAdd.php?" + getVariables;
		
		$('#warenkorb').load(url);
		$(this).replaceWith(ordered);

		return false;
	});
	
	/**************************************************************************/
	/* Animierte Sprungmarken
	/**************************************************************************/	
  $('a[href*=#], area[href*=#]').bind("click", function(event) {
		
		var ziel 			= $(this).attr("href");
		
		var slashBegin = ziel.match(/^\//);
		var httpBegin = ziel.match(/^http/);
		
		if(!slashBegin && !httpBegin) {
			ziel = '/' + ziel;
		}
		
		/* Nur ausführen, wenn der Link auch auf die eigene Seite verweist */
		/* Anker entfernen */
		var host = 'http://' + window.location.hostname;
		if(window.location.hostname == 'pilot.fast.de') {
			host+=':8181';
		}
		
//		var repl = /(http:\/\/\w.+)\//;
//		repl.exec(window.location.href);
//		var host = RegExp.$1;
		
		var urlPur	= window.location.href.replace(/\#[A-Za-z0-9]+$/, "");
		urlPur = urlPur.replace(host, '');
		var zielPur	= ziel.replace(/\#[A-Za-z0-9]+$/, "");

		if(urlPur == zielPur) {
			event.preventDefault();
			var url_parts = ziel.split("#");
			var hash 			= "#" + url_parts[1];

			if(window.location.pathname==url_parts[0]){
				$('html,body').animate({
					scrollTop: $(hash).offset().top
					}, 2000 , function (){location.hash = hash;}
				);
			}
		}
	});

	/**************************************************************************/
	/* Video-Archiv Seite
	/**************************************************************************/
	if($.prettyPhoto){ $("a.prettyPhoto").prettyPhoto();	}
});

/**************************************************************************/
/* ReadSpeaker Functions
/**************************************************************************/
function rsTextSelection()
{
	if (document.getSelection) { // older Mozilla versions
		var selectedString = document.getSelection();
	}
	else if (document.all) { // MSIE 4+
		var selectedString=document.selection.createRange().text;
	}
	else if (window.getSelection) { // recent Mozilla versions
		var selectedString=window.getSelection();
	}
	document.getElementById('rsform').selectedtext.value = selectedString;
	if (document.getElementById('rsform').url) {
		if (!document.getElementById('rsform').url.value) {
			if (window.location.href) {
				document.getElementById('rsform').url.value=window.location.href;
			}
			else if (document.location.href) {
				document.getElementById('rsform').url.value=document.location.href;
			}
		}
	}
}

function copyselected() {
	setTimeout("rsTextSelection()",50);
	return true;
}

function openAndRead() {
	document.getElementById('rs_button').setAttribute("target","rs");
	document.getElementById('rsform').setAttribute("target","rs");
    var mywindow = window.open("","rs","width=310,height=200,toolbar=0");
    setTimeout("document.getElementById('rsform').submit();",500);
}

document.onmouseup = copyselected;
document.onkeyup = copyselected;

/**************************************************************************/