/* =========================================================

// jquery.fader.js

// Datum: 2009-03-10
// Author: Philip Floetotto
// Version: 1.0
// Mail: mmeier23@gmail.com
// Web: http://philipf.alphacomplex.org/showroom/

// based on the innerfade plugin by medienfreund.de
// Web: http://medienfreunde.com/lab/innerfade/

 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $j('#news').fader({ 
 *	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
 *	  speed: Fading-/Sliding-Speed in milliseconds (Default: '600'), 
 *	  timeout: Time between the fades in milliseconds (Default: '2000'), 
 * 	  containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
 *	  runningclass: CSS-Class which the container get’s applied (Default: 'fader'),
 *	  previousButtonClass: CSS-Class of the previous Button. (Default: 'previousButton') Can be used to set other elements to control the fader,
 *	  nextButtonClass: CSS-Class of the next Button. (Default: 'nextButton'),
 *	  previousButtonText: text inside the previousButton (Default: '&nbsp;') Can be an image or HTML,
 *	  nextButtonText: text inside the nextButton (Default: '&nbsp;') Can be an image or HTML,
 *	  createButtons: true for false if you want the plugin to create navigation buttons (prev and next) automatically (Default: 'true'),
 *	  play: set to start or stop animation (Default: 'true'),
 *  }); 
 *

// ========================================================= */

