/* ********************************************************************************* */
//  Add social bookmark links to base of qualifying pages
//  Authored: Marty Green 09/02/06
/* ********************************************************************************* */

var excludeArr = new Array();	//Array of pages where bookmarks not to be written
var bookmarkArr = new Array();	//Array of bookmarking sites to use
var wbVal = "social";		//Variable name for WebTrends 'exit page' tracking
var helpURL = "http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/help/faq-socialbookmark.htm";		//link to Help page

//
//Array of bookmarking site items
//
//FORMAT:	([name*] , [base url*] , [action])
//
//		* = required
//
//NOTE:		If you add new items here you should also create accompanying styles in basestyles.css
//		The ID in the css should be the [name] of the item, in lower case.
//
bookmarkArr.push(new bookmarkItem('Delicious','http://collections.europarchive.org/tna/20080107210935/http://del.icio.us/post?url=','&amp;title='));
bookmarkArr.push(new bookmarkItem('Digg','http://collections.europarchive.org/tna/20080107210935/http://digg.com/submit?phase=2&amp;url=','&amp;title='));
bookmarkArr.push(new bookmarkItem('Facebook','http://collections.europarchive.org/tna/20080107210935/http://www.facebook.com/sharer.php?u='));
bookmarkArr.push(new bookmarkItem('Google','http://collections.europarchive.org/tna/20080107210935/http://www.google.com/bookmarks/mark?op=add&amp;bkmk=','&amp;title='));
bookmarkArr.push(new bookmarkItem('reddit','http://collections.europarchive.org/tna/20080107210935/http://reddit.com/submit?url=','&amp;title='));
bookmarkArr.push(new bookmarkItem('StumbleUpon','http://collections.europarchive.org/tna/20080107210935/http://www.stumbleupon.com/submit?url=','&amp;title='));
bookmarkArr.push(new bookmarkItem('Yahoo','http://collections.europarchive.org/tna/20080107210935/http://myweb.yahoo.com/myresults/bookmarklet?u=','&amp;t='));
//

//
//Array of URL patterns for pages where bookmarks are not to be written
//
//FORMAT:	([URL pattern*])
//
//		* = required
//
//NOTE:		If the pattern listed is found in the page URL bookmark links will
//		not be written.
//
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/documentsonline/download.asp");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/documentsonline/customer-receipt.asp");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/digitalexpress/receipt.aspx");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/digitalexpress/printreceipt.aspx");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/digitalexpress/basketstep2.aspx");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/digitalexpress/basketstep3.aspx");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/digitalexpress/receiptfailure.aspx");
excludeArr.push("http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/filestore");


/* ********************************************************************************* */
//  Style for TNA nav based on Project Seven PopMenu Magic Style Sheet
//  www.projectseven.com
//  Authored: Russ Back (contractor) 09/02/06
//	- functionality to write social bookmarks added to page by MGreen (19/09/07)
/* ********************************************************************************* */

// TNA menu options
var tnaXOff = 15; // submenu offset on the x axis
var tnaYOff = 25; // submenu offset on the y axis
var tnaAnim = 0; // animate submenu (0/1)
var tnaAnimSpeed = 30; // submenu animation speed
var tnaOnDelay = 60; // submenu activation delay
var tnaOffDelay = 300; // submenu deactivation delay

// detect browser (required to show/hide select/flash elements in win/ie mac/all
var tnaAgt=navigator.userAgent.toLowerCase();
var tnaIsIE = ((tnaAgt.indexOf("msie")!=-1) && (tnaAgt.indexOf("opera")==-1));

/* ********************************************************************************* */


// init menu (this method avoids the need for body onload on each page)
window.onload=function(){

	//initialise menu

	P7_initPM(1,12,tnaAnim,tnaYOff,tnaXOff,tnaOnDelay);

	
	//test whether social bookmarks should be added to this page
	if(testSocialBookmarks(excludeArr)) {
		writeSocialBookmarks(bookmarkArr,wbVal, helpURL);
	}

}

// TNA function: IE needs this to get the absolute position of a nested element
function getOffsetLeft(el){
 var ol=el.offsetLeft;
 while((el=el.offsetParent)!=null){ol+=el.offsetLeft;}
 return ol;
}

