(function() {

	var NewMind = window.NewMind || {};
	NewMind.Site = window.NewMind.Site || {};
	
	/**
	 * opener
	 * @Requires JQuery.1.2.2
	 */
	NewMind.Site.click = function(selector, hideblock) {
		//add click event
		$(selector).click(
			function(){				
				//get the classes of the div
				//using attr because it returns a string of the value
				
				if(hideblock=='' || hideblock==selector)
				{
					hideblock=selector;
				}
				var liObj = $(hideblock);
				var classAttr = String(liObj.attr('class'));
				if(classAttr.indexOf("closedblock")>0)
				{
					$(hideblock).removeClass('closedblock');
				}
				else
				{
					$(hideblock).addClass('closedblock');
				}
			}
		)
	};
	
	$(document).ready(
		function() { 
			//selector adds click to product items
			
			
			//modify any nav sub items starting with the word dartmoor, for the dartmoor to be on the first 
			//line and the rest to be on the second line.
			var liSubNavObj = $('.ctl_MainNavigation > ul > .selected > div > div > ul > li > a');
			if(liSubNavObj.length!=0)
			{
				iMyCounter=0;
				strTempText="";
				while(iMyCounter < liSubNavObj.length)
				{
					strTempText=String(liSubNavObj[iMyCounter].innerHTML);
					if(strTempText.indexOf("dartmoor")>-1)
					{
						strTempText=strTempText.replace("dartmoor", "<div class=\"subnavdartmoor\">dartmoor</div>");
						liSubNavObj[iMyCounter].innerHTML=strTempText;
					}
					iMyCounter++;
				}
			}
			
			//for the product detail content. The following opens and closes the tabs on the right
			
			
			//detailed product info
			NewMind.Site.click('#colRight > #detailedproductInfo','');
			//the item should only be open already for those without js, so close it now.
			$('#colRight > #detailedproductInfo').addClass('closedblock');
			
			//facilities list
			NewMind.Site.click('#colRight > #facilitiesList','');
			//the item should only be open already for those without js, so close it now.
			$('#colRight > #facilitiesList').addClass('closedblock');
			
			//whats nearby search
			NewMind.Site.click('#colRight > #refineBy-whatsNearby > .nearbySearchImg','#colRight > #refineBy-whatsNearby');
			//the item should only be open already for those without js, so close it now.
			$('#colRight > #refineBy-whatsNearby').addClass('closedblock');
			
			//road directions
			NewMind.Site.click('#colRight > #rightRoadDirections','');
			//the item should only be open already for those without js, so close it now.
			$('#colRight > #rightRoadDirections').addClass('closedblock');
					
			
			//show the product detail map control on the right if the product has a location map link
			//on the contact us tab.
			var liMapObj = $('#colRight > #detailedproductInfo > .detailedproductTxt > .locationMapLink');
			if(liMapObj.length!=0)
			{
				$('#colRight > #detailedproductMapLink').addClass('open');
			}
			
			
			//we can use this in a few places, so set it now
			var liProductDetailObj = $('#colCenter > #colCenterProd > #colCenterProdIntro > #description');	
			
			//put in the telephone number into the product detail content block by using the telephone number
			//in the contact info.
			var liProdTelObj = $('#colRight > #detailedproductInfo > .detailedproductTxt > p > .contactinfoTel');
			
			if(liProdTelObj.length!=0)
			{
				if(liProductDetailObj.length!=0)
				{
					strTelephoneNumber=String(liProdTelObj[0].innerHTML);
					strTelephoneNumber=strTelephoneNumber.split(":", 2);
					strTelephoneNumber=strTelephoneNumber[1];
					
					liProductDetailObj[0].innerHTML=liProductDetailObj[0].innerHTML + "<div class=\"productTel\">please call now on " + strTelephoneNumber + "</div>";
				}
			}
		
			//hide the search from product individual pages
			if(liProductDetailObj.length!=0)
			{
				//hide the search box if we can find it
				var liProdSearchBoxObj=$('#colRight > #refineBy');
				if(liProdSearchBoxObj.length!=0)
				{
					$(liProdSearchBoxObj[0]).addClass('hide');
				}
			}	
			
			
			//sort out the search by map link
			//if the page has a search box, then grab the link from there, else use the general one.
			var liSearchByMapObj=$('#colRight > #refineBy > div > #SearchByMap');
			if(liSearchByMapObj.length!=0)
			{
				strSearchByMapHref=liSearchByMapObj.attr("href");
				
				var liSearchMapBoxObj=$('#colRight > #detailedMapLink > a');
				if(liSearchMapBoxObj.length!=0)
				{
					$(liSearchMapBoxObj).attr("href", strSearchByMapHref);
					var liSearchMapBoxH2Obj=$('#colRight > #detailedMapLink > a > h2');
					liSearchMapBoxH2Obj[0].innerHTML="search by map";
					//liSearchMapBoxObj.attr("href",strSearchByMapHref);
				}
			}
			
			//change the Return to Listings link to be the back image and move the back button into the 
			//product detail content block
			var bFromSearchResults="False";
			var liProdReturnToListingsObj=$('#colCenter > #colCenterProd > #colCenterProdIntro > #colCenterProdImg > .listingsLink > a');
			if(liProdReturnToListingsObj.length!=0)
			{
				//basically the session back link is buggy and doesn't clear, if you do a search, then go to
				//a product another way, e.g. via Top Events.
				//this "fixes" it so that if the user hasn't come via search results page, then hide the title
				//as this product may not be in that group the search box on the right says.
				//of course this doesn't give a title of any kind, but at least its not confusing...much. ;-)
				strBackLink=document.referrer;
				if(strBackLink.indexOf("searchresults")>-1)
				{
					bFromSearchResults="True";					
					liProdReturnToListingsObj[0].innerHTML="<img src=\"/images/button_back.gif\" alt=\"Back\" />";
				}
				else
				{
					liProdReturnToListingsObj[0].innerHTML="";
				}
				//change the Return to Listings text to be the back button
				
				
				if(liProductDetailObj.length!=0)
				{
					//get the parent (so not just the a tag, but the listing div)
					var liListingParentObj=liProdReturnToListingsObj.parent();
					var strBackButton=liListingParentObj[0].innerHTML;
					liProductDetailObj[0].innerHTML=liProductDetailObj[0].innerHTML + strBackButton;
					
					//hide the previous back button
					$(liProdReturnToListingsObj[0]).addClass('hide');
				}
			}
			
			
			//now to move the book online button into the product detail content block
			var liProdBookOnlineObj=$('#colCenter > #colCenterProd > #colCenterProdIntro > #colCenterProdImg > .onlineBookingButton');
			if(liProdBookOnlineObj.length!=0)
			{
				//unfortunately IE 6 seems to lose the external tag when moved, so it no longer opens in a new 
				//window so this had to be added
				var liProdBookOnlineLinkObj =$('#colCenter > #colCenterProd > #colCenterProdIntro > #colCenterProdImg > .onlineBookingButton > a');
				if(liProdBookOnlineLinkObj.length!=0)
				{
					$(liProdBookOnlineLinkObj[0]).attr("target", "_blank");
				}
				
				var strBookOnlineButton=liProdBookOnlineObj[0].innerHTML;
				liProductDetailObj[0].innerHTML=liProductDetailObj[0].innerHTML + strBookOnlineButton;
				$(liProdBookOnlineObj[0]).addClass('hide');
			}


			//close the homepage tick boxes on the search form
			iInputCounter=-1;
			var liSearchFormObj = $('.home > #wrapper > #insidewrapper > #colsWrapper > #innerColsWrapper > #colRight > .refine-srcwts_d > div > #refineBySearchForm > #refineByDefList > dt > input.checkbox');
			if(liSearchFormObj.length!=0)
			{
				while(iInputCounter<liSearchFormObj.length-1)
				{
					iInputCounter=iInputCounter+1;
					liSearchFormObj[iInputCounter].checked=false;
					
					//we also have to hide the containing block
					var liSearchFormDDBlockObj = $('.home > #wrapper > #insidewrapper > #colsWrapper > #innerColsWrapper > #colRight > .refine-srcwts_d > div > #refineBySearchForm > #refineByDefList > dd.' + liSearchFormObj[iInputCounter].name);
					if(liSearchFormDDBlockObj.length!=0)
					{
						liSearchFormDDBlockObj.css("display", "none");
					}
				}
			}				
			
			//use the function to change the page titles headers using the categories searched for
			//alert(bFromSearchResults + ":" + liProductDetailObj.length);
			if(bFromSearchResults=="True" || liProductDetailObj.length==0)
			{
				funcBuildResultTitle ("dartmoor ", "dartmoor accommodation", ".where-to-stay", ".SRCWTS_D", "category", "True");
				funcBuildResultTitle ("dartmoor ", "dartmoor food & drink", ".what-to-do", ".SRCEAT_D", "category","False");
				funcBuildResultTitle ("dartmoor ", "dartmoor shopping", ".what-to-do", ".SRCRETA_D", "category", "False");
				funcBuildResultTitle ("dartmoor ", "dartmoor attractions", ".what-to-do", ".SRCATTR_D", "category", "False");
				funcBuildResultTitle ("dartmoor ", "dartmoor activities", ".what-to-do", ".SRCACTI_D", "category", "False");
				funcBuildResultTitle ("dartmoor ", "dartmoor events", ".what-to-do", ".SRCWO_D", "category", "False");
			}
			
			//funcBuildResultTitle ("dartmoor events", ".what-to-do", ".SRCWO_D", "category");
			
			
			//if we are on the product detail page (e.g. see right_maplink.asp and road directions on the page
			//and we have road directions in the main content, then show this link and chuck the info in the
			//right box
			var liRightRoadDirections = $('#insidewrapper > #colsWrapper> #innerColsWrapper > #colRight > #rightRoadDirections > .roadDirectionsDetails');
			if(liRightRoadDirections.length!=0)
			{
				var liCentreRoadDirections = $('#insidewrapper > #colsWrapper> #innerColsWrapper > #colCenter > #colCenterProd > #colCenterData > #roadDirections');
				if(liCentreRoadDirections.length!=0)
				{
					liRightRoadDirections[0].innerHTML=liCentreRoadDirections[0].innerHTML;		
					$(liCentreRoadDirections[0]).addClass('hide');
				}
				else
				{
					liRightRoadDirections[0].innerHTML="Please telephone for directions";		
				}
			}
			
			//change the title of "dates" in the product detail to say "opening times"
			var liOpeningTimesTitle = $('#colCenterProdIntro > #openingTimes > h2');
			if(liOpeningTimesTitle.length!=0)
			{
				liOpeningTimesTitle[0].innerHTML="opening times";
			}
			
			strOpeningTitle="";
			strStartDate="";
			strEndDate="";
			//sort out the opening times to be in the format of [name], [date from] to [date to]
			var liOpeningTimes = $('#colCenterProdIntro > #opening > p');
			if(liOpeningTimes.length!=0)
			{
				strTempText=liOpeningTimes[0].innerHTML;
				
				//if the content includes a bold link, then this is an opening.
				//otherwise it includes the general comments.
				iEndBold=strTempText.toLowerCase().indexOf("</b>");
				if(iEndBold>=0)
				{
					strOpeningTitle=String(strTempText).substring(iEndBold+8,String(strTempText).length);
				}
				else
				{
					strOpeningTitle=String(strTempText);
				}
			}
			var liOpeningDates = $('#colCenterProdIntro > #dates > #datesTable > tbody > tr > td');
			if(liOpeningDates.length!=0)
			{
				strStartDate=liOpeningDates[0].innerHTML;
				if(liOpeningDates.length>1)
				{
					strEndDate=liOpeningDates[1].innerHTML;
				}
			}
			if(strOpeningTitle!='')
			{
				liOpeningTimes[0].innerHTML=strOpeningTitle;
				if(strStartDate!='')
				{
					liOpeningTimes[0].innerHTML=liOpeningTimes[0].innerHTML + ", " + strStartDate;
				}
				
				if(strEndDate!='')
				{
					liOpeningTimes[0].innerHTML=liOpeningTimes[0].innerHTML + " - " + strEndDate;
				}
				var liOpeningDates = $('#colCenterProdIntro > #dates');
				if(liOpeningDates.length!=0)
				{
					liOpeningDates[0].innerHTML="";
				}
			}
			
			
			//swap the no image results for the colour coordinated images
			//however, there isn't really an easy way to check what section we are in, thus knowing what
			//colour image to use.
			//so we replace the no image with a span, set width and height, with the background colour
			//being done in css.
			
			//innerHTML unknown errors with IE when trying to insert a div into an href tag due to
			//W3C compliancy so we have to use a span.
			
			var liResultImages = $('#colCenterIntro > .searchResults > li > .images > .thumbnail > a');
			if(liResultImages.length!=0)
			{
				iMyCounter=0;
				while(iMyCounter < liResultImages.length)
				{
					strTempText=String(liResultImages[iMyCounter].innerHTML);
					if(strTempText.indexOf("noimage")>-1)
					{
						//liResultImages[iMyCounter].innerHTML="<div class=\"searchResultsMissingImage\">&nbsp;</div>";
						//liResultImages[iMyCounter].innerHTML="<div>hello</div>";
						liResultImages[iMyCounter].innerHTML="<span class=\"searchResultsMissingImage\"></span>";
					}
					iMyCounter++;
				}
			}		
			
			//do that for product detail as well
			var liProductDetailImage = $('#colCenterProd > #colCenterProdIntro > #colCenterProdImg');
			if(liProductDetailImage.length!=0)
			{
				iMyCounter=0;
				while(iMyCounter < liProductDetailImage.length)
				{
					strTempText=String(liProductDetailImage[iMyCounter].innerHTML);
					if(strTempText.indexOf("noimage")>-1)
					{
						liProductDetailImage[iMyCounter].innerHTML="<span class=\"searchResultsMissingImage\"></span>";
					}
					iMyCounter++;
				}
			}
						
			//due to the small amount of space that email and website addresses have on the right of product 
			//detail, and bronze / simple products not having a link, attempt to break up the email and 
			//website address so it can at least all be read
			var iMaxLength=18;
			var liProductInfoEmail=$('#colRight > #detailedproductInfo > .detailedproductTxt > p > .contactinfoEmailNoLink');
			if(liProductInfoEmail.length!=0)
			{
				liProductInfoEmailBlocks=String(liProductInfoEmail[0].innerHTML);
				liProductInfoEmailBlocks=liProductInfoEmailBlocks.split("-");
				
				iMyCounter=0;
				strEmailAddress="";
				while(iMyCounter < (liProductInfoEmailBlocks.length))
				{
					liProductInfoEmailBlockPart=liProductInfoEmailBlocks[iMyCounter];
									
					if(iMyCounter!=0)
					{
						strEmailAddress=strEmailAddress + "-";
					}
					
					if(liProductInfoEmailBlockPart.length>iMaxLength)
					{
						strLeftPart=String(liProductInfoEmailBlockPart).substring(0,iMaxLength+3);
						strEmailAddress=strEmailAddress + strLeftPart + "<br />" + String(liProductInfoEmailBlockPart).substring(String(strLeftPart).length,String(liProductInfoEmailBlockPart).length)
					}
					else
					{
						strEmailAddress=strEmailAddress + String(liProductInfoEmailBlockPart);
					}
					iMyCounter++;
				}
				liProductInfoEmail[0].innerHTML=strEmailAddress +"<br /><br />";
			}
			
			var liProductInfoWeb=$('#colRight > #detailedproductInfo > .detailedproductTxt > p > .contactinfoWebNoLink');
			if(liProductInfoWeb.length!=0)
			{
				liProductInfoWebBlocks=String(liProductInfoWeb[0].innerHTML);
				liProductInfoWebBlocks=liProductInfoWebBlocks.split("-");
				
				iMyCounter=0;
				strWebAddress="";
				while(iMyCounter < (liProductInfoWebBlocks.length))
				{
					liProductInfoWebBlockPart=liProductInfoWebBlocks[iMyCounter];
					
					if(iMyCounter!=0)
					{
						strWebAddress=strWebAddress + "-";
					}
					
					if(liProductInfoWebBlockPart.length>iMaxLength)
					{
						strLeftPart=String(liProductInfoWebBlockPart).substring(0,iMaxLength+3);						
						strWebAddress=strWebAddress + strLeftPart + "<br />" + String(liProductInfoWebBlockPart).substring(String(strLeftPart).length,String(liProductInfoWebBlockPart).length)
					}
					else
					{
						strWebAddress=strWebAddress + String(liProductInfoWebBlockPart);
					}
					iMyCounter++;
				}
				liProductInfoWeb[0].innerHTML=strWebAddress +"<br />";
			}
			
			//when grading and awards doesn't have an image, the title shows, but nothing below due to
			//dartmoor not wanting text shown alongside and wp doesn't realise this.
			//only show the text if no images are found at all just to not look broken
			var liProductGrading=$('#colCenter > #colCenterProd > #colCenterProdIntro > #grading');
			if(liProductGrading.length!=0)
			{
				var liProductGradingImage=$('#colCenter > #colCenterProd > #colCenterProdIntro > #grading > img');
				if(liProductGradingImage.length==0)
				{
					//show the text
					$('#colCenter > #colCenterProd > #colCenterProdIntro > #grading > .grading').addClass('showblock');
				}
			}
			
			var liProductAwards=$('#colCenter > #colCenterProd > #colCenterProdIntro > #awards');
			if(liProductAwards.length!=0)
			{
				var liProductGradingImage=$('#colCenter > #colCenterProd > #colCenterProdIntro > #awards > ul > li > .awardimage');
				if(liProductGradingImage.length==0)
				{
					//show the text
					$('#colCenter > #colCenterProd > #colCenterProdIntro > #awards > ul > li > .award').addClass('showblock');
				}
			}
		}
	);
})();


