
//this file needs jquery to work
//please minify this file when making changes to core.min.js for uploading to live
//use yui compressor here http://developer.yahoo.com/yui/compressor/ to do it 



(function(){

//NameSpace
window.NewMind = window.NewMind || {};
var NewMind = window.NewMind;
NewMind.WebPlatform = NewMind.WebPlatform || {};

$(document).ready(
		function() { 
			$("#colCenterProdImg .onlineBookingButton a").attr("target","_blank");
		})();


//Tunbridge Wells Specific
NewMind.WebPlatform.TunbridgeWells = function() {
	
	//private
	
	/**
	* Add alt text captions to all user content images
	*
	**/
	var imageCaptions = function(strSelector){
		//get all images with alt text that are descendants of a paragraph 
		//inside any element of #colCenterIntro, this is due to product listings and special offer problems
		$(strSelector).each(function(index){
			var thisCapImg = $(this);
			//if the image doesnt have a class of 'nocaption' then 
			//add a span after it with the caption in
			
			if(thisCapImg.attr('class') !== 'nocaption' && thisCapImg[0].tagName === 'IMG' ) {
					
				//insert the caption span after the image
				//dont want to wrap the image, changes to DOM hierarchy may affect styling
				$('<span class="imageCaption"><p>' + thisCapImg.attr('alt') + '</p></span>').insertAfter(thisCapImg);
				
				//set the width of the new span to match the image
				thisCapImg.next().css('width',thisCapImg.css('width')).css('display','block');
			}
		});
		
	};
	
	/**
	* Adding a class to use CSS to hide the itinerary basket when it is empty, this is so the ajax posts adding
	* items to the itinerary will then be able to display the basket without reloading the page.
	* This is a workaround for Config("ExPlanner_HideBasketWhenEmpty")
	**/
	var hideItinWhenEmpty = function() {
		if($('#excursionHelp .ex_baskettable').length === 0) {
			$('#excursionHelp').addClass('hidden');
		}
	};
	
	
	/**
	* replace text of an element by passing in the jquery selector expression and the text
	* Using this for a couple of the search box headers instead of image replacement styling
	**/
	var replaceText = function(strSelExp, strText) {
		try {
			$(strSelExp).text(strText);
		} catch(e) {
			
		}
	};
	
	/**
	* Toggle the display of product facilities in their groups
	*
	**/
	var prodFacilities = function(sContainer){
		$(sContainer + " h3").wrapInner('<a href="#" title="Show / Hide"></a>').toggle(function(event){
			$(this).parent().find("ul." + this.className + ", ul." + this.className+ " + h4").show();
		}, function(event){
			$(this).parent().find("ul." + this.className + ", ul." + this.className+ " + h4").hide();
		}).parent().find("ul, h4").hide();
	};
	
	
	//public
	return{
		init : function(){
			
			//itinerary hiding (not using this for the time being)
			//hideItinWhenEmpty();
			
			replaceText('#ir_book_online','Search Availability');
			//not using this for now, client wants it to stay as activities
			//replaceText('#ir_search_activities','Search Experiences');
			
			//product facilities dropdown
			prodFacilities("#facilitiesList");
			
		},
		
		imageCaptionsInit : function() {
			//user content captions use this onload to obtain the image widths correctly
			imageCaptions('body:not(.home) #headerImage img[@alt!=]');
			imageCaptions('#colCenterIntro > *:not(ul.searchResults) img[@alt!=]');
		}
	};
}();

$(document).ready(NewMind.WebPlatform.TunbridgeWells.init);
$(window).load(NewMind.WebPlatform.TunbridgeWells.imageCaptionsInit);
})();
