﻿//
// explore.js
// Copyright (c) Connect Group Ltd. All rights reserved.
//
// $Id: explore.js 550 2010-04-30 09:29:11Z 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 hideShareLink = false;
			if (typeof pageConfig.hideShare != 'undefined' && pageConfig.hideShare != null) {
				hideShareLink = pageConfig.hideShare;
			}
			if (!hideShareLink) {
				var shareLink = $('<li class="share"><a href="#">' + pageConfig.text.share.title + '</a></li>');
				shareLink.click(function(e) {
					e.preventDefault();

					$.nyroModalManual({
						closeButton:
							'<div class="close-nav"><a class="nyroModalClose" id="overlay-close-nav" title="'
							+ pageConfig.text.overlay.close
							+ '">'
							+ pageConfig.text.overlay.close
							+ '</a></div>',
						content:
							'<div style="width:300px"><h2>'
							+ pageConfig.text.share.title
							+ '</h2><p>'
							+ pageConfig.text.share.intro
							+ '</p><p><input value="'
							+ document.location.href
							+ '"/></p></div>',
						wrap: {
							manual: '<div id="nyroModalContentWrapper"></div>'
						},
						endShowContent: function(elts, settings) {
							$('input', elts.content).focus();
						},
						windowResize: false
					});
					return false;
				});
				ul.append(shareLink);
			}

			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);

			$(this).append(ul);
		});
	}
$('.ui-accordion-content').show();
	$('#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.connect-overlay a', $(this));
		children.height(getMaximumHeight(children));
		// alert(getMaximumHeight(children));
	}); */
	$('div.cta-link-block dl').each(function() {
		$('dd p').hide();
		var children = $('dd a', $(this));
		if(getMaximumHeight(children)>98){
			children.height(getMaximumHeight(children));
		} else {
			children.height(150);
		}
	});
	$('div.cta-link-block-wheels dl').each(function() {
		$('dd p').hide();
		var children = $('dd a', $(this));
		//alert(getMaximumHeight(children));
		if(getMaximumHeight(children)>98){
			children.height(getMaximumHeight(children));
		} else {
			children.height(220);
		}
	});

	$('div#primary-content div.car-select').each(function() {
		var children = $('div.car-select-block', $(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;
			});
		}
	}
});

