/* =========================================================
 
 // jquery.innerfade.js
 
 // Datum: 2008-02-14
 // Firma: Medienfreunde Hofmann & Baldes GbR
 // Author: Torsten Baldes
 // Mail: t.baldes@medienfreunde.com
 // Web: http://medienfreunde.com
 
 // based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
 // and Ralf S. Engelschall http://trainofthoughts.org/
 
 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $('#news').innerfade({ 
 *	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
 *	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
 *	  timeout: Time between the fades in milliseconds (Default: '2000'), 
 *	  type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), 
 * 		containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
 *	  runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
 *	  children: optional children selector (Default: null)
 *  }); 
 *
 
 // ========================================================= */


/*
 Additions to manual advance and click on bullet to jump to an image,
 and to allow href link for each image
 by Steven Chaitoff, 20 Oct 2011
 */


(function($) {
	
	var innerfade = null;
	
    $.fn.innerfade = function(options) {
		if (typeof options === 'object' || !options) {
			innerfade = $.innerfade(this, options);
		}
		else {
			var index = innerfade.current;
			if (options == 'next') index++;
			else if (options == 'previous') index--;
			else if (typeof options === 'number') index = options;
			
			if (index < 0) index = innerfade.elements.length - 1;
			else if (index >= innerfade.elements.length) index = 0;
			
			clearTimeout(innerfade.animation);
			$.innerfade.next(index, 1);
		}
		
        return this;
    };
	
    $.innerfade = function(container, options) {
        this.settings = {
        	'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         'img',
            'allowmanual':		true,
            'manualcontrolbottom' : 	0,
            'manualcontrolleft' : 	0,
            'manualspeed': 0,
            'jump_button_spacing' : 4,
        };
        var animation = null;
        var settings = this.settings;
        if (options)
		$.extend(settings, options);
//        if (settings.children === null)
		elements = $(container).find('img');  // SC: use find('img'), not children(), so we can wrap img tags in <a> and get clickable images
//        else
//		elements = $(container).find(settings.children);
        this.elements = elements;
        if (elements.length > 1) {
            $(container).css('position', 'relative')
            .css('height', settings.containerheight)
            .addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i))
                .css('position', 'absolute')
                .hide();
            };
            
			this.manual_timeout = settings.manualtimeout;
			if (settings.allowmanual) {
				var manual_control = $("<div id=\"manual_control\"></div>")
				.css({
					'z-index' : '99999',
					'position' : 'absolute',
					'bottom' : settings.manualcontrolbottom + 'px',
					'left' : settings.manualcontrolleft + 'px'
				});
				for (var i = 0; i < elements.length; i++) {
					var control_button = $("<img src=\"innerfade/innerfade_bullet_off.png\" />")
					.css({
						'cursor' : 'pointer',
						'padding' : '0px ' + settings.jump_button_spacing + 'px'
					})
					.mouseover(function() {
						$(this).attr("src", "innerfade/innerfade_bullet_on.png");
					})
					.mouseout(function() {
						var index = $(this).parent().children().index($(this));
						if (index != innerfade.current) {
							$(this).attr("src", "innerfade/innerfade_bullet_off.png");
						}
					})
					.click(function() {
						clearTimeout(innerfade.animation);
						var index = $(this).parent().children().index($(this));
						$.innerfade.next(index, 1);
					});
					manual_control.append(control_button);
				}
				manual_control.appendTo($(container));
			}
            
			var last = 0;
	        var current = 1;
            if (settings.type == "sequence") {
               	last = 0;
               	current = 1;
            }
            else if (settings.type == "random") {
				last = Math.floor ( Math.random () * ( elements.length ) );
				do {
					current = Math.floor ( Math.random ( ) * ( elements.length ) );
				} while (last == current);
			}
			else if ( settings.type == 'random_start' ) {
				settings.type = 'sequence';
				last = Math.floor ( Math.random () * ( elements.length ) );
				current = (last + 1) %  elements.length;
			}
			else {
				alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
			}

			this.current = last;
			$("#manual_control").children().eq(last).mouseover();
			$(elements[last]).show();
			this.animation = setTimeout(function() {
				$.innerfade.next(current, 0);
			}, settings.timeout);
		}
		return this;
    };

	
    $.innerfade.next = function(current, ismanual) {
		var last = innerfade.current;
		innerfade.current = current;
    	var elements = innerfade.elements;
    	var settings = innerfade.settings;
    	var speed = ismanual ? settings.manualspeed : settings.speed;

		if (settings.allowmanual) {
			var current_control = $("#manual_control").children().eq(current);
			current_control.mouseover();
			current_control.siblings().mouseout();
		}
		    	
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(speed);
            $(elements[current]).slideDown(speed);
        }
        else if (settings.animationtype == 'fade') {	
            $(elements[last]).fadeOut(speed);
            $(elements[current]).fadeIn(speed, function() {
				removeFilter($(this)[0]);
			});
        }
        else alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
		
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current++;
            }
            else {
                current = 0;
            }
        }
        else if (settings.type == "random") {
			do {
				current = Math.floor (Math.random() * (elements.length));
			} while (last == current);
        }
        else alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
		
        innerfade.animation = setTimeout((function() {
            $.innerfade.next(current, 0);
        }), settings.timeout);        
    };
	
})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

