//////////////////////////////////////////////////////////////////////

// preload images
// // preloading 1-4 numbers
num = new Array();
for(var i=1;i<5;i++)
{
	num[i] = new Array();
	num[i]['off'] = new Image();
	num[i]['over'] = new Image();

	num[i]['off'].src='/resources/media/images/home/numbers/'+i+'.gif';
	num[i]['over'].src='/resources/media/images/home/numbers/'+i+'_over.gif';
}

var arrow = new Image();
var arrow_over = new Image();

arrow.src='/resources/media/images/global/arrow.gif';
arrow_over.src='/resources/media/images/global/arrow_over.gif';

//////////////////////////////////////////////////////////////////////

function randomHomeCallouts()
{


	var id = new Array('blue','orange');
	for(var i=0;id.length>i;i++)
	{
		var index = 0;
		var a = document.getElementById(id[i]).getElementsByTagName('ol').item(1).getElementsByTagName('a');
		while(true)
		{
			var index = Math.ceil(Math.round(4*Math.random()));
			if(index>0)
				break;
		}

		homeCallouts(a[index]);
	}
}

//////////////////////////////////////////////////////////////////////

function homeCallouts(t)
{
	if(t)
		var a = t.parentNode.parentNode.getElementsByTagName('a');
	else
		var a = new Array();

	var index = getPosition(t,a);

	// get divs
	if(t) // t == this
	{
		var div = t.parentNode.parentNode.parentNode.getElementsByTagName('ol').item(0).getElementsByTagName('div');
		var a = t.parentNode.parentNode.getElementsByTagName('a')
	}
	else // t == null
		var div = document.getElementById('callout').getElementsByTagName('div');

	if(t)
	{
		for(var i=0;a.length>i;i++)
		{
			a[i].className='';
			var img = a[i].getElementsByTagName('img').item(0);
			img.src=num[i+1]['off'].src;

			if(t==a[i])
			{
				a[i].className='hLight';
				img.src=num[i+1]['over'].src;
			}
		}
	}

	var n=0;
	for(var i=0;div.length>i;i++)
	{
		// reset n value
		if(t==null && n>3)
			n=0;

		// get div.wrappers
		if(div[i].className=='wrapper')
		{
			if(index==n) // if num matches display div
				div[i].parentNode.style.display='block';
			else // if num does not match hide div
				div[i].parentNode.style.display='none';
			n++;
		}
	}
}

//////////////////////////////////////////////////////////////////////

function setImage(t, index)
{
	if(!(index=='off' && t.className=='hLight'))
	{
		img = t.getElementsByTagName('img').item(0);
		img.src=num[t.title][index].src;
	}
}

//////////////////////////////////////////////////////////////////////

