/*
ABC Home Page bigfeature script
Created by: Andrew Kesper, August 2006
Modified by: Andrew Kesper, June 2007
*/

// ***** ROTATING FEATURE *****
function bigFeaturePrepare () {
  request = makeHttpObject();
  request.open('GET', xmlurl+'?'+querystring, true);
  request.onreadystatechange = bigfeatureInit;
  request.send(null);
}



var bigfeatureDelay = 7; // seconds to wait between features
var bigfeatureFade = 0.5; // duration of fade in seconds
var timersize = 50; // size of timer bar (width in pixels)
var xmlurl = 'resources/xml/bigfeature.xml'; // location of XML file
var forceUpdate = 5; // force a fresh update of the XML file after this number of minutes

var bigfeatureMsec = bigfeatureDelay * 1000;
var bigfeaturePlay = false;
var sections = new Array(); // will contain all the sections (i.e. big features)
var currentSection = 0; // array position of the current section
var querystring = new Date().getTime();

querystring = Math.floor(querystring/(1000*60*forceUpdate)); // a unique value every 'forceUpdate' minutes

/*request = makeHttpObject();
request.open('GET', xmlurl+'?'+querystring, true);
request.onreadystatechange = bigfeatureInit;
request.send(null);*/

function bigfeatureInit() {
	if (request.readyState == 4 && (request.status == 200 || request.status == 304)) {
		var xml = request.responseXML.documentElement;
		
		var s = xml.getElementsByTagName('section');
		for (i=0; i<s.length; i++) {
			var tab = '';
			var image = '';
			var link = '';
			var title = '';
			var updated = '';
			var content = '';
			if (s[i].getElementsByTagName('tab')[0].firstChild) tab = s[i].getElementsByTagName('tab')[0].firstChild.nodeValue;
			if (s[i].getElementsByTagName('image')[0].firstChild) image = s[i].getElementsByTagName('image')[0].firstChild.nodeValue;
			if (s[i].getElementsByTagName('link')[0].firstChild) link = s[i].getElementsByTagName('link')[0].firstChild.nodeValue;
			if (s[i].getElementsByTagName('title')[0].firstChild) title = s[i].getElementsByTagName('title')[0].firstChild.nodeValue;
			if (s[i].getElementsByTagName('updated')[0].firstChild) updated = s[i].getElementsByTagName('updated')[0].firstChild.nodeValue;
			if (s[i].getElementsByTagName('content')[0].firstChild) content = s[i].getElementsByTagName('content')[0].firstChild.nodeValue;
			sections[sections.length] = new Section(i+1, image, link, title, updated, content);
		}
		
		if (sections.length > 1) {
			bigfeatureTabs = document.createElement('DIV');
			bigfeatureTabs.id = 'topicHighlightBar';
			bigfeatureTabsH3 = document.createElement('H3');
			bigfeatureTabsH3.appendChild(document.createTextNode('Latest Highlights'));
			bigfeatureTabsP = document.createElement('P');
			bigfeatureTabsP.appendChild(document.createTextNode('Select a highlight '));
			lastTabTitle = '';
			for (i=0; i<sections.length; i++) {
				if (lastTabTitle != sections[i].tab) { // add a tab (if one with the same title doesn't previously exist)
					tab = document.createElement('A');
					tab.href = 'javascript:stop();currentSection='+i+';bigfeatureDisplay(sections[currentSection]);';
					tab.onclick = function() {
						eval(this.href); // Using onclick event instead of href attribute to call JavaScript code (this avoids flickering problems in Firefox)
						return false;
					}
					tab.title = sections[i].tab;
					tab.appendChild(document.createTextNode(i+1));
					bigfeatureTabsP.appendChild(tab);
					bigfeatureTabsP.appendChild(document.createTextNode(' ')); // add a space after each link
					lastTabTitle = sections[i].tab;
				}
			}
			
			bigfeatureTabs.appendChild(bigfeatureTabsH3);
			bigfeatureTabs.appendChild(bigfeatureTabsP);
			document.getElementById('topicHighlight').appendChild(bigfeatureTabs);
			
		}
		
		bigfeatureDisplay(sections[currentSection]);
		
		window.onload = function () {
			bigfeatureTimer();
			start();
			preloadNextImage();
		};
		
	}
}

