
// created by: Geoff Pack, Feb 2008
// last modified: July 2010 - added election links

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function createDateNav() {
	// creates the previous/next links at the foot of the content area.
	
	// parse archiveNav list to get archive URLs:
	if (document.body.id != 'election') {
		if (document.getElementById('archiveList')) {
			var list = document.getElementById('archiveList').childNodes;
			var urlList = [];
			var n = 0;
			
			for (var i=0; i<list.length; i++) {
				if (list[i].nodeType == 1) {
					urlList[n] = list[i].firstChild.getAttribute('href');
					//alert(urlList[n]);
					n++;
				}
			}
			
			// find previous and next links:
			var previousPage = urlList[1]; // default values for home & archive pages
			var nextPage = '';
			
			for (var i=1; i<urlList.length; i++) {
				if (document.location == urlList[i]) {
					previousPage = urlList[i+1];
					nextPage = urlList[i-1];
					//urlInlist = true;
					break;
				}
			}
		
			// write previous, next links into page:
			if (previousPage) document.getElementById('previousPage').innerHTML = '<a href="'+ previousPage +'">Previous</a>';
			if (nextPage) document.getElementById('nextPage').innerHTML = '<a href="'+ nextPage +'">Next</a>';
		}
	}
}

function removeElectionLinks() {
	// removes 'Best of Election' links - if they are the child of a list item
	var links = document.getElementsByTagName('A');
	
	for (var i=0, j=links.length; i<j; i++) {
		var link = links[i];
		
		if (link && (link.href.indexOf('/bestof/election/') !=-1) && (link.parentNode.nodeName == 'LI')) {
			var li = link.parentNode;
			li.parentNode.removeChild(li);
		}
	}
}

function findElectionLinks() {
	// removes 'Best of Election' links - if they are the child of a list item - and adds link and overlay to story image
	var links = document.getElementsByTagName('A'); // only links in BestofABC

	for (var i=0, j=links.length; i<j; i++) {
		var link = links[i];
		
		if (link && (link.href.indexOf('/bestof/election/') !=-1) && (link.parentNode.nodeName == 'LI')) {
			var li = link.parentNode;
			var ul = li.parentNode;
			var div = ul.parentNode;
			ul.removeChild(li);

			// add image overlay to non-election pages
			if (document.body.id != 'election') {
				var featureImg = ul.parentNode.getElementsByTagName('IMG')[0];
				featureImg.style.backgroundImage = 'url('+featureImg.src+')'; // make the feature image its own background	
				featureImg.style.backgroundPosition = 'center center';			
				featureImg.style.backgroundRepeat = 'no-repeat';
				
				if (/story_small_image/i.test(div.className)) {	// 125 x 70
					featureImg.src = 'http://www.abc.net.au/bestof/styles/election_overlay_125x70.png'; // change image to election image					
					if (/MSIE 6/i.test(navigator.userAgent)) {
						// IE6 requires image sizes to be re-specified
						//alert('IE6 + small')
						featureImg.style.width = '125px';
						featureImg.style.height = '70px';
					}
				} else if (/story_med_image/i.test(div.className)) { // 310 x 174
					featureImg.src = 'http://www.abc.net.au/bestof/styles/election_overlay_310x174.png'; // change image to election image					
					if (/MSIE 6/i.test(navigator.userAgent)) {
						//alert('IE6 + med')
						featureImg.style.width = '310px';
						featureImg.style.height = '174px';
					}
				} else if (/story_big_image/i.test(div.className)) { // 440 x 220
					featureImg.src = 'http://www.abc.net.au/bestof/styles/election_overlay_440x220.png'; // change image to election image					
					if (/MSIE 6/i.test(navigator.userAgent)) {
						//alert('IE6 + big')
						featureImg.style.width = '440px';
						featureImg.style.height = '220px';
					}
				} else if (/story_huge_image/i.test(div.className)) { // 665 x 290
					featureImg.src = 'http://www.abc.net.au/bestof/styles/election_overlay_665x290.png'; // change image to election image					
					if (/MSIE 6/i.test(navigator.userAgent)) {
						//alert('IE6 + huge')
						featureImg.style.width = '665px';
						featureImg.style.height = '290px';
					}
				}
				
				if (featureImg.parentNode.nodeName == 'A') {
					featureImg.parentNode.href = "/bestof/election/"; // add link to election page
				} else {
					// wrap an anchor tag around the image!
					var anchor = document.createElement('A'); // create new anchor node
					anchor.href = '/bestof/election/';
					div.replaceChild(anchor, featureImg); // replace the image with the anchor
					anchor.appendChild(featureImg); // add the image back as a child of the anchor
				}
			}
		}
	}
}

addLoadEvent(createDateNav);
addLoadEvent(findElectionLinks);
