//------------------------------------------------
// FeatureBox script v2.0
// (c) Erik Gorton 2008


function featurebox_setup(path, data) {
	if(data.length > 4 || data.length < 4){alert("JAVA ERROR: You must not have 4 slides in the Feature Box (no more or less) - check the HTML");}
	featurebox_delay = 9;
	featurebox_path = path;
	featurebox_navtitles = [];
	featurebox_filenames = [];
	featurebox_links = [];
	featurebox_titles = [];
	featurebox_texts = [];
	for(i=0;i<data.length;i++) {
		featurebox_navtitles[i] = data[i][0]; // The text to appear in the navigation cells
		featurebox_filenames[i] = data[i][1]; // The filenames of the images
		featurebox_links[i] = data[i][2]; // The links to the articles
		featurebox_titles[i] = data[i][3]; // The text titles
		featurebox_texts[i] = data[i][4]; // The text summary copys
	}
	featurebox_frame = 1;
	featurebox_linknumber = 0;
	featurebox_images = [];
	featurebox_opacity = 100;
	featurebox_timer_id = null;
	featurebox_nav_setup();
	featurebox();
	//alert("hello");
}


function featurebox() {
	if (featurebox_images[featurebox_frame]) {
		if (featurebox_opacity < 100) {
			if (featurebox_opacity == 0) {
				document["featureboxIMAGE"].src = featurebox_images[featurebox_frame].src;
				featurebox_linknumber = featurebox_frame;
				if(featurebox_frame++ >= (featurebox_links.length-1)) {featurebox_frame = 0;}
				featurebox_nav_setup();
				featurebox_text_setup();
			}
			setOpacity(document["featureboxIMAGE"],featurebox_opacity);
			setOpacity(document.getElementById("featureboxTXT"),featurebox_opacity);
			featurebox_opacity = featurebox_opacity +10;
			featurebox_timer_id = setTimeout("featurebox()",10);
		}
		else {
			setOpacity(document["featureboxIMAGE"],100);
			setOpacity(document.getElementById("featureboxTXT"),100);
			featurebox_opacity = 0;
			featurebox_timer_id = setTimeout("featurebox()",featurebox_delay * 1000);
		}
	}
	else {
		for (i=0;i<featurebox_filenames.length;i++) {
			featurebox_images[i] = new Image;
			featurebox_images[i].src = featurebox_path + featurebox_filenames[i];
		}
		featurebox_timer_id = setTimeout("featurebox()",1);
	}
}

function featurebox_link() {
	clearTimeout(featurebox_timer_id);
	location.href = featurebox_links[featurebox_linknumber];
}

function featurebox_nav_setup() {
	var thecell, theclass;
	featurebox_nav_text = '<table width="385" height="20" cellpadding="0" cellspacing="0" id="featureboxNAVtable"><tr>'
	for(i=0;i<featurebox_navtitles.length;i++) {
		theclass='';
		if (i==featurebox_linknumber) {theclass='class="featureboxNAVtableSELECTED"';}
		thecell = '<td align="center" valign="middle" '+theclass+' id="MAINnav'+(i+1)+'" >';
		featurebox_nav_text = featurebox_nav_text + thecell + '<a href="#" onmouseup="featurebox_nav_click('+i+')">' +featurebox_navtitles[i] + '</a></td>';
	}
	featurebox_nav_text = featurebox_nav_text + '</tr></table>';
	//alert('"'+featurebox_nav_text+'"');
	document.getElementById("featureboxNAV").innerHTML = featurebox_nav_text;
}

function featurebox_text_setup() {
	featurebox_copy = '<span class="featureboxTXTheading">'+ featurebox_titles[featurebox_linknumber] +'</span>'+ featurebox_texts[featurebox_linknumber] +'<br>' + '<span class="featureboxTXTmore"><a href="javascript:featurebox_link();">Read more &gt;</a></span></div>' 
	//alert('"'+featurebox_nav_text+'"');
	document.getElementById("featureboxTXT").innerHTML = featurebox_copy;
}

function featurebox_nav_click(thelink) {
	clearTimeout(featurebox_timer_id);
	featurebox_linknumber = thelink;
	featurebox_frame = thelink;
	featurebox_opacity = 0;
	featurebox_nav_setup();
	featurebox();
}

// cross-browser adjust opacity function
function setOpacity(obj,val) {
	obj.style.filter = "alpha(opacity="+val+")";
	val=val/100;
  	obj.style.opacity = val;
  	obj.style.MozOpacity=val;
  	obj.style.KhtmlOpacity=val;
}

//------------------------------------------------



// DF1.1 :: domFunction 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
// GNU Lesser General Public License -- http://www.gnu.org/licenses/lgpl.html
//******************************************************
// Makes Javascript start without waiting for the entire page to load
//DOM-ready watcher

function domFunction(f, a)
{
	//initialise the counter
	var n = 0;
	
	//start the timer
	var t = setInterval(function()
	{
		//continue flag indicates whether to continue to the next iteration
		//assume that we are going unless specified otherwise
		var c = true;

		//increase the counter
		n++;
	
		//if DOM methods are supported, and the body element exists
		//(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
		//in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
		if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
		{
			//set the continue flag to false
			//because other things being equal, we're not going to continue
			c = false;

			//but ... if the arguments object is there
			if(typeof a == 'object')
			{
				//iterate through the object
				for(var i in a)
				{
					//if its value is "id" and the element with the given ID doesn't exist 
					//or its value is "tag" and the specified collection has no members
					if
					(
						(a[i] == 'id' && document.getElementById(i) == null)
						||
						(a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
					) 
					{ 
						//set the continue flag back to true
						//because a specific element or collection doesn't exist
						c = true; 

						//no need to finish this loop
						break; 
					}
				}
			}

			//if we're not continuing
			//we can call the argument function and clear the timer
			if(!c) { f(); clearInterval(t); }
		}
		
		//if the timer has reached 60 (so timeout after 15 seconds)
		//in practise, I've never seen this take longer than 7 iterations [in kde 3 
		//in second place was IE6, which takes 2 or 3 iterations roughly 5% of the time]
		if(n >= 60)
		{
			//clear the timer
			clearInterval(t);
		}
		
	}, 250);
};

//------------------------------------------------
