// JavaScript Document



$(document).ready(function(){
	if($('#flashLeaderPanel').length)
	{
		var flashvars = {
		area: $("#giHeaderBarNav a.activeHeaderBar").attr('id'),
		webhost: location.hostname
		};
		var params = {
		menu: "true",
		wmode: "opaque"
		};
		
		swfobject.embedSWF("/common/swf/advert/adverts3.swf", "flashLeaderPanel", "500", "350", "9.0.0","/common/js/swfobject/expressInstall.swf", flashvars, params);			
	}
	headlines();
	gibanner();
	$('a#portalMinimalPanelLink').click(function(e)
	{
		e.preventDefault();
		showPortal();
	});
	
});





function newsletter(){
	if($("#newsletterForm").length)
	{	
		$("#newsletterButton").click(
		  function () {
			newslettersubscribe();
			return false;
		  }  
		);	
	}
}





function getValue(radioGroupName) {
	radios = document.getElementsByName(radioGroupName);
	for (i = 0; i < radios.length; i++) {
		if (radios[i].checked) return radios[i].value;
	}
}



function newslettersubscribe(){
	email =  $("#email").attr('value')
	list_option = getValue('list_option');
	subscribe_option =  $("#subscribe_option").attr('value');
	if (!subscribe_option)
	{
		subscribe_option = getValue('subscribe_option');
	}

	$("#error_div").hide('slow');
	var dadaurl = '/cgi-bin/dada/mail.cgi?f='+subscribe_option+'_flash_xml&list='+list_option+'&option='+subscribe_option+'&email='+email+'';

	$("#loadingAnimation").show('slow');

	switch(subscribe_option)
	{
		case 'unsubscribe':
			newsletterunsubscribe_xml(dadaurl);
		break;
		default:
			newslettersubscribe_xml(dadaurl);
		break;
	}
}



function newslettersubscribe_xml (dadaurl)
{
	$.get(dadaurl, function(xmlDataSet) 
	{
		$("subscription", xmlDataSet).each(function(i) 
		{
			email	= $(this).find("email").text();
			status	= $(this).find("status").text();
			errors	= $(this).find("errors").text();
		});

		if ( status == '1' )
		{
			window.location = "/newsletter/subscribe_success";
		}	
		else
		{
			switch(errors)
			{
				case 'invalid_email':
				err_msg = 'Please enter valid email address.';
				break;    
				case 'subscribed':
				err_msg = 'Email address already subscribed. Thank you.';
				break;
				default:
				//err_msg = 'Error whilst subscribing:<br/>' + errors.replace(/_/, " ");
				err_msg = 'Error whilst subscribing. Please check your email address.';
				break;			  
			}

			$("#loadingAnimation").hide('slow');
			$("#error_div").html(err_msg);
			$("#error_div").show('slow');
		}
	});	
}



function newsletterunsubscribe_xml (dadaurl)
{
	$.get(dadaurl, function(xmlDataSet) {
		// format and output result
		$("unsubscription", xmlDataSet).each(function(i) {
			email	= $(this).find("email").text();
			status	= $(this).find("status").text();
			errors	= $(this).find("errors").text();
		});
		if ( status == '1' )
		{
			window.location = "/newsletter/unsubscribe_success";
		}	
		else
		{
			switch(errors)
			{
				case 'invalid_email':
				err_msg = 'Please enter valid email address.';
				break;    
				case 'not_subscribed':
				err_msg = 'Email hasn\'t been subscribed yet. Thank you.';
				break;
				case 'not_subscribedinvalid_email':
				err_msg = 'Please enter valid email address.';
				break;
				default:
				//err_msg = 'Error whilst unsubscribing:<br/>' + errors.replace(/_/, " ");
				err_msg = 'Error whilst unsubscribing. Please check your email address.';
				break;
			}
			$("#loadingAnimation").hide('slow');
			$("#error_div").html(err_msg);
			$("#error_div").show('slow');
		}
	});	
}

function showDropDown(obj)
{
	$('#list'+obj).toggle();
	$('#button'+obj).toggle();
}

function showPortal(){
	$('#portalTop').show();
	$('#portalMinimalPanel').height('60px');
	var flashvars = {
	  portalZone: portalZone,
	  hostZone: hostZone
	};
	var params = {
	  allowScriptAccess: "always",
	  wmode: "opaque"
	};
	swfobject.embedSWF("http://www.gold.org/common/swf/portal.swf", "portalMinimalPanel", "100%", "60", "9.0.0","/common/js/swfobject/expressInstall.swf", flashvars, params);			
}

function headlines(){
	if(!$("#headlinesScroller").length)
	{
		return;	
	}
	else
	{
		innitNews();
	}
}

headlinesPerPanel = 6;
headlinePanels = Array();
headlinePanelIndex = 0;
delay = 6000;

function innitNews(){
	//get the news
	$.get("/headlines/xml/", function(xmlDataSet) 
	{
		// format and output result
		headlines = $("headlines", xmlDataSet).children();
		i=0;
		thislength = headlines.length;
		for(c=0;c<thislength;c++)
		{
			if(i==0)
			{
				str = '<ul>';
			}
			str += '<li><a href="'+$(headlines[c]).attr("url")+'">'+$(headlines[c]).text()+'</a></li>';
			i++;
			if(i==headlinesPerPanel)
			{
				str += '</ul>';
				headlinePanels[headlinePanels.length] = str;
				i=0;
			}
		}		
		$("#headlinesScroller").html(headlinePanels[headlinePanelIndex]);
		headlinePanelIndex++;
		timer=setTimeout("animOut()",delay);
	});	
}

function animOut(){
	$("#headlinesScroller").slideUp("slow",animIn); 
}

function animIn(){
	$("#headlinesScroller").html(headlinePanels[headlinePanelIndex]);
	$("#headlinesScroller").slideDown("slow",delayTimer); 
}

function delayTimer(){
	if(headlinePanelIndex == (headlinePanels.length-1))
	{
		headlinePanelIndex=0;
	}
	else
	{
		headlinePanelIndex++;
	}
	timer=setTimeout("animOut()",delay);
}



// innit function
function gibanner()
{
	timer=setTimeout("giBannerFadeOut()",5000);
}

function giBannerFadeOut()
{
	$("#giHeaderBanner").fadeOut(1500);
	timer=setTimeout("giBannerFadeIn()",1500);
}

function giBannerFadeIn()
{
	// loads a page with HTML tags and cache setting
	$("#giHeaderBanner").load("/gibanner/");
	$("#giHeaderBanner").fadeIn(2000);
	timer=setTimeout("giBannerFadeOut()",8000);
}



