/*
	function toggleHoldings

	used on the Details Pages - Holdings tab.

	Paramters: none

	will toggle the the view all for the Top Holdings table

 ************************************/
function toggleHoldings(){
	if ($('allHoldings').visible()){

		$('allHoldings').hide();
		$('linkViewAllHoldings').innerHTML = "VIEW ALL HOLDINGS";
		//Effect.SlideUp(id);
	} else {
		$('allHoldings').show()
		$('linkViewAllHoldings').innerHTML = "SHOW ONLY TOP 10 HOLDINGS";
		//Effect.SlideDown(id);
	}
}

function hideFlash(){
	if (navigator.userAgent.indexOf("Safari") > 0) {
		$$('object').invoke('hide');
		$$('embed').invoke('hide');

	}
}

function showFlash(){
	if (navigator.userAgent.indexOf("Safari") > 0) {
		$$('object').invoke('show');
		$$('embed').invoke('show');


	}
}

function hideSelect(){
	$$('select').invoke('hide');

	$$('#overlay select').invoke('show');

}


function showSelect(){
	$$('select').invoke('show');

}

function openWindow(h, w, url)
{
var l=(screen.width-w)/2; /*this centers horizontally*/
var t=(screen.height-h)/2; /*this centers vertically*/


var features="left="+l+",top="+t+",width="+w+",height="+h+",scrollbars=auto,menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,fullscreen=no,channelmode=no";
window.open(url,"Investor Verification",features);

}

/*


 *******************************/
function toggleDetailDiv(id){
	/* used on details page
	   id = ['Overview','Holdings','Performance']
	   name of div to display and also switch the tab to on state */
	currentID = id;
    if (!id) { id = "Overview";}
    $$('#tabContents div.tabContent').invoke('hide');
    $$('#details div.detail').invoke('hide');

	$$('#div'+id).invoke('show');

	/* swap img */

	    $$('#tabList li').each(function(i){
			i.observe('click', function(){
			if(!i.hasClassName('arrow'))
				{
				$$('#tabList li').each(function(j){
					j.removeClassName('current');
				});

			    i.addClassName(' current');
				}
			});

			if (i.hasClassName('li'+currentID) && !(i.hasClassName('current'))){
			    i.addClassName(' current');
			}

	   });

}

 function toggleFundTabs(id, ticker){
	/* used on details page
	   id = ['Overview','Holdings','Performance']
	   name of div to display and also switch the tab to on state */
	currentID = id;
    if (!id) { id = "Overview";}
    $$('#tabContents div.tabContent').invoke('hide');
    $$('#details div.detail').invoke('hide');

	$$('#div'+id).invoke('show');

	/* swap img */

	    $$('#tabList li').each(function(i){
			i.observe('click', function(){
			if(!i.hasClassName('arrow'))
				{
				$$('#tabList li').each(function(j){
					j.removeClassName('current');
				});

			    i.addClassName(' current');
				}
			});

			if (i.hasClassName('li'+currentID) && !(i.hasClassName('current'))){
			    i.addClassName(' current');
			}

	   });

}

function toggleDiv(id){
//	  console.log (id);
	  /* id == the id of div to show or hide
	  	 state = true - open
		 state = false - close */
		if ($(id).visible()){

			$(id).hide()
			//Effect.SlideUp(id);
		} else {
			$(id).show()
			//Effect.SlideDown(id);
		}
  }





/*
 function addMouseOvers

 adds mouseover on off states to all images and input images with class="hasMouseOver"

 class="hasMouseOver" is required for images with an on off state

 images must be named:
 	off state: [imagename].gif 			ex: masthead_register.gif
 	on state:  [imagename]_on.gif 		ex: masthead_register_on.gif


 *******************************/
function addMouseOvers(){
	/* create an array with elements with class='hasMouseOver' */
	tmpArr = [$$('img.hasMouseOver'),$$('input.hasMouseOver')].flatten();

	/* adds img hover state to all images with class='hasMouseOver' */
	tmpArr.each( function(i){
		// preload the mouseover img
		i.oversrc = i.src.gsub('.gif','_on.gif');

		// mouse out event
		i.observe('mouseout',function(){

			i.src = (i.src.indexOf('_on.gif')>-1) ? i.src.gsub('_on.gif','.gif') : i.src;

		});

		// mouseover event
		i.observe('mouseover',function(){

			i.src = (i.src.indexOf('_on.gif')>-1) ? i.src :  i.oversrc;

		});

	});

}

