/**
 * Functions for Ajax Live Search.
 *
 **/
var tx_mhajaxsearch_spinner = '1';
var tx_mhajaxsearch_lastq = '';

function tx_mhajaxsearch_trim(_string) {
	while(_string.substring(0, 1) == '') {
		_string = _string.substring(1, _string.length);
	}

	while(_string.substring(_string.length - 1, _string.length) == '')	{
		_string = _string.substring(0, _string.length - 1);
	}

	return _string;
}

function tx_mhajaxsearch_search(_q) {

	_q = tx_mhajaxsearch_trim(_q);
	if(tx_mhajaxsearch_lastq == _q || _q == '') { return false; }
	
	/* Search word has to be at least 3 characters long! */
	if (_q.length < 49) {
		return false;
	}

	if(tx_mhajaxsearch_spinner != 1) {
		document.getElementById('tx_mhajaxsearch_spinner').style.display = 'inline';
	}

	tx_mhajaxsearch_lastq = _q;
	tx_mhajaxsearch_pi1Search(_q);
}
 
function reset() {
	document.getElementById('tx_mhajaxsearch_q').value = 'Suchbegriff';
	document.getElementById('tx_mhajaxsearch_result').style.display = 'none';
}
 
function reset_en() {
	document.getElementById('tx_mhajaxsearch_q').value = 'Searchword';
	document.getElementById('tx_mhajaxsearch_result').style.display = 'none';
}
 
function reset_cn() {
	document.getElementById('tx_mhajaxsearch_q').value = '搜索关键词';
	document.getElementById('tx_mhajaxsearch_result').style.display = 'none';
}

function init() {
	document.getElementById('tx_mhajaxsearch_q').name = 'tx_mhajaxsearch_q';
	if (document.getElementById('tx_mhajaxsearch_q').value == 'Suchbegriff' ||
		document.getElementById('tx_mhajaxsearch_q').value == 'Searchword' ||
		document.getElementById('tx_mhajaxsearch_q').value == '搜索关键词'
	) {
		document.getElementById('tx_mhajaxsearch_q').value='';
	}
}

function search() {
 	document.getElementById('tx_mhajaxsearch_result').style.display = 'block';
	tx_mhajaxsearch_search(xajax.getFormValues('tx_mhajaxsearch_form', 0, 'tx_mhajaxsearch_q'));
}




jQuery(document).ready(function(){

    var w = $( '#navigation_2012' ).width();

    $.position_level2 = function( item ){

        var l2 = $( ".level2" , item );

        //level 1 item
        var this_pos = $( item ).position();
        var this_w = $( item ).width();
        var li_center = this_pos.left + ( this_w / 2);

        var l2_w = l2.width();
        var l2_center = l2_w / 2;
        var l2_fix = ( li_center - l2_center );
        if( l2_fix < 0 ){
            l2_fix = 0;
        }
        l2.css( 'left' , l2_fix + 'px' );
        if( l2_fix + l2_w > w ){
            l2.css( 'left' , 'auto' );
            l2.css( 'right' , 0 + 'px' );
        }
    };

    $( '#navigation_2012 .level1 > li' ).each(
        function( i , item ){
            var l2 = $( ".level2:visible" , item);
            $( l2 ).css( 'z-index' , 0 );
            if( $( l2 ).length == 1 ){
                $.position_level2( this );
            }
        }
    );

    $( '#navigation_2012 .level1 > li:not(.active)' ).hover(
        function( e ){
            var l2 = $( ".level2" , this);
            $( l2 ).css( 'z-index' , 5 );
            if( $( l2 ).length == 1 ){
                $.position_level2( this );
            }
            $( 'li.active .level2' ).hide( 0 );

        },
        function( e ){
            // nothing
            $( 'li.active .level2' ).show( 0 );
        }
    );

    $( '#navigation_2012 .level1 > li.active' ).hover(
        function( e ){
            //$( 'li.active .level2' ).hide( 0 );
        },
        function( e ){
            // nothing
        }
    );

    $( "#navigation_2012 .level2 li:only-child" ).addClass( 'only-child' );
});
