var newwindow;
function poptastic(url)
{
	newwindow=window.open(url,'name','height=600,width=600');
	if (window.focus) {newwindow.focus()}
}


var changeDisplay = new function(){  
    this.init = function() {
        $('.resultsButton a').click(changeViews);
    }
    function changeViews(){
        var eleId = this.id.substr(3);
        /*Image*/
        $('#resultImgId'+eleId).fadeOut(); 
    }   
} 

$(document).ready(changeDisplay.init); 


/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}
 
/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}
 
 
 
function removeItems(array, item) {
	var i = 0;
	while (i < array.length) {
		if (array[i] == item) {
			array.splice(i, 1);
		} else {
			i++;
		}
	}
	return array;
}
 
 
$(document).ready(function () {
	
	// everything hinges on creating a string of class names, 
	// so i'll create a variable to hold that first
	
	var stringOfclassNames = '';
	
	// grab the class name of each list item to build that string
	$('.filterThis > li').each( function (i) {
		var thisclassString = $(this).attr('class');
		stringOfclassNames = stringOfclassNames +' '+ thisclassString
	});
	
	// now i have a space-delimited string of all class names stored
	// in the stringOfclassNames variable.	
	// Trim spaces from the ends of that string:
   stringOfclassNames = jQuery.trim(stringOfclassNames);
	
	// i can't really do anything with it until it's an array, so
	// convert that string to an array.
	var arrayclasses = stringOfclassNames.split(' ');
	
	
	// now for the isolating the filter that is common to all.
	// must do before extracting only the unique classes
	// one way to approach: count the number of times classes occur, and
	// if any occur the same number of times as how many list items i have,
	// assume that class is common to all list items, and remove it from
	// the filter list. duplicate class on same item = problem, but 
	// i'm not thinking about that right now.
	// i've also chosen sort the pre-unique'd array
	// instead of sorting the unique'd array.  i think i have to for the count.
	var arrayclasses = arrayclasses.sort();
	totalNumberOfItemsToFilter = $('.filterThis > li').length;
	
	
	// borrowed from http://stackoverflow.com/questions/348021/counting-results-in-an-array
	// for counting up items.  do i even need this?
	var result = new Object();
	for (var filterclass in arrayclasses) {
		if (result[arrayclasses[filterclass]] === undefined) {
			result[arrayclasses[filterclass]] = 1;
		} else {
			result[arrayclasses[filterclass]]++;
		}
	}
	var resultsToRemoveFromFilters = new Array();
	for (var item in result) {
		if (result[item] == totalNumberOfItemsToFilter) {
			resultsToRemoveFromFilters.push(item);
		}
	}
	
	
	
 
	// pull out only unique values from that array.  Otherwise
	// i'll end up with duplicate filter checkboxes.
	arrayUniqueclasses = (unique(arrayclasses));
 
 
	// and now remove classes that appear in every result from my 'unique
	// classes' array
	for (x in resultsToRemoveFromFilters) {
		arrayUniqueclasses = removeItems(arrayUniqueclasses,resultsToRemoveFromFilters[x]);
	}
	$('.filterThis').before('');
 
 
 
	// we only want to create filters if there are multiple classes. check
	// length of that array to see if it worth going forward.  I need at least
	// two, so my value has to be greater than 1.
	if (arrayUniqueclasses.length > 1) {
 
		// it must be worth it, because everything else is
		// within the true side of this if statement.	
		// so since we're going to have some filters, 
		// lets give them a place to live
		$('<ul class="filters"><\/ul>').insertAfter('.filterBoxContent');
		
		// then build the filter checkboxes based on all the class names
		$.each(arrayUniqueclasses, function() {
			$('<li><input type="checkbox" checked="checked" value="'+this+'" id="filterID'+this+'" /><label for="filterID'+this+'">'+this+'<\/label><\/li>').appendTo('ul.filters');
		});
		
		// lets throw in the 'show all' checkbox for giggles
		$('<li><input type="checkbox" checked="checked" value="filterAll" id="filterIDall" /><label for="filterIDall">Show all<\/label><\/li>').appendTo('ul.filters');
 
		// now lets give those filters something to do
		$('.filters input').click( function() {
			var value= $(this).val();
			if (value == 'filterAll') {
				if ($(this).is(':checked')) {
					$('.filters input').attr('checked','checked');
					$('.filterThis li').slideDown();
				} else {
					var one=1;
				}
			} else {
				stringValue = '.filterThis > li.'+value;
				if ($(this).is(':checked')) {
					$(stringValue).slideDown();
				} else {
					$(stringValue).slideUp();
					$('.filters #filterIDall').removeAttr('checked');
				}
			}
		});
 
 
	}
});

$("#filter").keyup(function () {
    var filter = $(this).val(), count = 0;
    $(".filtered:first li").each(function () {
        if ($(this).text().search(new RegExp(filter, "i")) < 0) {
            $(this).addClass("hidden");
        } else {
            $(this).removeClass("hidden");
            count++;
        }
    });
    $("#filter-count").text(count);
});

                $(function () {
                        var tabContainers = $('div.tabs > div');
                        tabContainers.hide().filter(':first').show();
                        
                        $('div.tabs ul.tabNavigation a').click(function () {
                                tabContainers.hide();
                                tabContainers.filter(this.hash).show();
                                $('div.tabs ul.tabNavigation a').removeClass('selected');
                                $(this).addClass('selected');
                                return false;
                        }).filter(':first').click();
                });
