/*
 *
 * PBGC JS Utilities
 *
 * Create private scope to allow usage of jQuery "$" namespace 
 * without fear of conflicts (for document.ready)
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
(function($) { 
	$(document).ready( function() {

		// Add focus/blur events to text inputs/areas 
		$("input[type=text]").inputFocusToggle();
		$("textarea").inputFocusToggle();
	
		if ($(".carousel").length > 0)
			$(".carousel").initCarousel();

		$("#home .slim_module:last").addClass("last");
		
		// Text Resize function calls 
		initTextResize('.article', '.text_small', '.text_normal', '.text_large');
		initTextResize('.FAQ', '.text_small', '.text_normal', '.text_large');
		initTextResize('.other', '.text_small', '.text_normal', '.text_large');
		
		$('a.print').click(function() { window.print(); return false; });
		$('a.email').click(function() {
			mail_str = 'mailto:?subject=Check out this page at PBGC: ' + document.title;
			mail_str += '&body=I thought you might be interested in this page on the PBGC website';
			mail_str += '. You can view it at: ' + location.href;
			location.href = mail_str;
			return false;
		});
		
		// Clean up A-Z Index page
		twoColumnIndex('.az_letters', '.az_nav', 'az_letters_second'); 
		
		// Accordion Elements
		$("ul.accordion").initAccordion();
		
		// Left Tab Container
		$("div.left_tabs").initLeftTabs();
		
		// Split list into two columns
		$(".plan_lists ul").splitList();
		
		// IE6: Main Nav Hover Fix
		$("ul#nav li").hover(
			function () { // Hover In
				$(this).addClass("hover");
			},
			function () { // Hover Out
				$(this).removeClass("hover");			
			}
		);
	}) // end document.ready
	
})(jQuery); 


// Outside of private scope, used to ensure no conflicts
jQuery.noConflict(); 



/*
 *
 * Featured Carousel Function
 *
 * 	Creates featured carousel based on this expected markup:
 * 	
 *		<div class="carousel">
 *			<ul class="slides">
 * 				<li> ... </li>
 *				...
 *			</ul>
 * 		</div> 
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
jQuery.fn.initCarousel = 

	function() {
		
		var carousel = jQuery(this);
		var slides   = jQuery(this).find(".slides");
		
		/** Initiaing Cycle plugin */
		slides.cycle({ 
		    fx:     "fade", 
		    speed:  "slow", 
		    timeout: 5000, 
		    next:   ".controls li a.next", 
		    prev:   ".controls li a.prev" 
		});		
		
		/** Setting click events for carousel controls */		
		carousel.find(".controls li a.pause").live("click", function() {
				carousel.find(".slides").cycle("pause");
				jQuery(this).removeClass("pause").addClass("play").text("Play");
		});
		
		carousel.find(".controls li a.play").live("click", function() {
			carousel.find(".slides").cycle("resume");
			jQuery(this).removeClass("play").addClass("pause").text("Pause");			
		});				
		
	} // end initCarousel()


/*
 *
 * Text Resize Function
 *
 * 	Used for setting up text resizing using javascript.  This should be used after the document is loaded.
 * 
 * 	@param resizedClass	- a selector of the element that should be resized    - '.news_article'
 * 	@param sizeSmaller	-   a selector of the element used as a trigger to shrink text -  '.text_small'
 * 	@param sizeDefault	-   a selector of the element used as a trigger to return the text to default size - '.text_normal'
 * 	@param sizeLarger	-   a selector of the element used as a trigger to enlarge text - '.text_large' *
 *
 *	example: initTextResize('.news_article', '.text_small', '.text_normal', '.text_large'); 
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	

    function initTextResize(resizedClass, sizeSmaller, sizeDefault, sizeLarger) {
			jQuery(resizedClass).each( function() {
				var elem = jQuery(this);
		        // Save original font-size
		        var defaultSize = elem.css('font-size');
		        
		        /* Click events for text increase/decrease */        
		        jQuery(sizeSmaller).click(function(){
					elem.css('font-size', parseInt(elem.css('font-size').replace('px', ''))-1+'px');
					
					return false;  // Prevent default
		        });
		
		        jQuery(sizeDefault).click(function(){
					elem.css('font-size', defaultSize);
					
					return false; // Prevent default
		        });
		        
		        jQuery(sizeLarger).click(function(){
					elem.css('font-size', parseInt(elem.css('font-size').replace('px', ''))+1+'px');

					return false; // Prevent default
		        });
		});
    } // end initTextResize ()
  
    
