$(document).ready(
	function() {
		LoadPage();
		wrapIt();
		addId();
		hideEm();
		clickAdd();
		closeEm();
		sTables();
		memberSelect();
		myToggles();
		addSubstring();
		chart();
		addFooter();
		rotateBanner();
		costcoPriv();
		addPops();
	});

	function addId() {
		$('span.box_top').each(function(n) {
		$(this).attr('id', 1+n);
		});

		$('span.popwrap').each(function(n) {
		$(this).attr('id', 1+n);
		});
		
		$('a.poplink').each(function(n) {
		 //add has identifier to href
		 var nAdd = 1+n;
		$(this).attr('href', '#'+nAdd);
			//add id to each anchor
		$(this).attr('id', 1+n+'link');
				
		function showWidth(ele, w) {
		//add css margin of anchor width plus some amount
		$("span.box_top").css({'margin-left' : w +15 + 'px'});
			}
				//get width of this anchor
			$(this).mouseover(function () { 
			    showWidth("anchor", $(this).width()); 
			    });
		
			var myW = $('span.popwrap').filter(this.hash);
			var myTop = $('span.box_top').filter(this.hash);
		
			$(this).prependTo(myW);
			myTop.after(this);
		});
	}


function costcoPriv() {
	$('#notoncostco').html('<div id="nc-block">Note:&nbsp;<\/div>\r\n     <div id="notcostco">&nbsp;You are no longer on Costco&rsquo;s site and are subject to the <a href="#" onclick="window.open(\'http:\/\/www.sharebuilder401k.com\/privacy.htm\', \'privacy\', \'location=0,statusbar=0,menubar=0,scrollbars=1,width=500,height=500\')">privacy policy<\/a> of the company hosting this site.\r\n     <\/div>');	
}

function rotateBanner() {
	var r=Math.random();
	if (r>0.5) {
		$('#sidebanner').html('<a href=\'../plans\/sub5.htm?id=7\'><img src="..\/images\/sidebanner-100.png" alt="" \/><\/a>');
	}
}


function chart() {
			$('#compounding').hide();
			$('a.compounding').click( function() {
				$('#compounding').show();
				return false;
			});
			$('a.compoundingclose').click( function() {
				$('#compounding').hide();
				return false;
		  	});
}
	

function wrapIt() {
	
	$('span.pop').wrap('<span class=\"box_bottom\"></span>');
	$('span.box_bottom').wrap('<span class=\"box_top\"></span>');
	$('span.box_bottom').before('<span class=\"box_top_close\"></span>');
	$('span.box_top').wrap('<span class=\"popwrap\"></span>');
	$('body').prepend('<div class="close"></div><div class="fade"></div>');
	}

	function hideEm() {
		$('span.box_top').hide();
		$('div.close').hide();
		$('div.fade').hide();
		
	}
					
	function clickAdd() {
		  
	   $(function() {
		   $(' a.poplink' ).click(
			  function() {
				//$('span.box_top').fadeOut("normal").filter(this.hash).fadeIn("normal");
				$('span.box_top').hide().filter(this.hash).show();
			
			//$('div.close').fadeIn("normal");
			//$('div.fade').fadeIn("normal");
			$('div.close').show();
			$('div.fade').show();
				
				return false;
			  });
	    });
	}
	
	function closeEm() {
		  
	   $(function() {
		   $('div.close').click(
			  function() {
				$('span.box_top').hide();	
				$('div.close').hide();
				$('div.fade').hide();
			  });
		   
		   $('span.box_top_close').click(
			  function() {
				$('span.box_top').hide();	
				$('div.close').hide();
				$('div.fade').hide();
			  });
	    });
	}

function addFooter() {
	var fCopy = '<p>&copy; 2008-2010 ShareBuilder Corporation. ShareBuilder is a registered trademark of ShareBuilder Corporation.<\/p>\r\n          <p>Advisory services are provided by ShareBuilder Advisors, LLC, a registered investment advisor and a subsidiary of ING Bank, fsb which is not affiliated with Costco. By opening an account, you will become a customer of ShareBuilder Advisors, LLC. Costco is not an advisor or broker and is not facilitating your 401k plan or portfolio selections. Costco will have no involvement in any account you establish with ShareBuilder. Costco is not responsible or liable, directly or indirectly, for any information provided by ShareBuilder or for any damage or loss arising out of or in connection with ShareBuilder Advisors or the 401k program.<\/p>\r\n		<p><strong>401k plans are: Not FDIC insured &middot; Not bank guaranteed &middot; May lose value<\/strong><\/p>';
	$('div#footer').html(fCopy);	
}
	
function sTables() {
$(".etf tr:even").css("background-color", "#e5ecff");
$(".etf tr:odd").css("background-color", "#f0f4ff");	
//$(".etf th:contains(Symbol)").css("width", "45px");
$(".etf th:contains(Fund)").css("width", "200px");
$(".etf th:contains(Category)").css("width", "200px");
$(".etf th:contains(Exp. Ratio)").css("width", "40px");

// orange colors #ffab73 and #ffd9bf
$("table.pricing > tbody > tr:even").css("background-color", "#ffab73");
$("table.pricing > tbody > tr:odd").css("background-color", "#ffd9bf");
$("table.pricing th:contains(Assets)").css("width", "75px");
$("table.pricing th:contains(Participant Asset Management Fee**)").css("width", "105px");

//whyus sub2 cost comparison table
$("table.costcomp th").css({'background-color':'#f1f1f1','font-weight':'bold'});
$("table.costcomp tr:even").css("background-color", "#e5ecff"); 
$("table.costcomp tr:odd").css("background-color", "#f0f4ff");	
//$(".etf th:contains(Symbol)").css("width", "45px");

//custom svces
//$("#customservices > tbody > tr:odd").css("background-color", "#f5f5f5");
//$("#customservices> tbody > tr > td > strong").parent().css("background-color", "#e1e1e1");

}

