/* =========================================================
// jquery.transclass.js 1.1
// Ofir Klinger <klinger.ofir@gmail.com> 2010
// https://launchpad.net/~klinger-ofir
// Code clean-up and a fix for IE
// Based on fufade version 1.0
// ========================================================= */
// jquery.fufade.js 1.0
// Chris McKee <pcdevils@gmail.com> 2008-9
// chrismckee.co.uk // http://bit.ly/chrisisagit
// re-coded & optimized for size + functionality
// Based on innerfade - see git for details
// ========================================================= */
/*
 * Default Settings
 * | Name            |  Default  | Type                 |
 * | type            | sequence  | sequence or random   |
 * | timeout         | 2 seconds | Microseconds (2000=2)|
 * | containerheight | auto      | Pixels               |
 * | runningclass    | transClass| class name           |
 * | children        | definer   | null                 |
 * | ienofade		 | true		 | boolean				|
 * |____________________________________________________|
 *
 * IE Fix
 * | When displayed on IE and ienofade set to true,	|
 * | no fade will be used and instead a simple			|
 * | transition will take place.						|
 * |____________________________________________________|
 *
 * Usage Example
 * | $(function(){
 * | 	$('ul.frontpage-screenshots').transClass({
 * | 		speed: 2000,
 * | 		timeout: 4000,
 * | 		containerheight: '380px'
 * | 	});
 * | });
 * |____________________________________________________|
 */

(function ($) {
	var settings, elements, i, current, last, prev, next, transTimer;
    
	//Main
	$.fn.transClass = function (options) {
		return this.each(function () {
			$.transClass(this, options);
		});
	};
    
	$.transClass = function (container, options) {
		settings = {
			'speed':			'normal',
			'type':				'sequence',
			'timeout':			2000,
			'containerheight':	'auto',
			'runningclass':     'transClass',
			'children':			null,
			'ienofade':			true
		};

		if (options){ $.extend(settings, options); }

		elements = (settings.children === null) ? $(container).children() : elements = $(container).children(settings.children);
        
		if (elements.length > 1) {
			$(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            
			for (i = 0; i < elements.length; i++) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
			}
            
			$(".nextbtn").bind('click',function (){
				transTimer = setTimeout(function () {
					$.transClass.next(elements, settings, 1, 0, transTimer);
				}, 0);
			});
			
			//wake up back button
			$(".backbtn").fadeTo("fast", 0.5);
            
			if (settings.type === "sequence") {
				setTimeout(function () {
					$.transClass.next(elements, settings, 1, 0);
				}, settings.timeout);
				$(elements[0]).show();
			} else if (settings.type === "random") {
				last = Math.floor ( Math.random () * ( elements.length ) );
				setTimeout(function () {
					do {
						current = Math.floor ( Math.random ( ) * ( elements.length ) );
					} while (last === current );
					$.transClass.next(elements, settings, current, last);
				}, settings.timeout);
				$(elements[last]).show();
			} else if ( settings.type === 'random_start' ) {
				settings.type = 'sequence';
				current = Math.floor ( Math.random () * ( elements.length ) );
				setTimeout(function (){
					$.transClass.next(elements, settings, (current + 1) %  elements.length, current);
				}, settings.timeout);
				$(elements[current]).show();
			} else {
				try {
					console.log('transClass-type must either be "sequence" or "random"');
				} catch (e) { }
			}
		}
	};

	$.transClass.next = function (elements, settings, current, last, transTimer) {
		clearTimeout(transTimer);
      
		next = current === (elements.length - 1) ? 0 : current + 1;
		prev = current === 0 ? elements.length - 1 : prev = current - 1;
         
		for ( i = 0; i < elements.length; i++ ) {
			if ((i !== last) && (i !== current)) {
				$(elements[i]).css('z-index', '1');
				$(elements[i]).css('top', 0).css('left', 0);

				if ($.browser.msie && settings.ienofade) {
					$(elements[i]).css('display', 'none');
				} else {
					$(elements[i]).fadeOut(settings.speed);
				}
			}
		}
		$(elements[last]).css('z-index', '190');
		$(elements[current]).css('z-index', '195');

		//Button Binding
		//Next
		$(".nextbtn").unbind('click');
		$(".nextbtn").bind('click',function () {
			clearTimeout(transTimer);
			$.transClass.next(elements, settings, next, current, transTimer);
			return false;
		});

		//Back
		$(".backbtn").unbind('click');
		$(".backbtn").bind('click',function (){
			clearTimeout(transTimer);
			$.transClass.next(elements, settings, prev, current, transTimer);
			return false;
		});

		//Fade Animation
		if ($.browser.msie && settings.ienofade) {
			$(elements[last]).css('display', 'none');
			$(elements[current]).css('display', 'block');
		} else {
			$(elements[last]).fadeOut(settings.speed);
			$(elements[current]).fadeIn(settings.speed, function () {
				$.transClass.removeFilter($(this)[0]);
			});
		}
		if ($.browser.msie && settings.ienofade) {
			$(".backbtn").css('display', 'block');
		} else {
			$(".backbtn").fadeTo("fast", 1);
		}
        
		//Images in Sequence
		if (settings.type === "sequence") {
			if ((current + 1) < elements.length) {
				current = current + 1;
				last = current - 1;
			} else {
				current = 0;
				last = elements.length - 1;
			}
		}
		//endbutton binding
            
		transTimer = setTimeout(function (){
			$.transClass.next(elements, settings, next, prev, transTimer);
		}, settings.timeout);
	};

	// **** remove Opacity-Filter in ie ****
	$.transClass.removeFilter = function(element) {
		if(element.style.removeAttribute){
			element.style.removeAttribute('filter');
		}
	};
})(jQuery);
