/**
 * GENIOS JavaScript core
 *
 * @author  Konstantyn Smirnov <konstantyn.smirnov@genios.de>
 *
 * Conventions:
 * Variables in camelCase
 * Classes in PascalCase
 * Function/Methods in camelCase
 */
 
function text2Tip( target, text, e ){
    e.stop();
    if(text.blank()) return false;

    $('hitPreview').update( text );
    $('hitPreview').appear({ duration: 0.3 });
    var elemDim = $('hitPreview').getDimensions();

    $('hitPreview').style.left = (e.pointerX() + 12) + 'px';

    // Beschränkung auf sichtbare Fenstergröße
    if (e.pointerY() >= elemDim.height){
        $('hitPreview').style.top = (e.pointerY() - (elemDim.height + 20) ) + 'px';
    }else{
        $('hitPreview').style.top =  (e.pointerY() + 10) + 'px';
    }
    var windowDim = document.viewport.getDimensions();
    if (windowDim.width < (e.pointerX() + (elemDim.width + 20))){
        $('hitPreview').style.left = windowDim.width - (elemDim.width + 20) + 'px';
    }
}

function expandTip( selector, e ){
    var target = ( 'string' == typeof selector ) ? e.findElement( selector ).down( 'span.hint' ) : selector;
    var text = target.innerHTML.strip();
    text2Tip( target, text, e );
}

function collapseTip( e ){
	try{
		e.stop();
		$( 'hitPreview' ).hide();
	}catch( e ){}
}


function addToolTips(){
	$$( 'a.boxList' ).each( function( a ){
		a.observe( 'mouseover', expandTip.curry( 'td.boxCol4' ) );
		a.observe( 'mouseout', collapseTip );
	} );
}

function extendTitleTips(){
    $$( '[title^=tip_]' ).each( function( a ){
        a.observe( 'mouseover', expandTip.curry( $( a.title.substring( 4
) ) ) );
        a.title = '';
        a.observe( 'mouseout', collapseTip );
    } );

    $$( '[title]' ).each( function( a ){
        if (a.title.substring(0, 8) == 'tiplayer'){
            a.observe( 'mouseover', expandTip.curry( $( a.title.substring( 9
) ) ) );
            a.title = '';
        }else{
            a.observe( 'mouseover', text2Tip.curry( $( a.title ), a.title ) );
            a.title = '';
            a.observe( 'mouseout', collapseTip );
        }
    } );
}

function extendMiniHitTips(){
	$$( 'dd.verlauf' ).each( function( a ){
		a.observe( 'mouseover', expandTip.curry( 'dd.verlauf' ) );
		a.observe( 'mouseout', collapseTip );
	} );
	extendTitleTips();
}

function tooltipShow(){
    $('hitPreview').show();
}

function tooltipHide(){
    if ($('hitPreview').down(0)){
        if ($('hitPreview').down(0).className != 'innerModuleTippList layer'){
        collapseTip();
    }
    }
}

function initializeTooltip()
{
	var preview = new Element( 'div', { id:'hitPreview', style:'display:none;' } );
	preview.addClassName( 'hitPreview' );
    preview.observe( 'mouseover', tooltipShow );
	preview.observe( 'mouseout', tooltipHide );
	document.observe( 'focus', tooltipHide );
	$$( 'body' )[ 0 ].insert( preview );
	addToolTips();
	extendTitleTips();

    // Layer-Absoftung
    $('layer_overlay').observe('click', GENIOS.SoftedLayer.blur);

    $('body').onclick = function(){
        if ($('box_mygenios')){
            
            if (GENIOS.layerMyGenios == true){
                GENIOS.layerMyGeniosStatus++;
                if (GENIOS.layerMyGeniosStatus == 2 || GENIOS.layerMyGeniosStatus == 4){
                    GENIOS.toogleLayer('layer_mygenios','link_mygenios');
                    GENIOS.layerMyGeniosStatus = 0;
                }
            }
        }
    }
}

document.observe( 'dom:loaded', function(){
	initializeTooltip();
	
});

