$(function() {

	$('div.image_cross_slide').each(function() {
		var file_bucket, extension_filters, transition, i, option_match;
		var that = $(this);
		var classes = $(this).attr('class').split(" ");
		var options = {
			'slide'		: {
				speed		: 45,
				fade		: 1,
				shuffle		: 0
			},
			'fade'		: {
				sleep		: 4,
				fade		: 1,
				shuffle		: 0
			},
			'ken_burns' : {
				fade		: 1
			}
		};
		for(i = 0; i < classes.length ; i++) {
			if (classes[i].substr(0, 3) == 'fb-') {
				file_bucket = classes[i].replace('fb-', '');
			}
			if (classes[i].substr(0, 3) == 'ef-') {
				extension_filters = classes[i].replace('ef-', '');
			}
			if (classes[i].substr(0, 3) == 'tr-') {
				transition = classes[i].replace('tr-', '');
			}
			if (classes[i].substr(0, 3) == 'op-') {
				option_match = classes[i].match(/op-(\w+)-(\w+)/);
				if (typeof(options[transition][option_match[1]]) != 'undefined') {
					options[transition][option_match[1]]	= option_match[2];
				}
			}
		}
		extension_filters = extension_filters.split('-').join(',');
		var url = '/component/json/file_bucket/' + file_bucket + '/' + extension_filters;
		$.getJSON(url, function(json){
			if (json.files.length === 0) {
				// aborting! no img to show
			} else if(json.files.length === 1) {
				that.css('overflow', 'hidden');
				var img = new Image();
				$(img)
					.load(function () {
						that.html('').append(this);
					})
					.attr('src', json.files[0].url);
			} else {
				var img_src = [], tr_index, prev_tr_index = - 1;
				switch(transition) {
					case "slide":
						var slide_init_value = ['up', 'down', 'left', 'right'];
						for (var i = 0; i < json.files.length; i++) {
							tr_index 		= Math.floor(Math.random()*slide_init_value.length);
							while (tr_index === prev_tr_index) {
								tr_index 		= Math.floor(Math.random()*slide_init_value.length);
							}
							prev_tr_index 	= tr_index;
							img_src.push({ 
								src		: 	json.files[i].url.replace(' ', '%20'),
								dir		:   slide_init_value[tr_index]
							});
						}
						break;
					case "ken_burns":
						var kb_init_values = [{
						    from: '100% 80% 1x',
						    to:   '100% 0% 1.3x',
						    time: 3
						  }, {
						    from: 'top left',
						    to:   'bottom right 1.2x',
						    time: 2
						  }, {
						    from: '100% 80% 1.5x',
						    to:   '80% 0% 1.1x',
						    time: 2
						  }, {
						    from: '100% 50%',
						    to:   '30% 50% 1.4x',
						    time: 2
						}];
						for (i = 0; i < json.files.length; i++) {
							tr_index 		= Math.floor(Math.random()*kb_init_values.length);
							while (tr_index === prev_tr_index) {
								tr_index 		= Math.floor(Math.random()*kb_init_values.length);
							}
							prev_tr_index 	= tr_index;
							img_src.push({ 
								src		: 	json.files[i].url.replace(' ', '%20'),
								from	:   kb_init_values[tr_index].from,
								to		:   kb_init_values[tr_index].to,
								time	:   kb_init_values[tr_index].time
							});
						}
						break;
					// case "fade":
					default:
						for (i = 0; i < json.files.length; i++) {
							img_src.push({ 
								src		: 	json.files[i].url.replace(' ', '%20')
							});
						}
				}
				that.crossSlide(options[transition], img_src);
			}
		});
	});
});