/*
ABC News common scripts
Created by: Andrew Kesper, October 2006
Modified by: Andrew Kesper, April 2008
*/

news = true; // News livery
if (!getQueryStringVariable('site').match(/^(news)?$/)) news = false; // one-story-many-views


/************* NEW FUNCTIONS ***************/
// Add new string functions
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
String.prototype.toTitleCase = function() { 
	var ls = this.toLowerCase();
	var la = ls.split(' ');
	for (var i=0; i<la.length; i++) la[i] = la[i].charAt(0).toUpperCase()+la[i].slice(1);
	return la.join(' ');
}

// Add encodeURIComponent and decodeURIComponent capability for old browsers (e.g. Win IE 5.0, Mac IE 5.x). Uses escape/unescape and converts '+' to/from '%2B'
if (!window.encodeURIComponent) encodeURIComponent = function (s) { return escape(s).replace(/\+/, '%2B'); };
if (!window.decodeURIComponent) decodeURIComponent = function (s) { return unescape(s.replace(/%2B/, '+')); };

// Native XMLHttpRequest (AJAX) object for IE
if (!window.XMLHttpRequest && window.ActiveXObject) {
	window.XMLHttpRequest = function() {
		try { return new ActiveXObject('Microsoft.XMLHTTP'); } 
		catch (e) { }
		return null;
	};
}

function addLoadEvent(func) { // Source: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
	var oldonload = window.onload;
	if (typeof window.onload != 'function') window.onload = func;
	else {
		window.onload = function() {
			if (oldonload) oldonload();
			func();
		}
	}
}

// Run the onload event now instead of when the page has fully finished loading
function runLoadEvent () {
	window.onload();
	window.onload = function () {};
}

// Add a CSS class to the specified element (will not add the class if it already exists)
function classAdd (element, theclass) {
	if (!element) return;
	if (!element.className) element.className = '';
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	if (element.className.search(reg) == -1) element.className = (element.className + ' ' + theclass).trim();
}

// Remove a CSS class from the specified element
function classRemove (element, theclass) {
	if (!element) return;
	if (!element.className) return;
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	element.className = element.className.replace(reg, ' ').trim();
}

// Return true if a class name exists in the specified element
function classExists (element, theclass) {
	if (!element) return false;
	if (!element.className) return false;
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	return reg.test(element.className);
}



// Return query string variable, or an empty string if it doesn't exist
// Example: getQueryStringVariable('latitude', 'lat') returns value of 'latitide', if that doesn't exist, returns value of 'lat', if that doesn't exist, returns an empty string
function getQueryStringVariable () {
	var query = window.location.search.substring(1);
	var vars = query.split('&');
	for (var i=0; i<getQueryStringVariable.arguments.length; i++) {
		for (var j=0; j<vars.length; j++) {
			var pair = vars[j].split('=');
			if (pair[0] == getQueryStringVariable.arguments[i]) return decodeURIComponent(pair[1]);
		}
	}
	return '';
}

/************ MISCELLANEOUS FUNCTIONS **************/

// Give focus to the first text/textarea form element in the document
function formFocus () {
	var inputs = document.getElementsByTagName('INPUT');
	for (var i=0; i<inputs.length; i++) {
		if (inputs[i].type == 'text') {
			inputs[i].focus();
			return;
		}
	}
	inputs = document.getElementsByTagName('TEXTAREA');
	if (inputs.length > 0) inputs[0].focus();
}

