document.write('<script type="text/javascript" src="http://static.krimi-couch.de/js/jquery/jquery.tooltip.mod.js"></script>');
document.write('<script type="text/javascript" src="http://static.krimi-couch.de/js/jquery/jquery.lightbox-0.5.pack.js"></script>');
document.write('<script type="text/javascript" src="http://static.krimi-couch.de/js/jquery/jquery.tablesorter.min.js"></script>');
document.write('<script type="text/javascript" src="http://static.krimi-couch.de/js/jquery/jquery.autocomplete.mod.js"></script>');
document.write('<script type="text/javascript" src="http://static.krimi-couch.de/js/jquery/jquery.livequery.js"></script>');

var isIE = false;

function insertCode(str)
{
	document.getElementById('msg').value += ' '+str;
};
 
function addTag(obj, targetField)
{
	tag = obj.innerHTML;
	if (document.getElementById(targetField)) {
		document.getElementById(targetField).value += tag +', ';
	}
	return false;
};
 
function changeEdition(selector)
{
	var editionASIN = selector[selector.selectedIndex].value;
	var amazonLink = String(document.getElementById('amazon-link').href);
	document.getElementById('amazon-link').href = amazonLink.replace(/\/product\/.*\?/ig, '/product/'+editionASIN+'?');
	document.getElementById('cover').src = 'http://meine.literatur-couch.de/covers/'+editionASIN.substr((editionASIN.length-2),2)+'/'+editionASIN+'_m.jpg';
}; 
 
function checkFieldValue(obj)
{
	if (obj.value.substr(0,1) == '<') obj.value='';
};

/**
 *
 */

function validateForm(f)
{
	if (f.id == 'book-comment') {
		var commentMinLength = 300;
		f.comment.value = strip_tags(trim(remove_duplicates(f.comment.value)));
		var commentPosted = f.comment.value;
		if (commentPosted.length < commentMinLength) {
			informUser('Bitte geben Sie mindestens '+commentMinLength+' Zeichen ein. Sie haben '+commentPosted.length+' eingegeben.');
			return false;
		} else {
			return true;
		}
	}
	f.submitButton.value= '... einen Moment ...';
	f.submitButton.disabled = true;
};

function addFormat(tag) {
	var aTag = '['+tag+']';
	var eTag = '[/'+tag+']';
	var textarea = document.getElementById('msg');
	textarea.focus();

	if(typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = aTag + insText + eTag;
/* Anpassen der Cursorposition */
		range = document.selection.createRange();
		if (insText.length == 0) {
			range.move('character', -eTag.length);
		} else {
			range.moveStart('character', aTag.length + insText.length + eTag.length);      
		}
		range.select();
	} else if(typeof textarea.selectionStart != 'undefined') {
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		var insText = textarea.value.substring(start, end);
		textarea.value = textarea.value.substr(0, start) + aTag + insText + eTag + textarea.value.substr(end);
		var pos;
		if (insText.length == 0) {
			pos = start + aTag.length;
		} else {
			pos = start + aTag.length + insText.length + eTag.length;
		}
		textarea.selectionStart = pos;
		textarea.selectionEnd = pos;
	}
};

function informUser(s)
{
	alert(s);
};

/**
 *
 */

function resizeTextarea(obj)
{
	var msgBox = document.getElementById(obj);
	if (msgBoxEnlarged == false) {
		msgBoxWidth = msgBox.style.width;
		msgBoxHeight = msgBox.rows;
		msgBox.style.width ="99%";
		msgBox.rows = msgBoxHeight*2;
		msgBoxEnlarged = true;
		document.getElementById("resizeButton").value = "Eingabefeld verkleinern";
		if (document.getElementById("msgHelp")) {
			document.getElementById("msgHelp").style.visibility = "hidden";
			document.getElementById("msgHelp").style.position= "absolute";
		}
	} else {
		document.getElementById("resizeButton").value = "Eingabefeld vergrößern";
		msgBox.style.width=msgBoxWidth;
		msgBox.rows=msgBoxHeight;
		msgBoxEnlarged = false;
		if (document.getElementById("msgHelp")) {
			document.getElementById("msgHelp").style.visibility = "visible";
			document.getElementById("msgHelp").style.position= "relative";
		}
	}
};