var $j = jQuery.noConflict();
(function($j) {

    $j.fn.fader = function(options) {
        return this.each(function() {   
            $j.fader(this, options);
        });
    };

    $j.fader = function(container, options) {
        var o = {
        	'animationType':    'fade',
            'speed':            600, // please use only numbers because of the jumpNav fade!! 600 is normal, 1200 is slow
            'type':             'sequence',
            'timeout':          2000,
            'containerHeight':  'auto',
            'runningclass':     'fader',
            'previousButtonClass':     'previousButton',
            'nextButtonClass':     'nextButton',
            'previousButtonText':     '<img src="/lg_images/left_arrow.png" />',
            'nextButtonText':     '<img src="/lg_images/right_arrow.png" />',
            'createButtons':     true,
            'play':     true
        };
        if (options)
            $j.extend(o, options);
        
        // checks
        if(isNaN(o.speed)){
        	alert('The speed settings needs to be in seconds, not a string');
        }
        
        // wrap nav and container
        var $jcontainer = $j(container);
        if (!$jcontainer.parent().hasClass('fader-container')){
        	
        	var $jfaderNav = $j("<div>")
    			.addClass('fader-nav')
    			.prepend($j("<span>").addClass('fader-jumpNav'))
            	.prepend($j("<span>").addClass('fader-navButtons'));
            	
        	$jcontainer
				.wrap('<div></div>')
				.before($jfaderNav)
        		.parent()
        			.addClass('fader-container');
            	
        }
        
        $jcontainer.parent = $jcontainer.parent('.fader-container');
        $jjumpNav = $j('.fader-jumpNav',$jcontainer.parent);
        
        
        // create Buttons
        if(o.createButtons){
        	$jnavButtons = $j('.fader-navButtons',$jcontainer.parent);
        	var $jpreviousButton = $j('<a>')
	    		.addClass(o.previousButtonClass)
	    		.html(o.previousButtonText)
	    		.attr('title','Previous')
	    		.appendTo($jnavButtons);
        	var $jnextButton = $j('<a>')
        		.addClass(o.nextButtonClass)
        		.html(o.nextButtonText)
        		.attr('title','Next')
        		.appendTo($jnavButtons);
        }else{
        	var $jnextButton = $j('.'+o.nextButtonClass);
        	var $jpreviousButton = $j('.'+o.previousButtonClass);
        }
         
        
        // hiding the elements and attaching the innerFade
        $jcontainer.o = o;
        $jcontainer.slides = [];
        $jcontainer.jumpNavAnchors = [];
        
        
        // set css properties for the elements
        var elements = $jcontainer.children();
        if (elements.length > 1) {
        	
        	$jcontainer
        		.css('position', 'relative')
        		.addClass($jcontainer.o.runningclass);
        	
            for (var i = 0; i < elements.length; i++) {
                
            	var anchor = $j('<a>')
				            	.attr('id',"fader-jump-"+i.toString())
				            	.attr('style','FILTER: alpha(opacity=100); ZOOM: 1')
				            	.html(i+1)
				            	.appendTo($jjumpNav);
            	
            	
            	var element = $j(elements[i])
			                	.css('z-index', String(elements.length-i))
			                	.css('position', 'absolute')
			                	.hide();
                
                // assigning slide to jumpnav and vice versa
            	element.slideNumber = i;
            	anchor.data('slide',element);
            	element.data('anchor',anchor);
                $jcontainer.slides.push(element);
                $jcontainer.jumpNavAnchors.push(anchor);
            };
            
           
            
            $jcontainer.currentSlide = $jcontainer.slides[0];
            $jcontainer.nextSlide = $jcontainer.slides[1];
            
            // initialise the sequence
            $jcontainer.currentSlide.data('anchor').addClass('active');
            $jcontainer.currentSlide.show();
            $j.fader.timedChange($jcontainer);
            
            // set the fader container height
            if($jcontainer.o.containerHeight=='auto')
            	$jcontainer.o.containerHeight = $jcontainer.slides[0].eq(0).get(0).clientHeight + $jfaderNav.eq(0).get(0).clientHeight;
           // $jcontainer.parent.css({'height':$jcontainer.o.containerHeight});
            
                
		}
        
        // attach events to jumpNav
        $j.each($jcontainer.jumpNavAnchors,function(){
        	this.click(function(){
            	$jcontainer.nextSlide = $j(this).data('slide');
            	$j.fader.forceChange($jcontainer);
            });
        });
        
        
        
        // attach events to prev and next
        $jnextButton.click(function(){
        	if($jcontainer.currentSlide.slideNumber + 1 > ($jcontainer.slides.length-1)){
	        	$jcontainer.nextSlide =	$jcontainer.slides[0];
	        }else{
	        	$jcontainer.nextSlide = $jcontainer.slides[$jcontainer.currentSlide.slideNumber + 1];
	        }
        	$j.fader.forceChange($jcontainer);
        });
        
        $jpreviousButton.click(function(){
        	if($jcontainer.currentSlide.slideNumber - 1 < 0){
	        	$jcontainer.nextSlide =	$jcontainer.slides[$jcontainer.slides.length -1];
	        }else{
	        	$jcontainer.nextSlide = $jcontainer.slides[$jcontainer.currentSlide.slideNumber - 1];
	        }
        	$j.fader.forceChange($jcontainer);
        });
        
    };
        
    $j.fader.forceChange = function($jcontainer){
        clearTimeout($jcontainer.innerFadeTimer);
        $jcontainer.o.play =  false;
        $jcontainer.innerFadeTimer = $j.fader.next($jcontainer);
    };
    
    $j.fader.timedChange = function($jcontainer){
    	$jcontainer.innerFadeTimer = setTimeout(function() {
            $j.fader.next($jcontainer);
        }, $jcontainer.o.timeout);
    };
    
    $j.fader.next = function($jcontainer) {
    	
    	if($jcontainer.currentSlide.slideNumber != $jcontainer.nextSlide.slideNumber){
	        if ($jcontainer.o.animationType == 'slide') {
	        	$jcontainer.currentSlide.slideUp($jcontainer.o.speed);
	        	$jcontainer.nextSlide.slideDown($jcontainer.o.speed);
	        } else if ($jcontainer.o.animationType == 'fade') {
	        	$jcontainer.currentSlide.fadeOut($jcontainer.o.speed);
	        	$jcontainer.nextSlide.fadeIn($jcontainer.o.speed, function() {
								removeFilter($j(this)[0]);
							});
	        } else
	            alert('Innerfade-animationType must either be \'slide\' or \'fade\'');
	        
	        
	        
	        // sequence
	        $jcontainer.currentSlide = $jcontainer.nextSlide;
	        if($jcontainer.currentSlide.slideNumber + 1 > ($jcontainer.slides.length-1)){
	        	$jcontainer.nextSlide =	$jcontainer.slides[0];
	        }else{
	        	$jcontainer.nextSlide = $jcontainer.slides[$jcontainer.currentSlide.slideNumber + 1];
	        }
	        
	        // fade the jumpNav
	        $j('.fader-jumpNav .active',$jcontainer.parent)
	    		.animate({opacity:0.3},$jcontainer.o.speed/2,false,function(){$j(this).removeClass('active');})
				.animate({opacity:1},$jcontainer.o.speed/2);
	        
	        $jcontainer.currentSlide.data('anchor')
	        		.animate({opacity:0.3},$jcontainer.o.speed/2,false,function(){$j(this).addClass('active');})
    				.animate({opacity:1},$jcontainer.o.speed/2);
    	}
    	
        // check if play or not
        if($jcontainer.o.play){
        	$j.fader.timedChange($jcontainer);
        }
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