function thumnbailScrollerGenerate (containerid) {
	var container;
	var colnames = new Array('column5a','column5b','column5c','column5d','column5e');
	if (container = document.getElementById(containerid)) {
		if (!window.highlightScroller) highlightScroller = new Object();
		var controls = document.createElement('DIV');
		controls.id = containerid+'_controls';
		controls.className = 'highlightscrollercontrols';
		var temp = container.childNodes;
		var divs = new Array();
		for (var i=0; i<temp.length; i++) {
			if (classExists(temp[i], 'headline') || classExists(temp[i], 'section')) { // check that class name is 'section' or 'headline'
				divs[divs.length] = temp[i];
			}
		}
		delete temp;
		var aprev = document.createElement('A');
		aprev.id = containerid+'_show_prev';
		aprev.className = 'prev';
		aprev.href = 'javascript:highlightScrollerMoveBy("'+containerid+'", -1);';
		aprev.onclick = function (f) {
			eval(this.href.replace(/^javascript:/, ''));
			return false;
		};
		var aprevimg = document.createElement('IMG');
		aprevimg.src = 'images/img/2007/blank.gif';
		aprevimg.width = 26;
		aprevimg.height = 16;
		aprev.appendChild(aprevimg);
		if (divs.length > 5) controls.appendChild(aprev);
		for (var i=0; i<divs.length; i+=5) {
			var num = (i/5);
			highlightScroller[containerid+'_length'] = num+1;
			var columns = document.createElement('DIV');
			columns.className = 'columns';
			columns.id = containerid+'_'+num;
			for (var j=0; j<5; j++) {
				if (divs[i+j]) {
					var newcolumn = document.createElement('DIV');
					newcolumn.className = colnames[j];
					newcolumn.appendChild(divs[i+j]);
					columns.appendChild(newcolumn);
				}
			}
			container.appendChild(columns);
			var a = document.createElement('A');
			a.id = containerid+'_show_'+num;
			a.href = 'javascript:highlightScrollerMoveTo("'+containerid+'", '+num+');';
			a.onclick = function (f) {
				eval(this.href.replace(/^javascript:/, ''));
				return false;
			};
			var aimg = document.createElement('IMG');
			aimg.src = 'images/img/2007/blank.gif';
			aimg.width = 16;
			aimg.height = 16;
			a.appendChild(aimg);
			if (divs.length > 5) controls.appendChild(a);
		}
		var anext = document.createElement('A');
		anext.id = containerid+'_show_next';
		anext.className = 'next';
		anext.href = 'javascript:highlightScrollerMoveBy("'+containerid+'", 1);';
		anext.onclick = function (f) {
			eval(this.href.replace(/^javascript:/, ''));
			return false;
		};
		var anextimg = document.createElement('IMG');
		anextimg.src = 'images/img/2007/blank.gif';
		anextimg.width = 26;
		anextimg.height = 16;
		anext.appendChild(anextimg);
		if (divs.length > 5) controls.appendChild(anext);
		container.parentNode.insertBefore(controls, container.nextSibling);
		highlightScrollerMoveTo(containerid, 0);
	}
}
//mypage
function mypageScrollerGenerate (containerid) {
	var container;
	var colnames = new Array('column5a','column5b','column5c','column5d','column5e');
	if (container = document.getElementById(containerid)) {
		if (!window.highlightScroller) highlightScroller = new Object();
		var controls = document.createElement('DIV');
		controls.id = containerid+'_controls';
		controls.className = 'highlightscrollercontrols';
		var temp = container.childNodes;
		var divs = new Array();
		for (var i=0; i<temp.length; i++) {
			if (classExists(temp[i], 'mypage') || classExists(temp[i], 'section')) { // check that class name is 'section' or 'mypage'
				divs[divs.length] = temp[i];
			}
		}
		delete temp;
		var aprev = document.createElement('A');
		aprev.id = containerid+'_show_prev';
		aprev.className = 'prev';
		aprev.href = 'javascript:highlightScrollerMoveBy("'+containerid+'", -1);';
		aprev.onclick = function (f) {
			eval(this.href.replace(/^javascript:/, ''));
			return false;
		};
		var aprevimg = document.createElement('IMG');
		aprevimg.src = 'images/img/2007/blank.gif';
		aprevimg.width = 26;
		aprevimg.height = 16;
		aprev.appendChild(aprevimg);
		if (divs.length > 3) controls.appendChild(aprev);
		for (var i=0; i<divs.length; i+=3) {
			var num = (i/3);
			highlightScroller[containerid+'_length'] = num+1;
			var columns = document.createElement('DIV');
			columns.className = 'columns';
			columns.id = containerid+'_'+num;
			for (var j=0; j<3; j++) {
				if (divs[i+j]) {
					var newcolumn = document.createElement('DIV');
					newcolumn.className = colnames[j];
					newcolumn.appendChild(divs[i+j]);
					columns.appendChild(newcolumn);
				}
			}
			container.appendChild(columns);
			var a = document.createElement('A');
			a.id = containerid+'_show_'+num;
			a.href = 'javascript:highlightScrollerMoveTo("'+containerid+'", '+num+');';
			a.onclick = function (f) {
				eval(this.href.replace(/^javascript:/, ''));
				return false;
			};
			var aimg = document.createElement('IMG');
			aimg.src = 'images/img/2007/blank.gif';
			aimg.width = 16;
			aimg.height = 16;
			a.appendChild(aimg);
			if (divs.length > 3) controls.appendChild(a);
		}
		var anext = document.createElement('A');
		anext.id = containerid+'_show_next';
		anext.className = 'next';
		anext.href = 'javascript:highlightScrollerMoveBy("'+containerid+'", 1);';
		anext.onclick = function (f) {
			eval(this.href.replace(/^javascript:/, ''));
			return false;
		};
		var anextimg = document.createElement('IMG');
		anextimg.src = 'images/img/2007/blank.gif';
		anextimg.width = 26;
		anextimg.height = 16;
		anext.appendChild(anextimg);
		if (divs.length > 3) controls.appendChild(anext);
		container.parentNode.insertBefore(controls, container.nextSibling);
		highlightScrollerMoveTo(containerid, 0);
	}
}

