/* Global Items */
$(document).ready(function() {
	
	$("li:last-child, #secondary > div:last").addClass("last");
	$("li:first-child").addClass("first");
	$(".choicegrid tr th:first-child, .choicegrid tr td:first-child").addClass("first");
	$(".section-survey .question:last").addClass("question-last");
    $(".topic-explore p:first-child").addClass("first");
    $("#questions li:first-child").addClass("first");
    $("#aboutUsNav li:first-child").addClass("first");
    $(".help li:first-child").addClass("first");
    $(".popup .buttonsSplitLeft li:first-child").addClass("first");

    $("#more_icons").click(function() {
        $("#hidden_popup").addClass("show_popup");
    });
    
    $('#hidden_popup').click(function(e)
	{
		e.stopPropagation();
	});
    
    $('body').click(function(event) {
        if(event.target.className != "more") {
           event.stopPropagation();
           $('#hidden_popup').removeClass("show_popup");
           $('#show_all').removeClass("show_popup");
           $('#more_link').removeClass("hide_link"); 
        }
    });
    
    $('#close_button').click(function() {
        $('#hidden_popup').removeClass("show_popup");
        $('#show_all').removeClass("show_popup");
        $('#more_link').removeClass("hide_link");
    });
    
    $('#more_link').click(function() {
       $('#show_all').addClass("show_popup");
       $('#more_link').addClass("hide_link");
       return false;
    });
	

	$("#subnav-links > li").each(function(){
		$(this).css('height', $(this).height()+5);
	});
	$(".dropdown ul").each(function(){
		$lis = $(this).find("li");
		$inc = parseInt(($lis.length/4) + ($lis.length % 4 > 0 ));
		$.map([$inc, $inc*2, $inc*3, $inc*4], function(value) {
			$lis.slice(value-$inc, value).wrapAll("<div></div>");
		});
	});
	$(".dropdown").hover(function(){
		$(this).children("a, ul").addClass("active");
		$(".filter select").addClass("hidden"); // also, hide the select for ie6
		}, function(){
		$(this).children("a, ul").removeClass("active");
		$(".filter select").removeClass("hidden"); // ...then reveal the select for ie6
	}).children("a").click(function(){return false;});

	$(".choicegrid tr:nth-child(even), .choicelist li:nth-child(odd)").addClass('stripe');

	$(".filter").each(function(){
		$(this).find("#bydate").click(function(){
			$(this).parent("li").addClass("active");
			$("#bytype").parent("li").removeClass("active");
			$("#filter-date").css('display', 'block');
			$("#filter-type").css('display', 'none');
			return false;
		});
		$(this).find("#bytype").click(function(){
			$(this).parent("li").addClass("active");
			$("#bydate").parent("li").removeClass("active");
			$("#filter-type").css('display', 'block');
			$("#filter-date").css('display', 'none');
			return false;
		});
	});



	// hover switch
	$("input[type='image']").rolloverSubmit();



	$("input[type='text']:not(.input-text-retain)").each(function(){
		this.orig_value = $(this).val();
		$(this).focus(function(){ 
			if ( $(this).val() == this.orig_value) 
				$(this).val(''); 
		}).blur(function(){
			if ( ($(this).val() == this.orig_value) || (!$(this).val().length)) 
				$(this).val(this.orig_value);
		});
	});


	$("#refinesearch a").click(function(){
		$("#refinesearch").toggleClass("refine-active");
		$("#refinesearch-form").toggle();
		return false;
	})	

	// date picker	
		if ($('input.date-pick').size() > 0) {
			$('input.date-pick').datePicker();
			$('input.date-pick').dpSetStartDate('01/01/1950'); // allows for selecting dates in the past
		}



	// "topic main" exceptions for 2cols or 1col
	
	$('div.topics').each(function(){
		var ct = $(this).children().size(); 
		if(ct == 2) {
			$(this).addClass("topics-two");
		} else if(ct == 1) {
			$(this).addClass("topics-one");
		}
	});


	$("#survey-submit").click(function() {
		if (parent) parent.location.hash = '#';
	});



});

