/* mobile browser detection */
function isHandheld() {
	var uagent = navigator.userAgent.toLowerCase();
	return $('#breadcrumb').is(':hidden');
}



$(document).ready(function () {
	
	/* Alles, was die lokalen #-Links betrifft */
	if($.cookie('deactivateScroll') == null) {
	
		// used for scrolling on url hashes
		/* $.localScroll.hash({
			queue:true,
			lock:false,
			duration:1500
		}); */
	
		// used for scrolling when clicking on an item
		$.localScroll({
			queue:true,
			lock:false,
			duration:1000,
			hash:true
		});
		
		$(window).mousewheel(function(event, delta) {
			$.scrollTo.window().queue([]).stop();
			return true;
		});
		/* $(window).click(function() {
			$.scrollTo.window().queue([]).stop();
			return true;
		}); */
	}
	
	$('.up').click(function() {
		$.scrollTo( '#main' , 1900);
		return false;
	});

	
	/* Alles, was die Suchbox betrifft */
	
	var searchText = "ActiveVB durchsuchen";
	
	$('#searchboxText').val(searchText);
	$('#searchboxText').addClass('searchboxHover');

	$('#searchboxText').click(function() {
		if ($('#searchboxText').val() == searchText) {
			$('#searchboxText').val('');
			$('#searchboxText').removeClass('searchboxHover');
		}
	});
	
	$('#searchboxText').focusout(function() {
		if ($('#searchboxText').val() == '') {
			$('#searchboxText').val(searchText);
			$('#searchboxText').addClass('searchboxHover');
		}
	});
	
	if(!isHandheld() && $('#socialshareprivacy').length > 0) {
		$('#socialshareprivacy').socialSharePrivacy({
			services : {
				facebook : {
					'app_id' : '0123456789',
					'dummy_img' : '/resources/static/images/socialshareprivacy/dummy_facebook.png'
				}, 
				twitter : {
					'status' : 'on',
					'dummy_img' : '/resources/static/images/socialshareprivacy/dummy_twitter.png'
				},
				gplus : {
					'status' : 'on',
					'dummy_img' : '/resources/static/images/socialshareprivacy/dummy_gplus.png'
				}
			},
			css_path : '/resources/static/styles/socialshareprivacy.css'
		});
		$('#socialshareprivacy').find('ul').append('<li class="xing"><a href="http://www.xing.com/app/user?op=share;url=' + document.location + '" target="_blank" title="Auf XING teilen"><img src="/resources/static/images/xing_icon_20x20.png" width="20" height="20" alt="Ihren XING-Kontakten zeigen" /></a></li>');
	}

	/* disable root navigation links for mobile devices*/
	if(isHandheld()) {
		$('#menu').show();
		$('#content').prepend("<div id='mobilesearch'>" + $('#menu-inner li.last').html() + "<span class='clear'></span></div>");
		$('#menu-inner li.right').remove();
		
		// Detect whether device supports orientationchange event, otherwise fall back to
		// the resize event.
		var supportsOrientationChange = "onorientationchange" in window,
		    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
		
		window.addEventListener(orientationEvent, function() {
		    initMobileMenu();
		}, false);
	
		initMobileMenu();

	}
	
	/* backward compatibility for notizinsertform */
	if($('#usercomments').length > 0) {
		$('div.notizinsertcontent').hide();
		$('p.notizinsertcontent').click(function() {
			$(this).hide();
			$('div.notizinsertcontent').slideDown('slow');
		});
	}
});

function initMobileMenu() {
	
	$('#menu-inner > ul > li > a > img').parent('a').attr('href', '#menu');
	var menu = $('#menu');
	var hiddenMenuTop = -($('#menu-inner').outerHeight()) + $('#heading').outerHeight();
	var visibleMenuTop = $('#heading').outerHeight();
	menu.css({top : hiddenMenuTop});
	
	$('#mobileMenu').unbind().click(function() {
		/* -1 is a workaround for windows phone 7 */
		if(menu.position().top == visibleMenuTop || menu.position().top == visibleMenuTop - 1)
			menu.animate({top : hiddenMenuTop});
		else
			menu.animate({top : visibleMenuTop});
	});
}

$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
};
