$(function($) {

	$.fn.found = function(myFunction) {
		if (this.length) {
			myFunction.call(this);
		}
	};

	$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};


	// -========================
	// = loginform top - begin
	// -========================
	$('a#buttonlogin').click(function() {
		$('#toploginbox').slideDown(500, function() {
			$('#top_login_username').focus();
		});
		return false;
	});

	$('#toploginbox').focus().keypress( function(e) {
		if ( e.keyCode == 27 ) {
			$(this).slideUp(500);
		};
	});

	$('#closetoplogin').click(function() {
		$('#toploginbox').slideUp('fast');
		return false;
	}); // loginform - end


	// -=============
	// = Hyperlinks
	// -=============
	$('a').click(function() {
	  $(this).blur();
	});

	// open external links in new window
	$('a[href*="http://"]:not([href*="' + location.hostname + '"])').attr('target', '_blank');

	// add rel="nofollow" to links with class="nofollow"
	$('a.nofollow').attr('rel','nofollow');

	// -============
	// = Hideables
	// -============
	$('.hideableSimple').each(function() {
		$(this).prev().html('<a href="#" onclick="return showHide(this, false);">' + $(this).prev().html() + '</a>');
		$(this).hide();
	});

	// TODO: Funktion verallgemeinern
	$('.showlink-next').click(function() {
		$(this).next('div.hidden').slideToggle().removeClass('hidden');
		$(this).hide();
		return false;
	});


	/* UI-Tabs helper */

	$('ul.ui-tabs-nav a').click(function() {
		$(this).parent('li').siblings().removeClass('ui-tabs-selected');
		$(this).parent('li').addClass('ui-tabs-selected');
	});

	// -=======================
	// = sidebar teaser stripes
	// -=======================
	$('.stripes').children(':odd').addClass('even');
	$('.stripes').children(':even').addClass('odd');

	// -=======================================
	// = errorhandling in plugin tx_comments:
	// -=======================================
	$('span.errormsg').found(function() {
		$(this).parent().addClass('error');
	});

	// -======
	// = CSH
	// -======
	// select all desired input fields and attach tooltips to them
//	$('form.yform :input').tooltip({
//	    position: 'center right',
//	    offset: [-2, 10],
//	    effect: 'fade',
//	    opacity: 0.7,
//	    // use this single tooltip element
//	    tip: '.tooltip'
//	});

	// Tooltips
//	$('.tip').tooltip({
//	    position: 'top center',
//	    offset: [-2, 10],
//	    effect: 'fade',
//	    opacity: 0.7,
//	    // use this single tooltip element
//	    tip: '.tooltip'
//	});


	//Fancybox
	$('.jqfancybox').fancybox({
		'hideOnContentClick': 1,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 500,
		'overlayShow': 0,
		'overlayOpacity': 0.4
	});

	// chosen for select boxes
	$('.chzn-select').chosen();

	// selectContent in element oncklick
	function selectAllText(textbox) {
		textbox.focus();
		textbox.select();
	}

	$('.selectContent').click(function() {
		selectAllText($(this));
	});

});(jQuery);

/* simple print div-containers */
function PrintElem(elem) {
	Popup($(elem).text());
}
function Popup(data) {
	var mywindow = window.open('', 'my div', 'height=400,width=600');
	mywindow.document.write('<html><head><title>my div</title>');
	/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
	mywindow.document.write('</head><body >');
	mywindow.document.write(data);
	mywindow.document.write('</body></html>');
	mywindow.document.close();
	mywindow.print();
	return true;
}


function showHide(elem, withTextChange) {
	$(elem).parent().next().toggle();
	if (withTextChange) {
		if ($(elem).html() == 'show') {
			$(elem).html('hide');
		} else {
			$(elem).html('show');
		}
	}
	return false;
};

// default JS from TYPO3
var browserName=navigator.appName;var browserVer=parseInt(navigator.appVersion, 10);var version="";var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){version="n3";}else{version="n2";}
function blurLink(theObject){if(msie4){theObject.blur();}}
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-2);}
