// JavaScript Document

$('div.json_image_rotator').each(function() {
	var file_bucket, bucket_folder, i, option_match, t;
	var obj_id = $(this).attr('id');
	var that = $(this);
	var cur_lang = maui_runtime.current_language;
	var classes = $(this).attr('class').split(" ");
	var options = {
		json_file			: null,
		default_lang		: 'en',
		timeout				: 3000,
		transition_speed	: 500,
		endless				: 1,
		navigation_show		: 'always',
		caption_show		: 'always',
		effect				: 'fade'
	};
	that.addClass('loading');
	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) == 'op-') {
			option_match = classes[i].match(/op-(\w+)-(\w+)/);
			if (typeof(options[option_match[1]]) != 'undefined') {
				if (typeof(options[option_match[1]]) === 'number') {
					options[option_match[1]]	= parseInt(option_match[2], 10);
				} else {
					options[option_match[1]]	= option_match[2];
				}
			}
		}
	}
	bucket_folder	= maui_runtime.uploads_dir + '/FileBucket/' ;
	if (options.json_file) {
		json_file 		= bucket_folder + options.json_file + '.json';
	} else {
		json_file 		= bucket_folder + file_bucket + '/offerte.json';
	}
	$.getJSON(json_file, function(json) {
		var images_content_left, animating = false;
		var num_images = json.images.length;
		if (num_images < 2) {
			that.removeClass('loading');
			return false;
		}
		var images_container	= that.children('.image');
		if (options.endless) {
			images_content_left	= '-' + images_container.width() + 'px';
		} else {
			images_content_left	= '0px';
		}
		var preloader_info		= $('<div />').addClass('preloader-info').appendTo(that);
		var images_content 		= $('<div/>').attr('id', obj_id + '-content')
									.css('overflow', 'hidden')
									.css('position', 'absolute')
									.css('display', 'none')
									.css('left', images_content_left)
									.width(images_container.width() * (num_images + 2))
									.height(images_container.height());
		var caption_content 	= $('<div />')
									.addClass('caption-texts')
									.html('<div class="caption-title" /><div class="caption-text" />')
									.appendTo(that.children('.caption'));
		
		var img;

		var request = function(data) {
			preloader_info.html('Preloading images ' + data.loaded + '/' + data.total);
		};

		var complete = function(data) {
			preloader_info.html('Preloading images ' + data.loaded + '/' + data.total);
		};

		var detect_text = function(current_img, text_type)
		{
			var progress = '';
			if (text_type === 'title') {
				progress = ' <span class="progress">(' + (current_img + 1) + '/' + num_images + ')</span>';
			}
			if (typeof(json.images[current_img].caption[cur_lang]) != 'undefined' && typeof(json.images[current_img].caption[cur_lang][text_type]) != 'undefined') {
				return json.images[current_img].caption[cur_lang][text_type] + progress;
			} else if(typeof(json.images[current_img].caption[options.default_lang]) != 'undefined' && typeof(json.images[current_img].caption[options.default_lang][text_type]) != 'undefined') {
				return json.images[current_img].caption[options.default_lang][text_type] + progress;
			} else {
				return null;
			}
		};
		
		var animate_image = function (direction)
		{
			if (animating) {
				return;
			}
			animating = true;
			var increment, current_img;
			if (options.endless) {
				current_img = (Math.abs(parseInt(images_content.css('left'), 10)) /  images_container.width()) - 1;
			} else {
				current_img = (Math.abs(parseInt(images_content.css('left'), 10)) /  images_container.width());
			}
			if (direction === 'right') {
				increment = "-=" + images_container.width();
				current_img++;
			} else {
				increment = "+=" + images_container.width();
				current_img--;
			}
			if (options.endless) {
				if (direction === 'left' && Math.abs(parseInt(images_content.css('left'), 10)) === images_container.width()) {
					images_content.css('left', '-' + ((num_images + 1) * images_container.width()) + 'px');
					current_img = num_images - 1;
				}
				if (direction === 'right' && Math.abs(parseInt(images_content.css('left'), 10)) === (num_images * images_container.width())) {
					images_content.css('left', 0);
					current_img = 0;
				}
			} else {
				if (direction === 'left' && Math.abs(parseInt(images_content.css('left'), 10)) === images_container.width()) {
					that.children('.arrow-left').removeClass('nav-active').unbind().hide();
				} else if(that.children('.arrow-left').css('display') === 'none') {
					that.children('.arrow-left').addClass('nav-active').unbind().click(function() {
						animate_image('left');
					});
				}
				if (direction === 'right' && Math.abs(parseInt(images_content.css('left'), 10)) === ((num_images - 2) * images_container.width())) {
					that.children('.arrow-right').removeClass('nav-active').unbind().hide();
				} else if(that.children('.arrow-right').css('display') === 'none') {
					that.children('.arrow-right').addClass('nav-active').unbind().click(function() {
						animate_image('right');
					});
				}
			}
			
			switch(options.effect) {
				case 'animate':
					caption_content.fadeOut(options.transition_speed, function() {
						caption_content.children('.caption-title').html(detect_text(current_img, 'title'));
						caption_content.children('.caption-text').html(detect_text(current_img, 'text'));
						caption_content.fadeIn(options.transition_speed);
						images_content.animate({
							'left': increment
						}, options.transition_speed, function() {
							animating = false;
						});
					});
					break;
				case 'fade':
					$('*', caption_content).fadeOut(options.transition_speed, function() {
						caption_content.children('.caption-title').html(detect_text(current_img, 'title'));
						caption_content.children('.caption-text').html(detect_text(current_img, 'text'));
					});
					images_content.fadeOut(options.transition_speed, function() {
						$(this).animate({
							'left': increment
						}, 0, function() {
							$('*', caption_content).fadeIn(options.transition_speed);
							$(this).fadeIn(options.transition_speed, function() {
								animating = false;
							});
						});
					});
					break;
				default:
					$('*', caption_content).hide();
					caption_content.children('.caption-title').html(detect_text(current_img, 'title'));
					caption_content.children('.caption-text').html(detect_text(current_img, 'text'));
					images_content.hide().animate({
						'left': increment
					}, 0, function() {
						$('*', caption_content).show();
						$(this).show();
						animating = false;
				});
			}
		};

		var json_image_rotator = function(preloader_response) {
			that.removeClass('loading');
			preloader_info.hide();
			images_content.appendTo(images_container).fadeIn(300);
			switch(options.caption_show) {
				case 'hover':
				caption_content.children('.caption-title').html(detect_text(0, 'title'));
				caption_content.children('.caption-text').html(detect_text(0, 'text'));
				caption_content.show();
				that.hover(function() {
					that.children('.caption').fadeIn(300);
				}, function() {
					that.children('.caption').fadeOut(300);
				});
				break;
				case 'always':
				caption_content.children('.caption-title .caption-text').html(detect_text(0, 'title'));
				caption_content.children('.caption-text').html(detect_text(0, 'text'));
				caption_content.show();
				that.children('.caption').show(300);
				break;
				default:
			}
			switch(options.navigation_show) {
				case 'hover':
					that.children('.arrow-right').addClass('nav-active').click(function() {
						animate_image('right');
					});
					if (options.endless) {
						that.children('.arrow-left').addClass('nav-active').click(function() {
							animate_image('left');
						});
					}
					that.hover(function() {
						that.children('.nav-active').fadeIn(300);
					}, function() {
						that.children('.nav-active').fadeOut(300);
					});
					break;
				case 'always':
					that.children('.arrow-right').addClass('nav-active').show().click(function() {
						animate_image('right');
					});
					if (options.endless) {
						that.children('.arrow-left').addClass('nav-active').show().click(function() {
							animate_image('left');
						});
					}
					break;
				default:
			}
		};

		for (i = 0; i < num_images; i++) {
			img = new Image();
			$(img).attr('src', '/Uploads/FileBucket/' + file_bucket + '/' + json.images[i].src);
			$('<div/>').css('float', 'left').css('overflow', 'hidden').width(images_container.width()).height(images_container.height()).append($(img)).appendTo(images_content);
		}
				
		images_content.children('div').children('img').preload({
			enforceCache	: true,
			onRequest 		: request,
			onComplete 		: complete,
			onFinish		: json_image_rotator
		});

		if (options.endless) {
			images_content.append($("div:first", images_content).clone()).prepend($("div:nth(" + (num_images - 1) + ")", images_content).clone()).fadeIn(300);
		}

		if (options.timeout > 0) {
			t = setInterval(function() {animate_image('right');}, options.timeout); 
			$(that).hover(function() {
				clearInterval(t); 
		    }, function() { 
				t = setInterval(function() {animate_image('right');}, options.timeout); 
		    });
		}

	});
});