/* initialize-image-marquee.js */

function imageMarqueeInit(width, height) {

    // this is NOT PH's imageMarqueeInit()

    // span.carouselText is now INSIDE the li; it wasn't previously

    var liMarkup,
    	imgSrc,
    	imgMarkup,
		$link,
		linkHref,
		linkText,
		linkTarget,
		nListItems = 0;

	$(".droppableArea .small-block").each(function() {
		imgSrc = $(".small-block-image img", this).attr('src');

		if (imgSrc.length) {
			imgMarkup = '<img src="' + imgSrc + '" width="' + width + '" height="' + height + ' alt="" />';
		} else {
			imgMarkup = '<!-- missing image -->';
		}

		// only expecting one, but only take the first
		// ignore anything else within .small-block-text
		$link = $(".small-block-content > .small-block-text a", this).eq(0);

		if ($link.length) {
			linkHref = $link.attr('href');
			linkText = $link.text().trim();
			linkTarget = $link.attr('target');
		} else {
			linkHref = '';
			linkText = '';
			linkTarget = '';
		}

		// for simplicity, we assume if there is an href, there's text so we don't check for that being empty
		// plus, since it's all predicated on having a link, no href means this is an error
		if (linkHref.length) {
			liMarkup = '<li><a class="aCarousel" href="' + $link.attr('href') + '"' +
						(linkTarget ? ' target="' + linkTarget + '"' : '') +
						'>' +
						imgMarkup +
						'<span class="carouselText">' + linkText + '</span></a></li>';
		} else {
			// no link href!
			liMarkup = '<li><a class="aCarousel" href="#">' +
						imgMarkup +
						'<span class="carouselText"><em>Missing Target Page in Small Block w/Image content<em></span></a></li>';
		}

		nListItems++;
		$("#mycarousel").append(liMarkup);
    });

	// This assumes it is only ever called when NOT in the page editor, so it would always
	// hide .droppableArea. However, that calling requirement was not always being met.


	// when Intranet gets ocms-extensions, use: if (!OCMS.inPageEditor)
	if ($('#edit-header').length === 0) {
		$(".droppableArea").hide();
	}

	if (console && $.isFunction(console.log)) {
		console.log('imageMarqueeInit: # Small Block w/Image\'s found: ' + nListItems);
	}

	return nListItems;
}	// imageMarqueeInit