/*
 function userLogin

 temporary function

 if user clicks 'login' show logged in view

 *******************************/

function userLogin(){

	$$('#loginBox').invoke('hide');
	$$('#userBox').invoke('show');

	$$('#register').invoke('hide');
	$$('#settings').invoke('show');


}



/*
 function sfHover

 adds mouseover and mouseout states to the main navigation

 *******************************/

function sfHover () {

	$$('#menu li').each(function(menuitem){
		/* add mouse events to list items with child elements */
//		if(menuitem.immediateDescendants().size()>1){
			menuitem.observe('mouseout',function(){
				showFlash();
				showSelect();
				$('menu').getElementsByClassName('sfhover').invoke('removeClassName', 'sfhover');
			});

			menuitem.observe('mouseover',function(){
				hideFlash();
				hideSelect();
				menuitem.addClassName(' sfhover');

			});
//		}
	});


}

/*
 function gotoResults
 function gotoMFResults

 Used for the Enter Ticker Field in the nav bar.
 Once the user enters a search item, and selects the item,
 the functions below will process that data - usually a redirect
 to the details page of the fundname the user has selected

 *******************************/
function gotoResults(txt, li){

	  window.location.href='ETF_Details.html';

}

function gotoMFResults(txt, li)
{

	window.location.href='MF_Details.html';

}


/*
 function toggleOverlay

 param id = the id of container div of content
 			to be displayed in the overlay

 *******************************/
