/*
2008-07-18 sm222
An issue with the View All Holdings functionality was reported.
In some instances, the view all holdings div was being rendered twice.
This could be replicated, but not consitently. After investigation,
it was determined that some clicks were being recognized twice. 
To correct this, I added the two variables below, and when the click occurs,
I increment the variable by one. I then check the value before allowing the div
to be rendered.
Shawn
*/

var allHoldingsClickCount = "0";
var viewAllHoldingsClickCount = "0";

// change text input
function changeInputType(
	oldElm, // a reference to the input element
	iType, // value of the type property: 'text' or 'password'
	iValue, // the default value, set to 'password' in the demo
	blankValue, // true if the value should be empty, false otherwise
	noFocus) {  // set to true if the element should not be given focus

	if (!oldElm || !oldElm.parentNode || (iType.length<4) || 
		!document.getElementById || !document.createElement) return;

	var newElm = document.createElement('input');
	newElm.type = iType;
	if(oldElm.name) newElm.name = oldElm.name;
	if(oldElm.id) newElm.id = oldElm.id;
	if(oldElm.className) newElm.className = oldElm.className;
	if(oldElm.size) newElm.size = oldElm.size;
	if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
	if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
	newElm.onfocus = function() { return function() {
		if (this.hasFocus) return;
		var newElm = changeInputType(this, 'password', iValue,
			(this.value.toLowerCase() == iValue.toLowerCase()) ? true : false);
		if (newElm) newElm.hasFocus = true;
	}}();

	newElm.onblur = function() {return function() {
		if (this.hasFocus)
			if (this.value == '' || (this.value.toLowerCase() == iValue.toLowerCase())) {
				changeInputType(this,'text',iValue,false,true);
			}
	}}();
 
	// hasFocus is to prevent a loop where onfocus is triggered over and over again
	newElm.hasFocus = false;
	oldElm.parentNode.replaceChild(newElm,oldElm);
	if (!blankValue) newElm.value = iValue;
	if (!noFocus || typeof(noFocus) == 'undefined') {
		window.tempElm = newElm;
		setTimeout("tempElm.hasFocus = true; tempElm.focus();", 1);
	}
	return newElm;
}