function setNumberNav()
{
	// get the number nav
	var ol = document.getElementById('callout').getElementsByTagName('ol');

	for(var i=0;ol.length>i;i++)
	{
		// get ol.numNav
		if(ol[i].className=='numNav')
		{
			// get a tags
			var a = ol[i].getElementsByTagName('a');
			for(var n=0;a.length>n;n++)
			{
				var img = a[n].getElementsByTagName('img').item(0);
				index=n+1;

				// set onclick event
				a[n].onclick=function() { homeCallouts(this); }

				// set mouseover state
				a[n].onmouseover=function() { setImage(this, 'over'); }

				// set mouseout state
				a[n].onmouseout=function() { setImage(this, 'off'); }
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////

function randomFeatureCallouts()
{


	var id = new Array('video','photo','camera');
	for(var i=0;id.length>i;i++)
	{
		var index = 0;
		var a = document.getElementById(id[i]).getElementsByTagName('ol').item(1).getElementsByTagName('a');
		while(true)
		{
			var index = Math.ceil(Math.round(4*Math.random()));
			if(index>0)
				break;
		}

		featureCallouts(a[index]);
	}
}

//////////////////////////////////////////////////////////////////////

function featureCallouts(t)
{
	if(t)
		var a = t.parentNode.parentNode.getElementsByTagName('a');
	else
		var a = new Array();

	var index = getPosition(t,a);

	// get divs
	if(t) // t == this
	{
		var div = t.parentNode.parentNode.parentNode.getElementsByTagName('ol').item(0).getElementsByTagName('div');
		var a = t.parentNode.parentNode.getElementsByTagName('a')
	}
	else // t == null
		var div = document.getElementById('featuredcolumn').getElementsByTagName('div');

	if(t)
	{
		for(var i=0;a.length>i;i++)
		{
			a[i].className='';
			var img = a[i].getElementsByTagName('img').item(0);
			img.src=num[i+1]['off'].src;

			if(t==a[i])
			{
				a[i].className='hLight';
				img.src=num[i+1]['over'].src;
			}
		}
	}

	var n=0;
	for(var i=0;div.length>i;i++)
	{
		// reset n value
		if(t==null && n>3)
			n=0;

		// get div.wrappers
		if(div[i].className=='wrapper')
		{
			if(index==n) // if num matches display div
				div[i].parentNode.style.display='block';
			else // if num does not match hide div
				div[i].parentNode.style.display='none';
			n++;
		}
	}
}

//////////////////////////////////////////////////////////////////////

function setFeatureNumberNav()
{
	// get the number nav
	var ol = document.getElementById('featuredcolumn').getElementsByTagName('ol');

	for(var i=0;ol.length>i;i++)
	{
		// get ol.numNav
		if(ol[i].className=='numNav')
		{
			// get a tags
			var a = ol[i].getElementsByTagName('a');
			for(var n=0;a.length>n;n++)
			{
				var img = a[n].getElementsByTagName('img').item(0);
				index=n+1;

				// set onclick event
				a[n].onclick=function() { featureCallouts(this); }

				// set mouseover state
				a[n].onmouseover=function() { setImage(this, 'over'); }

				// set mouseout state
				a[n].onmouseout=function() { setImage(this, 'off'); }
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////

function newsTabs(t)
{
	var tabs = document.getElementById('tabs');
	var h3 = tabs.getElementsByTagName('h3');
	var div = tabs.getElementsByTagName('div');
	var index=-1;

	for(var i=0;h3.length>i;i++)
	{
		var parent = h3.item(i).parentNode;
		if(t.parentNode==h3.item(i))
		{
			if(t.parentNode.id=='explore') // justin - set this to justin when the old explore tab is the default
				tabs.className='explore'; //explore
			else
				tabs.className='';

			div.item(i).className='';
		}
		else
			div.item(i).className='closed';
	}
}

//////////////////////////////////////////////////////////////////////

function setTabs()
{
	if(document.getElementById('tabs'))
	{
		var h3 = document.getElementById('tabs').getElementsByTagName('h3');
		for(var i=0;h3.length>i;i++)
		{
			h3.item(i).getElementsByTagName('a').item(0).onclick=function()
			{
				newsTabs(this);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////
google.load("feeds", "1");
var feedcontainer = "";
var feedurl="http://wwfblogs.org/climate/rss.xml";
var feedlimit=3;
var rssoutput="<ul class='list'>";

function rssfeedsetup(){
	if (document.getElementById("feeddiv")) {
		feedcontainer=document.getElementById("feeddiv");
		var feedpointer=new google.feeds.Feed(feedurl); //Google Feed API method
		feedpointer.setNumEntries(feedlimit); //Google Feed API method
		feedpointer.load(displayfeed); //Google Feed API method
	}
}

function displayfeed(result){
	if (!result.error){
		var thefeeds=result.feed.entries;
		for (var i=0; i<thefeeds.length; i++) {
			var pubDate = new Date(thefeeds[i].publishedDate);
			rssoutput+="<li style='margin-bottom: 10px;'><a href='" + thefeeds[i].link + "'>" + thefeeds[i].title + " <i>" + (pubDate.getMonth() + 1) +  "/" + pubDate.getDate() + "/" + pubDate.getFullYear() + "</i></a></li>";
		}
		rssoutput+="</ul><ul class='links'><li><a href='http://www.wwfblogs.org/climate/'>View All Entries</a></li><li class='lastItem'><a href='http://wwfblogs.org/climate/rss.xml' >Subscribe to WWF Climate</a></li></ul>";
		feedcontainer.innerHTML=rssoutput;
	}
}

//////////////////////////////////////////////////////////////////////

onloadVars[onloadIndex]='randomHomeCallouts()';
onloadIndex++;

onloadVars[onloadIndex]='setNumberNav()';
onloadIndex++;

onloadVars[onloadIndex]='randomFeatureCallouts()';
onloadIndex++;

onloadVars[onloadIndex]='setFeatureNumberNav()';
onloadIndex++;

onloadVars[onloadIndex]='setTabs()';
onloadIndex++;

onloadVars[onloadIndex]='rssfeedsetup()';
onloadIndex++;

//////////////////////////////////////////////////////////////////////
