/*
MAIN PAGE RIGHT COLUMN FUNCTIONS
--------------------------------------------------*/
var init = function() {
	dojo.query("#insuranceTabList .selected").forEach(function(currentNode) {
    		var theElementId = dojo.attr(currentNode, "id");
    		selectTab('insurance', theElementId.substring(2));
	});
  
	dojo.query("#financialTabList .selected").forEach(function(currentNode) {
    		var theElementId = dojo.attr(currentNode, "id");
    		selectTab('financial', theElementId.substring(2));
	});
	
  dojo.query("#insuranceProdMod .tabInfoContainer a").forEach(function(currentNode) {
			var uri = dojo.attr(currentNode, "href");
			var t = currentNode.innerHTML.replace(/<[iI][mM][gG].*>/g, "");
			if ((t == null) || (t == ""))
			{
				t = "Learn More";
			}
			
   		dojo.connect(currentNode, "onclick", function(event) {
   			dcsMultiTrack('WT.ac_url',uri,'WT.ac',t,'WT.ti','Microsite','WT.cg_n','AgentMicrositeInsProductsPortlet','WT.cg_s','Link');
   		});
  });
  
  dojo.query("#financialProdMod .tabInfoContainer a").forEach(function(currentNode) {
			var uri = dojo.attr(currentNode, "href");
			var t = currentNode.innerHTML.replace(/<[iI][mM][gG].*>/g, "");
			if ((t == null) || (t == ""))
			{
				t = "Learn More";
			}
			
   		dojo.connect(currentNode, "onclick", function(event) {
   			dcsMultiTrack('WT.ac_url',uri,'WT.ac',t,'WT.ti','Microsite','WT.cg_n','AgentMicrositeFinProductsPortlet','WT.cg_s','Link');
   		});
  });

	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "internal") 
		{
			anchor.target = "_blank"; 
		}

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "internal") || (anchor.getAttribute("rel") == "external")) 
		{
			anchor.innerHTML = anchor.innerHTML + ' <span class="hint">(Opens in a new window.)</span>';
		}
	}

	jsReady = true;
};

var jsReady = false;

function trackVideo(trackValue, index)
{
  if ((trackValue == "clickPlay") && (index != null) && (document.getElementById('videoTitle' + index) != null))
  {
    var title = document.getElementById('videoTitle' + index).innerHTML;
    dcsMultiTrack('WT.ti',title,'WT.cg_n','AgentMicrositeVideoPortlet','WT.cg_s','Video','WT.ac',trackValue);
  }
  else
  {
    dcsMultiTrack('WT.ti','Microsite','WT.cg_n','AgentMicrositeVideoPortlet','WT.cg_s','Video','WT.ac',trackValue);
  }
}


// this function will be called by Flash to retrieve the value of jsReady
function isReady() {
	return jsReady;
}
	 

function selectTab(group, tabName)
{
	if (tabName == "other")
	{
		tabName = "others";
	}
	
	dojo.query("a[rel="+ group + "PortletTab]").removeClass("selected");
	dojo.query("#t-" + tabName).addClass("selected");

	if (tabName == "others")
	{
		tabName = "oproducts";
	}
	
	dojo.query("#" + group + "TabList .tabInfoContainer").style("display","none");
	dojo.query("#tc-" + tabName).style("display","block");
}

function showNext(newCont) {
	var container = dijit.byId(newCont);
	container.forward();
}

function showPrevious(newCont) {
	var container = dijit.byId(newCont);
	container.back();
}

/** hide & show Dialog windows ***/
function closeSelf(x) {
  if (document.getElementById("statefarm-video") != null)
  {
    document.getElementById("statefarm-video").style.display = "";
  }
	var self = dijit.byId(x);
	self.hide();
}

function showDialog(y) {
  if (document.getElementById("statefarm-video") != null)
  {
    document.getElementById("statefarm-video").style.display = "none";
  }


	var d = dijit.byId(y);
	d.closeButtonNode.style.display='none';
	d.show();
}


/**** functions for reordering options listings ***/

function move(f,bDir,sName) {
	var el = f.elements["ro_" + sName]
	var idx = el.selectedIndex
	if (idx==-1) 
		alert("You must first select the item to reorder.")
	else {
		var nxidx = idx+( bDir? -1 : 1)
		if (nxidx<0) nxidx=el.length-1
		if (nxidx>=el.length) nxidx=0
		var oldVal = el[idx].value
		var oldText = el[idx].text
		el[idx].value = el[nxidx].value
		el[idx].text = el[nxidx].text
		el[nxidx].value = oldVal
		el[nxidx].text = oldText
		el.selectedIndex = nxidx
	}
}

/**** functions for reordering options listings ***/
function processForm(f) {
	for (var i=0;i<f.length;i++) {	
		var el = f[i]
		if (el.name.substring(0,4)=="ro_") {
			var strIDs = ""
			for (var j=0;j<f[i].options.length;j++)
				strIDs += f[i].options[j].value + ", "
			f.elements[f.elements[i].name.substring(4)].value = strIDs.substring(0,strIDs.length-2)
		}
	}
}

var externalUrl;
function openExternal(url)
{
  externalUrl = url;

  showDialog('elAmd66');
}

function commitExternal()
{
  hideExternal();
  
  var ua = navigator.userAgent.toLowerCase();
  if (ua.indexOf('safari/') != -1){
    window.open(externalUrl);
  }
  else {
    setTimeout("window.open('" + externalUrl + "')", 500);
  }
}

function hideExternal()
{
  closeSelf('elAmd66');
}

dojo.addOnLoad(init);


