

var lastQuery = '';
var divLayername = 'suggest-box';


function suggestInit()
{

	$j('#tx-indexedsearch-searchbox-sword').attr( "autocomplete","off" );

	$j('form[name=searchform] input[name=tx_indexedsearch[sword]]').keyup
	 (
		function (e)
		{
			if (e.keyCode == 38)
			{
				ffMoveUp();
			}
			else if(e.keyCode == 40)
			{
				ffMoveDown();
			}
			else if(e.keyCode == 13) // Enter
			{
				ffSetSearchWord();
			}
			else if (e.keyCode == 27)
			{
				$j('#'.divLayername).hide();
			}
			else
			{
				getSuggestions();
			}
		}
	);

	$j('body').click(function () {
      //hide layer
			$j('#'+divLayername).hide();
   });


}


function getSuggestions()
{
	var query = $j('#tx-indexedsearch-searchbox-sword').attr('value');
	var languageUid = ( sysLanguageUid >= 0 ) ? sysLanguageUid : 0;
	
	//check if the same query was asked before
	if(lastQuery == query)
	{
		return;
	}
	else
	{
		lastQuery = query;
	}

	$j.ajax
	(
		{
			type: "POST",
			data: {},
			url: "index.php?id=4&L="+languageUid+"&type=549&tx_indexedsearch[sword]="+encodeURI(query)+"&tx_indexedsearch[lang]="+escape($j('#tx-indexedsearch-selectbox-lang').attr('value'))+"&tx_indexedsearch[results]=5&tx_indexedsearch[cropDesc]=65",
			contentType: "application/x-www-form-urlencoded; charset=UTF-8",
			success:
				function (outputText, textStatus)
				{
					if (outputText.length > 0)
					{
						//show layer
						$j('#'+divLayername).html(outputText).show();
					}
					else
					{
						//hide layer
						$j('#'+divLayername).hide();
					}
				}
		}
	);
}


function addGoogleEventTracking() {
	$j('a.download').each( function() {
		$j(this).bind('click', function() {
			if(pageTracker) {
				pageTracker._trackEvent( $j(this).attr('class'), $j(this).attr('title'), $j(this).text() );
				/*if( console ) {
					console.log( $j(this).attr('class') + ',' + $j(this).attr('title') + ',' + $j(this).text() )
				}*/
			}
		});
	});
}


function showHideSharethis() {
	$j('div#sharethis').hover( function() {
		$j('div#sharethis-layer').show();
	}, function() {
		$j('div#sharethis-layer').hide();		
	});
	// create xing share link
	var xingShare = 'https://www.xing.com/app/user?op=share;url=' + encodeURIComponent(window.location.href) + ";title=" + encodeURIComponent(document.title);
	$j('a.share-xing').attr('href', xingShare);
}




$j(document).ready(function(){
	
	addGoogleEventTracking();
	
	suggestInit();
	
	showHideSharethis();

	$j(".faq").accordion(
	{
		autoHeight: false
	});

});

