/*
	Background Stretcher 2 jQuery Plugin
	© 2010 cyberward.net
	This is a heavily modified version of the 1.2v Background Stretcher
	jQuery Plugin found at ajaxBlender.
	The stop/start funcions and all support for albums was added. 

	Version: 2.0
	
	Background Stretcher jQuery Plugin
	© 2009 ajaxBlender.com
	For any questions please visit www.ajaxblender.com 
	or email us at support@ajaxblender.com
	
	Version: 1.2
*/
;(function($){
	var container = null;
	var allImgs = '', allLIs = '', containerStr = '';
	var _bgStretcherPause = false;
	var _bgStretcherTm = null;

	$.fn.bgStretcher = function(settings){
		settings = $.extend({}, $.fn.bgStretcher.defaults, settings);
		$.fn.bgStretcher.settings = settings;
		
		function _build(){
			_genHtml();
			
			containerStr = '#' + settings.imageContainer;
			container = $(containerStr);
			allImgs = '#' + settings.imageContainer + ' .resizeable IMG';
           // alert(allImgs);
			allLIs = '#' + settings.imageContainer + ' LI';
			
			$(allLIs).hide();
			$(allLIs + ':first').show().addClass('bgs-current');
			$(allLIs + ':first').children('.rahmen_inhalt').fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
            
			if(!container.length){ return; }
			$(window).resize(_resize);
			
			if(settings.slideShow && $(allImgs).length > 1){
				_bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', settings.nextSlideDelay);
			}
			_resize();
		};
		
		function _resize(){
			var winW = $(window).width()-240;
			var winH = $(window).height();
			var imgW = 0, imgH = 0;

			container.width(winW);
			container.height(winH);
			
			if(!settings.resizeProportionally){
				imgW = winW;
				imgH = winH;
			} else {
				var initW = settings.imageWidth, initH = settings.imageHeight;
				var ratio = initH / initW;
				
				imgW = winW;
				imgH = winW * ratio;
				
				if(imgH < winH){
					imgH = winH;
					imgW = imgH / ratio;
				}
                if(imgW < winW){
					imgW = winW;
					imgH = imgW / ratio;
				}
			}
            
			if(!settings.resizeAnimate){
				$(allImgs).width(imgW).height(imgH);
			} else {
				$(allImgs).animate({width: imgW, height: imgH}, 'normal');
			}
		};
		
		function _genHtml(){
			var code = '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul>';
			jQuery.each(settings.albums, function(album, imageArray) {
				for(i = 0; i < imageArray.length; i++){
					code += '<li class="' + album + '">' + imageArray[i] + '</li>';
				}
			});
			code += '</ul></div>';
			$(code).prependTo('#rahmen_links');
		};
		_build();
	};
	
	$.fn.bgStretcher.play = function(){
	   if (_bgStretcherPause == true) { 
        _bgStretcherPause = false;
        $.fn.bgStretcher._clearTimeout();
        $.fn.bgStretcher.slideShow();
       }
	};
	
	$.fn.bgStretcher._clearTimeout = function(){
       if(_bgStretcherTm != null){
           clearTimeout(_bgStretcherTm);
           _bgStretcherTm = null;
       }
	}
	
	$.fn.bgStretcher.pause = function(){
	   _bgStretcherPause = true;
	   $.fn.bgStretcher._clearTimeout();
     //  _bgStretcherTm = setTimeout('$.fn.bgStretcher.play()', $.fn.bgStretcher.settings.nextSlideDelay);
       _bgStretcherTm = setTimeout('$.fn.bgStretcher.play()', 4000);
	};
	
    $.fn.bgStretcher.stop = function(){
	   _bgStretcherPause = true;
	   $.fn.bgStretcher._clearTimeout();
	};
    
	$.fn.bgStretcher.slideShow = function(){
	    $(containerStr + ' LI').not('.bgs-current').hide(0);
		current = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album).filter('.bgs-current');
		next = current.next('.'+$.fn.bgStretcher.settings.album);
       
       var ct = $('#bgstretcher ul').children().size();

        //alert(ct);
		
		if(!next.length){
			next = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album+':first');
		}
		
        current.children('.rahmen_inhalt').fadeOut( $.fn.bgStretcher.settings.slideShowSpeed, function () {
            current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );    
            next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed, function () {
    		  next.children('.rahmen_inhalt').fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
    		} );
            
           pageClassArr = next.children('.rahmen_inhalt').attr('class').split(' ');
           pageClass = pageClassArr[pageClassArr.length-1];
            
            $("#rahmen_presenter div img").attr('src', 'fileadmin/system/image/presenter_link.gif');
            $("#rahmen_presenter div img").attr('alt', '');
            $("#rahmen_presenter div." + pageClass + " img").attr('src', 'fileadmin/system/image/presenter_link_aktiv.gif');
   	    var temp1 = $('.bgs-current .rahmen_inhalt').attr('id');
   	    var Teil = temp1.substr(5, 1);
		if (Teil == 4 || Teil == 5 || Teil == 6 || Teil == 7 || Teil == 8){
		       //  alert(Teil);
		$('.normal_animation #abbruchbutton').addClass('uid131')
		         $('.normal_animation #abbruchbutton').show(0);
               
		}else{
			 $('.normal_animation #abbruchbutton').hide(0);
		} 
        } );
		
        current.removeClass('bgs-current');
        next.addClass('bgs-current');
        
		if(!_bgStretcherPause){
		  _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
		}
	};
	
	$.fn.bgStretcher.next = function(){
	    $(containerStr + ' LI').not('.bgs-current').hide(0);
		var current = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album).filter('.bgs-current');
		var next = current.next('.'+$.fn.bgStretcher.settings.album);
		if(!next.length){
			next = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album+':first');
		}
        current.children('.rahmen_inhalt').fadeOut( $.fn.bgStretcher.settings.slideShowSpeed, function () {
            current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
            next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed, function () {
    		  $('#rahmen_links').bgStretcher.pause();
    		  next.children('.rahmen_inhalt').fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
    		  
    		} );

            var pageClassArr = next.children('.rahmen_inhalt').attr('class').split(' ');
            var pageClass = pageClassArr[pageClassArr.length-1];
            
            //alert("#rahmen_presenter div." + pageClass + " img");
            $("#rahmen_presenter div img").attr('src', 'fileadmin/system/image/presenter_link.gif');
            $("#rahmen_presenter div img").attr('alt', '');
            $("#rahmen_presenter div." + pageClass + " img").attr('src', 'fileadmin/system/image/presenter_link_aktiv.gif');
        });	
        current.removeClass('bgs-current');
        next.addClass('bgs-current');	
        window.setTimeout(function(){$('.p_weiter_bild').css('display','block')}, 4000);
        var temp1 = $('.bgs-current .rahmen_inhalt').attr('id');
   	    var Teil = temp1.substr(5, 1);
		if (Teil == 4 || Teil == 5 || Teil == 6 || Teil == 7 || Teil == 8){
		       //  alert(Teil);
		$('.normal_animation #abbruchbutton').addClass('uid131')
		         $('.normal_animation #abbruchbutton').show(0);
               
		}else{
			 $('.normal_animation #abbruchbutton').hide(0);
		}
 };
	
	$.fn.bgStretcher.previous = function(){
	    $(containerStr + ' LI').not('.bgs-current').hide(0);
		var current = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album).filter('.bgs-current');
		var previous = current.prev('.'+$.fn.bgStretcher.settings.album);
		if(!previous.length){
			previous = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album+':last');
		}	
        
        current.children('.rahmen_inhalt').fadeOut( $.fn.bgStretcher.settings.slideShowSpeed, function () {
            current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
            previous.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed, function () {
    		  $('#rahmen_links').bgStretcher.pause();
    		  previous.children('.rahmen_inhalt').fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
    		  
    		} );
            
            var pageClassArr = previous.children('.rahmen_inhalt').attr('class').split(' ');
            var pageClass = pageClassArr[pageClassArr.length-1];
            
            //alert("#rahmen_presenter div." + pageClass + " img");
            $("#rahmen_presenter div img").attr('src', 'fileadmin/system/image/presenter_link.gif');
            $("#rahmen_presenter div img").attr('alt', '');
            $("#rahmen_presenter div." + pageClass + " img").attr('src', 'fileadmin/system/image/presenter_link_aktiv.gif');    
        } );
        current.removeClass('bgs-current');
        previous.addClass('bgs-current');
        window.setTimeout(function(){$('.p_zurueck_bild').css('display','block')}, 4000);
    	    var temp1 = $('.bgs-current .rahmen_inhalt').attr('id');
   	    var Teil = temp1.substr(5, 1);
		if (Teil == 4 || Teil == 5 || Teil == 6 || Teil == 7 || Teil == 8){
		       //  alert(Teil);
		$('.normal_animation #abbruchbutton').addClass('uid131')
		         $('.normal_animation #abbruchbutton').show(0);
               
		}else{
			 $('.normal_animation #abbruchbutton').hide(0);
		}
	};
	
	$.fn.bgStretcher.changeAlbum = function(album){
		$.fn.bgStretcher.settings.album = album;
		var current = $(containerStr + ' LI.bgs-current');
		var next = $(containerStr + ' LI.'+$.fn.bgStretcher.settings.album+":first");
		next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
		current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
		if(!_bgStretcherPause){
			$.fn.bgStretcher.play();
		}
	};
	
    $.fn.bgStretcher.gotoPage = function(pageUid) {
        $(containerStr + ' LI').not('.bgs-current').hide(0);
	var current = $(containerStr + ' LI.bgs-current');
        var pageClassArr = pageUid.split(' ');
        var pageClass = pageClassArr[pageClassArr.length-1];
         
		var next = $('.'+pageClass+":first").parent();
        
        if (!current.children('.rahmen_inhalt').hasClass(pageClass)) {
                        
            current.children('.rahmen_inhalt').fadeOut( $.fn.bgStretcher.settings.slideShowSpeed, function () {
                current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
                next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed, function () {
        		  next.children('.rahmen_inhalt').fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
        		} );
                
                var pageClassArr = next.children('.rahmen_inhalt').attr('class').split(' ');
                var pageClass = pageClassArr[pageClassArr.length-1];
                
                $("#rahmen_presenter div img").attr('src', 'fileadmin/system/image/presenter_link.gif');
                $("#rahmen_presenter div img").attr('alt', '');
                $("#rahmen_presenter div." + pageClass + " img").attr('src', 'fileadmin/system/image/presenter_link_aktiv.gif');
            });	
            current.removeClass('bgs-current');
  		    next.addClass('bgs-current');	
        }
        var temp1 = $('.bgs-current .rahmen_inhalt').attr('id');
   	    var Teil = temp1.substr(5, 1);
		if (Teil == 4 || Teil == 5 || Teil == 6 || Teil == 7 || Teil == 8){
		       //  alert(Teil);
		$('.normal_animation #abbruchbutton').addClass('uid131')
		         $('.normal_animation #abbruchbutton').show(0);
               
		}else{
			 $('.normal_animation #abbruchbutton').hide(0);
		}
	};
     $.fn.bgStretcher.gotolastPage = function(pageUid) {
        $(containerStr + ' LI').not('.bgs-current').hide(0);
    	var current = $(containerStr + ' LI.bgs-current');
        var pageClassArr = pageUid.split(' ');
        var pageClass = pageClassArr[pageClassArr.length-1];
         
	var next = $('#text_9').parent();
              
                current.children('.rahmen_inhalt').fadeOut( $.fn.bgStretcher.settings.slideShowSpeed, function () {
                current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
                next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed, function () {
        		  next.children('.rahmen_inhalt').fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
        		} );
                $('#rahmen_links').bgStretcher.pause();
             //   var pageClassArr = next.children('.rahmen_inhalt').attr('class').split(' ');
             //   var pageClass = pageClassArr[pageClassArr.length-1];
             });	
            current.removeClass('bgs-current');
  		    next.addClass('bgs-current');	
      
	};    
 
    
	/*  Default Settings  */
	$.fn.bgStretcher.defaults = {
		imageContainer:             'bgstretcher',
		resizeProportionally:       true,
		resizeAnimate:              false,
		albums: 					[],
		album: 						'', 
		imageWidth:                 1024,
		imageHeight:                768,
		nextSlideDelay:             10000,
		slideShowSpeed:             1500,
		slideShow:                  true
	};
	$.fn.bgStretcher.settings = {};
})(jQuery);

/*
function doPlay() {
    $('#rahmen_links').bgStretcher.play();
}
function doStop() {
    $('#rahmen_links').bgStretcher.stop();
}

var config = {    
     over: doStop, // function = onMouseOver callback (REQUIRED)    
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out: doPlay // function = onMouseOut callback (REQUIRED)    
};


$('#rahmen_rechts').hoverIntent(config);
*/