/* =========================================================

// This was an jquery.innerfade.js file that has been modified to add additional thumnbail control,
// slideshow stop, slideshow resume, and next and previous.

// innerfade_gallery.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_gallery({ 
 *	  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_gallery'),
 *	  children: optional children selector (Default: null)
 *  }); 
 *
 * Slideshow controls look like this:
 * <a href="javascript: gallery_prev();" >&laquo; previous</a> 
 * <a href="javascript: gallery_next();">next &raquo;</a> 
 * <a href="javascript: gallery_stop();">stop</a>
 * <a href="javascript: gallery_start();">resume</a>
 *
 * Slideshow thumbnails look like this:
 * <div class="gallery_thumbs">
 *  <img src="http://www.solarenergy.org/files/imagecache/gallery_thumb/files/BGET_HybridSystem_0.jpg" alt="" title="" />
 *  <img src="http://www.solarenergy.org/files/imagecache/gallery_thumb/files/PG-Arie_Angie2.jpg" alt="" title=""  />
 * </div>
 
// ========================================================= */


var fade_timeout;
//var fade_current = 0;
//var fade_elements;
var fade_settings;
var slideshow_on = true;
var fade_current = [];
var fade_elements = [];
    

(function($) {


    $.fn.innerfade_gallery = function(options) {
        return this.each(function() {   
            $.innerfade_gallery(this, options);
            gallery_stop();
        });
    };

    $.innerfade_gallery = function(container, options) {
        var settings = {
        		'animationtype':    'fade',
            'type':             'sequence',
            'speed':            'normal',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade_gallery',
            'children':         null
        };
        fade_settings = settings;
        
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
        
            fade_elements[fade_elements.length] = elements;
            fade_current[fade_current.length] = 0;

            $(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();
            };
            if (settings.type == "sequence") {
                fade_timeout = setTimeout(function() {
                    $.innerfade_gallery.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                fade_timeout = setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$.innerfade_gallery.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								fade_timeout = setTimeout(function(){
									$.innerfade_gallery.next(elements, settings, (current + 1) %  elements.length, current);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade_gallery.next = function(elements, settings, current, last, gallery, thumb) {       
        // If we've passed the last thumb or gone before the first, cycle to other end of thumbs
        if (current >= elements.length) {
         current = 0;
        }
        
        if (current < 0) { 
         current = elements.length - 1;
        }
        
        fade_current[gallery] = current;
        
        // Reset thumb selected classes
        $(thumb_selector).removeClass("selected");        
        
        // If this is the Nth slide, give the Nth thumbnail a selected class
        //$($(thumb_selector).get(current)).addClass("selected");
        $(thumb).addClass("selected");
                       
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.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 = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        fade_timeout = setTimeout((function() {
            $.innerfade_gallery.next(elements, settings, current, last);
        }), settings.timeout);
        
    };

})(jQuery);

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

var thumb_selector = ".gallery_thumbs img, img.gallery_thumb";

// Initialize thumbnails
$(document).ready(function() {
 // Set thumbs to override slideshow 
 $('.slideshow_gallery').each(function(g_index) {
  
  $(this).find(thumb_selector).each (function(index) {
   if (index == 0) {    
    $(this).addClass("selected"); 
   }
   
   $(this).mouseover(function() {
    if (index != fade_current[g_index]) {
     gallery_stop();    
     $.innerfade_gallery.next(fade_elements[g_index], fade_settings, index, fade_current[g_index], g_index, this);
     gallery_stop(); 
    }
    $(this).addClass("hover");
   }).mouseout(function() {
    $(this).removeClass("hover");
   });
  });   
 }); 
}); 

// Add supplemental functionality for next, prev, start, stop, and thumbnail selector
function gallery_next() {
 clearTimeout(fade_timeout);
 $.innerfade_gallery.next(fade_elements, fade_settings, fade_current + 1, fade_current);
 if (!slideshow_on) {
  clearTimeout(fade_timeout);
 } 
}

function gallery_prev() {
 clearTimeout(fade_timeout);
 $.innerfade_gallery.next(fade_elements, fade_settings, fade_current + -1, fade_current);
 if (!slideshow_on) {
  clearTimeout(fade_timeout);
 }
}

function gallery_start() {
 clearTimeout(fade_timeout); 
 $.innerfade_gallery.next(fade_elements, fade_settings, fade_current + 1, fade_current);
 slideshow_on = true;
}

function gallery_stop() {
 clearTimeout(fade_timeout);
 slideshow_on = false;
}

$(document).ready(function() {
 // Pass in settings and start slideshow
 $(".pdetail .gallery_slides").innerfade_gallery({
  animationtype: 'fade',
  speed: 1000,                
  timeout: 4000, 
  containerheight: 307
 });
});
