$(function () {
	// Drop down navigation.
	$("ul#nav").superfish({
						  'hoverClass' : 'sfHover'
						  });
	
	// Maintain selected state?
	$("ul#nav li a.selected").parent().addClass("selected");
	
	// Right column image banners and homepage sector pods.
	$("ul.sectors li").hoverPod();
	$(".aside .image").hoverPod();
	
	// Sort out stupid EpiServer WYSIWYG mark-up.
	// This is the breaking function
	$(".cms").lockDown();
	
	// Convert content images into collapsable tumbnails.
	//$(".cms img").imageSlider({'height' : 91});
	
	// Fit the width of images to the centeral column width - maintaining aspect ratio.
	$(".cms .imageContainer").imagefit();
	$(".wideList div a").imagefit();
	
	$(".wideList div a").centreImages();
	
	// Left hand drop down with AJAX callback.
	//$("#subNav").superNav();
	
	// Detect external links and handle them appropriately, includes mailto:
	$(document).externalLink();
	
	// Validate product form.
	/*
	
	Validation switched off because on conflict.
	Stops search from working if contact form is on page
	No time to fix :(
	
	$("form").submit(function () {
		var error = false;
		$("div.contact fieldset").each(function() {
			$("select", this).each(function() {
				$(this).change(function () {
					if ($(":selected", this).val() != $(":first", this).val()) {
						$(".error", $(this).parent()).remove();
						error = false;
					}
				});
								
				if ($(this).val() == $(":first", this).val()) {
					if ($(this).parent().find(".error").length <= 0) {
						$(this).parent().append('<span class="error">Please select an option</span>');
					}
					
					error = true;
				}
			});
		});
		
		$("fieldset.fcontact").each(function () {
			$("select", this).each(function() {
				$(this).change(function () {
					if ($(":selected", this).val() != $(":first", this).val()) {
						$(".error", $(this).parent()).remove();
						error = false;
					}
				});
								
				if ($(this).val() == $(":first", this).val()) {
					if ($(this).parent().find(".error").length <= 0) {
						$(this).parent().append('<span class="error">Please select an option</span>');
					}
					
					error = true;
				}
			});									  
		});
		
		if (error)
			return false;
	});
	*/

	// Custom implimentation of plugin to make sure all pods are the same height
	$("ul#productListings").equalHeights({"elementToApply" : "h3"});
	$("ul#productListings").equalHeights({"elementToApply" : "p"});
	$("ul.searchresultpods").equalHeights({"elementToApply" : "h6"});
});

/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library						  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
 
 !IMPORTANT !IMPORTANT !IMPORTANT !IMPORTANT !IMPORTANT !IMPORTANT 
 	modified to do what I want it to do gavincooper@redweb.com
--------------------------------------------------------------------*/

$.fn.equalHeights = function(options) {  
	var defaults = {  
		'elementToApply'  : 'h3'
	}, options = $.extend(defaults, options);  
  
  
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().find(options.elementToApply).each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().find(options.elementToApply).css({'height': currentTallest}); }
		$(this).children().find(options.elementToApply).css({'min-height': currentTallest}); 
	});
	return this;
};

/* jquery.imagefit 
 *
 * Version 0.2 by Oliver Boermans <http://www.ollicle.com/eg/jquery/imagefit/>
 *
 * Extends jQuery <http://jquery.com>
 *
 */
(function($) {
	$.fn.imagefit = function(options) {
	    if($(this).parent().attr('expand') == true)
		{
		    alert('got here');
		    var fit = {
			    all : function(imgs){
				    imgs.each(function(){
					    fit.one(this);
					    })
				    },
			    one : function(img){
				    $(img)
					    .width('100%').each(function()
					    {
						    $(this).height(Math.round(
							    $(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						    );
					    })
				    }
		    };
		};
		
		this.each(function(){
		        if($(this).parent().attr('expand') == true)
		        {
		            alert('got here2');
				    var container = this;
    				
				    // store list of contained images (excluding those in tables)
				    var imgs = $('img', container).not($("table img"));
    				
				    // store initial dimensions on each image 
				    imgs.each(function(){
					    $(this).attr('startwidth', $(this).width())
						    .attr('startheight', $(this).height())
						    .css('max-width', $(this).attr('startwidth')+"px");
    				
					    fit.one(this);
				    });
				    // Re-adjust when window width is changed
				    $(window).bind('resize', function(){
					    fit.all(imgs);
				    });
				}
			});
		return this;
	};
})(jQuery);

/* jquery.cenreImages 
 *
 * Centres images in the provided container.
 * gavincooper@redweb.com
 */
(function($) {
	$.fn.centreImages = function(options) {		
		return this.each(function(){
			var $$ = $(this);
			
			var imgs = $("img", $$);
			
			// store initial dimensions on each image 
			imgs.each(function(){
				$(this).css({
						   'top'		: ((parseInt($$.height()) / 2) - (parseInt($(this).height()) / 2)),
						   'position'	: 'absolute'
						   });
			});
		});
	};
})(jQuery);