// standard project seven script
function P7_setPM(){ //v533 alpha by PVII-www.projectseven.com
 var i,d='',h="<sty"+"le type=\"text/css\">",tA=navigator.userAgent.toLowerCase();
 if(window.opera){if(tA.indexOf("opera 5")>-1 || tA.indexOf("opera 6")>-1){return;}}
 if(document.getElementById){for(i=1;i<20;i++){d+='ul ';
 h+="\n#p7PMnav "+d+"{position:absolute;left:-9000px;}";}
 document.write(h+"\n<"+"/sty"+"le>");}}P7_setPM();

// TNA customisation: see inline comments;
function P7_initPM(){ //v538 alpha by PVII-www.projectseven.com
 var i,g,tD,tA,tU,pp,lvl,tn=navigator.userAgent.toLowerCase();
 if(window.opera){if(tn.indexOf("opera 5")>-1||tn.indexOf("opera 6")>-1){
 return;}}else if(!document.getElementById){return;}p7PMp=arguments;
 p7PMct= new Array;tD=document.getElementById('p7PMnav');if(tD){
 tA=tD.getElementsByTagName('A');for(i=0;i<tA.length;i++){tA[i].p7PMcl=p7PMct.length;
 p7PMct[p7PMct.length]=tA[i];g=tA[i].parentNode.getElementsByTagName("UL");
 tA[i].p7PMsub=(g)?g[0]:false;
 ev=tA[i].getAttribute("onmouseover");
 if(!ev||ev=='undefined'){
 tA[i].onmouseover=function(){P7_PMtrig(this);};}ev=tA[i].getAttribute("onfocus");
 if(!ev||ev=='undefined'){tA[i].onfocus=function(){P7_PMtrig(this);};}if(tA[i].p7PMsub){pp=tA[i].parentNode;
 lvl=0;while(pp){if(pp.tagName&&pp.tagName=="UL"){lvl++;}pp=pp.parentNode;}
 tA[i].p7PMlv=lvl;}}tD.onmouseout=P7_PMclose;
 // TNA: remove P7_PMopen() function as the functionality (current menu marker) does not work on much of the TNA site
 // P7_PMopen();
 }
}

// TNA customisation: see inline comments
function P7_PMtrig(a){ //v530 alpha by PVII-www.projectseven.com
 var b,t;if(document.p7PMt){clearTimeout(document.p7PMt);}document.p7PMa=1;
 // replaced hard-coded delay val with tnaOnDelay param
 b=(a.p7PMsub)?'P7_PMshow(':'P7_PMtg(';t='document.p7PMt=setTimeout("'+b+a.p7PMcl+')",tnaOnDelay)';
 eval (t);
}