/**
 *
 */

function pasteQuotation(id)
{
		var thisMsgHTML = document.getElementById("msg-"+id);
		var thisMsg = thisMsgHTML.innerHTML;
		if (isIE) {
			var msgBody = thisMsg.split("<P");
		} else {
			var msgBody = thisMsg.split("<p");
		}
		msgBody[0] = trim(msgBody[0]);
		msgBody[0] = msgBody[0].replace(/<(\/)?strong>/ig,"[$1fett]"); 
		msgBody[0] = msgBody[0].replace(/<(\/)?em>/ig,"[$1kursiv]");
		//msgBody[0] = msgBody[0].replace(/<(\/)?blockquote>/ig,"[$1zitat]");
		msgBody[0] = msgBody[0].replace(/layout/ig,"$1.gif"); 
		msgBody[0] = msgBody[0].replace(/(<([^>]+)>)/ig,""); 
		document.getElementById("msg").value = "[zitat]"+msgBody[0]+"[/zitat]";
		document.getElementById("reply2Msg").value = id;
		document.getElementById("msg").focus();
};

/**
 *
 */

function pop(obj, width, height, scrolling)
{ 
	var x=(screen.width-width)/2; 
	var y=(screen.height-height)/2;
	if (scrolling) {
		scrollbars = "yes";
	} else {
		scrollbars = "no";
	}
	popUpWindow = window.open(obj.href, "", "width="+width+",height="+height+",resizable=no,scrollbars="+scrollbars+",screenX="+x+",screenY="+y+",left="+x+",top="+y);
	popUpWindow.focus();
	return false;
};

function getCookie(key) 
{
	var s, e;
	if (document.cookie) {
		s = document.cookie.indexOf(key+"=");
		if (s < 0) return null;
		s = document.cookie.indexOf("=", s) + 1;
		e = document.cookie.indexOf(";", s);
		if (e < 0) {
			e = document.cookie.length;
		}
		return unescape(document.cookie.substring(s, e));
 	} else {
 		return null;
 	}
};

function setCookie(key, value, lifetime)
{
	var now = new Date();
	var expiry = new Date(now.getTime() + lifetime*24*60*60*1000);
	if ((key != null) && (value != null)) {
		document.cookie = key + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/; domain=.krimi-couch.de";
		alert('Diese Ansicht wurde als Ihre neue Standard-Ansicht gespeichert.');
	}
};

function dropCookie(key) 
{
	document.cookie = key+ "=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.krimi-couch.de";
};

function savePreferredDisplay(obj)
{
	if (obj.value && obj.checked) {
		setCookie('BookshelfPreferredDisplay', obj.value, 30);
	}
};

function openMailDialogue(obj)
{
	if (w3cDOMsupport) {
		var m = obj.innerHTML;
		m = m.replace(/\s+at\s+/, '@');
		document.location = 'mailto:'+m;
	}
};

function confirmSubmit(question)
{
	var agree = confirm(question);
	return agree ? true : false;
};

function adjustTopNavigation()
{
	if (w3cDOMsupport && !window.opener) {
		var header = document.getElementById('header');
		var interactive = document.getElementById('interactive');
		var news = document.getElementById('news-related');
		var topNav = document.getElementById('top-navigation');
		var level2Lists = topNav.getElementsByTagName('UL')[0].getElementsByTagName('UL');
		
		var newHeight = news.offsetTop-(interactive.offsetTop+interactive.offsetHeight);
		
		for (i=0; i<level2Lists.length; i++) {
			level2Lists[i].style.width = (header.offsetWidth - topNav.offsetWidth)+'px';
			if (newHeight >= 160) {
				level2Lists[i].style.height = newHeight+'px';
			}
		}
	}
};

function printJSDependendFunctions() {
	var liClass, newStyleName, newStyle;
	document.write('</li><li><a id="print" href="javascript:window.print()">Seite ausdrucken</a>');
};

function trim(s) {
	return s.replace(/^\s+(.*)\s+$/,"$1")
};

function strip_tags(s) {
	return s.replace(/(<([^>]+)>)/ig,""); 
};

function remove_duplicates(s) {
	return s.replace(/([\d\D])\1{3,}/ig, "$1"); 
};