$(document).ready(function(){
	
	// IE6 PNG Replacement
	if (jQuery.browser.msie && (Math.floor(jQuery.browser.version) == 6))
	{
	   for(var i=0; i<document.images.length; i++)
	   {
	      var img = document.images[i]
	      var imgName = img.src.toUpperCase()
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	         var imgStyle = "display:inline-block;" + img.style.cssText 
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
	         img.outerHTML = strNewHTML
	         i = i-1
	      }
	   }
	}

	// persona select drop down
	$("#investor").click(function () {
		
		function investorAssets() {
			$("#investor").addClass("active");
		}		
		
		if ($("#personaTypes").is(":hidden")) {
			$("#personaTypes").empty();				

			$.ajax({
				url: "/etf/nets.jsp?m=xml/ETFList_Map&s=persona_popup",
				cache: false,
				success: function(html) {
					$("#personaTypes").append(html);
					$("#personaTypes").show();
					return false;					
				}
			});		
			setTimeout(investorAssets, 50);
			
		} else {
			$("#investor").removeClass("active");
			$("#personaTypes").hide();			
		}
	});
	
	//$("#personaTypes").click(function() {
	//	return false;
	//});
	
		$("span.modalErrorChartClose").click(function () {
			//alert("b");
			//$(".viewErrorChart").removeClass("active");
			//$(".errorChart").hide();
		});


	
	/*
	// click anywhere except "modal" window to close
	$("body").click(function() {

		// persona types
		if ($("#personaTypes").not(":hidden")) {
			$("#investor").removeClass("active");
			$("#personaTypes").hide();
		} else {
			$("#investor").addClass("active");
		}

		// map modal
		if ($(".map").not(":hidden")) {
			$(".map-view a").removeClass("active");
			$(".map").hide();
		} else {
			$(".map").addClass("active");
		}
		

		// discount chart modal
		if ($(".discountChart").not(":hidden")) {
			$(".viewDiscountChart").removeClass("active");
			$(".discountChart").hide();
		} else {
			$(".viewDiscountChart").addClass("active");
		}

		// error chart modal
		if ($(".errorChart").not(":hidden")) {
			$(".viewErrorChart").removeClass("active");
			$(".errorChart").hide();
		} else {
			$(".viewErrorChart").addClass("active");
		}
		
	});
	
	*/
	
	
	
	
	//allHoldings
	$(".allHoldings").click(function() {
		// etf modal
		
		allHoldingsClickCount ++;
	
		if (allHoldingsClickCount == 1) {
		
		$(".allHoldings").hide();
		$(".allHoldings").empty();	
		}
	});
	
	// map modal -- returns
	$(".map-view-returns a").click(function () {
		$.preloadImages('callout-bottom.gif','callout-top.gif','africa.jpg', 'asia-pacific.jpg', 'canada.jpg', 'europe.jpg', 'latin-america.jpg', 'middle-east.jpg', 'us.jpg');

		function investorAssets() {
			$(".map-view-returns a").addClass("active");
		}		
		
		if ($(".map").is(":hidden")) {
			$(".map").empty();				

			$.ajax({
				url: "/etf/nets.jsp?m=xml/ETFList_Map&s=list_map&caller=returns",
				cache: false,
				success: function(html) {
					$(".map").append(html);
					$(".map").show();
					$("map *").Tooltip({ 
						track: true, 
						delay: 0,
						showURL: false, 
						extraClass: "region", 
						top: -35, 
						left: -90
					});
					return false;
				}
			});		
			setTimeout(investorAssets, 50);
			
		} else {
			$(".map-view-returns a").removeClass("active");
			$(".map").hide();			
		}
	});


	// map modal -- profile
	$(".map-view a").click(function () {

		$.preloadImages('callout-bottom.gif','callout-top.gif','africa.jpg', 'asia-pacific.jpg', 'canada.jpg', 'europe.jpg', 'latin-america.jpg', 'middle-east.jpg', 'us.jpg');
		
		function investorAssets() {
			$(".map-view a").addClass("active");
		}		
		
		if ($(".map").is(":hidden")) {
			$(".map").empty();				

			$.ajax({
				url: "/etf/nets.jsp?m=xml/ETFList_Map&s=list_map&caller=profile",
				cache: false,
				success: function(html) {
					$(".map").append(html);
					$(".map").show();
					$("map *").Tooltip({ 
						track: true, 
						delay: 0,
						showURL: false, 
						extraClass: "region", 
						top: -35, 
						left: -90
					});
					return false;
				}
			});		
			setTimeout(investorAssets, 50);
			
		} else {
			$(".map-view a").removeClass("active");
			$(".map").hide();			
		}
	});
	
	// etf modal
	$(".viewAllHoldings").click(function () {
		var holdingsUrl = "/etf/nets.jsp?m=" + etfXmlName + "&s=holdings_popup";
		function investorAssets() {
			$(".viewAllHoldings").addClass("active");
		}		
		
		function buildHoldings() {
		
			$.ajax({
				url: holdingsUrl,
				cache: false,
				success: function(html) {
					$(".allHoldings").append(html);
					$(".allHoldings").show();
					return false;					
				}
			});		
		}
		
		if ($(".allHoldings").is(":hidden")) {
			viewAllHoldingsClickCount ++;
			
			if (viewAllHoldingsClickCount == 1){
				$(".allHoldings").empty();
			
		
				buildHoldings();			

			//setTimeout(investorAssets, 50);
			$(".viewAllHoldings").removeClass("active");
			}
			
		} else {
			$(".viewAllHoldings").removeClass("active");
			
			//$(".allHoldings").hide();
			//$(".allHoldings").empty();
			
			
		}
	});

	// discount chart modal
	$("a.viewDiscountChart").click(function () {
		//$("#top-10-holdings").css( {position:"absolute"} ); 

		var chartUrl = "/etf/nets.jsp?m=" + etfXmlName + "&s=discount_chart_popup";

		function chartActive() {
			$(".viewDiscountChart").addClass("active");
		}		
		
		if ($(".discountChart").is(":hidden")) {
			$(".discountChart").empty();				

			$.ajax({
				url: chartUrl,
				cache: false,
				success: function(html) {
					$(".discountChart").append(html);
					$(".discountChart").show();
					return false;					
				}
			});		
			setTimeout(chartActive, 50);
			
		} else {
			$(".viewDiscountChart").removeClass("active");
			$(".discountChart").hide();			
		}
	});

	// discount chart modal
	$("a.viewErrorChart").click(function () {
		//$("#top-10-holdings").css( {position:"absolute"} ); 

		var chartUrl = "/etf/nets.jsp?m=" + etfXmlName + "&s=error_chart_popup";

		function chartActive() {
			$(".viewErrorChart").addClass("active");
		}		
		
		if ($(".errorChart").is(":hidden")) {
			$(".errorChart").empty();				

			$.ajax({
				url: chartUrl,
				cache: false,
				success: function(html) {
					$(".errorChart").append(html);
					$(".errorChart").show();
					return false;					
				}
			});		
			setTimeout(chartActive, 50);
			
		} else {
			$(".viewErrorChart").removeClass("active");
			$(".errorChart").hide();			
		}
	});
	
	// Left hand menu
	$("#topLevel dd").hide();
	//if (etfCurrentSection == 'list_profile' || etfCurrentSection == 'faq') {
	//	$("#topLevel dd:eq(1)").show();
	//}
	if (etfCurrentSection == 'edu') {
		$("#topLevel dd:eq(2)").show();
	}
	if (etfCurrentSection == 'about' || etfCurrentSection == 'news') {
		$("#topLevel dd:eq(4)").show();
	}
	
	$("#topLevel dt a").hover(
		function(){
		//alert('hi there s=[' + etfCurrentSection + ']');
		if ( $(this).parent().next().is(":hidden") && !$(this).parent().next().hasClass("empty") ) {
			$("#topLevel dd:visible").slideUp("slow");
			$(this).parent().next().slideDown("slow");
		}
			return false;
		},
		function () {
			return false;
		}
	);
	
	// form field clearing
	$(".autoClear").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	// display "Password" in input field - Login Page
	if ($("#login #signInPassword").size() > 0) {
		$("#login #signInPassword").ready(function() {
			changeInputType(document.login.signInPassword,'text','Password',false,true);
		});
	}

	// display "Password *" in input field - Register Page
	if ($("#register #registerPassword").size() > 0) {
		$("#register #registerPassword").ready(function() {
			changeInputType(document.register.registerPassword,'text','Password *',false,true);
		});
	}

	// PL02 table hover state
	$("#pl-content TABLE.returns TD").hover(function() {
		$(this).parent().addClass("selected");
	}, function() {
		$(this).parent().removeClass("selected");
	});


	// tooltips
	if (typeof $.Tooltip != "undefined") {
		if ($("#splash").size() > 0)
			$("#splash li a").Tooltip({ 
				track: false, 
				delay: 300, 
				showURL: false, 
				opacity: 1, 
				extraClass: "tooltip", 
				top: -10, 
				left: -55
			});
		
	}

	
	// select boxes
	//$("#content select").selectBox({css:'select'});


	// home page tabs
	if (typeof $.tabs != "undefined") {
		$("#home #tabs").tabs();

		// swap tab heading images on click
		$("#home #tabs A.tabLinks").click(function() {
			if (!$(this).parents("LI").hasClass("tabs-selected")) {
				var imgSrc = $(this).find("IMG").attr("src");
				$(this).find("IMG").attr("src", imgSrc.substr(0, imgSrc.length - 4).concat("-active.gif"));

				var imgSelected = $(this).parent().siblings("LI.tabs-selected").find("IMG");
				var imgSelectedSrc = imgSelected.attr("src")
				imgSelected.attr("src", imgSelectedSrc.substr(0, imgSelectedSrc.length - 11).concat(".gif"));
			}
		});
	}

	extContentServer = "http://www-ac.northerntrust.com/";
	// home page flash using SWFObject
	if (typeof SWFObject != "undefined") {
		var so = new SWFObject(etfServerBase + "nets/incs/flash/home.swf", "myMovie", "685", "164", "8");
		var NETSLink = extContentServer + "content/media/attachment/data/product/0801/document/NETS_kit.pdf";
		var NETSLinkNewWindow = "_blank";
		so.addParam("wmode", "transparent");
		so.addParam("quality", "high");
		so.addParam("FlashVars", "contentServer=" + etfServerBase + "&amp;download=" + NETSLink + "&amp;newWindow=" + NETSLinkNewWindow);
		so.write("flashcontent");
		$("#home #masthead").css({background: "none", margin: "0 -4px -6px 0"});
		$("#home #masthead img.title").css({display: "none"});
	}


	// registration page 
	if ($("#register").size() > 0) {
		
		$("#registerPersonaII").click(function() {
			if ($("#registerFP").not(":hidden")) {
				$("#registerFP").slideUp("fast");
			}
		});

		$("#registerPersonaFP").click(function() {
			if ($("#registerFP").is(":hidden")) {
				$("#registerFP").slideDown("fast");
			}
		});


		if (!$("#registerPersonaFP").is(":checked")) {
			$("#registerFP").hide();
		}
	}
	
});

// map highlighting
function highlightRegion(region, state) {
	var imgRegion = region.substring(region.indexOf('-')+1, region.length);
	var imgMap = document.getElementById('themap');
	if( state == 'on') {
		imgMap.src = etfServerBase + 'nets/images/maps/'+imgRegion+'.jpg';
	} else {		
		imgMap.src = etfServerBase + 'nets/images/maps/map.jpg';
	}
}

jQuery.preloadImages = function() {
	//alert('calling preload');
	for(var i = 0; i < arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}