
// created by: Geoff Pack, Feb 2008
// modified May 2010 - replaced suckerfish drop-downs with js (to add a delay)
// modified June 2010 - added code for new radio tab (showRadioTab, showTempGauge)
// modified July 2010 - added code to find Best of Election links (findElectionLinks)
// modified August 2010 - added query string methods
// modified November 2010 - moved scroller functions to separate file and cleaned up

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function setLocation(location) {
	createCookie("local", location, 14);
}

// need to do this before onload event!
var abcLocal = readCookie("local");

function popIt(width, height) {
	if (width && height) {
		popup = window.open('', 'popup', 'width=' + width + ',height=' + height + ',,top=20,left=20,resizable=yes,status=no');
	 }
	else {
		popup = window.open('', 'popup', 'width=420,height=280,,top=20,left=20,resizable=yes,status=no');
	}
	popup.focus();
}

function getQueryVariable(qName) { 
	var pairs = window.location.search.substring(1).split("&");

	for (var i=0; i<pairs.length; i++) { 
		var pair = pairs[i].split("="); 
		if (pair[0] == qName) { 
			//alert('Query Variable ' + qName + ' = ' + pair[1]);
			return pair[1]; 
		}
	} 
	//alert('Query Variable ' + qName + ' not found');
	return null;
} 

var debug = false;

function logIt() {
	// logs messages to window.console or sends window.alert
	// usage: logIt('label', value, 'label2', value2, ....)
	var message = '';	
	if (debug) {
		for (var i=0; i<arguments.length; i+=2) {
			if (window.console) {
				console.info(arguments[i]+": %s", arguments[i+1]);
			} else {
				//message += arguments[i] + ": " + arguments[i+1] + "\n";
			}
		}
		//if (message != '') window.alert(message);
	}
}

// ------------------------------------------------------------------
// Show/Hide functions clean up later...

function show(id) {
	document.getElementById(id).style.display = 'block';
}
function hide(id) {
	document.getElementById(id).style.display = 'none';
}


// ------------------------------------------------------------------
// find and modify iView and election links

function findiViewLinks() {
	// converts furtherInfo links to iView into buttons next to the header
	var links = document.getElementsByTagName('A');

	for (var i=0, j=links.length; i<j; i++) {
		var link = links[i];
		
		if (link.href.indexOf('/iview/#/') !=-1) {
			var li = link.parentNode;
			var ul = li.parentNode;
			var div = ul.parentNode;
			
			// write link before heading			
			var newNode = link.cloneNode(true);
			div.insertBefore(newNode,div.firstChild);

			// change text to image
			newNode.innerHTML = '<img class="iView_button" src="/homepage/2010/styles/buttons/iview.png" alt="watch this on iView" height="26" width="111">';			
			
			// remove li from list
			ul.removeChild(li);
		}
	}
}

// ------------------------------------------------------------------
// add image overlays to Featured Sites stories

function findFeaturedSitesImages() {
	// find the images in Featured Sites section
	var images = document.getElementById('featuredSitesScroller').getElementsByTagName('IMG');

	for (var i=0, j=images.length; i<j; i++) {
		//var portal = images[i].parentNode.href.split('/')[3];
		var portal = images[i].className.substring(13).toLowerCase();
		//alert(portal);
		
		switch (portal) {
			case 'arts':
			case 'environment':
			case 'rampup':
			case 'religion':
			case 'technology':
			case 'thebigdiary':
			case 'bigdiary':
			case 'bigideas':
				// make the image its own background	
				images[i].style.backgroundImage = 'url('+images[i].src+')';
				images[i].style.backgroundPosition = 'top center';			
				images[i].style.backgroundRepeat = 'no-repeat';
				  
				// set src to overlay image
				images[i].src = 'homepage/2010b/styles/overlays/' + portal + '.png';					

				if (/MSIE 6/i.test(navigator.userAgent)) {
					// IE6 requires image sizes to be re-specified
					images[i].style.width = '220px';
					images[i].style.height = '120px';
				}
				break;
		}
	}
}

// ------------------------------------------------------------------
// Radio tabs and Temp Gauge

function showRadioTab(id) {
	if (id=='listen') {
		document.getElementById('listenTab').style.zIndex = '4'
		document.getElementById('listen').style.zIndex = '3';
		document.getElementById('podcastsTab').style.zIndex = '2'
		document.getElementById('podcasts').style.zIndex = '1';

		classAdd(document.getElementById('listenTab'),'front');
		classRemove(document.getElementById('podcastsTab'),'front');
	} else {
		document.getElementById('podcastsTab').style.zIndex = '4'
		document.getElementById('podcasts').style.zIndex = '3';
		document.getElementById('listenTab').style.zIndex = '2'
		document.getElementById('listen').style.zIndex = '1';

		classAdd(document.getElementById('podcastsTab'),'front');
		classRemove(document.getElementById('listenTab'),'front');
	}
	return false;
}

function showTempGauge() {
	// Add class to current temp to add thermometer image
	if (document.getElementById('currentTemp')) {
		var ctemp = parseInt((document.getElementById('currentTemp').innerHTML));
		//alert(ctemp);
		var thermo = document.getElementById('thermometerImg');
	
		if (ctemp < 1) {
			thermo.className = 'temp1';
		} else if (ctemp < 11 && ctemp > 0) {
			thermo.className = 'temp2';
		} else if (ctemp < 18 && ctemp > 10) {
			thermo.className = 'temp3';
		} else if (ctemp < 24 && ctemp > 17) {
			thermo.className = 'temp4';
		} else if (ctemp < 30 && ctemp > 23) {
			thermo.className = 'temp5';
		} else if (ctemp < 35 && ctemp > 29) {
			thermo.className = 'temp6';
		} else if (ctemp < 40 && ctemp > 34) {
			thermo.className = 'temp7';
		} else if (ctemp < 50 && ctemp > 39) {
			thermo.className = 'temp8';
		} else if (ctemp > 49) {
			thermo.className = 'temp9';
		}
	}
}


// ------------------------------------------------------------------
// load events

addLoadEvent(showTempGauge);
addLoadEvent(findiViewLinks);
addLoadEvent(findFeaturedSitesImages);

// webtrends clicktracking:
var abcClickTracking = true;

