/*	Revoltz JS, Slideshow
	This code is copyright (c) Ernesto Mendez 2010.
	http://der-design.com */

//slideshow_fade_time = 0.7;

//enable_slideshow_loop = false;

//slideshow_transition_delay = 7;

(function($) { // Encapsulate in Namespace

$(document).ready(function() {

	slideshow_conversions();

	add_action('slideshow_before_preload', slideshow_loading );
	
	add_action('slideshow_after_preload', slideshow_ready );

	add_action('slideshow_after_preload', slideshow_loop );

	var counter = 0;

	var total_images = slideshow_images.length;

	do_action('slideshow_before_preload');

	$.cacheImage(slideshow_images, {
		load	 : function(e) {counter += 1;},
		error	 : function(e) {total_images -= 1;},
		complete : function(e) {
			if ( counter == total_images ) {

				//setTimeout(function() {
					do_action('slideshow_after_preload');
				//},2000);
				
			}
		}
		
	});

});


function slideshow_conversions() {

	try {

		slideshow_fade_time *= 1000;

		slideshow_transition_delay *= 1000;

	} catch(e) { /* pass */ }

}

function slideshow_loading() {

	if ( slideshow_images.length == 0 ) { $('#slider .active').remove(); return false; }

	var control = $('#slider ul');

	control.find('li').remove();

	slideshow_loading = false;

	slideshow_tid = setTimeout(function() {

		slideshow_loading = true;

		control.append('<li class=loading>' + slideshow_loading_text + '</li>');

	},500);

}

function slideshow_ready() {

	clearTimeout(slideshow_tid);

	var control = $('#slider ul');

	if ( slideshow_loading == true ) {

		control.find('li.loading').fadeOut(800);

		setTimeout(function() {

			control.find('li.loading').remove();

			control.hide();

			for ( var i=0; i < slideshow_images.length; i++ ) {

				if ( i == 0 ) {

					control.append('<li><a class="active" href="#" rel="0"></a></li>');

				} else {

					control.append('<li><a href="#" rel="' + i + '"></a></li>');

				}

			}

			slideshow_click();

			control.fadeIn(600);

		},500);

	} else {

		control.hide();

		for ( var i=0; i < slideshow_images.length; i++ ) {

			if ( i == 0 ) {

				control.append('<li><a class="active" href="#" rel="0"></a></li>');

			} else {

				control.append('<li><a href="#" rel="' + i + '"></a></li>');

			}

		}

		slideshow_click();

		control.show();

	}

}

function slideshow_click() {

	slideshow_busy = false;

	$('#slider ul li a').click(function() {

		if ( $(this).hasClass('active') || slideshow_busy ) {return false;}

		try {

			clearInterval(slideshow_loop_intid);

		} catch(e) { /* pass */ }

		$('#slider ul li .active').removeClass('active');

		$(this).addClass('active');

		slideshow_busy = true;

		slideshow_transition( $(this).attr('rel') );

		return false;

	});

}

function slideshow_transition(rel, auto) {

	if ( auto ) {

		slideshow_busy = true;

		$('#slider ul li a.active').removeClass('active');

		$('#slider ul li a[rel=' + rel + ']').addClass('active');

	}

	var above = $('#slider .slide .above');

	var below = $('#slider .slide .below');

	var title = $('#slider .slide-meta h3');

	var category = $('#slider .slide-meta small');

	var slideshow_permalink = $('#slideshow-permalink');

	var new_href = $(slideshow_meta[rel].title).attr('href');


	slideshow_permalink.attr('href', new_href);

	below.attr('src', slideshow_images[rel]);

	above.stop().animate({opacity: 0}, slideshow_fade_time);


	if ( ! $.browser.msie ) {

		title.stop().animate({opacity: 0}, slideshow_fade_time/2.0);
		category.stop().animate({opacity: 0}, slideshow_fade_time/2.0);

		setTimeout(function() {

			title.html(slideshow_meta[rel].title);
			category.html(slideshow_meta[rel].category);

			title.stop().animate({opacity: 1}, slideshow_fade_time/2.0);
			category.stop().animate({opacity: 1}, slideshow_fade_time/2.0);

		}, slideshow_fade_time/2.0);

	} else {

		title.hide();
		category.hide();

		title.html(slideshow_meta[rel].title);
		category.html(slideshow_meta[rel].category);

		title.show();
		category.show();

	}

	setTimeout(function() {

		above.attr('src', slideshow_images[rel]);

		above.css('opacity', 1);

		if ( ! auto ) { slideshow_loop(); }

		slideshow_busy = false;

	},slideshow_fade_time + 50);

}

function slideshow_loop() {

	try {

		if ( enable_slideshow_loop == true ) {

			slideshow_loop_intid = setInterval(function() {

				var current = parseInt($('#slider ul li a.active').attr('rel'));

				current += 1; // Indexes start from zero

				var next_rel = cycle(1, current, slideshow_images.length );

				next_rel -= 1; // Make index start from zero

				slideshow_transition(next_rel, true);

			}, slideshow_transition_delay + slideshow_fade_time);

		}

	} catch(e) { /* pass */ }

}

})(jQuery);