function memberSelect() {
	$('#hideIt table.pricing').slice(0,3).wrapAll('<div id=\"goldstar\"></div>');
	$('#hideIt table.pricing').slice(3,6).wrapAll('<div id=\"executive\"></div>');
	$('#hideIt #executive').nextAll().wrapAll('<div id=\"eElse\"></div>');
	$('#goldstar, #executive, #hideIt, #eElse').hide();
	
		$("select").bind("change", function(){
			var s = $('select').val();
			if(s =='exec') {
			$('#goldstar').hide();
			$('#executive').show();
			$('#hideIt').show();
			$('#plusMinus').attr('src','images/minus.png');
			$('#eElse').show();
			
			}
			else if(s =='gold') {
			$('#goldstar').show();
			$('#executive').hide();
			$('#eElse').show();
			$('#hideIt').show();
			$('#plusMinus').attr('src','images/minus.png');
			}
			else {
			$('#goldstar, #executive').hide();	
			$('#eElse').hide();
			}
		});
}

//toggles for help page
function myToggles () {
  $('#helppg dd').hide();
  $("dt > a").toggle(
      function () {
	   $(this).parent().next().show();
	   var myImg = $(this).children();
	   $(myImg).attr('src','images/minus.png');
      },
      function () {
	   $(this).parent().next().hide();
	    var myImg = $(this).children();
	   $(myImg).attr('src','images/plus.png');
      });
  //toggles for Q & A for products
  $('#qa dl dd').hide();
  $("#qa dl dt").css('cursor','pointer');
  $("#qa dt").bind("mouseover", function() {
	$(this).css('color','#000');
	});
  $("#qa dt").bind("mouseout", function() {
	$(this).css('color','#006');
	});
  $("#qa dt").toggle(
      function () {
	   $(this).next().show();
      },
      function () {
	   $(this).next().hide();
      });
  //smarter toggles for why401k/sub1 and why401k/sub2 and plans/sub5
	$('div.tContent').hide();
	$("h2.toggle > a").click(
	function () {
		var el = $(this).parent().next();
		var elh = $(this).parent().next(':hidden');
		var imageObj = $(this).parent().children().find('img');

		if ( elh.length > 0) {
			el.show();
			imageObj.attr('src', 'images/minus.png');
		}
		else {
			el.hide();
			imageObj.attr('src', 'images/plus.png');
		}
		return false;	
	});
}


//globals remove? no
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function LoadPage() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  	addSubstring();
	//doPopups();
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

//switch style sheets for text sizes
function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

//works for why401k/sub1.htm and why401k/sub2.htm and pricing page
function addSubstring()
    {
      var sCurrentPage = location.href;
      var iPositionQuote0 = sCurrentPage.indexOf("?id=0");
	 var iPositionQuote1 = sCurrentPage.indexOf("?id=1");
	 var iPositionQuote2 = sCurrentPage.indexOf("?id=2");
	 var iPositionQuote3 = sCurrentPage.indexOf("?id=3");
	 var iPositionQuote4 = sCurrentPage.indexOf("?id=4");
	 var iPositionQuote7 = sCurrentPage.indexOf("?id=7");
      if(iPositionQuote0 > 0) {
	$('#plusMinus').attr('src','images/minus.png'); 
	$('#toggleIt').show();
	} 
	else if(iPositionQuote1 > 0) {
	$('#plusMinus1').attr('src','images/minus.png'); 
	$('#toggleIt1').show();
	} 
	else if(iPositionQuote2 > 0) {
	$('#plusMinus2').attr('src','images/minus.png'); 
	$('#toggleIt2').show();
	} 
	else if(iPositionQuote3 > 0) {
	$('#plusMinus3').attr('src','images/minus.png'); 
	$('#toggleIt3').show();
	} 
	else if(iPositionQuote4 > 0) {
	$('#plusMinus2').attr('src','images/minus.png'); 
	$('#hideIt2').show();
	} 
	else if(iPositionQuote7 > 0) {
	$('#plusMinus').attr('src','images/minus.png');
	$('#hideIt').show();
 	}
}

function addPops() {
	//add target blank

	$('a[href^="http"]').attr('target','_blank');
	//go direct for get a quote link
	$('img[alt^="Get a free quote"]').parent().removeAttr('target');
	//go direct for Log in
	$('a:contains(Log in)').removeAttr('target');
	//run pdf links through js redirect file for tracking
	$('a[href$=".pdf"]').each(function(n) {
		var redir = $(this).attr('href');
		redir = 'http://401kpricing.com/costco/pdf_redirect.htm?f=' + redir;
		//$(this).attr('title', redir);
		$(this).attr('href', redir);
	});
};



