jQuery(document).ready(function() {	

	if (jQuery('.year_selector_link').length > 0) {
	
		var year_link = "";
		var year_submit_object = 0;
	
		jQuery('body').append('<div class="year_selector_overlay"><div class="year_selector_inner"><div class="year_selector_close"><a href="#"><img src="/images/grey_close_white_bg.png" /></a></div>    <h3>Please select a plan year</h3><p><a href="" id="2012_select_link"><img src="/images/unselected.png" class="faux_radio" alt="2012">2012 Plan Year</a></p><p><a href="" id="2011_select_link"><img src="/images/unselected.png" class="faux_radio" alt="2011">2011 Plan Year</a></p><input type="image" src="/images/year_selector_go_btn.png" /></div></div>');
		
		if ( (jQuery.browser.msie == true) && (parseInt(jQuery.browser.version) < 8) ) {
			jQuery('.year_selector_inner h3').css( 'padding-top', '17px' );
		}
  
		jQuery('.year_selector_link').click(function() {
			year_link = "";			
			if (this.nodeName == "INPUT") { //special case for form submission links	
				year_submit_object = this;
				var twourls = year_submit_object.parentNode.action.split("?|");
				if (twourls.length == 2) {					
					document.getElementById('2011_select_link').href = twourls[0];
					document.getElementById('2012_select_link').href = twourls[1];
				} else {
					return true;
				}
			} else {
				var twourls = this.href.split("?|");
				document.getElementById('2011_select_link').href = twourls[0];
				if (twourls.length == 2) { //ordinairy links
					document.getElementById('2012_select_link').href = twourls[1];
				} else { //special case for 'Find Plans & Costs' header link
					if (twourls[0].search('staging') >= 0) { //staging
						document.getElementById('2012_select_link').href = twourls[0].replace(/2011\/Home\/IndivLanding.aspx/, "2012/Home/Home.aspx");
					} else { //prod
						document.getElementById('2012_select_link').href = twourls[0].replace(/INDIVIDUAL/, "2012/individual");
					}
				}
				
				year_submit_object = 0;
			}
			if (jQuery('.year_selector_overlay').modal({overlayId: 'year_selector_overlay'}) == false) {
				//modal launch failed because another modal is already displaying
				jQuery.modal.close();
				setTimeout("jQuery('.year_selector_overlay').modal({overlayId: 'year_selector_overlay'})", 100);
			}
			jQuery('.year_selector_overlay .year_selector_close').click(function() {
				jQuery.modal.close();
				return false;
			});
			return false;   
		});
	  
		jQuery('.year_selector_inner p a').click(function(e) {
			if ( jQuery(this).find('img').attr('src').search('/images/unselected.png') >= 0 ) {
				jQuery('.year_selector_inner p a .faux_radio').attr('src', '/images/unselected.png');
				jQuery(this).find('img').attr('src', '/images/selected.png');
				year_link = jQuery(this).attr('href');
			}
			e.preventDefault();
		});
	  
		jQuery('.year_selector_inner input').click(function() {
			if (year_link.length > 0) {
				if (year_submit_object == 0) {
					window.location = year_link;
				} else { //special case for form submission links
					year_submit_object.parentNode.action = year_link;
					year_submit_object.click();
				}
			} else {
				alert('Please make a selection.');
			}
		});  
	
	}
	
});

