//
// explore.js
// Copyright (c) Connect Group Ltd. All rights reserved.
//
// $Id: explore.js 718 2011-03-31 13:42:40Z paul $
//

//
// getMaximumHeight(group)
// Gets the maximum height for a group of elements.
// PARAMS
//  group: A group of elements.
// RETURNS
//  The maximum height for the specified group of elements.
// REMARKS
//  If the maximum height is less than the original height of the group 
//  (for example, if the height of the group is specified in a CSS file), the original height is 
//  retained. 
//
function getMaximumHeight(group) {
	var defaultHeight = group.height();
	var tallestChild = group.children().height();
	var height = (defaultHeight < tallestChild) ? tallestChild : defaultHeight;

	group.each(function() {
		var itemHeight = $(this).height();
		height = (itemHeight > height) ? itemHeight : height;
	});
	return height;
}

$(document).ready(function() {
	// Create and set up the print and share links
	if (typeof pageConfig != 'undefined' && pageConfig != null) {
		$("div#model-further-details-header, div#model-further-details-footer").each(function() {
			var ul = $('<ul></ul>');

			var printLink = $('<li class="print"><a href="#">' + pageConfig.text.print + '</a></li>');
			printLink.click(function(e) {
				e.preventDefault();
				window.print();
				return false;
			});
			ul.append(printLink);

			var hideShareLink = false;
			if (typeof pageConfig.hideShare != 'undefined' && pageConfig.hideShare != null) {
				hideShareLink = pageConfig.hideShare;
			}

			if (!hideShareLink) {
				var shareLink = $('<li class="share"><a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;pubid=xa-4d89c7a123708509"></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4d89c7a123708509"></script></li>');
				ul.append(shareLink);
			}

			$(this).append(ul);
		});
	}

	$('#model-navigation').each(function() {
		var children = $('dd', $(this));
		var height = getMaximumHeight(children);
		children.height(height).filter(':not(.selected)').filter(':not(.last)').height(height - 1);
	});

	$('#bp-engine-select-block ul li').each(function() {
		var children = $('span.type', $(this));
		children.height(getMaximumHeight(children));
	});

	$('#bp-model-select-block').each(function() {
		var children = $('dl > dt', $(this));
		children.height(getMaximumHeight(children));
	});

	$('div.cta-link-block dl').each(function() {
		$('dd p.extended-description').hide();
		var children = $('dd a', $(this));
		children.height(getMaximumHeight(children));
	});

	$('div#primary-content div.car-select').each(function() {
		var children = $('div.car-select-block', $(this));
		children.height(getMaximumHeight(children));
	});
	
	$('div.compare-cars-block').each(function() {
		var children = $('dl', $(this));
		children.height(getMaximumHeight(children));
	});

	$('#secondary-navigation ul').each(function() {
		var children = $('li a', $(this));
		children.height(getMaximumHeight(children));
	});

	$("#model-further-details-content h3").each(function() {
		$(this).toggleClass("ui-state-active");

		var content = $(this).next('div.ui-accordion-content');

		content.hide();
	
		$(this).click(function() {
			$(this).toggleClass("ui-state-active");
			content.slideToggle('slow');
			return false;
		});
	
		$(this).hover(
			function() { $(this).addClass("ui-state-hover"); },
			function() { $(this).removeClass("ui-state-hover"); }
		);
	});
	// Initalise 3D model overlay
	if (typeof flash3dConfig != 'undefined' && typeof flash3dConfig.exterior360s != 'undefined') {
		var carImage = $('div#car-image');
		if (carImage.length) {
			carImage.append(
				'<a href="#" class="view">'
					+ flash3dConfig.text.exterior360s.view
				+ '</a>'
			);
			$('a', carImage).click(function(e) {
				e.preventDefault();
				
				var overlayContent
					= '<div id="photo" style="width: 864px; height: 570px">'
					+ '<div id="main-content" style="padding:0;width:850px;height:425px;"></div>'
					+ '<div id="text">'
					+ '<div class="options-price-block">'
					+ '<h3><strong></strong> <span></span></h3>';

				if (flash3dConfig.text.vehicle.trimlevels.selected.price.length > 0) {
					overlayContent += '<span class="price"><span></span></span>'
					+ '<ul class="disclaimers"></ul>'
					+ '</div>'
				}

				overlayContent += '</div></div>';

				var overlaySettings = {
					closeButton:
						'<div class="close-nav"><a class="nyroModalClose" id="overlay-close-nav" title="'
						+ flash3dConfig.text.overlay.close
						+ '">'
						+ flash3dConfig.text.overlay.close
						+ '</a></div>',
					content: overlayContent,
					wrap: {
						manual: '<div id="nyroModalContentWrapper" class="exterior360"></div>'
					},
					windowResize: false
				};

				overlaySettings.endFillContent = function(elts,settings) {
					swfobject.embedSWF(
						flash3dConfig.exterior360s.selected.large.swf,
						'main-content',
						flash3dConfig.exterior360s.selected.large.width,
						flash3dConfig.exterior360s.selected.large.height,
						"9.0.0",
						false,
						flash3dConfig.exterior360s.selected.large.params.flashvars,
						flash3dConfig.exterior360s.selected.large.params,
						{ id: flash3dConfig.exterior360s.selected.large.id }
					);
					
					$('h3 strong', elts.content).html(flash3dConfig.text.vehicle.nameplate);
					$('h3 span', elts.content).html(flash3dConfig.text.vehicle.trimlevels.selected.name);
					$('span.price span', elts.content).html(flash3dConfig.text.vehicle.trimlevels.selected.price);
					refreshCufon();
				};
				$.nyroModalManual(overlaySettings);

				return false;
			});
		}
	}

	var compareHeaderBlock = $('div#wrapper div#content div#primary-content div#primary-content-body div#model-further-details > div#compare-header-block:not(.block-closed)');
	if (compareHeaderBlock.length == 1) {
		compareHeaderBlock.children('a').toggle(
			function() { $(this).removeClass('addremove').addClass('hidemodels'); compareHeaderBlock.next('div#compare-modelselect-block').slideDown(); $(this).children('span').text(pageConfig.text.compare.hide); },
			function() { $(this).removeClass('hidemodels').addClass('addremove'); compareHeaderBlock.next('div#compare-modelselect-block').slideUp(); $(this).children('span').text(pageConfig.text.compare.addRemove); }
		);
		compareHeaderBlock.next('div#compare-modelselect-block').hide();
	}

	var compareDataBlock = $('div#wrapper div#content div#primary-content div#primary-content-body div#model-further-details > div.compare-cars-block');
	if (compareDataBlock.length == 1) {
		var openSections = $.cookie('compare.opensections');
		if (openSections == null || openSections.length == 0) {
			$.cookie('compare.opensections', 'overview');
		}
	}

	$('div#wrapper div#content div#primary-content div#primary-content-body div#model-further-details div#model-further-details-content div.compare-data-block table tbody tr.header th div.navigate a').click(function() {
		var open = [];
		$('div#wrapper div#content div#primary-content div#primary-content-body div#model-further-details div#model-further-details-content h3.ui-state-active').prev('a').each(function() {
			open.push($(this).attr('name'));
		});
		$.cookie('compare.opensections', open.join(','));
	});
	
	var openSections = $.cookie('compare.opensections');
	if (openSections != null && openSections.length > 0) {
		var open = openSections.split(/,/);
		for (var index = 0; index < open.length; index++) {
			var anchor = $('div#wrapper div#content div#primary-content div#primary-content-body div#model-further-details div#model-further-details-content a[name="' + open[index] + '"]');
			var header = anchor.next('h3');
			var data = header.next('div.ui-accordion-content');
			
			header.addClass('ui-state-active');
			data.show();
		}
	}
	$.cookie('compare.opensections', null);

	$('div.compare-data-block table tr th').each(function() {
		var ul = $('<ul class="disclaimers"></ul>');

		$('div.tooltip', $(this)).each(function() {
			var li = $('<li><span class="tooltip-trigger">' + $('span.content-internal span.header', $(this)).text() + '</span></li>');
			var className = 'tooltip-block ' + /*((isInSidebar) ? 'sidebar-disclaimer-tooltip-block' :*/ 'disclaimer-tooltip-block'/*)*/;
			$(this).hide().insertAfter($('span.tooltip-trigger', li)).addClass(className).prepend($('<div class="content"></div>')).children('span.content-internal').appendTo($('div.content', $(this)));
			ul.append(li);
		});

		$(this).append(ul);
		$('li span.tooltip-trigger', ul).each(function() {
			var trigger = $(this);
			trigger.tooltip({
				position: 'bottom right',
				relative: false,
				onBeforeShow: function(event, position) {
					var tip = this.getTip();
					$('body').append(tip);
					this.getConf().offset = [-15,15];
					return true;
				}
			});
		});
	});
});
