// xtended search einblenden
var pmi = 1;

function arrow() 
{		
	if(pmi==1)
	{
		document.getElementById('xs_switch').className='xs_switch_closed';
		pmi = 0;
	}
	else
	{
		document.getElementById('xs_switch').className='xs_switch_open';
		pmi = 1;
	}
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

// Lupenfunktion
	
$(function() {
	$(".zoom_area").jqzoom({
		zoomWidth: 200,
		zoomHeight: 200,
		title: false,
		showPreload: true,
		xOffset: 30,
		showEffect: 'fadein',
		fadeinSpeed: 'slow',
		hideEffect: 'fadeout',
		fadeoutSpeed: 'slow'
	});
});

var newBlock = '<li>Neuer Block Test</li>';

function carousel_callback( carousel, state) {
    // BWA-HA...I have the carousel now....FEEL THE POWER!

    // reset empties it out
    $('#reset').click( function( evt ) {
        carousel.reset();
    });

    // here's how to call add
    $('#add').click( function( evt ) {
        // ..this just adds to the first spot..modify as needed
        carousel.add(5, newBlock);
        $('#scroll_right').click();$('#scroll_right').click();
        
    });
}

var showAllText = 'alle Begriffe anzeigen';
var hideText = '_minimieren';

function closeList(){
    $(this).parent().parent().css('display','none')
     .parent()
     .find('.min_list_wrapper')
     .fadeIn(1000);
     return false;
}

function openList(){
    $(this)
    .closest('.min_list_wrapper')
    .css('display','none')
     .parent()
     .find('.extend_list_wrapper')
     .fadeIn(1000);
     return false;
}

function showDetailInfo(){
    var next = this.nextSibling;
    while(next){
        if($(next).hasClass('detail_trigger')){
            break;
        }
        if($(next).hasClass('detail_trigger_info')){
            $(next).slideToggle(350);
			return false;
            break;
        }
        var next = next.nextSibling;
    }
}

function ScrollHelper(ele){
    $(window).scroll(keepElementInRange);
    oldTop = $(window).scrollTop();
    function keepElementInRange(){
        var top  = $(window).scrollTop();
        var dirUp = (top<oldTop)?true:false;
        var eleTop = $(ele).offset()['top'];
        oldTop = top;
        if(dirUp){
            if(eleTop>top+10){
                $(ele).css('top',top+10);
            }
        }else{
            var eleHeight = $(ele).height();
            var windowHeight = $(window).height();
            if(eleTop+eleHeight<top+windowHeight-10){
                $(ele).css('top',top+windowHeight-10-eleHeight);
            }
        }
        
    }
    this.unbind = function(){
        $(window).unbind('scroll',keepElementInRange);
    }
    return this;
}

$(document).ready(function() {
	$('body').delegate('.openList','click',openList);
    $('body').delegate('.closeList','click',closeList);
    $('body').delegate('.detail_trigger','click',showDetailInfo);
    $('.marginal_thesaurus_detail').css('display','none');
    $('.limit_list').each(
        function(index){
            var listLenght = 5;
            if($(this).children().length>listLenght){
                var wrapper = document.createElement('div');
                $(wrapper).addClass('extend_list_wrapper').css('display','none');
                
                var openHelper = document.createElement('div');
                $(openHelper).addClass('arrow min_list_wrapper').html('<a class="openList" href="#">'+showAllText+'</a>');
                var closeHelper = document.createElement('div');
                $(closeHelper).addClass('arrow').html('<a class="closeList" href="#">'+hideText+'</a>');
                $(wrapper).append(closeHelper);
                $(this).children().each(function(listIndex){
                    if(listIndex>=listLenght){
                        $(wrapper).append(this);
                    }
                });
                $(wrapper).append(closeHelper.cloneNode(true));
            }
            $(this).append(openHelper);
            $(this).append(wrapper);
        }
    );
    
    $('.limit_length').each(function(){
        var maxLength = 100;
        var length = 0;
        var oldLength = 0;
        var child = null;
        var longText= '';
        var found = false;
        var children = $(this).children();
        var wrapper = document.createElement('div');
        var minWrapper = document.createElement('div');
        $(minWrapper).addClass('min_list_wrapper');
        $(wrapper).addClass('extend_list_wrapper').css('display','none');
        var openHelper = document.createElement('div');
        $(openHelper).addClass('arrow').html('<a class="openList" href="#">mehr</a>');
        var closeHelper = document.createElement('div');
        $(closeHelper).addClass('arrow').html('<a class="closeList" href="#">ausblenden</a>');
        
        for(var i=0,len=children.length;i<len;i++){
            oldLength = length;
            child = children[i];
            length += $(child).text().length;
            if(length<maxLength){
                var theClone = child.cloneNode(true);
                $(wrapper).append(theClone);
                $(minWrapper).append(child);
            }
            if(oldLength>=maxLength){
                $(wrapper).append(child);
                found = true;
            }
            if(oldLength<maxLength && length>=maxLength){
                found = true;
                var splitChild = child.cloneNode(true);
                $(wrapper).append(splitChild);
                longText = $(child).text();
                var start = longText.indexOf(' ',maxLength-oldLength);
                start = (start==-1)?maxLength-oldLength:start;
                var newText = longText.substr(0,start);
                //var splitText = longText.substr(start);
                $(child).text(newText);
                $(minWrapper).append(child);
            }
        }
        $(this).append(minWrapper);
        if(found){
          $(minWrapper).append(openHelper);
          $(wrapper).append(closeHelper);
          $(this).append(wrapper);
        }
            
    });
    
    //TruncExpandText();
    
  	// Carousel

	jQuery('#cover').jcarousel({
        auto: 4,
        scroll: 1,
        wrap: 'circular',
        buttonNextHTML: '<div class="scroller" id="scroll_right"></div>',
        buttonPrevHTML: '<div class="scroller" id="scroll_left"></div>',
        animation: 800,
        easing: 'easeOutCubic',
        initCallback: mycarousel_initCallback
    });

	// xtended search einblenden
	$('#extsearch').addClass("hide_me");		
	
	$("#xs_switch").click().toggle(function() {
		$('.extended_search_content').animate({
			opacity: 'show',
			height: 'toggle'
		}, 400);
	}, function() {
		$('.extended_search_content').animate({
			opacity: 'hide',
			height: 'toggle'
		}, 400);
	});
	
	$('#scroll_left').removeAttr("style");	
	$('#scroll_right').removeAttr("style");	
	
	$('#carousel_box').mouseenter(function() {
  		$('#scroll_left').fadeIn();	
  		$('#scroll_right').fadeIn();	
	});
	
	$('#carousel_box').mouseleave(function() {
  		$('#scroll_left').fadeOut();	
  		$('#scroll_right').fadeOut();	
	});
	
	// Inner Fade
	
	$('#person_fade').innerfade({ 
		speed: 1000, 
		timeout: 6000, 
		type: 'sequence', 
		containerheight: '292px' 
	}); 
	
	// Viewer - Fancybox
	
	$("a.page_viewer").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'width'			: 920,
		'height'		: 680,
		'margin'		: 30,
		'titlePosition'	: 'outside',
		'scrolling'		: 'no',
		'autoscale'		: false,
		'autoDimensions': false,
		'overlayColor' 	: '#000',
		'type'			: 'ajax',
		'centerOnScroll' : true
	});	
	
	$('.help_off').click(function() {
		if($(this).attr('class').match(/help_off/)){
			$('#tool_helper').fadeIn();
  			$('#help').removeClass('help_off');
  			$('#help').addClass('help_on');
		}else{
			$('#tool_helper').fadeOut();
  			$('#help').removeClass('help_on');
  			$('#help').addClass('help_off');
		}
  		
	});
	
	$('.close_callout').click(function() {
  		$('#tool_helper').fadeOut();
  		$('#help').removeClass('help_on');
  		$('#help').addClass('help_off');
	});
	
	$('#lupe').click(function() {
		if($(this).attr('class').match(/lupe_on/)){
			$('#lupe').removeClass('lupe_on');
  			$('#lupe').addClass('lupe_off');			
		}else{
			$('#lupe').removeClass('lupe_off');
  			$('#lupe').addClass('lupe_on');
		}		
	});

	   
	// Carousel - Begriffsregister
  
  var lastEle = $('#thes_nav_ul').children().length;
  
	jQuery('#thes_nav_box').jcarousel({
        auto: 0,
        scroll: 1,
        visible: 3,
        buttonNextHTML: '<div class="scroller" id="scroll_right"></div>',
        buttonPrevHTML: '<div class="scroller" id="scroll_left"></div>',
        animation: 800,
        easing: 'easeOutCubic',
        initCallback: carousel_callback
    });
    var carousel = $('#thes_nav_box').data('jcarousel');
    if(carousel){
      var last = carousel.list.children().length;
      
      window.setTimeout(function(){carousel.scroll(last,true);},1000)
      
    }
    $("#fancybox-close").attr('href','javascript:;');
	
   
   
    
	
	
});





