var homepageInterval;
var homepage_rotation = {
	'gold'    :[{'period':'day','showing':0}],
	'silver'  :[{'period':'day','showing':0}],
	'platinum':[{'period':'day','showing':0}]
//	'gold'    :[{'period':'day','showing':0},{'period':'week','showing':0},{'period':'month','showing':0}],
//	'silver'  :[{'period':'day','showing':0},{'period':'week','showing':0},{'period':'month','showing':0}],
//	'platinum':[{'period':'day','showing':0},{'period':'week','showing':0},{'period':'month','showing':0}]
};

$(function(){
//	var charts = {
//		'Gold'    :"http://www.24hgold.com/UserControls/exportdataxml.aspx?WorldCom=true&codecom=GOLD&changecom=chgecom&valuecom=valecom",
//		'Silver'  :"http://www.24hgold.com/UserControls/exportdataxml.aspx?WorldCom=true&codecom=SILVER&changecom=chgecom&valuecom=valecom",
//		'Platinum':"http://www.24hgold.com/UserControls/exportdataxml.aspx?WorldCom=true&codecom=PLATINIUM&changecom=chgecom&valuecom=valecom"
//	};

	$('#feature3_container a.feature3_textlink').each(function(){
		$(this).attr({'href':'javascript://Show ' + $(this).text() + ' Chart'});
		$(this).bind('click',function(){
			//$('#feature3_container iframe').attr({'src':charts[$(this).text()]});
			var user_metal = $(this).text().toLowerCase();
			show_rotate_homepage_metal_chart(user_metal);
		});
	});
	show_rotate_homepage_metal_chart('gold');

	//also any extra setup for the homepage searchbox thing.
	var dst = 'Enter Search Terms'; //default search text
	$('#home_searchInput').val(dst);
	$('#home_searchInput').bind('focus',function(){ if ($(this).val() == dst) { $(this).val('');  } });
	$('#home_searchInput').bind('blur',function() {	if ($(this).val() == '')  { $(this).val(dst); }	});
	$('#home_searchInput').bind('keypress',function(e) {	
		if (e.keyCode == 13) { $('#home_searchForm').submit(); }
	});

});


function show_rotate_homepage_metal_chart(user_metal) {
	//set up interval to rotate the specific metal type.
	//alert('here2: ' + user_metal);

	//hide all of them.
	for (var metal in homepage_rotation) {
		for (var period_index = 0; period_index < homepage_rotation[metal].length; period_index++) {
			homepage_rotation[metal][period_index]['showing'] = 0;
			$('#home_' + metal + '_' + homepage_rotation[metal][period_index]['period']).hide();
		}
	}	
	//show the one that the user chose.
	homepage_rotation[user_metal][0]['showing'] = 1;
	$('#home_' + user_metal + '_' + homepage_rotation[user_metal][0]['period']).show();

	//set an interval to rotate through all the period charts for that metal.
	//DumperAlert(['before setting interval, homepage_rotation is:', homepage_rotation ]);
	clearInterval(homepageInterval);
	homepageInterval = setInterval(function(){
		rotate_homepage_metal_chart(user_metal);
	}, 7000);

}

function rotate_homepage_metal_chart(metal)	{
	//alert('here1');
	for (var period_index = 0; period_index < homepage_rotation[metal].length; period_index++) {
		//DumperAlert(['checking rotation index item:',period_index,'homepage_rotation is:', homepage_rotation, 'and if this has a 1 for showing we should do something!', homepage_rotation[metal][period_index], 'for:', metal ]);
		if (homepage_rotation[metal][period_index]['showing']) {
			//DumperAlert(['clearly this one is showing.',period_index]);
			//when we find the one that is showing, hide it and show the next one
			//DumperAlert({'stuff at period index':period_index,'is like':homepage_rotation[metal][period_index] });
			var show_index = period_index+1; //next one
			if (period_index == (homepage_rotation[metal].length-1)) {
				//unless we're at the end then the next one is the first one!
				show_index = 0;
			}
			//DumperAlert({'i think i need to show the one with index:':show_index,'which has':homepage_rotation[metal][show_index]});
			//note the id will only be found because its in the html we sucked down from remote.
			homepage_rotation[metal][period_index]['showing'] = 0;
			homepage_rotation[metal][show_index]['showing'] = 1;
			$('#home_' + metal + '_' + homepage_rotation[metal][period_index]['period']).hide();
			$('#home_' + metal + '_' + homepage_rotation[metal][show_index]['period']).show();
			//DumperAlert(['we should hide it and show',show_index,'instead. after setting up for that rotation looks like:', homepage_rotation ]);

			break; //#found it, did stuff, done.
		}
	}
}