// TNA customisation: amended where commented to facilitate central nav and max-right positiong of submenus (see inline comments)
function P7_PMshow(a,bp){ //v1.0 by PVII-www.projectseven.com
 var u,lv,oft,ofr,uw,uh,pp,aw,ah,adj,mR,mT,wW=0,wH,w1,w2,w3,sct,pw,lc,pwv,xx=0,yy=0,wP=true;
 var iem=(navigator.appVersion.indexOf("MSIE 5")>-1)?true:false,dce=document.documentElement,dby=document.body;document.p7PMa=1;
 if(!bp){P7_PMtg(a);}u=p7PMct[a].p7PMsub;if(u.p7pmax&&u.p7pmax==1){return;}u.p7pmax=1;lv=(p7PMp[0]==1&&p7PMct[a].p7PMlv==1)?true:false;
 p7PMct[a].className=p7PMct[a].className.replace("p7PMtrg","p7PMon");oft=parseInt(p7PMp[3]);ofr=parseInt(p7PMp[4]);
 uw=u.offsetWidth;uh=u.offsetHeight;pp=p7PMct[a];aw=pp.offsetWidth;ah=pp.offsetHeight;while(pp){xx+=(pp.offsetLeft)?pp.offsetLeft:0;
 yy+=(pp.offsetTop)?pp.offsetTop:0;if(window.opera||navigator.userAgent.indexOf("Safari")>-1){
 if(p7PMct[a].p7PMlv!=1&&pp.nodeName=="BODY"){yy-=(pp.offsetTop)?pp.offsetTop:0;}}pp=pp.offsetParent;}
 if(!window.opera&&iem&&navigator.userAgent.indexOf("Mac")>-1){yy+=parseInt(dby.currentStyle.marginTop);}adj=parseInt((aw*ofr)/100);mR=(lv)?0:aw-adj;
 adj=parseInt((ah*oft)/100);mT=(lv)?0:(ah-adj)*-1;w3=dby.parentNode.scrollLeft;if(!w3){w3=dby.scrollLeft;}w3=(w3)?w3:0;
 if(dce&&dce.clientWidth){wW=dce.clientWidth+w3;}else if(dby){wW=dby.clientWidth+w3;}if(!wW){wW=0;wP=false;}wH=window.innerHeight;
 // TNA CODE get the position of the centred TNA menu & decrease the available window width measurement by the size of the menu's left offset
 var menu_left_boundary=document.getElementById("p7PMnav").offsetLeft;
 wW-=menu_left_boundary;
 // END TNA CODE
 if(!wH){wH=dce.clientHeight;if(!wH||wH<=0){wH=dby.clientHeight;}}sct=dby.parentNode.scrollTop;if(!sct){sct=dby.scrollTop;if(!sct){
 sct=window.scrollY?window.scrollY:0;}}pw=xx+mR+uw;if(pw>wW&&wP){mR=uw*-1;mR+=10;if(lv){mR=(wW-xx)-uw;}}lc=xx+mR;if(lc<0){mR=xx*-1;}
 // TNA CODE define the right-hand boundary of the menu and add this figure to the script's right-margin property for drop-downs if this is a level 2 menu
 var menu_pos_offset=wW-getOffsetLeft(u.parentNode)-aw+1;
 if(mR&&u.parentNode.parentNode.getAttribute("id"))mR-=menu_pos_offset;
 // END TNA CODE
 pw=yy+uh+ah+mT-sct;pwv=wH-pw;if(pwv<0){mT+=pwv;}u.style.marginLeft=mR+'px';u.style.marginTop=mT+'px';
 if(p7PMp[2]==1){if(!iem){P7_PMtnaAnim(a,20);}}u.className="p7PMshow";
 // TNA added hide select menu call:
 P7_hideSelect(1);
 }

// standard project seven script
function P7_PMhide(u){ //v532 alpha PVII-www.projectseven.com
 var i,tt,ua;u.p7pmax=0;
 u.className="p7PMhide";ua=u.parentNode.firstChild;
 ua.className=ua.className.replace("p7PMon","p7PMtrg");
}

// standard project seven script
function P7_PMtg(a,b){ //v532 alpha by PVII-www.projectseven.com
 var i,u,tA,tU,pp;tA=p7PMct[a];pp=tA.parentNode;while(pp){if(pp.tagName=="UL"){break;}
 pp=pp.parentNode;}if(pp){tU=pp.getElementsByTagName("UL");for(i=tU.length-1;i>-1;i--){
 if(b!=1&&tA.p7PMsub==tU[i]){continue;}else{P7_PMhide(tU[i]);}}}
}

// standard project seven script
function P7_PMclose(evt){ //v532 alpha by PVII-www.projectseven.com
 var pp,st,tS,m=true;evt=(evt)?evt:((event)?event:null);st=document.p7PMa;
 if(st!=-1){if(evt){tS=(evt.relatedTarget)?evt.relatedTarget:evt.toElement;
 if(tS){pp=tS.parentNode;while(pp){if(pp&&pp.id&&pp.id=="p7PMnav"){m=false;
 document.p7PMa=1;break;}pp=pp.parentNode;}}if(m){document.p7PMa=-1;
 if(document.p7PMt){clearTimeout(document.p7PMt);}
 document.p7PMt=setTimeout("P7_PMclr()",tnaOffDelay);}}}
}

// TNA customisation: see inline comments
function P7_PMclr(){ //v532 alpha by PVII-www.projectseven.com
 var i,tU,tUU;document.p7PMa=-1;tU=document.getElementById('p7PMnav');
 if(tU){tUU=tU.getElementsByTagName("UL");if(tUU){
 for(i=tUU.length-1;i>-1;i--){P7_PMhide(tUU[i]);}}}
 // TNA added select menu reset:
 P7_hideSelect(0);
}

// TNA customisation: see inline comments
function P7_PMtnaAnim(a,st){ //v532 alpha by PVII-www.projectseven.com
 // replace hard-coded animation speed value with tnaAnimSpeed param (referenced below by the internal var 'inc')
 var g=p7PMct[a].p7PMsub,sp=20,inc=tnaAnimSpeed;st=(st>=100)?100:st;g.style.fontSize=st+"%";
 if(st<100){st+=inc;setTimeout("P7_PMtnaAnim("+a+","+st+")",sp);}
}

