$(function(){
	$('.slideshowTool').each(function(n){
		var toolSelector = $(this);
		var isTouchSlideshow = toolSelector.children('input[name=touch_slideshow]').length;
		var effect = $(this).children('input[name=transition_effect]').val();
		var transition_delay = $(this).children('input[name=transition_delay]').val();
		var transition_speed = $(this).children('input[name=transition_speed]').val();
		var not_configured = $(this).find('#no_slideshow_placeholder').length;
		
		var images = toolSelector.find('img');
		var imgCounter = images.length;
		
		if (not_configured != 0){
			return;
		} else {
			
			images.each(function(){
				var img = $(this);
				
				img.hide();
				
				img.load(function(){
					var loadedImg = $(this);
					
					imgCounter--;
					loadedImg.scaleImage({scale:'fit'});
					
					if(imgCounter === 0){
						
						images.show();
						
						if(isTouchSlideshow != 0){
							toolSelector.find('#slidebar').attr('id','slidebar-'+n);
							touchslider.createSlidePanel('#slidebar-'+n, 320, 0);
						} else {
							switch (effect){
							case 'Fade':
								callEffect(toolSelector, 'fade', transition_delay, transition_speed);
								break;
							case 'Slide':
								callEffect(toolSelector, 'scrollLeft', transition_delay, transition_speed);
								break;
							}
						}
						
					}
					
				});
				
			});
			
		}
		
	});
	
	function callEffect(selector, effect, transition_delay, transition_speed){
        $(selector).find('.slideshow_placeholder').cycle({
            fx: effect,
            timeout: getEffectDelayMiliseconds(transition_delay),
            speed: getEffectSpeedMiliseconds(transition_speed)
        });
    }

    function getEffectDelayMiliseconds(speedString){
        switch(speedString){
            case 'slow':
                return 7000;
                break;
            case 'medium':
                return 5000;
                break;
            case 'fast':
                return 3000;
                break;
        }
    }

    function getEffectSpeedMiliseconds(speedString){
        switch(speedString){
            case 'slow':
                return 1300;
                break;
            case 'medium':
                return 650;
                break;
            case 'fast':
                return 300;
                break;
        }
    }
    
});