function toggleOverlay(src,id){
//	console.log(navigator.vendor);
showFlash();
	showSelect();

	$$('div.overlay_content').invoke('hide');

	if (id == null){
		$$('#overlay').invoke('hide');
	} else {
		hideFlash();
		hideSelect();





		// extend transparent background
		Element.setStyle('overlay',{height:document.body.offsetHeight+'px'});
		$$('#overlay').invoke('show');
		$$('#'+id).invoke('show');
		// position the overlay content within the viewport
//if($('overlay_container').getHeight() < 800)
		//{
		//	Element.setStyle('overlay_container',{top:((Position.realOffset(src)[1]+screen.availHeight/4)-(screen.availHeight-($('overlay_container').getHeight()))/2)+'px'});
		//}
		//else
		//{
			Element.setStyle('overlay_container',{top:((Position.realOffset(src)[1]+screen.availHeight/4))+'px'});
			Element.setStyle('overlay_container',{height:(screen.availWidth/4)+'px'});
		//}
//		console.log(Position.realOffset(src)[1]+'     '+screen.availHeight/4);




	}
}
function replaceInlineEditorText(content){

    // Standardize the XML notations to Ascii characters.
    content = content.replace(/&nbsp;/g, " ");                // Space            -> Ascii Single Quote.
	content = content.replace(/&apos;/g, "'");                // Apostrophe            -> Ascii Single Quote.
    content = content.replace(/&#39;/g, "'");                 // Escaped Apostrophe    -> Ascii Single Quote.
    content = content.replace(/&lsquo;/g, "'");               // Left single quote     -> Ascii Single Quote.
    content = content.replace(/&rsquo;/g, "'");               // Right single quote    -> Ascii Single Quote.
    content = content.replace(/&quot;/g, '"');                // Double quote          -> Ascii Double Quote.
    content = content.replace(/&#34;/g, '"');                 // Escaped quote         -> Ascii Double Quote.
    content = content.replace(/&ldquo;/g, '"');               // Left Double quote     -> Ascii Double Quote.
    content = content.replace(/&rdquo;/g, '"');               // Right Double quote    -> Ascii Double Quote.
    content = content.replace(/&minus;/g, '-');               // Minus                 -> Ascii Minus Sign.
    content = content.replace(/&ndash;/g, '-');               // Short Dash            -> Ascii Minus Sign.
    content = content.replace(/&mdash;/g, '-');               // Long Dash             -> Ascii Minus Sign.
    // Return the nice clean content.
    return content;


}
function setTooltips(){
	/* create an array with elements with class='hasMouseOver' */
	tmpArr = [$$('td.hasTooltip'),$$('p.hasTooltip'),$$('a.hasTooltip'),$$('img.hasTooltip')].flatten();

	tmpArr.each(function(element){
		/* create the tooltip div */
		element.tooltip = $(document.createElement("div"));
      	document.body.appendChild(element.tooltip);
      	element.tooltip.addClassName("tooltip");

		element.tooltipContent = $(document.createElement("div"));
      	element.tooltip.appendChild(element.tooltipContent);
      	element.tooltipContent.addClassName("tooltip_content");

		/* assign the tooltip text from the title attribute */
		element.tooltipContent.innerHTML = element.readAttribute('title');
		/* remove title attribute to prevent default tooltip from appearing */
		element.removeAttribute("title");

		element.tooltip.hide();

		/* add mouse event to show and hide the tooltip */
		element.observe('mouseout',function(){
		  	element.tooltip.hide();
		});

		Event.observe(element,'mouseover',function(event){
			/* position the tooltip */
			var mouseX = Event.pointerX(event);
			var mouseY = Event.pointerY(event);
			x_pos = mouseX - (element.tooltip.getWidth());
			y_pos = mouseY - (element.tooltip.getHeight()) ;

			/* check to make sure tooltip will appear with the viewport */
			y_pos = (Position.realOffset(element.tooltip)[1]>y_pos)? Position.realOffset(element.tooltip)[1] : y_pos;
			element.tooltip.setStyle({
				left: x_pos+'px',
				top: y_pos+'px'
			});

		  	element.tooltip.show();

		});


	});
}
var ie7 = (navigator.appVersion.indexOf('MSIE 7.')==-1) ? false : true;
function setClickTooltips(){
	/* create an array with elements with class='hasMouseOver' */
	tmpArr = [$$('td.hasClickTooltip'),$$('p.hasClickTooltip'),$$('a.hasClickTooltip'),$$('img.hasClickTooltip')].flatten();

	tmpArr.each(function(element){
		/* create the tooltip div */
		element.tooltip = $(document.createElement("div"));
      	document.body.appendChild(element.tooltip);
      	element.tooltip.addClassName("tooltip");

		element.tooltipContent = $(document.createElement("div"));
      	element.tooltip.appendChild(element.tooltipContent);
      	element.tooltipContent.addClassName("tooltip_content");

		/* assign the tooltip text from the title attribute */
      	element.tooltipContent.innerHTML = replaceInlineEditorText(element.readAttribute('name'));
		/* remove title attribute to prevent default tooltip from appearing */
		element.removeAttribute("name");

		element.tooltip.hide();

		/* add mouse event to show and hide the tooltip */
		if(ie7 != true)
		{
		Event.observe(element, 'mouseout',function(){
		  	element.tooltip.hide();
		});
		}
		else
		{
Event.observe(element, 'blur',function(){
		  	element.tooltip.hide();
		});
		Event.observe(element.parentNode, 'mousemove', function(){
		  	element.tooltip.hide();
		});
		}

		/*Event.observe(element,'mouseover',function(event){ //changing mouseover to click // */
		Event.observe(element,'click',function(event){
			/* position the tooltip */
			var mouseX = Event.pointerX(event);
			var mouseY = Event.pointerY(event);
			x_pos = mouseX - (element.tooltip.getWidth());
			y_pos = mouseY - (element.tooltip.getHeight()) ;

			/* check to make sure tooltip will appear with the viewport */
			y_pos = (Position.realOffset(element.tooltip)[1]>y_pos)? Position.realOffset(element.tooltip)[1] : y_pos;
			element.tooltip.setStyle({
				left: x_pos+'px',
				top: y_pos+'px'
			});

		  	element.tooltip.show();

		});


	});
}

/**
 * Scans through all hrefs to determine whether a link should display
 * a disclaimer before exiting the current site.
 *
 * Links to be excluded from the disclaimer will need to
 * be added to arrLinks array
 *
 * @name prepLinks
 * @param none
 *
 */
function prepLinks(x){
	arrLinks = new Array ();
	arrLinks =x.split(",");

	$$("a").each(function(link){
		var url = link.readAttribute('href');

		link.observe("click",function(e){
			var isExternal = true;  // true - show disclaimer then open new window
									 // false - open new window

			if (url.indexOf("http")>-1){
				Event.stop(e);

				// link is on external host
				arrLinks.each(function(e){
					if (url.toLowerCase().indexOf(e.toLowerCase()) > -1){
						isExternal = isExternal && false;
					}
				});

				if(x.length <= 0)
				{
					isExternal = true;
				}

				if (isExternal){
				var i = 0;
					addDisclaimer();

					toggleOverlay(link,"confirmExit");

					$('cont_cancel').observe("click",function(event){i++;});
					$('cont_close').observe("click",function(event){i++;});
					$('cont_logo').observe("click",function(event){i++;});
					$('cont_exit').observe("click",function(event){
						// open the link in a new window

						if(i == 0){
							window.open(url,"_blank");
							toggleOverlay('overlay');
						}
							i++;
						});


				}else{
					window.open(url,"_blank");
					return false;
				}



			}else{
				// link is on same host
				// do nothing
			}

		})
	})
}

/**
 * adds disclaimer copy into the overlay
 *
 * @name addDisclaimer
 * @param none
 *
 *
 */
function addDisclaimer(){
	if ($$('#overlay').length>0){
		// overlay already exists do nothing
	}else{
		// add div elements for overlay
		new Insertion.Bottom("wrapper",'<div id="overlay" style="display:none"></div>');
		new Insertion.Bottom("overlay",'<div id="overlay_container"><div class="clear"></div></div>');
		new Insertion.Bottom("overlay",'<div id="overlaybg"></div>');
	}

	if ($$('#confirmExit').length>0){
		// do nothing
	}else{
		new Insertion.Top("overlay_container",'<div id="confirmExit" class="overlay_content" style="display:none;"/>');
		new Insertion.Bottom("confirmExit",'<p class="closebtn"><a href="javascript:void(0);" onclick="toggleOverlay()" class="logo" id="cont_logo"></a><a href="javascript:void(0);" onclick="toggleOverlay()" id="cont_close">x  CLOSE</a><br clear="all" /></p>');
		new Insertion.Bottom("confirmExit",'<div class="clear"></div><p><br />You are leaving SSgAFunds.com. This link is provided as a courtesy for informational purposes only and leads to web pages which we do not maintain. We do not provide any information directly to the linked website, nor do we endorse or affirm any of the information provided by it. We make no representations or warranties with respect to the information contained on the site and we take no responsibility for supplementing, updating or correcting any such information. By providing this link, we are not providing you with investment advice or offering securities for sale to you.</p>')
		new Insertion.Bottom("confirmExit",'<p><a href="#" class="continue" id="cont_exit"><img src="/site-content/images/buttons/btn_ok.gif" class="hasMouseOver" alt="Continue"/></a><a href="javascript:void(0);" onclick="toggleOverlay()"><img src="/site-content/images/buttons/btn_cancel.gif" class="hasMouseOver" alt="cancel" id="cont_cancel"/></a></p>');
	}
}

function addDisclaimerEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

document.observe('dom:loaded', function() {
	/* swap img: combind the click event to elements like "#tabList li"*/
	$$('#tabList li').each(function(i){
		i.observe('click', function(){
			if(!i.hasClassName('arrow'))
			{
				$$('#tabList li').each(function(j){
					j.removeClassName('current');
				});
				i.addClassName(' current');
			}
		});
	});
});

function openNewWindow(URLtoOpen, windowName, windowFeatures)
{
    newWindow=window.open(URLtoOpen, windowName, windowFeatures);
}

function openSpotlightNewWindow(myURL, site_section, windowFeatures){
    openNewWindow(myURL, "page", windowFeatures);
    dcsMultiTrack('WT.z_site_Section', site_section, 'WT.z_site_visits','1');
}
function openSpotlightSameWindow(myURL, site_section){
    window.location=myURL;
    dcsMultiTrack('WT.z_site_Section', site_section, 'WT.z_site_visits','1');
}