// splits a sku_code every 2 characters - for fluid images.
function split_sku_code_for_fluid_image_path(sku_code) {
	var multi_split_sku_code_subdir = '';

	for (var i = 0; i < sku_code.length; i++) {

		// not the first and is even
		if ((i > 0) && (i % 2 == 0)) {
			multi_split_sku_code_subdir = multi_split_sku_code_subdir + '/';
		}

		multi_split_sku_code_subdir = multi_split_sku_code_subdir
				+ sku_code.substring(i, i + 1);
	}

	return multi_split_sku_code_subdir;
}

// mimics FinaHelper's methods
function get_generated_fluid_image_path(sku_code, width, height, view) {
	return pathInfo['managed_image_path'] + '/fluid/customers/c905/'
			+ split_sku_code_for_fluid_image_path(sku_code) + '/generated/'
			+ sku_code + '_default_' + view + '_' + width + 'x' + height
			+ '.jpg';
}

// mimics FinaHelper's methods
function get_detailed_fluid_image_path(sku_code, width, height, view) {
	return pathInfo['managed_image_path'] + '/fluid/customers/c905/'
			+ split_sku_code_for_fluid_image_path(sku_code) + '/' + sku_code
			+ '_detail/main_variation_default_view_' + view + '_' + width + 'x'
			+ height + '.jpg';
}

// size: 'small', 'medium' (see dropin_img.tag)
// container_id: where to put the image
// static_url: whether to use use static url or dynamic url (non empty = static)
// position: 'before' (existing content in container_id), anything else (after)
// insert_zoom: set to true if you want to show a zoomed-in image on image hover
// make_container_pos_relative: whether to make the container's position
// 'relative' (only considered if 'insert_zoom' is true)
// do_not_link: set to true if you do not want to make the image (or zoomed-in
// image) a link to the product
function insert_image(size, container_id, product, static_url, position,
		insert_zoom, make_container_pos_relative, do_not_link) {
	if ($(container_id) && product.has_image) {
		if ('small' == size) {
			var file_height = 50;
			var file_width = 50;
			var zoom_file_height = 130;
			var zoom_file_width = 130;
		} else if ('medium' == size) {
			var file_height = 130;
			var file_width = 130;
			var zoom_file_height = 250;
			var zoom_file_width = 250;
		} else {
			return;
		}

		if (insert_zoom && make_container_pos_relative) {
			var relative_container_id = container_id;

			// if we are going to make the container relative, we need to give
			// it a position.
			var div_pos = 'top: 0px; right: 0px;';
		} else {
			var div_pos = '';
		}

		var img_src = get_generated_fluid_image_path(product.sku, file_width,
				file_height, 1);
		var zoom_img_src = get_generated_fluid_image_path(product.sku,
				zoom_file_width, zoom_file_height, 1);

		if (static_url != '') {
			var link = product.static_url;
		} else {
			var link = contextPath+product.dynamic_url;
		}

		if (link.substring(0, 1) != '/') {
			link = '/' + link;
		}

		var html_snippet = '';

		// the hidden div containing the zoomed-in image
		if (insert_zoom) {
			html_snippet = '<div id="div_product_img_'
					+ product.id
					+ '" style="display: none; position: absolute; padding: 0px; '
					+ div_pos + ' z-index: 1000;">';

			if (!do_not_link) {
				html_snippet = html_snippet + '<a href="' + link + '">';
			}

			// zoomed-in image
			html_snippet = html_snippet + '<img src="' + zoom_img_src
					+ '" height="' + zoom_file_height + '" width="'
					+ zoom_file_width + '" style="border: 1px solid #ebeced" '
					+ 'onmouseout="hide_image(\'div_product_img_' + product.id
					+ '\', \'' + relative_container_id + '\')">';

			if (!do_not_link) {
				html_snippet = html_snippet + '</a>';
			}

			html_snippet = html_snippet + '</div>';

			// regular image
			html_snippet = html_snippet + '<img height="' + file_height
					+ '" width="' + file_width + '" src="' + img_src
					+ '" onmouseover="show_image(\'div_product_img_'
					+ product.id + '\', \'' + relative_container_id + '\')">';
		} else {
			if (!do_not_link) {
				html_snippet = html_snippet + '<a href="' + link + '">';
			}

			// regular image
			html_snippet = html_snippet + '<img height="' + file_height
					+ '" width="' + file_width + '" src="' + img_src + '">';

			if (!do_not_link) {
				html_snippet = html_snippet + '</a>';
			}
		}

		var existing_html_snippet = $(container_id).innerHTML;

		if (position == 'before') {
			$(container_id).update(html_snippet + existing_html_snippet);
		} else {
			$(container_id).insert(html_snippet);
		}
	}
}

