/* rewritten by michael aug 7, 2009 */

$(document).ready(function() {

	// highlight default anchors
	$('.pressitemsfilter').find('a[href=#]').each(function(){
		$(this).addClass('active');
	});

	//attach click detectors
	$('.pressitemsfilter a').click(function (e) {
		e.preventDefault();
		link = $(e.target);
		hash = link.attr('href').replace(/^(.*)#/,'');
		
		// make all links of that div level not active
		link.closest('div').find('a').removeClass('active');
		
		// make clicked on link look active
		link.addClass('active');
		
		// check for other active elements 2 div levels up
		active_elements = new Array;
		link.closest('.pressitemsfilter').find('a.active').each(function() {
			active_elements.push($(this).attr('href').replace(/^(.*)#/,''));
		});
		
		// filter all press entries and hide those that are do not have all "active_elements" designations
		hidden_count = 0;
		$(".pressitemtr").each(function () {
			show = true;
			item = $(this);
			$(active_elements).each(function() {
				if (this != '' && !item.hasClass(this)) {
					show = false;
					return false;
				}
			});
			if (show) {
				item.fadeIn();
			} else {
				item.hide();
				hidden_count++;
			}
		}); 
		
		if (hidden_count > 0) {
		
			// remove any existing <span> inside the link, presumably with the last hidden_count inside
			$('a.showall span').remove();
			$('a.showall').append(' <span>(' + hidden_count + ')</span>');
			
			// show 'showall' link
			$("a.showall").fadeIn();
		} else {
			$("a.showall").fadeOut();
		}
	});
	
	// hide link initially
	$('a.showall').hide();
	
	$('a.showall').click(function (e) {
		e.preventDefault();
		// show all press
		$(".pressitemtr").fadeIn();

		// clear active filters
		$('.pressitemsfilter a').removeClass('active');
	
		// highlight default anchors
		$('.pressitemsfilter').find('a[href=#]').each(function(){
			$(this).addClass('active');
		});
		
		// hide this link
		$(this).fadeOut();
	})
});


/*
updated to use jquery by michael, jul 24, 2009
*/





/*





var getHash = function(elem) {
	var index = elem.href.indexOf('#');
	if (index != -1) {
		return elem.href.substring(index).replace(/^#/g, '');
	}
	return '';
}

function strip(dis) {
    return dis.replace(/^\s+/, '').replace(/\s+$/, '');
}


  
function selectCountry(country) {
	// hide all news elements that don't have that class
	var elems = $(".pressitem");
	
	// flags for hiding or showing the section headers
	var existsOneVideo = false;
	var existsOneArticle = false; 
	// loop over all press items
	for (var i = 0; i < elems.length; i++) {
		// if no classname is set for elems[i], assume it's "us"
		if (country == "all" || (country == "us" && elems[i].className == "pressitem") || $(elems[i]).hasClass(country)) {
			elems[i].style.display = "block";
			
			// check to see whether it was a video or article and set flags accordingly
			if (!existsOneVideo || !existsOneArticle) {
				// check the parent div to see if it has class video
				if ($(elems[i].parentNode).hasClass("video")) {
					existsOneVideo = true;
				} else {
					existsOneArticle = true;
				}
			}
		} else {
			elems[i].style.display = "none";
		}
	}
	
	// hide or show section headers
	var videosHeading = document.getElementById("pressVideosHeading");
	var articlesHeading = document.getElementById("pressArticlesHeading");
	if (existsOneVideo) { 
		videosHeading.style.display = "block";
	} else { 
		videosHeading.style.display = "none"; 
	} if (existsOneArticle) { 
		articlesHeading.style.display = "block"; 
	} else { 
		articlesHeading.style.display = "none"; 
	}
	
	// change selection nav
	// set one button to active
	$(".countrySelector").each(function() {
		if (getHash(this) == country) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	
	}); 
}
  
function countrySelectorClick(evt) { 
	evt = (evt) ? evt : ((window.event) ? window.event : "")
	if (evt) {
		var elem = getTargetElement(evt)
		if (elem) {
			var country = getHash(elem);
			selectCountry(country);
		}
	}
}

$(document).ready( function() { 
	$(".countrySelector").click(function(e) {
		countrySelectorClick(e);
	});
	// set the shown country by the #value passed in
	// filter for non country hashes, e.g. "video"
	if (getHash(location) != "" && getHash(location) != "video"
								&& getHash(location) != "news"
								&& getHash(location) != "releases") {
		selectCountry(getHash(location));
	} else {
		selectCountry('all');
	}
});

// shared function
function getTargetElement(evt) {
	var elem
	if (evt.target) {
		elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target
	} else {
		elem = evt.srcElement
	}
	return elem

}

*/