//
// create closure
//
(function($) {
	 //
	 // Adds a bbcom selection filter (checkboxes) to a div
	 //
	 $.fn.selectionFilter = function(data, options) {

	 	// build main options before element iteration
	    var opts = $.extend({}, $.fn.selectionFilter.defaults, options);

		// iterate and reformat each matched element
		return this.each(function() {
			// get a reference to the current matched element		
			$this = $(this);
			
			// build element specific options
			var o = opts;

			// get the number of items for each column
            var altCount = 0;
			if (o.columns == 1) {
				col1count = data.length;
				col2count = 0;
			}
			else {
				col1count = Math.ceil(data.length /2);
				col2count = data.length - col1count + 1;
				altCount = Math.ceil(data.length /2);
			}

			// create a new div that will wrap around the checkboxes
			filterDiv = $("<div id='" + o.name + "Filter'></div>");
			
			// create a Title
			titleDiv = $("<div style='padding-bottom: 0px'></div>");
			
			titleDiv.append(o.title + ":&nbsp;&nbsp;");

            if (o.showAllLink) {
                // create the [S/Des]elect All link
                allLink = $("<a href='javascript: //' class='blueLink'></a>");
                allLink.click(function () { // Make it's onclick toggle all the checkboxes.
                    $.fn.selectionFilter.toggleSelectDeselectAll(o.name);
                });
                allLink.append('Select All');
                $this.allLink = allLink;

                // only display the 'All' if we have more than one checkbox filter
                if (data.length * 1 > 1) {
                    titleDiv.append(allLink);
                }
            }

            // Display the title
            if (o.showTitle) {
                header = $("<h3>");
                header.append(titleDiv);
                filterDiv.append(header);
            }

			// setup table
			filterTable = $("<table class='filterTable' id='" + o.name + "FilterTable'></table>");
			row = $("<tr />");
			col1 = $("<td valign='top' width='50%'></td>");
			col2 = $("<td valign='top' width='50%' style='padding-left: 5px'></td>");

			// iterate through all the data items
			for (x in data)
			{
                row2 = $("<tr>");

		if (data.length * 1 == 1)
                {
                    // Only one checkbox, simple format.
                    starter = $("<td  class='filterCheckBox'>");
                    checkboxLeft = $("<input type='checkbox' style='vertical-align: middle;margin:0;width:15px;height:15px;overflow:hidden;margin-bottom: 0px;' name='filter_" +
                        o.name + "_" + data[x][o.idName] + "' id='filter_" + o.idName + "_" + data[x][o.idName] + "' checked='checked' value='" + data[x][o.idName] + "' />");

                    checkboxLeft.click(function() {o.changeEvent.call(this)});

                    filterOptionLeft = "<td colspan='3' class='filterLabel'><label id='label_" + o.idName + "_" + data[x][o.idName] + "' onclick=\"finderLabelOnClick('filter_" + o.idName + "_" + data[x][o.idName] + "');\" >" + data[x][o.valueName] + "</label>&nbsp;<div style='display:inline; color:#9A3B26;' id='countlabel_" + o.idName + "_" + data[x][o.idName] + "'></div></td>";

                    starter.append(checkboxLeft);

                    row2.append(starter);
                    row2.append(filterOptionLeft);

                    filterTable.append(row2);
                }
                else
                {
                    // More than one, may need to process columns.
                    if ((x * 1) < col1count)
                    {

                        starter = $("<td class='filterCheckBox'>");
                        checkboxLeft = $("<input type='checkbox' style='vertical-align: middle;margin:0;width:15px;height:15px;overflow:hidden;margin-bottom: 0px;' name='filter_" +
                            o.name + "_" + data[x][o.idName] + "' id='filter_" + o.idName + "_" + data[x][o.idName] + "' checked='checked' value='" + data[x][o.idName] + "' />");

                        checkboxLeft.click(function() {o.changeEvent.call(this)});

        				filterOptionLeft = "<td class='filterLabel'><label id='label_" + o.idName + "_" + data[x][o.idName] + "' onclick=\"finderLabelOnClick('filter_" + o.idName + "_" + data[x][o.idName] + "');\" >" + data[x][o.valueName] + "</label>&nbsp;<div style='display:inline; color:#9A3B26;' id='countlabel_" + o.idName + "_" + data[x][o.idName] + "'></div></td>";

                        starter.append(checkboxLeft);

                        row2.append(starter);
                        row2.append(filterOptionLeft);

                        var altIndex = ((x * 1) + altCount);

                        if ((altIndex < (data.length * 1)) && (col2count != 0))
                        {
                            starter = $("<td class='filterCheckBox'>");
                            checkboxRight = $("<input type='checkbox' style='vertical-align: middle;margin:0;width:15px;height:15px;overflow:hidden;margin-bottom: 0px;' name='filter_" +
                                o.name + "_" + data[altIndex][o.idName] + "' id='filter_" + o.idName + "_" + data[altIndex][o.idName] + "' checked='checked' value='" + data[altIndex][o.idName] + "' />");
                            checkboxRight.click(function() {o.changeEvent.call(this)});
                            filterOptionRight = "<td class='filterLabel'><label id='label_" + o.idName + "_" + data[altIndex][o.idName] + "' onclick=\"finderLabelOnClick('filter_" + o.idName + "_" + data[altIndex][o.idName] + "');\" >" + data[altIndex][o.valueName] + "</label>&nbsp;<div style='display:inline; color:#9A3B26;' id='countlabel_" + o.idName + "_" + data[altIndex][o.idName] + "'></div></td>";

                            starter.append(checkboxRight);
                            row2.append(starter);
                            row2.append(filterOptionRight);
                        }
                        else
                        {
                            emptyRow = $("<td></td><td></td>");
                            row2.append(emptyRow);
                        }

                        filterTable.append(row2);
                    }
				}
			}
			
			
			if (data.length * 1 > 0) {
				filterDiv.append(filterTable);
			}
			
			if (o.displayHR)
				filterDiv.append("<div class='filterSep'></div>");
				
			$this.append(filterDiv);
 		});

  	};
  	
  	//
	// plugin defaults
	//
	$.fn.selectionFilter.defaults = {
		columns: 2,
		title: "Selection Filter",
		name: "selection",
		idName: "selectionID",
		valueName: "selectionName",
		changeEvent: filterCheckboxClicked,
		displayHR: true,
        showAllLink: true,
        showTitle: true
	};

    //
    // Function that checks all checkboxes in a filter if the link is Select All,
    // deselects checkboxes if the link is Deselect All.
    //
    $.fn.selectionFilter.toggleSelectDeselectAll = function(filterName) {
        thisLink = $("#" + filterName + "Filter h3 div a");
        if (thisLink.text() == 'Select All')
        {
            thisLink.empty();
            thisLink.append('Deselect All');
	    	$("#" + filterName + "Filter table tr td :checkbox").attr('checked', 'checked');
        } else {
            thisLink.empty();
            thisLink.append('Select All');
	    	$("#" + filterName + "Filter table tr td :checkbox").attr('checked', '');
        }

        finderFiltersChanged(true);
    }
	
    //
    // Function that sets the state of the [S/Des]elect All link in a filter.
    //
    $.fn.selectionFilter.setSelectDeselectAll = function(filterName) {
        thisLink = $("#" + filterName + "Filter h3 div a");
        unCheckedBoxes = $("#" + filterName + "Filter table tr td :checkbox:not(:checked)");

        if (unCheckedBoxes.length == 0) {
            thisLink.empty();
            thisLink.append('Deselect All');
        } else {
            thisLink.empty();
            thisLink.append('Select All');
        }
    }

    $.fn.doubleSliderFilter = function(data, options) {

        // build main options before element iteration
	    var opts = $.extend({}, $.fn.doubleSliderFilter.defaults, options);

		// iterate and reformat each matched element
		return this.each(function() {

            // get a reference to the current matched element
			$this = $(this);

			// build element specific options
			var o = opts;

            filterDiv = $("<div id='" + o.name + "Filter'></div>");
            titleDiv = $("<div style='padding-bottom: 0px'></div>");
            titleDiv.append(o.title + ":&nbsp;&nbsp;<div id='" + o.name + "Range' style='color:#9A3B26; font-weight:bold; display:inline;'>\n\
                                        <div style='display:inline;' id='"+o.name + "Range_min'>" + o.min + "</div> - \n\
                                        <div style='display:inline;' id='" + o.name + "Range_max'>" + o.max + "</div></div>");
            header = $("<h3>");
            header.append(titleDiv);
            filterDiv.append(header);
            filterDiv.append('<div id="' + o.name + 'Slider" style="margin:10px;"></div>');
            $this.append(filterDiv);
            $('#'+o.name+'Slider').slider(
                {range: true,
                 min: o.min,
                 max: o.max,
                 values: [o.min, o.max],
                 slide: function(e, ui){
                     handle1 = $(this).slider('values', 0);
                     handle2 = $(this).slider('values', 1);
                     $('#'+o.name+'Range_min').text(handle1);
                     $('#'+o.name+'Range_max').text(handle2);
                     o.changeEvent.call(this);
                     }
                 });

            if (o.displayHR)
				filterDiv.append("<div class='filterSep'></div>");

            $this.append(filterDiv);
        });
    }

    //
	// plugin defaults
	//
	$.fn.doubleSliderFilter.defaults = {
		title: "Rating",
		name: "rating",
        min: 1,
        max: 10,
		changeEvent: finderFiltersChanged,
		displayHR: true,
        showAllLink: true,
        showTitle: true
	};

})(jQuery); // end of closure

