function toggle_buttons(img1, img2) {
	var tmp_src = img1.attr('src');
	img1.attr('src', img2.attr('src'));
	img2.attr('src', tmp_src);
}

//timeouts per slide (in seconds) 
var timeouts = [10,21,22,16,16]; 
function calculateTimeout(currElement, nextElement, opts, isForward) { 
    var index = opts.currSlide; 
    return timeouts[index] * 1000; 
} 

$(document).ready(function() {
	$('#slideshow-left').fadeIn(1000);
	$('#slideshow').cycle({
		fx:      'fade',
		delay:   -1000,
		sync:    0,
		random:  1,
		timeoutFn: calculateTimeout,
		before:  function(curr, next, opts) {
			// Reset animated items in slide 1
			$('div.slide-1 #items li').each(function(){
				if($.browser.msie){
					$(this).stop(true, true).css({'opacity':'0','left':'20px','top':'-64px','line-height':'140px'}).show();
				}else{
					$(this).stop(true, true).css({'opacity':'0','left':'20px'}).show();
				}
			});
			$('div.slide-1 #items li span').each(function(){
				$(this).stop(true, true).css({'font-size':'140px'}).show();
			});
			
			// Reset animated items in slide 2
			$('div.slide-2 .slide-2-text').each(function(){
				$(this).stop(true, true).css({'opacity':'0'}).show();
			});
			$('div.slide-2 .red-text').each(function(){
				$(this).stop(true, true).css({'opacity':'0'}).show();
			});
			$('div.slide-2 #products-scroll div').each(function(){
				$(this).css('display','none');
			});
			$('div.slide-2 #products-scroll div:first').each(function(){
				$(this).css('display','block');
			});
			
			// Reset animated items in slide 4
			$('div.slide-4 #s4-text-1').stop(true, true).css({'opacity':'0'}).show();
			$('div.slide-4 #s4-items li').each(function(){
				$(this).stop(true, true).css({'opacity':'0','top':'190px','left':'70px'}).show();
			});
			
			// Reset animated items in slide 5
			$('div.slide-5 #s5-text-1').stop(true, true).css({'opacity':'0','line-height':'140px','font-size':'140px'}).show();
			$('div.slide-5 #s5-text-2').stop(true, true).css({'opacity':'0'}).show();
			$('div.slide-5 #s5-items li').each(function(){
				$(this).stop(true, true).css({'opacity':'0','left':'0'}).show();
			});
			
			// Reset animated items in slide 6
			$('div.slide-6 #s6-text-1').stop(true, true).css({'opacity':'0','line-height':'140px','font-size':'140px'}).show();
			$('div.slide-6 #s6-text-2').stop(true, true).css({'opacity':'0'}).show();
			$('div.slide-6 #s6-items li').each(function(){
				$(this).stop(true, true).css({'opacity':'0','left':'0'}).show();
			});
		},
		after:   function(curr, next, opts) {
			// Start animations in slide 1
			$('div.slide-1 #item-1 span').animate({fontSize:"12px"}, 700, 'swing', function() {});
			$('div.slide-1 #item-1').animate({opacity:'1',left:'-=40',top:'0',lineHeight:'20px'}, 1000, 'swing', function() {
				$(this).show().css({'left':'-20px'});
				$('div.slide-1 #item-2 span').delay(600).animate({fontSize:"12px"}, 700, 'swing', function() {});
				$('div.slide-1 #item-2').delay(600).animate({opacity:'1',left:'-=40',top:'0',lineHeight:'20px'}, 1000, 'swing', function() {
					$(this).show().css({'left':'-20px'});
					$('div.slide-1 #item-3 span').delay(600).animate({fontSize:"12px"}, 700, 'swing', function() {});
					$('div.slide-1 #item-3').delay(600).animate({opacity:'1',top:'0',left:'-=40',lineHeight:'20px'}, 1000, 'swing', function() {
						$(this).show().css({'left':'-20px'});
					});
				});
			});
			
			// Start animations in slide 2
			$('#products-scroll').cycle({
				fx:            'scrollUp',
				timeout:       2650,
				speed:         500,
				cleartype:     true,
				cleartypeNoBg: true,
				autostop:      6
			});
			
			$('div.slide-2 #s2-text-1').animate({opacity:'1'}, 1000, 'swing')
			.delay(2200)
			.animate({opacity:'0'}, 1000, 'swing', function() {
				$(this).hide();
				$('div.slide-2 #s2-text-2').animate({opacity:'1'}, 1000, 'swing', function() {
					$(this).show();
					$('div.slide-2 #s2-text-3').animate({opacity:'1'}, 1000, 'swing')
					.delay(2000)
					.animate({opacity:'0'}, 1000, 'swing', function() {
						$(this).hide();
						$('div.slide-2 #s2-text-4').animate({opacity:'1'}, 1000, 'swing')
						.delay(2000)
						.animate({opacity:'0'}, 1000, 'swing', function() {
							$(this).hide();
							$('div.slide-2 #s2-text-5').animate({opacity:'1'}, 1000, 'swing');
						});
					});
				});
			});
			
			// Start animations in slide 4
			$('div.slide-4 #s4-text-1').animate({opacity:'1'}, 1000, 'swing', function() {
				$(this).show();
				$('div.slide-4 #s4-item-1').animate({opacity:'1',left:'85px'}, 1000, 'swing')
				.delay(2200)
				.animate({opacity:'0',left:'100px'}, 1000, 'swing', function() {
					$(this).hide();
					$('div.slide-4 #s4-item-2').animate({opacity:'1',left:'85px'}, 1000, 'swing')
					.delay(2200)
					.animate({opacity:'0',left:'100px'}, 1000, 'swing', function() {
						$(this).hide();
						$('div.slide-4 #s4-item-3').animate({opacity:'1',left:'85px'}, 1000, 'swing')
						.delay(2200)
						.animate({opacity:'0',left:'100px'}, 1000, 'swing', function() {
							$(this).hide();
							$('div.slide-4 #s4-item-4').animate({opacity:'1',left:'85px'}, 1000, 'swing')
							.delay(2200)
							.animate({opacity:'0',left:'100px'}, 1000, 'swing', function() {
								$(this).hide();
								$('div.slide-4 #s4-item-5').animate({opacity:'1',left:'85px'}, 1000, 'swing');
							});
						});
					});
				});
			});
			
			// Start animations in slide 5
			$('div.slide-5 #s5-text-1').animate({opacity:'1',lineHeight:'15px',fontSize:'15px'}, 600, 'swing', function() {
				$(this).show();
				$('div.slide-5 #s5-text-2').delay(1200).animate({opacity:'1'}, 700, 'swing', function() {
					$(this).show();
					$('div.slide-5 #s5-item-1').delay(1200).animate({opacity:'1'}, 100, 'swing', function() {
						$(this).show();
						$('div.slide-5 #s5-item-2').delay(1200).animate({opacity:'1'}, 100, 'swing', function() {
							$(this).show();
							$('div.slide-5 #s5-item-3').delay(1200).animate({opacity:'1'}, 100, 'swing', function() {
								$(this).show();
							});
						});
					});
				});
			});
			
			// Start animations in slide 6
			$('div.slide-6 #s6-text-1').animate({opacity:'1',lineHeight:'15px',fontSize:'15px'}, 600, 'swing', function() {
				$(this).show();
				$('div.slide-6 #s6-text-2').delay(1200).animate({opacity:'1'}, 700, 'swing', function() {
					$(this).show();
					$('div.slide-6 #s6-item-1').delay(1200).animate({opacity:'1'}, 100, 'swing', function() {
						$(this).show();
						$('div.slide-6 #s6-item-2').delay(1200).animate({opacity:'1'}, 100, 'swing', function() {
							$(this).show();
							$('div.slide-6 #s6-item-3').delay(1200).animate({opacity:'1'}, 100, 'swing', function() {
								$(this).show();
							});
						});
					});
				});
			});
		}
	});
	
	$('.animation-play-pause').click(function(event){
        event.preventDefault();
        if(!$(this).hasClass('paused')) {
            $(this).addClass('paused');
        } else {
        	$(this).removeClass('paused');
        	$('#slideshow').cycle('next');
        }
        toggle_buttons($(this).find('img'), $('#hidden-button'));
        $('#slideshow').cycle('toggle');
    });

    $('.animation-forward').click(function(event){
        event.preventDefault();
        $('#slideshow').cycle('next');
    });
});
