var nested_checkbox_lists = window.nested_checkbox_lists || {};

var overall_selected_count = 0;

nested_checkbox_lists.update_count = function(jq_object){
	//Takes a jQuery object which sits within a <div class="fake_select"> element
	//that can be an <input> or <div> or whatever, so long as it's within a fake_select div.
	//Updates the "x selected" count (or "item1 item2" summary, whichever will fit) in the fake select div
	use_count = false;
	fake_select = jq_object.closest("div.fake_select");
	info_bar = fake_select.find(".selected_info").first();
	summary_span = info_bar.find(".selected_summary").first();
	selected_inputs = fake_select.find("input:checked");
	if(selected_inputs.length == 0){
		use_count = true;
	}else{
		//collect up the labels for all of the selected inputs, and concatenate them into a string for the summary
		summary_text = '';
		selected_inputs.each(
			function(){
				label = $("label[for='"+ $(this).attr('id') +"']").first();
			    summary_text += label.text() + ", "; // TODO: use LIST_JOINER
			}
		);
		summary_text = summary_text.replace(/, $/, ""); //remove the last comma - TODO MAKE THIS WORK WITH LIST_JOINER
		//hide the "x selected" count and put the summary text in its place
		info_bar.find(".selected_count").first().hide();
		summary_span.text(summary_text);
		summary_span.show();
		//now we have to see if the text is too big to fit in the space, and if so use the "x selected" count instead
		container_width = info_bar.attr('offsetWidth'); //needs overflow:hidden;
		summary_width = summary_span.attr('offsetWidth'); //needs white-space:nowrap;
		if(summary_width > container_width){ //too big
			use_count = true;
		}
	}
	if(use_count){ //if we need to use the count, either because there are no inputs selected or the text is too big
		summary_span.hide(); //abandon the text summary idea
		info_bar.find(".selected_count span").first().text(selected_inputs.length);
		info_bar.find(".selected_count").first().show();
	}
	//now make all of the .is_header elements bold if they have checkboxes in their sub list(s) selected
	fake_select.find(".is_header").each(
		function(){
			jq_this = $(this);
			jq_this.removeClass('has_checked_items');
			if(jq_this.closest("li").find("ul input:checked").length){ //if there is a checked checkbox the/a sub list
				jq_this.addClass('has_checked_items');
			}
		}
	);

    /* Update the overall count - NOT YET WORKING */
    /*
    overall_selected_count = 0;
    var count_string="";
    var count_change = selected_inputs.length - old_count;
    */
    /*
    $(".checkbox_container input").each(
	function() {
	    fake_select = $(this).closest("div.fake_select");
	    selected_inputs = fake_select.find("input:checked");
	    // count_string += selected_inputs.length + "-"
	    count_string += $(this).id + "-";
	    // overall_selected_count += selected_inputs.length;
	    overall_selected_count++;
	}
    );
    */
    
    /*alert("Overall selected count = [" + count_change + "]");*/
}


nested_checkbox_lists.init = function(){
	
	//set on-click functions for the fake drop downs, to expand/hide the checkboxes
	$(".select_bar .expand").each(
		function(){
			$(this).click(
				function(){
					jq_this = $(this);
					cont_sel=jq_this.closest(".fake_select");
					cont_div = jq_this.closest(".fake_select").find(".checkbox_container");
					if(cont_div.hasClass('showing')){
						cont_div.removeClass('showing').hide();
					}else{ //if the .checkbox_container div is hidden, so we're opening it
						//close all checkbox_container divs on the page
						$(".checkbox_container").each(
							function(){
								$(this).removeClass('showing').hide();
							}
						);
						// and make sure they are at normal depth (hack for IE)
						$(".fake_select").each(
							function(){
							    $(this).removeClass('fake_select_front');
							}
						);
						// bring element's parent to front (hack for IE)
						cont_sel.addClass('fake_select_front');
						//and then just show this one
						cont_div.addClass('showing').slideDown('fast');
					}
				}
			);
		}
	);
	
	//on-click function for the 'X' buttons at the bottom of the drop downs
	$(".fake_select .controls .done").each(
		function(){
			$(this).click(
				function(){
				$(this).closest(".fake_select").find(".checkbox_container").eq(0).removeClass('showing').hide();
				}
			);
		}
	);
	
	//set an on-click function for the 'Deselect all' buttons
	$(".fake_select .controls .deselect").each(
		function(){
			$(this).click(
				function(){
					$(this).closest(".checkbox_container").find("input").each(
						function(){
							jq_this = $(this);
							jq_this.removeAttr('checked');
							nested_checkbox_lists.update_count(jq_this);
						}
					);
				}
			);
		}
	);
	
	//set an on-click function for the buttons that expand the sub options
	$(".fake_select .sub").each(
		function(){
			$(this).click(
				function(){
					jq_this = $(this);
					ul = jq_this.closest('li').children('ul').eq(0); //the <ul> that this button shows/hides
					//in the questions section, both the buttons and the headings are clickable
					//but we only want to change the +/- sign in the button, not in the heading
					if(jq_this.is("button")){
						button = jq_this;
					}else{
						button = jq_this.siblings("button").eq(0);
					}
					if(ul.hasClass('showing')){
					    ul.hide('fast').removeClass('showing');
					    button.text( $("#plus_button_text").text() ).removeClass('minus').addClass('plus');
					}else{
					    ul.slideDown('fast').addClass('showing');
					    button.text( $("#minus_button_text").text() ).removeClass('plus').addClass('minus');
					}
				}
			);
		}
	);
	
	
	//make the checkbox_container elements collapse when you click elsewhere on the page 
	$(document).click(
		function(e){
			if($(e.target).closest(".fake_select").length){//clicked element is within a fake_select
				return;
			}
			$(".fake_select .checkbox_container").each(
				function(){
					$(this).removeClass('showing').hide();
				}
			);
		}
	);
	
	
	
	//set an on-change function for the checkboxes, that will update the count at the top
	$(".checkbox_container input").each(
		function(){
			$(this).change(
				function(){
					nested_checkbox_lists.update_count($(this));
				}
			);
		}
	);
	
	//set the initial values of the "x selected" counts
	$(".select_bar").each(
		function(){
			nested_checkbox_lists.update_count($(this));
		}
	);
	
}



nested_checkbox_lists.init();
