var sb_effects = {}

jQuery(document).ready(function() {
	jQuery(".slideshow_bob .slideshow_element").hide();
	jQuery(".slideshow_bob .slideshow_element .caption").hide();
});
	
function slideshow_bob_init(config) {
	slideshow_bob_nextImage(config, 0, null);
}

function slideshow_bob_nextImage(config, imageToShow, previousImage) {
	var wait = {};
	wait.transition = 0;
	wait.show_caption = config.imageConf[imageToShow].itd + config.imageConf[imageToShow].cwb;
	wait.hide_caption = config.imageConf[imageToShow].dur - (config.imageConf[imageToShow].ctde + config.imageConf[imageToShow].cwa);
	wait.run_again = config.imageConf[imageToShow].dur;

	// fade out / fade in
	setTimeout(function() {
		slideshow_bob_transition(config, imageToShow, previousImage);
	}, wait.transition * 1000);

	if (config.showOnlyOnce == 1 && previousImage != null) {
		return;
	}

	// show caption
	setTimeout(function() {
		slideshow_bob_showCaption(config, imageToShow);
	}, wait.show_caption * 1000);

	// hide caption
	setTimeout(function() {
		slideshow_bob_hideCaption(config, imageToShow);
	}, wait.hide_caption * 1000);

	// call me again
	var nextImage = imageToShow + 1;
	if (nextImage == config.imageCount)	{
		nextImage = 0;
	}

	setTimeout( function() {
		slideshow_bob_nextImage(config, nextImage, imageToShow);
	}, wait.run_again * 1000);
}

function slideshow_bob_hideCaption(config, imageToHide) {
	var caption = jQuery("#slideshow_bob_" + config.uid + " .sbe" + imageToHide + " .caption");
	//sb_effects.caption["slide"]( "hide", caption, config );
	slideshow_bob_runEffect(config.imageConf[imageToHide].cte, "hide", caption, config );
}

function slideshow_bob_transition(config, imageToShow, previousImage) {
	if (previousImage != null) {
		var prevImg = jQuery("#slideshow_bob_" + config.uid + " .sbe" + previousImage);
		slideshow_bob_runEffect(config.imageConf[previousImage].ite, "hide", prevImg, config );
	}

	if (config.showOnlyOnce == 0 || (config.showOnlyOnce == 1 && previousImage == null)) {
		var nextImg = jQuery("#slideshow_bob_" + config.uid + " .sbe" + imageToShow);
		slideshow_bob_runEffect(config.imageConf[imageToShow].its, "show", nextImg, config);
	}
}

function slideshow_bob_showCaption(config, imageToShow ) {
	var caption = jQuery("#slideshow_bob_" + config.uid + " .sbe" + imageToShow + " .caption");
	//sb_effects.caption["slide"]( "show", caption, config );
	slideshow_bob_runEffect(config.imageConf[imageToShow].cts, "show", caption, config);
}

function slideshow_bob_runEffect(effect, type, element, config) {
	var found = false;
	jQuery.each(sb_effects, function(key, value) {
		if (key == effect) {
			found = true;
			effect = value;
		}
	});
	if (found) {
		effect.effect(type, element, config);
	}

}

function slideshow_bob_calcDimensions(element) {
	var back = {
		width: sb_parseInt(element.css("padding-left"))
		+ sb_parseInt(element.css("padding-right"))
		+ sb_parseInt(element.css("margin-left"))
		+ sb_parseInt(element.css("margin-right"))
		+ sb_parseInt(element.css("border-left-width"))
		+ sb_parseInt(element.css("border-right-width")),
		height: sb_parseInt(element.css("padding-top"))
		+ sb_parseInt(element.css("padding-bottom"))
		+ sb_parseInt(element.css("margin-top"))
		+ sb_parseInt(element.css("margin-bottom"))
		+ sb_parseInt(element.css("border-top-width"))
		+ sb_parseInt(element.css("border-bottom-width"))
	};

	return back;
}

function sb_parseInt(property) {
	var value = parseInt(property);

	if (isNaN(value)) {
		value = 0;
	}

	return value;
}

function slideshow_bob_updateZIndex(toogletype, element) {
	switch (toogletype) {
		case "show":
			element.css("z-index", "102");
			break;
		case "hide":
			element.css("z-index", "101");
			break;
	}
}

function slideshow_bob_getTransitionTime(element, config, toogletype) {
	var time = 0;

	if (jQuery(element).hasClass("caption")) {
		if (toogletype == "show") {
			time = config.imageConf[parseInt(element.parent().attr("iid"))].ctds
		} else {
			time = config.imageConf[parseInt(element.parent().attr("iid"))].ctde
		}

		time = time * 1000;
	}

	if (jQuery(element).hasClass("slideshow_element")) {
		time = config.imageConf[parseInt(element.attr("iid"))].itd * 1000;
	}

	if (jQuery(element).hasClass("slideshow_element_wiz")) {
		time = parseFloat(jQuery(".time").val()) * 1000;
	}

	return time;
}

function slideshow_bob_setCaptionStandardStyles( element, config ) {
	var minify = slideshow_bob_calcDimensions( element );
	element.css("position", "absolute");
	element.css("bottom", "0px");
	element.css("left", "0px");
	element.css("height", (parseInt(config.height) - minify.height) + "px");
	element.css("width", (parseInt(config.width) - minify.width)+ "px");
}

function slideshow_bob_initEffects() {
	sb_effects.fade = {
		effect: function(toogletype, element, config) {
			var time = slideshow_bob_getTransitionTime(element, config , toogletype);
			if (jQuery(element).hasClass("caption") && toogletype == "show" ) {
				slideshow_bob_setCaptionStandardStyles( element, config )
			}

			if (jQuery(element).hasClass("slideshow_element")) {
				slideshow_bob_updateZIndex( toogletype, element )
			}

			switch(toogletype) {
				case "show":
					//element.fadeIn( time );
					element.animate({
						opacity: "show"
					}, time, "easeInOutQuad");
					break;
				case "hide":
					//element.fadeOut( time );
					element.animate({
						opacity: "hide"
					}, time, "easeInOutQuad");
					break;
			}
		},
		name: "Fade"
	}

	sb_effects.slide = {
		effect: function(toogletype, element, config) {
			var time = slideshow_bob_getTransitionTime(element, config, toogletype);
			if (jQuery(element).hasClass("caption") && toogletype == "show") {
				slideshow_bob_setCaptionStandardStyles( element, config )
			}

			if(jQuery(element).hasClass("slideshow_element")) {
				slideshow_bob_updateZIndex( toogletype, element )
			}

			switch(toogletype) {
				case "show":
					element.slideDown(time);
					//element.animate({height: "show"}, time, "easeInOutQuad");
					break;
				case "hide":
					element.slideUp(time);
					//element.animate({height: "hide"}, time, "easeInOutQuad");
					break;
			}
		},
		name: "Slide"
	}
}