// TNA customisation: see inline comments
function P7_hideSelect(a){ //v1.0 by PVII
 var e,f,g=document.getElementsByTagName("select"),h=(a==1)?"hidden":"visible";
 // TNA added tnaIsIE var to ensure that <select> hidden only for IE
 if(document.getElementsByTagName&&tnaIsIE&&g){for(var i=0;i<g.length;i++){g[i].style.visibility=h;}}
 }
 
/* ***************************************************************** */
// WRITE SOCIAL BOOK-MARKING LINKS TO PAGES
// Written by: M Green, (10/10/07)
/* ***************************************************************** */
 
function testSocialBookmarks(excludeArr) {
//
//Check whether social bookmark links should be added to the current page
//

	var thisURL = document.location.href;			//URL of the current page
	
	// if pattern exists in current URL then return. Do not write links
	for (var i=0; i<excludeArr.length; i++) {
	
		if (thisURL.indexOf(excludeArr[i]) > -1) {
			return false;
		}
	}

	return true;
}

function writeSocialBookmarks(bookmarkArr,wbVal, helpURL) {
//
//Append set of social bookmarking links at the end of the document.
//

	var thisURL = document.location.href;			//URL of the current page
	var thisTitle = encodeURIComponent(document.title);	//Title of current page

	//Process URL to remove webtrends specific tracking to prevent duplicates in the stats.
	thisURL = stripQS(thisURL,"source");
	thisURL = stripQS(thisURL,"homepage");
	thisURL = stripQS(thisURL,"wt.mc_id");
	//

	// Encode the URL for portability
	thisURL = encodeURIComponent(thisURL);
	//
	
	//Create item for MyPage bookmark option

	myBookmark = new bookmarkItem ('myPage', 'http://collections.europarchive.org/tna/20080107210935/http://www.nationalarchives.gov.uk/mypage/bookmarks.aspx?url='  , '&amp;name=');
	//

	//Create container on page for bookmark content and populate with new content;
	var bookmark = document.createElement('div');
	bookmark.setAttribute('id', 'social-bookmarks');
	bookmark.innerHTML = createBookmarkList(bookmarkArr, myBookmark, thisURL, thisTitle, wbVal, helpURL);
	//

	//Append the new content to the end of the page.
	document.body.appendChild(bookmark);	
	//
}

function bookmarkItem(name, url, action) {
//
//Create new bookmark object
//

	this.name = name;
	this.url = url;
	this.action = action;
}

function createBookmarkList(contentArr, myBookmark, target, title, trackingVar, helpURL) {
//
//Create HTML for list of bookmark links
//

	var content = "\n\t<h3>Bookmark with:</h3>\n\t<ul>\n";

	for (var i=0; i<contentArr.length; i++) {

		content+="\t\t<li><a id=\"" + contentArr[i].name.toLowerCase() + "\" title=\"Bookmark this page with " + contentArr[i].name + "\" href=\"" + contentArr[i].url + target;
		if (contentArr[i].action) {
			content+=contentArr[i].action + title;
		}
		if (trackingVar) {
			content+= "&amp;" + trackingVar + "=" + contentArr[i].name.toLowerCase();
		}
		content+="\">" + contentArr[i].name + "</a></li>\n";
	}
	content+= "\t</ul>\n";

	// add TNA mypage bookmark.
	content+="\t<div>\n\n";


	// write link to add to 'my bookmarks' (Currently disabled)
//	content+="\t\t<a id=\""+ myBookmark.name.toLowerCase() + "\" title=\"Add to MyPage bookmarks\" href=\"" + myBookmark.url + target;
//	content+="&amp;name=" + title + "&amp;ReturnUrl=" + target + "\">MyPage Bookmarks</a>\n\n";


	content+="\t\t<a href=\"" + helpURL + "\">(what are these links?)</a>\n\t</div>\n";
	return content;
	//

}

function stripQS(tString, pattern) {
//
//Removes entire querystring from a URL if a positive match for the supplied pattern is found
//
	if (tString.indexOf(pattern) >=0 ) {
		tString = tString.substring(0,tString.indexOf('?'));
	}
	
	return tString;
}

/* ***************************************************************** */

