// Redefine, bugfix
// Below function definition belongs to bootstrap.js, we need to migrate this asap
function initModuleProperties() {
	// Grab the CMS properties and make them available for JS via (module's parent element).moduleProperties
	$(".module>script").each(function(){
		var json = $(this).html();
		if (!json.match(/^\s*\(\{/m)) {
			return true;
		}
		json = JSON.parse(json);
		this.parentNode.moduleProperties = json;
	});
}




$(function(){
	/*
	* listofobject.js
	* Make the images clickable too if the object has a link
	*/
	$(".module-listOfObjects li.has-thumb a.morebutton").each(function(){
		var onclick = this.onclick,
			href = this.href,
			$a = $("<a/>").attr("href", href).click(onclick);
		$(this).closest(".has-thumb").find(".bgImg img").wrap($a);
	});
	// Remove the buttons from some of the modules
	$("body.layout-category-pages-2 #moduleslot3 .module-listOfObjects .has-thumb").each(function(){
		// 2010-11-15 First check if we have this already fixed in the module itself
		var $morebutton = $("a.morebutton", this);
		if (!$morebutton.length) {
			return false;// Nothing to do, we don't need this fix anymore.
		}
		var $a = $("a:first", this).css("text-decoration", "none");
		$("a.morebutton", this).parent("div").remove();
		$("img", $a).unwrap();
		$(this).wrapInner($a);
		$("div.description", this).css("color", "#000");
	});
	$("body.layout-tv-stunts #moduleslot4 .module-tableOfObjects .has-thumb").each(function(){
		var $a = $("a:first", this).css("text-decoration", "none");
		$("img", $a).unwrap();
		$(this).wrapInner($a);
		$("h4", this).css("color", "#000");
	});
	/* remove width and height attributes from the loo images */
	$(".module-listOfObjects li.has-thumb").each(function(){
		$(this).find(".bgImg img").removeAttr("width").removeAttr("height");
	});

	
	
	/*
	* Fix for Detailed item module
	* Hides empty .dfysubtitle overlays from the image. (Can't remove the overlay divs because the module is dependent on their margins.)
	*/
	$(".module-detailed-item a.dfysubtitle").each(function(){
		if (! this.innerHTML.replace(/\s/gm,"").length) {
			$(this).parent("span.dfysubtitle").css("visibility", "hidden");
		}
	});
	
	
	
	/*
	* Identify multi lined headers (h3.title)
	*/
	$("h3.title").each(function(){
		// Create a single-lined reference title
		var ref = $('<h3 class="title" style="position:absolute; width:0">1<\/h3>').insertBefore(this),
			// Measure the height of the current title tag
			is2Lined = (ref.height() < $(this).height());
		ref.remove();
		if (is2Lined) {
			$(this).addClass("multilines-2");
		}
	});
	
	
	
	/*
	* Ensure that however tall the viewport is, the footer and the content area always touch.
	*/
	function touchContentAndFooter(){
		var $content = $("#content"), top = $content.offset().top, height = $content.outerHeight(), paddingOffset = height - $content.height(),
			footerHeight = $("#disney-common-footer").height(),
			viewportHeight = $(window).height(),
			gap = viewportHeight - footerHeight - top - height - paddingOffset;
		if (gap > 0) {
			$content.css( "height", height + gap + "px" );
		}
	}
	$(window).load(touchContentAndFooter).resize(touchContentAndFooter);
	
	
	
	/*
	* Support for multi lined menu items on Left Hand Navigation module. Removing empty submenus.
	*/
	var menuItems = $(".module-leftHandNav ol li");
	menuItems.wrapInner('<table class="vertical-align"><tbody><tr><td><\/td><\/tr><\/tbody><\/table>');
	$("ol a", menuItems).each(function(){
		if ($(this).attr("href").length) {
			return true;
		}
		$(this).closest("ol").remove();
	});
});