function coverTooltip(anch, isReview)
{
	if (!jQuery('body').hasClass('home')) {
		var authorName = document.getElementById('breadcrumbs').getElementsByTagName('B')[0];
		var firstChar = authorName.innerHTML.substring(0, 1).toLowerCase();
	} else {
		var pattern = /.*\((.+\s+(\w{1}).+)\)/;
		pattern.exec(anch.innerHTML);
		if (RegExp.$1) {
			var authorName = RegExp.$1;
			if (authorName.indexOf(' &') != -1) {
				var pattern = /.*?\s+(\w{1})/;
				pattern.exec(authorName);
				var firstChar = RegExp.$1.toLowerCase();
			} else {
				var firstChar = RegExp.$2.toLowerCase();
			}
		}
	}
	
	href = anch.href.replace('http://'+window.location.host+'/krimis/', '');
	href = 'http://www.krimi-couch.de/public/images/books/'+firstChar+'/'+href.replace('.html', '.jpg');

	var text = '';
	
	if ((jQuery('body').hasClass('home') && anch.parentNode.parentNode.id != 'most-viewed-pages' && anch.parentNode.parentNode.id != 'newest-comments') || anch.parentNode.className == 'review' || anch.parentNode.innerHTML.indexOf('rezension.gif') != -1) {
		text = 'lesen Sie die Rezension der Krimi-Couch';
	} else {
		text = 'mehr zu diesem Buch';
	}
	
	tip = '<img src="'+href+'" width="80" />'+text;
	return tip;
}

