(function($){

	//Trigger the filters to run whenver the form state changes
	$(document).ready( function() {
		//Only run this on the exposed filters page
			if(
				$('.view-my-home-finder').length != 0
				&& $('.front').length == 0
			){

				//This is a bit of a bad selector to use, but it's limited to home finder pages and there isn't a better one
				$('.region-sidebar-first form').change(function() {
					run_filters();
				});

				$(window).load(function(){
					run_filters();
				});

			}
	});

	//Get the current filters and start add it to the page
	function run_filters(){
		var options_data = applied_filters_fetch_options_data();
		var options_html = applied_filters_fetch_options_html(options_data);

		if(options_html == ''){
			options_html = '<p class="no-filters">You have currently have no active filters.</p>';
		}

		$('.filter-options').hide();
		$(
			'<div class="filter-options">\
				<h3 class="now-viewing-title">Now Viewing</h3>'
				 + options_html +
			'</div>'
		).insertBefore('.quicksand-destination');

		//After the dynamic HTML has been added, apply Cufon
		Cufon.replace('.now-viewing-title',{fontFamily:'myriad-bold'});
		
		$('.layered-filter-wrapper').click(function(){
			applied_filters_toggle_filter($(this).attr('id').replace('filter--',''));
		});
	}

	function applied_filters_fetch_options_data() {
		var selected_options = new Array();
		var selected_options_data = new Array();


		//Fetch all selected check boxes
		$('.sidebar-first input[type=checkbox]:checked:not(:disabled)').each( function() {

			var option_label = $(this).attr('id');

			if (!(selected_options.indexOf(option_label) > 0)) {
				selected_options.push(option_label);
			}
		});


		//Build an array of filter data from the chosen options
		for (var i in selected_options) {

			var option = $('label[for=' + selected_options[i] + ']');

			selected_options_data[i] = {
				title: option.parents('.views-exposed-widget').find('label').html().replace(/(<([^>]+)>)/ig,""),
				value: option.html().replace(/(<([^>]+)>)/ig,""),
				id: selected_options[i]
			}
		}

		return selected_options_data;

	}

	//Format the HTML of the option data
	function applied_filters_fetch_options_html(options_data) {
		var filter_links = '';

		for (var i in options_data) {
			filter_links += '<span class="layered-filter-wrapper" id="filter--' + options_data[i].id + '">';
			filter_links += '<span class="layered-filter-inner"><span class="layered-filter-title">';
			filter_links += options_data[i].title + ' (' + options_data[i].value + ')</span></span></span>';
		}

		return filter_links;
	}



	function applied_filters_toggle_filter(filter_id) {
		//Because we're using $ Transform on checkboxes, we need to select the previous element to the actual checkbox
		$('#' + filter_id).prev().trigger('click');
	}

})(jQuery);;
/**
 *
 * It's a thing, http://stackoverflow.com/questions/3629183
 *
 *  IE<9 doesn't have an .indexOf() function for Array
 *  to define the exact spec version, run this before trying
 *  to use it.
 *
 *
 */
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
};
/**
 * Fetch and return settings for a field, given a scope (e.g. node)  
 */
function js_fields_get_settings(scope, content_type, field_name) {
  return Drupal.settings.js_fields.field_instances[scope][content_type][field_name].settings;
};

