if (window.adgroupid == undefined) {//Needed for Adtech
	window.adgroupid = Math.round(Math.random() * 1000);
}

//Adtech integration
var adtech = {
	timestamp: new Date().getTime(),
	liveNetworkID: "987.1",
	testNetworkID: "72.49",
	liveAdTagData: {"LB" : "2241372|0|277", "TA" : "2241371|0|750", "MA" : "2241373|0|751", "L2": "2366222|0|1358"},
	testAdTagData: {"LB" : "2180432|0|277", "TA" : "2180431|0|750", "MA" : "2180433|0|751"},
	liveAdTagPrefix: "WLP",
	testAdTagPrefix: "LP",
	networkID: this.liveNetworkID, // test = 72.49, live = 987.1
	adTagData: this.liveAdTagData,
	adTagPrefix: this.liveAdTagPrefix,
	contentID: $("meta[name='content_id']").attr("content"), 
	keywords:  $("meta[name='categories']").attr("content"), 
	hostname : "www.which.co.uk",
	setHostname : function(_hostname) {
		if (_hostname!=undefined) 
			this.hostname = _hostname;
	},
	getHostname : function() {
		return this.hostname;
	},
	log : function() {
		if (window.console!=undefined) 
			try {
				window.console.log(arguments);
			} catch (err) {
			}
	},
	setModeLive: function() {
		this.networkID = this.liveNetworkID;
		this.adTagData = this.liveAdTagData;
		this.adTagPrefix = this.liveAdTagPrefix;
	},
	setModeTest: function() {
		this.networkID = this.testNetworkID;
		this.adTagData = this.testAdTagData;
		this.adTagPrefix = this.testAdTagPrefix;
	},
	getReviewData: function(data) {
		if (data.adclick==undefined) data.adclick=""; // temporary measure while new adtech panels introduced
		var reviewdata = { p: data.pcount, b: data.bcount, d: data.dcount, v: 0, u: "/reviews" };
		try {
			reviewdata = r[data.reviewid];
		} catch (err) { // in case CMS reviewdataset.js not published from CMS
		}	
		return reviewdata;
	},
	invokeURL: function(url) {
		if ((url!=undefined) && (url!='')) {
			var timg = new Image();
			timg.src = url;
		};
	},	
	renderCallback : function() {}, // optional function that is called on jquery do
	adTag : function(componentID, _isCompanion) {
		var isCompanion = (_isCompanion == undefined) ? true : _isCompanion;
		var groupid = (!isCompanion) ? Math.round(Math.random() * 1000) : window.adgroupid;
		var placementAlias = this.adTagPrefix + this.contentID + componentID; // For test use prefix "LP"
		var adtag = 'http://adserver.adtech.de/addyn|3.0|' + this.networkID + '|'
				+ this.adTagData[componentID]
				+ '|ADTECH'
				+ (isCompanion ? ';alias='+placementAlias : '')
				// strip whitespace then convert csv to +sv
				+ (this.keywords ? (';key=' + this.keywords.replace(/\s+/g, "").split(",").join("+")) : '')
				+ ';cookie=info;loc=100;target=_blank;grp='
				+ groupid+';misc='+this.timestamp;
		return adtag
	},
	cookieBind: function(cookieURL) { // on setting the adtech cookie, after a short pause, reload the page...
		$.ajax({
		   type: "GET",
		   url: cookieURL,
		   async: false,
		   dataType: "script",
		   complete: function(msg){
			 setTimeout('window.location.reload()', 350)
		   }
		});	
	},
	writeCookie: function(cookieName, lifetime, cookieValue) {
		var expires = "";
		if ((cookieValue==null)||(cookieValue=='remove'))
			expires = ";expiresDays=-1";
		else
			if (lifetime=='session')
				expires = ";expiressession=1";
			else
				expires = ";expiresDays=" + lifetime;
				
		this.cookieBind("http://adserver.adtech.de/bind?ckey1=" + cookieName + ";cvalue1=" + cookieValue + expires + ";adct=text/javascript");		
	},
	writeSessionCookie: function(cookieName, cookieValue) {
		this.writeCookie(cookieName, 'session', cookieValue);
	},	
	bindOfferCookie: function(offerCode) {
		this.writeCookie("user_offer", 90, offerCode);
	},
	bindSourceCookie: function(sourceCode) {
		this.writeCookie("user_source", 90, sourceCode);
	},
	bindMemberCookie: function(memberCode) {
		this.writeSessionCookie("member", memberCode); 
	},
	adTagWrite : function(componentID, isCompanion) {
		if (componentID == "LB") {
			document.write('<scr'+'ipt src="' + this.adTag(componentID, isCompanion) + '"></scri'+'pt>');
			document.write('<scr'+'ipt src="' + this.adTag("L2", isCompanion) + '"></scri'+'pt>');
			}
		else
			document.write('<scr'+'ipt src="' + this.adTag(componentID, isCompanion) + '"></scri'+'pt>');
		return;
	},
	renderTA : function(data) {
		var reviewdata = this.getReviewData(data);
		$("#promo-TA").setTemplateURL("http://"+this.getHostname()+"/assets/scripts/templates/lpta.tpl")
					  .setParam('rdata', reviewdata)
					  .processTemplate(data)
					  .find('a.adrecord').click(function() {
							adtech.invokeURL(data.adclick + this.href)
					  });
		if (tb_init!=undefined)	tb_init('div.a-grow a.thickbox');
		return this.renderCallback;
	},
	renderMA : function(data) {
		var reviewdata = this.getReviewData(data);
		$("#promo-MA").setTemplateURL("http://"+this.getHostname()+"/assets/scripts/templates/lpma.tpl")
					  .setParam('rdata', reviewdata)
					  .processTemplate(data)
					  .find('a.adrecord').click(function() {
							adtech.invokeURL(data.adclick + this.href)
					  });
		if (tb_init!=undefined)	tb_init('div.a-grow a.thickbox');
		return this.renderCallback;
	},
	renderLB : function(data) {
		var reviewdata = this.getReviewData(data);
		$("#promo-LB").setTemplateURL("http://"+this.getHostname()+"/assets/scripts/templates/lplb.tpl")
					  .setParam('rdata', reviewdata)
					  .processTemplate(data)
					  .hide()
					  .find('a.adrecord').click(function() {
							adtech.invokeURL(data.adclick + this.href)
					  });
					  
		return this.renderCallback;
	},
	beacon: function(page) {
		this.invokeURL("https://secserv.adtech.de/pcsale/3.0/987/0/0/0/BeaconId=4511;rettype=img;subnid=1;SalesValue=1;custom1="+page+" width='1' height'1'");
	},
	updateLB: function(adclick) {
		$("#WhichLightbox div.promo-detail a, #WhichLightbox p.pdf-download a").eq(1).each(function() {
			var href = $(this).attr("href");
			var linkStr = "link=";
			var linkStart = href.indexOf(linkStr);
			if (linkStart>=0)
				href=href.slice(linkStart + linkStr.length);
			$(this).attr("href", adclick + href)}
		)
	},
	init: function() {
		this.setModeLive();
	}
};

adtech.init();
var host="www.which.co.uk";
try {
	host = adtech.setHostname(document.location.host);
} catch (Err) {
}
//End Adtech integration

$(document).ready(function (){
	$(".promo-ba").hide();
	$("div[class^='js-promo'] a.thickbox").each(function() {
		var currentHref = this.href;
		var intTempIndexOf = currentHref.indexOf("#"); 
		if (intTempIndexOf != -1) {
			var contentId = currentHref.substr(intTempIndexOf + 1);
			currentHref = "#TB_inline?keepThis=true&amp;height=558&amp;width=736&amp;modal=true&amp;inlineId=" + contentId;
		}
		$(this).attr("href", currentHref);
		setElementsToHide(); //set troublesome elements such as <select>s and <object>s to be hidden when pop-up is launched.
	});
});