// Generate a scrollable feature thingy
function highlightScrollerGenerate (containerid) {
	var container;
	if (container = document.getElementById(containerid)) {
		if (classExists(container, 'thumbnailscroller')) {
			thumnbailScrollerGenerate(containerid);
			return;
		}
		if (!window.highlightScroller) highlightScroller = new Object();
		var controls = document.createElement('DIV');
		controls.id = containerid+'_controls';
		controls.className = 'highlightscrollercontrols';
		var temp = container.childNodes;
		var divs = new Array();
		for (var i=0; i<temp.length; i++) {
			if (temp[i].tagName && temp[i].tagName.toLowerCase() == 'div' && (classExists(temp[i], 'headline') || classExists(temp[i], 'section'))) { // check that class name is 'section' or 'headline'
				divs[divs.length] = temp[i];
			}
		}
		delete temp;
		var aprev = document.createElement('A');
		aprev.id = containerid+'_show_prev';
		aprev.className = 'prev';
		aprev.href = 'javascript:highlightScrollerMoveBy("'+containerid+'", -1);';
		aprev.onclick = function (f) {
			eval(this.href.replace(/^javascript:/, ''));
			return false;
		};
		var aprevimg = document.createElement('IMG');
		aprevimg.src = 'images/img/2007/blank.gif';
		aprevimg.width = 26;
		aprevimg.height = 16;
		aprev.appendChild(aprevimg);
		if (divs.length > 2) controls.appendChild(aprev);
		for (var i=0; i<divs.length; i+=2) {
			var num = (i/2);
			highlightScroller[containerid+'_length'] = num+1;
			var columns = document.createElement('DIV');
			columns.className = 'columns';
			columns.id = containerid+'_'+num;
			var column2a = document.createElement('DIV');
			column2a.className = 'column2a';
			column2a.appendChild(divs[i]);
			columns.appendChild(column2a);
			if (divs[i+1]) {
				var column2b = document.createElement('DIV');
				column2b.className = 'column2b';
				column2b.appendChild(divs[i+1]);
				columns.appendChild(column2b);
			}
			container.appendChild(columns);
			var a = document.createElement('A');
			a.id = containerid+'_show_'+num;
			a.href = 'javascript:highlightScrollerMoveTo("'+containerid+'", '+num+');';
			a.onclick = function (f) {
				eval(this.href.replace(/^javascript:/, ''));
				return false;
			};
			var aimg = document.createElement('IMG');
			aimg.src = 'images/img/2007/blank.gif';
			aimg.width = 16;
			aimg.height = 16;
			a.appendChild(aimg);
			if (divs.length > 2) controls.appendChild(a);
		}
		var anext = document.createElement('A');
		anext.id = containerid+'_show_next';
		anext.className = 'next';
		anext.href = 'javascript:highlightScrollerMoveBy("'+containerid+'", 1);';
		anext.onclick = function (f) {
			eval(this.href.replace(/^javascript:/, ''));
			return false;
		};
		var anextimg = document.createElement('IMG');
		anextimg.src = 'images/img/2007/blank.gif';
		anextimg.width = 26;
		anextimg.height = 16;
		anext.appendChild(anextimg);
		if (divs.length > 2) controls.appendChild(anext);
		container.parentNode.insertBefore(controls, container.nextSibling);
		highlightScrollerMoveTo(containerid, 0);
	}
}