function funcBuildResultTitle(strpAddToTitle, strpDefaultTitle, strpClass, strpFormClass, strpDropDownType, bpAddSEnding)
{
	//close all the tick boxes apart from the refine-category box
	iInputCounter=-1;
	var liSearchFormObj = $(strpClass + ' ' + strpFormClass + ' > #refineByDefList > dt > input.checkbox');
	
	if(liSearchFormObj.length!=0)
	{
		while(iInputCounter<liSearchFormObj.length-1)
		{
			iInputCounter=iInputCounter+1;
			if(liSearchFormObj[iInputCounter].name!='refine-category' && liSearchFormObj[iInputCounter].name!='refine-date')
			{
				liSearchFormObj[iInputCounter].checked=false;
			}
		}
	}
	
	
	
	var strOutputTitle=strpDefaultTitle;
	var liSearchFormObj = $(strpClass + ' ' + strpFormClass + ' > #refineByDefList > dt.refine-' + strpDropDownType + ' > #refine-' + strpDropDownType);
	
	if(liSearchFormObj.length!=0)
	{	
		if(liSearchFormObj[0].checked==true)
		{
			// yes the item is opened.
				
			//now to find out what the drop down list is set as
			var liSearchSelectObj = $(strpFormClass + ' > #refineByDefList > dd.refine-' + strpDropDownType + ' > .row > .field > #src_' + strpDropDownType + ' option:selected');
			if(liSearchSelectObj.length!=0)
			{
				
				var selectedAttr=String(liSearchSelectObj[0].text);
				
				if(selectedAttr!="-- Any --")
				{
					var strTypeDescription=selectedAttr.toLowerCase();
					//alert("right string: " + String(strTypeDescription).substring(String(strTypeDescription).length,String(strTypeDescription).length-1));
					if(String(strTypeDescription).substring(String(strTypeDescription).length,String(strTypeDescription).length-1)!="s" && bpAddSEnding=="True")
					{
						strTypeDescription=strTypeDescription+"s";
					}
					
					//only change the title if we are on a search results page or a product detail page
					var liIsSearchResultsObj = $('#colCenter > #colCenterIntro > .searchResults');
					var liIsProductDetailObj = $('#colCenter > #colCenterProd > #colCenterProdIntro > #description');
					
					//only do this on search results and product detail pages
					if(liIsSearchResultsObj.length!=0 || liIsProductDetailObj.length!=0)
					{
						var liSectionHeaderObj = $('#colCenter > #colCenterIntro > h1');	
						if(liSectionHeaderObj.length!=0)
						{
							liSectionHeaderObj[0].innerHTML="dartmoor " + strTypeDescription;
						}
						else
						{
							strOutputTitle=strpAddToTitle + strTypeDescription;
						}
					}
				}
			}
		}
	
		var liSectionProductHeaderObj = $('#colCenter > #colCenterProd > #colCenterProdIntro');	
		if(liSectionProductHeaderObj.length!=0)
		{
			liSectionProductHeaderObj[0].innerHTML="<div class='productpagetitle'>" + strOutputTitle + "</div>" + liSectionProductHeaderObj[0].innerHTML;
		}
	}
};