

//Allows user to filter query search by category & tag
var LinkFilter = {}
LinkFilter.getParam = function(parameter){
	var p = escape(unescape(parameter));
	var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
	var match = regex.exec(window.location.search);
	var value = null;
	if( match != null ){
		value = match[1];
	}
	return value;
}
LinkFilter.setup = function(){


	//set refs
	var $tagItems = $('.tag-item');
	var $currentTagItem = $tagItems.filter('.current-tag');
	var $catItems = $('.cat-item');
	var $currentCatItem = $catItems.filter('.current-cat');
	

	
	//set initial tag nav state
	if(!$currentTagItem.length)	{
		//create array of tags
		var tags = [];
		$tagItems.each(function() {
			var _href = $(this).find('a').attr('href');
			var _tag = _href.substring(_href.indexOf('tag/')+4, _href.length - 1);
			tags.push(_tag);
		});
		//get tag query
		var tagParam = this.getParam('tag');
		//determine if tag exists and is valid; get the index from the array
		if (tagParam !== null && ($.inArray(tagParam, tags)) != -1) {
			var tagIndex = $.inArray(tagParam, tags);
			$tagItems.eq(tagIndex).addClass('current-tag');
		} else if ($currentCatItem.length) {
			//handle filtered stories page
			$tagItems.eq(0).addClass('current-tag');
		}
	}
	
/*
	//set initial cat nav state
	if(!$('#filter-navigation-secondary .breadcrumbs li.current-cat').length) {
		$('#filter-navigation-secondary .breadcrumbs li:first-child').addClass('current-cat');
	}
*/
		
	
	//returns str || 'all' || null
	var getActiveTag = function() {
		if(!$currentTagItem.length) {
			return null;
		} else if ($tagItems.find(':first-child').hasClass('current-tag')) {
			return 'all';
		} else {
			var href = $currentTagItem.find('a').attr('href'),
				tag = href.substring(0,href.length-1);
			return tag.substring(tag.lastIndexOf('/')+1,tag.length);
		}
	}
	
	//handle type (cat) nav
	$catItems.find('a').click(function() {
		var $this = $(this);		
		if ($this.parents('li.cat-item').hasClass('current-cat')) {
			//return if selected already
			return false;
		}
		var currentTag = getActiveTag()
		if (currentTag === 'all' || currentTag === null) {
			return true;					
		} else {
			window.location = $(this).attr('href') + '?tag=' + currentTag;
		}
		return false;
	});

	//handle type (cat) nav
	$tagItems.find('a').click(function() 
	{
		var $this = $(this);
		//return if selected already
		if ($this.parents('li.tag-item').hasClass('current-tag')) 
		{
			return false;
		}
		var href = $this.attr('href'),
			tag = href.substring(0,href.length-1);
			tag =  tag.substring(tag.lastIndexOf('/')+1,tag.length);
		
		
		document.location = ($currentCatItem.length ? $currentCatItem.find('a').attr('href') : $catItems.eq(0).find('a').attr('href')) + '?tag=' + tag;
		return false;
	});
} // end LinkFilter
		
		
		


//Disclaimer Tooltip
var ToolTip = {};

ToolTip.setup = function()
{
	$("p.disclaimer_ a").tooltip({

		offset: [10, 2],  // tweak the position
		effect: 'slide', // use the "slide" effect
		tipClass: 'tooltip',
		position:  'center right'
		
		// add dynamic plugin with optional configuration for bottom edge
	});
}// end ToolTip




// Adds forward slash as divider for pagination links
var PaginateDivider = {};

PaginateDivider.setup = function()
{
	var $secondToLast = $('#story-pagination li:last-child').prev();
	var $links = $('#story-pagination').find('li').not(':first').not(':last').not($secondToLast);
	$links.append(' / ');
}// end PaginateDivider



$(document).ready(function() 
{
	//Inits
	LinkFilter.setup();
	ToolTip.setup();
	PaginateDivider.setup();
}); //end document.load
