function selectID(sId){
	var myElement = document.getElementById(sId);

	if (window.getSelection) {
		var selection = window.getSelection();
		if (selection.setBaseAndExtent) { /* for Safari */
			selection.setBaseAndExtent(myElement, 0, myElement, 1);
		} else { /* for FF, Opera */
			var range = document.createRange();
			range.selectNodeContents(myElement);
			selection.removeAllRanges();
			selection.addRange(range);
		}
		} else { /* for IE */
			var range = document.body.createTextRange();
			range.moveToElementText(myElement);
			range.select();
		}
}/* Ende selectID */


function getSelected() {
	if(window.getSelection) { return window.getSelection(); }
	else if(document.getSelection) { return document.getSelection(); }
	else {
		var selection = document.selection && document.selection.createRange();
		if(selection.text) { return selection.text; }
		return false;
	}
	return false;
}/* Ende getSelected */


function autotoccontent(element) {
	var urlCleaned=document.location.href.split('#')[0];
	var thisRel=$(element).attr('rel').split('_');
	
	var whichDiv=thisRel[0];
	if (whichDiv == null || whichDiv==0){
		whichDiv="content";
	}
	
	var thisDepth=0;
	if (thisRel[2]!='undefined') {
		thisDepth=thisRel[2];
	}

	//initial vars
	var finders="h3";
	var strHtml="<ul>";
	
	if (typeof thisDepth== 'undefined' || thisDepth=='0') {
		finders = "h2,h3";
		strHtml += "<li><a class='men' href='" + urlCleaned + "#top'>Seitenanfang</a></li>";
	}

	//find all elements in div and filter for headings afterwards (structure will be held)
	elements=$('#' + whichDiv + ' *').filter(finders);
	var i=elements.length;
	var lastClass="";
	
	elements.each(function(index){
		if (typeof thisDepth== 'undefined' || thisDepth=='0') {
				additionalClass=" "+this.tagName.toLowerCase();
			}
			else {
				additionalClass="";
			}
			if (index==i-1) lastClass=" last";
			strHtml+="<li><a class='men" + additionalClass + lastClass + "' href='" + urlCleaned + "#" +this.id +"'>" + jQuery.trim($(this).text()) + "</a></li>";
	});
	strHtml += "</ul>";
	return strHtml;
}/* Ende autotoccontent */


function autotoctitle(element) {
	var thisRel=$(element).attr('rel').split('_');
	var strTitle=thisRel[1].replace(/:\./,'&raquo;').replace(/\.:/,'&laquo;');
	if (strTitle== null) {
		strTitle="Inhalt auf dieser Seite";
	}
	return strTitle;
}/* Ende autotoctitle */


function qrcontent(element,nmbr){
	var qrLink=document.location.href.split('#')[0];
	var qrLinkText=jQuery.trim($(element).text());
	var elementId=$(element).attr("id");

	if (elementId!="") {
		qrLink += "#"+elementId;
	}
	
	var textToInsert = "<table><tr>";
	textToInsert += "<td><img height='125' width='125' src='http://chart.apis.google.com/chart?chs=125x125&chf=bg,s,65432100&cht=qr&chl="+escape(qrLink)+"' /></td>";
	textToInsert += "<td valign='middle'><ul>";
	textToInsert += "<li id='qrlin"+nmbr+"' onclick='selectID(\"qrlin"+nmbr+"\");' title='Link'>"+qrLink+"</li>";
	textToInsert += "<li id='qrhtm"+nmbr+"' onclick='selectID(\"qrhtm"+nmbr+"\");' title='HTML'>&lt;a href=&quot;"+qrLink+"&quot;&gt;"+qrLinkText+"&lt;/a&gt;</li>";
	textToInsert += "<li id='qrbbc"+nmbr+"' onclick='selectID(\"qrbbc"+nmbr+"\");' title='BB-Code (Foren)'>&#91;url="+qrLink+"&#93;"+qrLinkText+"&#91;/url&#93;</li>";
	textToInsert += "<li id='qrwik"+nmbr+"' onclick='selectID(\"qrwik"+nmbr+"\");' title='Wiki'>&#91;"+qrLink+" "+qrLinkText+"&#93;</li>";
	textToInsert += "</ul></td></tr></table>";
	return textToInsert;
};/* Ende qrcontent */



$(document).ready(function() {
	$('a.autotoc').each(function(index,domElement){
		$(this).qtip({
			content: {text: autotoccontent($(domElement)), title: {text: autotoctitle($(domElement))}},
			position: {
				at: 'bottom left',
				my: 'top left',
				adjust: { screen: true}
			},
			show: {
				event: 'mouseover',
				solo: true,
				effect: function(){
					$(this).slideDown(200);
				}
			},
			hide: {
				delay: 300,
				event: 'click mouseleave',
				fixed: true,
				inactive: 5000,
				effect: function(){
					$(this).hide(200);
				}
			}
		});
	});/* Ende autotoc */
	
	
	$('h1,h2[id],h3[id]').each(function(index,domElement){
		$('<img>').addClass('rno').qtip({
		content: {text: qrcontent($(domElement),index), title: {text: 'QR-Code usw. f&uuml;r &raquo;'+jQuery.trim($(domElement).text())+'&laquo;'}},
		position: {
				at: 'bottom left',
				my: 'top left',
				adjust: { screen: true}
			},
			show: {
				event: 'click',
				solo: true, 
				effect: function(){
					$(this).slideDown(200);
				}
			},
			hide: {
				delay: 300,
				event: 'click mouseleave',
				fixed: true,
				effect: function(){
					$(this).hide(200);
				}
			}
		}).attr({src: 'assets/images/link.png', alt: 'QR-Links anzeigen', title: 'QR-Links anzeigen (Klick)'}).prependTo($(this))
	});/* Ende QR-Codes */
	
	
if(jQuery.isFunction(jQuery.fn.colorbox)){
		$("#loginlink").colorbox({title:"Anmeldung auf www.ardiman.de",transition: "none", inline:true, href:"#loginform"}).show();
	}
	
	
	$("div.bookmarks a img").fadeTo(0,0.6);
	$("div.bookmarks a img").mouseover(function (){
		$(this).fadeTo('slow',1);
	});
	$("div.bookmarks a img").mouseout(function (){
		$(this).fadeTo('slow',0.6);
	});/* Ende Bookmark-Buttons */


	var url = 'http://www.ardiman.de/home/suchergebnisse.html?search={term}', selectionImage;
	$('#content').mouseup(function(e) {
		var selection = getSelected();
		if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,'')) && selection.length > 2 && selection.length < 81) {
			if(!selectionImage) {
				selectionImage = $('<a>').attr({
					href: url, 
					title: 'Klicken Sie hier, um nach dem markierten Ausdruck zu suchen',
					target: '_blank',
					id: 'selection-image'
				}).hide();
				$(document.body).append(selectionImage);
			}
			selectionImage.attr('href',url.replace('{term}',encodeURI(selection))).css({
				top: e.pageY - 30,	//offsets
				left: e.pageX - 13 //offsets
			}).fadeIn();
		}
	});
	
	
	$(document.body).mousedown(function() {
		if(selectionImage) { selectionImage.fadeOut(); }
	});
	
}); /* Ende $(document).ready */

