Adcorp = {
	hiliteThumbs:function(){
		var links = $(".home-stories-storyimg a");
		for(var x=0; x<links.length; x++){
			var wStr = links[x].childNodes[0].alt;
			var wUrl = links[x].href;
			$(links[x]).after('<div class="storyHilite"><a href="'+wUrl+'">'+wStr+'</a></div>');
		}		
		
		
		var rLinks = $(".relStory-storyimg a");
		for(var x=0; x<rLinks.length; x++){
			var wStr = rLinks[x].childNodes[0].alt;
			var wUrl = rLinks[x].href;
			$(rLinks[x]).after('<div class="relStoryHilite"><a href="'+wUrl+'">'+wStr+'</a></div>');
		}			
		
		
		/*var aLinks = $(".awardsImg a");
		for(var x=0; x<aLinks.length; x++){
			var wStr = aLinks[x].childNodes[0].alt;
			var wUrl = aLinks[x].href;
			$(aLinks[x]).after('<div class="awardsHilite"><a href="'+wUrl+'">'+wStr+'</a></div>');
		}*/			
		
		
		var aLinks = $(".awardsImg img");
		for(var x=0; x<aLinks.length; x++){
			var wStr = aLinks[x].alt;
			$(aLinks[x]).after('<div class="awardsHilite" title="'+wStr+'"></div>');
		}	
		
		
		var pLinks = $(".peopleImg img");
		for(var x=0; x<pLinks.length; x++){
			var wStr = pLinks[x].alt;
			$(pLinks[x]).after('<div class="peopleHilite" title="'+wStr+'"></div>');
		}			
		
		
		var cImgs = $(".contactdetailsImg img");
		for(var x=0; x<cImgs.length; x++){
			var wStr = cImgs[x].alt;
			$(cImgs[x]).after('<div class="contactHilite" title="'+wStr+'"></div>');
		}
	},
	pullquote:function(){
	// Check that the browser supports the methods used
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		var oElement, oPullquote, oPullquoteP, oQuoteContent, i, j, oPullquoteQ1, oPullquoteQ2;
	// Find all span elements with a class name of pullquote
		var arrElements = document.getElementsByTagName('span');
		var oRegExp = new RegExp("(^|\\s)pullquote(\\s|$)");
		for (i=0; i<arrElements.length; i++) {
	// Save the current element
			oElement = arrElements[i];
			if (oRegExp.test(oElement.className)) {
	// Create the blockquote and p elements
				oPullquote = document.createElement('blockquote');
				oPullquote.className = oElement.className
				oPullquoteP = document.createElement('p');
				
				oPullquoteQ1 = document.createElement('img');
				oPullquoteQ1.src = "/img/quotes-open.gif";
				oPullquoteQ1.alt = "";
				oPullquoteQ2 = document.createElement('img');
				oPullquoteQ2.src = "/img/quotes-close.gif";
				oPullquoteQ2.alt = "";
				//oPullquoteP.appendChild(oPullquoteQ1);
	// Insert the pullquote text
				for(j=0;j<oElement.childNodes.length;j++) {
					oPullquoteP.appendChild(oElement.childNodes[j].cloneNode(true));
				}
				oPullquoteP.appendChild(oPullquoteQ2);
				oPullquote.appendChild(oPullquoteP);
	// Insert the blockquote element before the span elements parent element
				//oElement.parentNode.parentNode.insertBefore(oPullquote,oElement.parentNode);
	// ctm: Tweaked Roger's original script to insert the pullquote parallel with the start of the text rather than the parent paragraph
				oElement.parentNode.insertBefore(oPullquote,oElement);
			}
		}
	},
	flyouts:function() {
		$(".flyoutDiv").remove();
		$(".flyoutTrigger").remove();
		
		var flyImgs = $("a.flyout img#rotateImg");
		for (var x=0; x<flyImgs.length; x++) {
			var iA = $(flyImgs[x]).attr("alt");
			var iX = $(flyImgs[x].parentNode.parentNode.parentNode).offset(); 	// location of parent div
			var iX2 = $(flyImgs[x]).offset(); 									// location of actual image
			var wH3 = $("#"+flyImgs[x].parentNode.parentNode.parentNode.id+" h3");
			
			var iW = $(flyImgs[x].parentNode.parentNode.parentNode).width();
			var iH = $(flyImgs[x].parentNode.parentNode.parentNode).height();
			
			var fDiv = document.createElement('div');
			var cX = $("#outerContainer").offset();
			
			var flyTarget = flyImgs[x].parentNode.href;
			
			fDiv.id = "flyout"+x;
			fDiv.className = "flyoutDiv";
			fDiv.style.position = "absolute";
			fDiv.style.left = (iX["left"]-cX["left"])+"px";
			fDiv.style.top = (iX2["top"]-cX["top"])+"px";
			fDiv.style.width = iW+"px";
			$(fDiv).append("<p>"+iA+"</p>");
			
			document.getElementById("outerContainer").appendChild(fDiv);
			
			var fTrigger = document.createElement('a');
			fTrigger.target = "_blank";
			fTrigger.id = "flyoutTrigger"+x;
			fTrigger.className = "flyoutTrigger";
			fTrigger.style.left = (iX["left"]-cX["left"])+"px";
			fTrigger.style.top = (iX["top"]-cX["top"])+"px";
			fTrigger.style.width = iW+"px";
			fTrigger.style.height = iH+"px";
			fTrigger.href = flyTarget;
			document.getElementById("outerContainer").appendChild(fTrigger);
			
			fTrigger.onmouseover = function() {
				Adcorp.flyoutSlide(this.id, 0, wH3);
			};
			fTrigger.onmouseout = function() {
				Adcorp.flyoutSlide(this.id, 1, wH3);
			};
		}
	},
	flyoutSlide:function(wId, wDir, wH){
		wId = "flyout"+wId.charAt(wId.length-1);
		if(wDir == 0) {
			$("#"+wId).slideDown("fast");
			$(wH).addClass("hi");
		} else if (wDir == 1) {
			$("#"+wId).slideUp("fast");
			$(wH).removeClass("hi");
		}
	},
	flyoutRotate:function(wBox){
		if(!wBox) {
			wBox = 0;
		}
		
		var boxes = [ // with thanks to Christian Heilmann for exposing the array shorthand to me, the 'average developer'. ctm.
			"Shop With Us|http://cellarandkitchen.adnams.co.uk|img/photos/front-shopwithus.jpg|Our immensely popular Cellar &amp; Kitchen stores have an amazing array of wines and kitchenware.",
			
			"Stay With Us|http://www.adnams.co.uk/hotels/|img/photos/front-staywithus.jpg|Join us at one of our award winning hotels &ndash; great wines, beers and loads of character.",
			
			"Drink With Us|http://www.beerfromthecoast.co.uk/index.html|img/photos/front-drinkwithus.jpg|Delicious ales, innovative beers and great wine."
		];
		
		var cBox = boxes[wBox].split("|");
		$("#promo-rotator h3 span").text(cBox[0]);
		$("#promo-rotator a").attr("href",cBox[1]);
		Adcorp.crossfader(cBox[2],cBox[3]);
		$("#promo-rotator img#rotateImg").attr("alt",cBox[3]);
		
		wBox++;
		if(wBox > boxes.length-1) {
			wBox = 0;
		}
		
		var wait = setTimeout("Adcorp.flyoutRotate("+wBox+")", 5000);
	},
	crossfader:function(wImg,wAlt){
		// causes a strange text-thickening effect on Mac/Firefox during animation, though not on all machines!
		// For Mac/Firefox, will do a simple image swap instead of the crossfade.
		
		var wB = BrowserDetect.browser;
		var wO = BrowserDetect.OS;
		
		if((wB === "Firefox") && (wO === "Mac")) {
			$("#promo-rotator img#rotateImg").attr("src",wImg).attr("alt",wAlt);
			Adcorp.flyouts();
		} else {
			var newImg = $("#promo-rotator img#rotateImg").before('<img id="tempImg" src="'+wImg+'" alt="'+wAlt+'" />').fadeOut(1000, function(){ Adcorp.flyouts(); });
			$("#promo-rotator img#tempImg").attr("id","rotateImg");
		}
	},
	setBlogImage:function(){
		$("img.blogImage:first").css("display", "inline").remove().insertBefore("h3.newsStoryTitle");
	},
	patchThings:function(){
		$("div#home-stories div.home-stories-entry:gt(3)").remove();
		$("div#newsStoryMain p a").addClass("stdLink");
		
		if((document.getElementById("ctl00_newsPanel")) || (document.getElementById("ctl00_lifePanel"))){
			$("#promo-rotator").remove();
			$(".flyoutTrigger").remove();
			$(".flyoutDiv").remove();
		}
	}
}






BrowserDetect.init();
/*
	the mighty mighty load event queue.
	adding load events in a responsible, sustainable, post-modern, non-ironic, ecologically sound way.
*/
Adtools.addLoadEvent(Adcorp.patchThings);
Adtools.addLoadEvent(Adcorp.hiliteThumbs);
Adtools.addLoadEvent(Adcorp.setBlogImage);
Adtools.addLoadEvent(Adcorp.flyoutRotate);
Adtools.addLoadEvent(Adcorp.pullquote);
Adtools.addLoadEvent(Adtools.externaliseLinks);