/*
 *
 * Two Column Index Function
 *
 *	On the A-Z Index page this function is responsible for taking an index and separating 
 *  it dynamically into two columns for display to the user.
 *
 *	 results (letterList) should be in the form of:
 *        <ul>
 *            <li>
 *               <h2>Navigation element</h2>
 *               <ul>
 *                   <li>Navigation result</li>
 *               </ul>
 *           </li>
 *       </ul>
 *       
 *  If a navigation element has no navigation results, a <ul> containing no <li> elements should be present*
 *
 *  navigation (navList) should be in the form of: 
 *  <ul>
 *       <li>
 *      	<a href="#">Navigation element</a>
 *  	</li>
 *  </ul>
 *       
 *  Note that the navigation element present in navList must be identical to navigation elements in letterList
 *
 * 	@param letterList - a selector of the element that is the list of elements that the navigation points to - '.az_letters'
 * 	@param navList - a selector of the element that is the list of elements that acts as navigation - '.az_nav'
 * 	@param secondList - the name you wish to use for the second column of results - 'az_letters_second'
 *
 *	example: twoColumnIndex('.az_letters', '.az_nav', 'az_letters_second'); 
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	    
 
    function twoColumnIndex(letterList, navList, secondList) {
    
        //Hide letters that don't have entries
        jQuery(letterList + ' li ul').each(function(){
            if (jQuery(this).children().length == 0){
                jQuery(this).parent().hide();
            }
        });
        //create the second column
        jQuery(letterList).after('<ul class="'+secondList+'"></ul>');
        //grab how many valid letters we have
        var letters = jQuery(letterList).children('li:visible');
        var letters_length = letters.length;
        
        //cut them in half to put in the second column
        for (var i = Math.floor(letters_length/2); i < letters_length; i++){
            jQuery('.'+secondList).append(letters[i]);
        }
        //find all the empty letters and remove them
        var bad_letters = [];
        jQuery(letterList).children('li:hidden').each(function(){
            bad_letters += jQuery(this).children('h2').text();
            jQuery(this).remove();
        });
        //also remove the links to the empty letters
        jQuery(navList +' li a').each(function(){
            var letter = jQuery(this).text();
            if (jQuery.inArray(letter, bad_letters) >= 0 ){
                jQuery(this).replaceWith(letter);
            }
        });
    }

/*
 *
 * Create Expand/Collapse Accordion List
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */    
    jQuery.fn.initAccordion = 
    
    	function() {

    		var accordion  = jQuery(this);
    		
    		/* Set height of the accordion div's to fix "jerky" animation */	
    		accordion.find("li").each( function() {
    			var elem = jQuery(this).find("div");
    			elem.hide();
    			elem.parents("li").removeClass("active");
    		});
    		
    		
    		/* Click events for accordion links */
    		accordion.find(".arrow").click( function() {
				
				var clicked_li = jQuery(this).parents("li");
				
    			clicked_li.find("div").toggle();
    			
    			if (clicked_li.hasClass("active")) {
    				clicked_li.removeClass("active");
    			} else {
    				clicked_li.attr("class","active");  				
    			}  
    			
    			return false; // prevent default action  		
    		});


    		/* Click events for accordion links */
			var accordion_options = accordion.prev(".accordion_options");

			// "Expand All" link clicked
			accordion_options.find(".expand").click( function() {
				accordion.find("div").slideDown("fast");
				accordion.find("li").attr("class","active");

    			return false; // prevent default action				
			});
			
			// "Collapse All" link clicked
			accordion_options.find(".collapse").click( function() {
				accordion.find("div").slideUp("fast");
				accordion.find("li.active").removeClass("active");
    			
    			return false; // prevent default action
    							
			});


			
    	}

/*
 *
 * Input Toggle Plugin
 *
 * This plugin handles text input blur/focus auto remove/addition
 *  of default text.
 *
 * On focus - default text is removed
 * On blur  - default text is added
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
jQuery.fn.inputFocusToggle = 

	function() {
		jQuery(this).each( function() {
		
			// Cache "this"
			var input_field = jQuery(this);
			
			// Grab default text	
			var default_text = input_field.val();
	
			// Check if default text exists (this is used to skip over input fields in forms)
			if (default_text != ""){
			
				// Removes text on focus
				input_field.focus( function() {
					if (input_field.val() == default_text){
						input_field.val("");
					}
				});
				
				// Inserts default text on blur
				input_field.blur( function() {
					if (input_field.val() == ""){
						input_field.val(default_text);
					}	
				});
			}
		});
	} // end jQuery.fn.inputToggle
    

/*
 *
 * Left Tab Container Plugin
 *
 * This plugin creates working left (vertical) tabs, that hide/show content
 *
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
jQuery.fn.initLeftTabs = 

	function() {
		jQuery(this).each (function() {
			
			var elem = jQuery(this);
			var tabs = elem.find("ul.tabs li");
			
			// Adds the click event to the left tabs
			tabs.find("a").click( function() {
				// When a tab is clicked, its corresponding content is shown
				elem.find(".active").removeClass("active");
				
				var clicked_li = jQuery(this).parent();
				clicked_li.addClass("active");
				
				var idx = tabs.index(clicked_li);
				elem.find("div.tab_content:eq("+idx+")").addClass("active");
				
				return false; // prevent default action
			});
		
		});
	} 

/*
 *
 * Split List
 *
 * Splits a given list into two
 *
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
jQuery.fn.splitList =

	function () {
	
		var list = jQuery(this);
		var items = list.find("li"); 
		var len = Math.floor(items.length / 2);
		
		var col1 = items.slice(0,len);
		var col2 = items.slice(len+1, items.length);
		
		var ul1 =  jQuery("<ul class=\"col_1\"></ul>").append(col1);
		var ul2 =  jQuery("<ul class=\"col_2\"></ul>").append(col2);
		
		list.parent().append(ul1, ul2);
		
		list.remove();

	}	 
	
function surfto(jump) {
	var path = window.location.protocol + "//" + window.location.host + "/";
	var i = jump.selectedIndex;
	var selection = jump.options[i].value;
	var url;
	if (i != "0") {
		 url = path + selection;
		 location.href = url;
	}	 
}
function togglebutton (form) {
	if(form.release_archives.selectedIndex === 0) {
		form.btJump.disabled = true;
	}
	else {
		form.btJump.disabled = false; 
	}	
 }
 
 //popup for demo
function popdemo (dest) 
{
	demowin = window.open(dest, "demowin", "status=no,toolbars=no,location=no,menubar=no,scrollbars=no,width=785,height=530");
	demowin.focus();
}