jQuery(function() {
	
	if (jQuery.browser.msie) {
		isIE = true;
	}

	jQuery.tablesorter.defaults.widgets = ['zebra'];
	
	jQuery("table.sortable").livequery(function() {
		jQuery("table.sortable").tablesorter({
			headers: { 
				0: { 
					sorter: false 
				}
			}
		})
	});
		
	if (isIE && parseInt(jQuery.browser.version.substr(0,1)) == 6) {
		jQuery('#top-navigation li').mouseover(function(){			
			if (jQuery(this).parent().parent().parent().get(0).tagName == 'DIV') {
				jQuery(this).addClass('hover');
			}
		}).mouseout(function(){
			if (jQuery(this).parent().parent().parent().get(0).tagName == 'DIV') {
				jQuery(this).removeClass('hover');
			}
		});		
		jQuery('#couch-hopper').mouseover(function(){
			jQuery(this).addClass('hover');
		}).mouseout(function(){
			jQuery(this).removeClass('hover');
		});
	}
	
	jQuery('#couch-hopper').append('<a href="#" onclick="return false;">Couch wechseln</a><ul><li><a href="http://www.histo-couch.de/">Histo-Couch.de</a></li><li><a href="http://www.kinderbuch-couch.de/">Kinderbuch-Couch.de</a></li><li><a href="http://www.kochbuch-couch.de/">Kochbuch-Couch.de</a></li><li class="l"><a href="http://www.phantastik-couch.de/">Phantastik-Couch.de</a></li></ul>');		

	jQuery('input#tag-search').autocomplete({
		serviceUrl:'http://www.krimi-couch.de/krimis/schlagwort-suche.json',
		delimiter: /(,|;)\s*/,
		width: 290,
		classname: 'tag-search'
	});
	
	jQuery('div#search-functions input#query').autocomplete({
		serviceUrl:'http://www.krimi-couch.de/krimis/suche.json',
		delimiter: /(,|;)\s*/,
		width: 180,
		maxHeight: 206,
		deferRequestBy: 300,
		minChars: 3, 
		onSelect: function(value, data) { 
			jQuery('div#search-functions input#query-direct').attr('value', data);
			jQuery('form#search-fulltext').submit();
		},
		classname: 'content-search'
	});
	
	/**
	 *
	 */

	jQuery('a[rel=lightbox]').lightBox({ 
		overlayOpacity: 0.3, 
		containerResizeSpeed: 350, 
		imageLoading: '/images/layout/ajax-loader.gif', 
		txtImage: 'Ausgabeart', 
		txtOf: 'von'
	});
	
	/**
	 *
	 */	
	
	if (jQuery('body').hasClass('home')) {
	
		jQuery('#edition-notes ul li a').tooltip({ 
		    showURL: false, 
		    bodyHandler: function() { 
		        return coverTooltip(this, true); 
		    } 
		});
		
		jQuery('ol#most-viewed-pages li a').tooltip({ 
		    showURL: false, 
		    bodyHandler: function() { 
		        return coverTooltip(this, false); 
		    } 
		});
		
	} else {
	
		jQuery('div#content dl.bib a').tooltip({ 
			showURL: false, 
			bodyHandler: function() { 
				return coverTooltip(this, false); 
			} 
		});
		
		jQuery('div#content h2 + ul').find("a[href^='/krimis/']").tooltip({ 
			showURL: false, 
			bodyHandler: function() {
				return coverTooltip(this, false); 
			} 
		});
		
		jQuery('div#content dl.bib dt.org').attr('title', 'Titel der Originalausgabe').css('cursor', 'help');
		jQuery('div#content dl.bib dd.org').attr('title', 'Titel der Originalausgabe').css('cursor', 'help');
		jQuery('div#content dl.bib dd.year').attr('title', 'Erscheinungsjahr der Originalausgabe').css('cursor', 'help');
	}
	
	jQuery('ul#newestcomments li a').tooltip({ 
		showURL: false, 
		bodyHandler: function() { 
			return coverTooltip(this, false); 
		} 
	});
	
	/**
	 *
	 */	
	
	if (jQuery('body').hasClass('book')) {

		/**
		 *
		 */
	
		jQuery('select#edition-selector').change(function() 
	    { 
	        editionASIN = jQuery('select#edition-selector').val();
			jQuery('img.cover').each(function (i) {
				coverASIN = this.id.substr(6);
				coverASIN == editionASIN ? jQuery(this).show() : jQuery(this).hide();
			});
		});
		
		/**
		 *
		 */
		if (jQuery('#biblio-data').length) {
			jQuery('#biblio-data').hide();
			jQuery('#bookdata h3').addClass('bib-toggler');
			jQuery('#bookdata h3').prepend('<a href="#" id="bib-toggler"><img id="bib-toggler-img" src="/images/layout/2007/icons/bib-toggler.gif" alt="Details anzeigen"/></a> '); 
			jQuery('a#bib-toggler').toggle(function()
			{ 
				jQuery('#bib-toggler-img').attr({
					'src' : '/images/layout/2007/icons/bib-toggler-x.gif',
					'alt' : 'Details verstecken'
				});
				jQuery('#biblio-data').attr('title', 'Details verstecken');
				jQuery('#biblio-data').animate({ height: 'show', opacity: 'show' }, 'slow');
			},function()
			{ 
				jQuery('#bib-toggler-img').attr({
					'src' : '/images/layout/2007/icons/bib-toggler.gif',
					'alt' : 'Details anzeigen'
				});
				jQuery('#biblio-data').attr('title', 'Details anzeigen');
				jQuery('#biblio-data').animate({ height: 'hide', opacity: 'hide' }, 'slow'); 
				
			});
		}
	
	}
	
	if (jQuery('body').hasClass('board')) {
	
		jQuery('div.msg div').after('<p class="txtsmall tar posting-action"><a class="paste-quotation quote-link"">zitieren</a></p>');
		/*
		jQuery('div.msg.editable p.posting-action').prepend('<a class="edit-posting quote-link">editieren</a> | ');

		jQuery('a.edit-posting').click(function() {
			var msg = jQuery(this).parent().parent().children('div').html();

			msg = msg.replace(/\<(\/?)(strong)\>/ig,"[$1fett]");
			msg = msg.replace(/\<(\/?)(i)\>/ig,"[$1kursiv]");
			msg = msg.replace(/<img src\=\"http\:\/\/www\.krimi-couch\.de\/images\/layout\/emoticons\/wink\.gif\" alt\=\"\*zwinkert\*\"\>/ig, ";-)");
			msg = msg.replace(/<img src\=\"http\:\/\/www\.krimi-couch\.de\/images\/layout\/emoticons\/biggrin.gif\" alt\=\"\*grinst breit\*\"\>/ig, ":-D");
			msg = msg.replace(/<img src\=\"http\:\/\/www\.krimi-couch\.de\/images\/layout\/emoticons\/silenced.gif" alt\=\"\*schweigt lieber\*\"\>/ig, ":-#");
			msg = msg.replace(/<img src\=\"http\:\/\/www\.krimi-couch\.de\/images\/layout\/emoticons\/sad.gif\" alt\=\"\*schaut traurig\*\"\>/ig, ":-(");
			
			msg = trim(strip_tags(msg));
	
			jQuery(this).parent().parent().children('div').html(msg);
			jQuery(this).parent().parent().children('div').wrapInner('<textarea cols="70" rows="15"></textarea>');
			jQuery(this).parent().parent().wrap('<form action="/" method="post" class="edit-posting" onsubmit="alert(\'here\'); return false;"></form>');
			jQuery(this).parent(0).hide();
			jQuery(this).parent().parent().children('p:first').before('<p class="tar"><input type="submit" class="submit" value="Änderungen speichern" /></p>');
		});
		*/
		jQuery('a.spoiler').click(function() {
			if (jQuery(this).next().hasClass('shown')) {
				jQuery(this).text('anzeigen');
				jQuery(this).next().animate({ height: 'hide', opacity: 'hide' }, 'slow'); 
				jQuery(this).next().removeClass('shown');
			} else {
				jQuery(this).text('verbergen');
				jQuery(this).next().animate({ height: 'show', opacity: 'show' }, 'slow'); 
				jQuery(this).next().addClass('shown');
			}
			jQuery(this).blur();
			return false;
		});
	}
	 
	/**
	 *
	 */
	 
	jQuery('#user-comments td.message input[type*=image]').click(function()
	{
		var ajaxURL        = (jQuery(this).parent().attr('action')).replace(/\.html/, '.xml');
		var ajaxAction     = jQuery(this).parent().children('input[name=action]').val().replace(/\.html/, '.xml');
		var commentQuality = jQuery(this).parent().children('input[name=commentQuality]').val();		
		var commentForm	   = jQuery(this).parent().parent();
		var commentTH	   = jQuery(this).parent().parent().parent().parent().prev();
		var commentTD	   = jQuery(this).parent().parent().parent().parent();
		var loadingImg	   = jQuery(this).parent().parent().parent().children('div').children('img');

		if (!commentQuality) commentQuality = 0;
		
		commentForm.hide();
		loadingImg.show();	
		
		$.post(ajaxURL,{
			idComment: jQuery(this).parent().children('input[name=idComment]').val(),
			commentQuality: commentQuality,
			action: ajaxAction
			}, function(xml) {
				if (ajaxAction == 'removeProposal') {
					commentTH.hide();
					commentTD.hide();
				} else {			
					loadingImg.hide();
					commentForm.replaceWith('<div class="txtsmall tar"><br /><b>Vielen Dank für Ihre Mithilfe!</b></div>');	
					commentForm.show();
				}
			}			
		);
		
		return false;
	});
	
	jQuery('#user-comments td.message form').submit(function()
	{
		var ajaxURL        = (jQuery(this).attr('action')).replace(/\.html/, '.xml');
		var ajaxAction     = jQuery(this).children('input[name=action]').val().replace(/\.html/, '.xml');
		var commentQuality = jQuery(this).children('input[name=commentQuality]').val();

		if (!commentQuality) commentQuality = 0;
			
		$.post(ajaxURL,{
			idComment: jQuery(this).children('input[name*=idComment]').val(),
			commentQuality: commentQuality,
			action: ajaxAction
			}, function(xml) {}
		);
		jQuery(this).parent().parent().replaceWith('<p class="txtsmall tar"><br /><b>Vielen Dank für Ihre Mithilfe!</b></p>');	
		return false;
	});
	 
	jQuery('input.text').focus(function() { 
		jQuery(this).addClass('focused');
		if (jQuery(this).val().substr(0,1) == '<') jQuery(this).val('');
	});
	jQuery('input.text').blur(function() { 
		jQuery(this).removeClass('focused'); 
	});	
	
	jQuery('textarea.text').focus(function() { 
		jQuery(this).addClass('focused');
		if (jQuery(this).val().substr(0,1) == '<') jQuery(this).val('');
	});
	jQuery('textarea.text').blur(function() { 
		jQuery(this).removeClass('focused'); 
	});		
	
});

//if (parent.frames.length && document.getElementsByTagName("BODY")[0].id != 'www.krimi-couch.de') window.top.location.replace(document.location.href);