function highlightScrollerMoveTo (containerid, num) {
	highlightScroller[containerid+'_current'] = num;
	displayThisHideSiblings(containerid+'_'+highlightScroller[containerid+'_current']);
	setClassUnsetSiblings(containerid+'_show_'+highlightScroller[containerid+'_current'], 'active');
}

function highlightScrollerMoveBy (containerid, num) {
	highlightScroller[containerid+'_current'] += num;
	if (highlightScroller[containerid+'_current'] >= highlightScroller[containerid+'_length']) highlightScroller[containerid+'_current'] -= highlightScroller[containerid+'_length'];
	else if (highlightScroller[containerid+'_current'] < 0) highlightScroller[containerid+'_current'] += highlightScroller[containerid+'_length'];
	displayThisHideSiblings(containerid+'_'+highlightScroller[containerid+'_current']);
	setClassUnsetSiblings(containerid+'_show_'+highlightScroller[containerid+'_current'], 'active');
}

function displayThisHideSiblings (element) { // 'element' can be a DOM element or a string representing the ID of a DOM element
	if (typeof element == 'undefined') return;
	if (typeof element == 'string') element = document.getElementById(element);
	var x = element.previousSibling;
	while (x != null) {
		if (x.nodeType == 1) x.style.display = 'none';
		x = x.previousSibling;
	}
	x = element.nextSibling;
	while (x != null) {
		if (x.nodeType == 1) x.style.display = 'none';
		x = x.nextSibling;
	}
	element.style.display = 'block';
}

function setClassUnsetSiblings (element, theclass) { // 'element' can be a DOM element or a string representing the ID of a DOM element
	if (element == null) return;
	if (typeof element == 'undefined') return;
	if (typeof element == 'string') {
		if (document.getElementById(element)) element = document.getElementById(element);
		else return;
	}
	var x = element.previousSibling;
	while (x != null) {
		classRemove(x, theclass);
		x = x.previousSibling;
	}
	x = element.nextSibling;
	while (x != null) {
		classRemove(x, theclass);
		x = x.nextSibling;
	}
	classAdd(element, theclass);
}

// Open link in a popup window
function popup (url, width, height, windowname) {
	if (!width) var width = 600;
	if (!height) var height = 400;
	if (!windowname) var windowname = 'abcnewspopup'+new Date().getTime();
	if (url.indexOf('?') != -1) url += '&layout=popup';	else url += '?layout=popup';
	var left = screen.width/2 - width/2;
	var top = screen.height/2 - height/2;
	window.open(url, windowname, 'width='+width+',height='+height+',toolbar=0,resizable=1,scrollbars=1,left='+left+',top='+top);
	return false;
}