function stop () {
	bigfeaturePlay = false;
}

function start () {
	bigfeaturePlay = true;
}

function bigfeatureTimer () {
	if (bigfeaturePlay == true) {
		bigfeatureMsec -= bigfeatureDelay*1000/timersize;
		if (bigfeatureMsec <= 10+(bigfeatureDelay*1000/timersize)) {
			changeOpac(0, 'topicHighlightWrapper'); // set opacity to zero just before moving on to the next section (this avoids flickering problems in Firefox)
		}
		if (bigfeatureMsec <= 0) {
			bigfeatureMsec = bigfeatureDelay * 1000;
			bigfeatureDisplay(nextSection());
		}
	}
	setTimeout('bigfeatureTimer()', bigfeatureDelay*1000/timersize);
}

// Return the next section (or loop back to the first one if the end is reached)
function nextSection () {
	currentSection++;
	if (currentSection == sections.length) currentSection = 0;
	return sections[currentSection];
}

// Return the next section (or loop back to the first one if the end is reached)
function previousSection () {
	currentSection--;
	if (currentSection < 0) currentSection = sections.length-1;
	return sections[currentSection];
}

// Preload the feature image due to appear next
function preloadNextImage () {
	next = currentSection+1;
	if (next == sections.length) next = 0;
	preload = new Image();
	preload.src = sections[next].image;
}

function bigfeatureDisplay (s) {
	changeOpac(0, 'topicHighlightWrapper');
	// Replace image
	img = document.getElementById('topicHighlightBg').getElementsByTagName('img');
	for (i=0; i<img.length; i++) {
		img[i].src = '/health/img/blank.gif';
		img[i].src = s.image;
		img[i].alt = s.title;
	}
	document.getElementById('topicHighlightBg').style.backgroundImage = 'url(/news/img/2007/blank.gif)';
	document.getElementById('topicHighlightBg').style.backgroundImage = 'url('+s.image+')';
	
	// Replace links
	a = document.getElementById('topicHighlightBlurb').getElementsByTagName('a');
	for (i=0; i<a.length; i++) {
		a[i].href = s.link;
	}
	// Replace title
	h = document.getElementById('topicHighlightBlurb').getElementsByTagName('h3')[0].getElementsByTagName('a')[0];
	while (h.hasChildNodes()) {
		h.removeChild(h.lastChild);
	}
	h.appendChild(document.createTextNode(s.title));
	// Remove existing content inside bigfeatureText
	t = document.getElementById('topicHighlightBlurbText');
	while (t.hasChildNodes()) {
		t.removeChild(t.lastChild);
	}
	t.appendChild(document.createTextNode(s.content));
	highlightTab(s.tab);
	preloadNextImage();
	opacity('topicHighlightWrapper', 0, 100, bigfeatureFade*1000);
}

function highlightTab (theTitle) {
	a = bigfeatureTabsP.getElementsByTagName('A');
	for (i=0; i<a.length; i++) {
		if (a[i].title == theTitle) {
			a[i].className = 'active';
			a[i].id = 'selected';
		}
		else {
			a[i].className = '';
			a[i].id = '';
		}
	}
}

function Section (tab, image, link, title, updated, content) {
	this.tab = tab;
	this.image = image;
	this.link = link;
	this.title = title;
	this.updated = updated;
	this.content = content;
}

// Create an XMLHttpRequest object (or the ActiveX equivalent for IE)
function makeHttpObject() {
	var xmlHttpObj;
	// branch for Activex version (Microsoft IE)
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlHttpObj = false;
			}
		}
	@else
		xmlHttpObj = false;
	@end @*/
	// branch for native XMLHttpRequest object (Mozilla & Safari)
	if (!xmlHttpObj && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlHttpObj = new XMLHttpRequest();
		} catch (e) {
			xmlHttpObj = false;
		}
	}
	return xmlHttpObj;
}


// Fade from one opacity setting to another
function opacity(id, opacStart, opacEnd, millisec) {
	var skip = 5;
    var speed = Math.round((millisec / 100) * skip);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i-=skip) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i+=skip) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = (opacity < 100 ? "alpha(opacity=" + opacity + ")" : "none");
}