$(function(){
	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	$("h3.trigger").click(function(){
		$(this).toggleClass("closed").parent().next().slideToggle("fast");
		return false;
	});

	var urllocation = window.location.href;
	var urllocation = $.trim( urllocation.toLowerCase( ) );

	var isLibraryPage = urllocation.match( "library" ) ? true : false;
	var isToolsPage = urllocation.match( "tools" ) ? true : false;
	var isFaqPage = urllocation.match( "faq" ) ? true : false;

	if( !( isLibraryPage || isToolsPage || isFaqPage ) )
	{
		$.post( '/healthcarereform/index.php/timeline/_get_timeline',
			function( data )
			{
				if( data == 1 )
				{
					//window.location.hash = "timelinetool";

					var targetOffset = $( 'a[name|="timelinetool"]' ).offset().top;
					var scrollDuration = 2000;
					$('html,body').animate({scrollTop: targetOffset}, scrollDuration);
				}
				save_timeline( "FALSE" );
			}
		);
	}

	$( "#timelineanchor" ).click( function( e ){
		window.location.href = "/healthcarereform/index.php";
		save_timeline( "TRUE" );
	       	e.preventDefault();
	});


	var save_timeline = function( $value )
	{
		$.post( '/healthcarereform/index.php/timeline/_save_timeline', {flag:$value},
			function( data ) {
			}
		);

	}

	//Target links that need to be parsed
var linkForAnalytics = $('#library_topic_list li a'),
		thumbnailImage = $('#library_topic_list li .associated_image a');

baseElq = "/healthcarereform/elqNow/elqRedir.htm?ref=";
	//loop through all the links in #library_topic_list
	linkForAnalytics.each(function(index) {
		href = $( this ).attr( "href" );

		if( !href.match( "entry_id" ) )
		{
			var parseDocTitle = $(this).attr("href").split('/').slice(-1),
			parseDocType = $(this).attr("href").toUpperCase().split('.').slice(-1),
			//create analytics code
			filePath = 'javascript: _gaq.push([\'_trackEvent\', \' ' + parseDocType + '\', \'Downloaded\', \'' + parseDocTitle + '\']);';
			//add analytics information to link
			$(this).attr('onClick', filePath);

			href= "http://www.makinghealthcarereformwork.com"+href;
			elqLink = baseElq + href;
			$( this ).attr( 'href', elqLink );
		}
  });


  //loop through all the links in #library_topic_list that get thumnail image backgrounds
  thumbnailImage.each(function(index) {
		var parseThumbnailIcon = $(this).attr("href").toLowerCase().split('.').slice(-1);
		$(this).parent().addClass('icon_' + parseThumbnailIcon);
  });

  $(".global_list li:odd").addClass('alt');


	//Library page sort by date or title buttons
	$(".sort_options a").click(function(){
		$("a").removeClass("sort_selected");
		if ($(this).hasClass('sort_title')){
			$(".sort_title").addClass('sort_selected').css('background-position','right top');
		} else {$(".sort_date").addClass('sort_selected');}
		return false;
	});

		var url = location.href;
		url = url.toLowerCase( );
		var url_matches = url.match( 'library' );

		var isSearchPage = url.match( 'search' ) ? true : false;
		var isDocumentPage = url.match( 'document' ) ? true : false;
		if( url_matches == null || isSearchPage || isDocumentPage ){}
		else
		{
			//get the current search type
			$.post( '/healthcarereform/index.php/library/_get_sort_type', function( data )
				{
					var sort_option = $.trim( data );
					if( data == "title" )
					{
						$( ".sort_title" ).addClass( 'sort_selected' ).css( 'background-position', 'right top' );
						$( ".sort_date" ).removeClass( 'sort_selected' );
					}
				}
			      );
		}

	//hide list on load
	$(".faq_list").hide();

	//toggle opening and closing the list
	$("h2.closed").click(function(){
		$(this).toggleClass("trigger").parent().next().slideToggle("fast");
		return false;
	});


  //for unordered lists
  $(".global_list li:odd").addClass('alt');

  $(".popular .utility_toggle li:last").addClass('last');
  $(".quick_links .utility_toggle li:last").addClass('last');

	//styling for dropdowns
	$('.current_filterDD').selectbox();

	//styling for search bar
  $("#search").focus(function() {
   	if ($(this).val() == "Search") {
   		$(this).val("").add('input').css('color','#313131');
   		return false;
    }
  });

  $("#grandfather").click(function() {
		$(this).addClass('link_clicked'); //Add "active" class to selected link
		return false;
	});
});

//Grabs the hight of the right side content
function capture_height(){
	var $contentContainer = $('#content_innner_wrapper .content_right'),
			$navContainer = $('#content_innner_wrapper .content_left'),
			$contentWrapper = $('#content_innner_wrapper .innerwrapper'),
			$contentUl = $('#library_topic_list'),
			//sets the hight of the right side container
			navHeight = $navContainer.height(),
			containerHight = $contentContainer.height(),
			wrapperHeight = $contentWrapper.height(),
			ulHight = $contentUl.height();

		if (wrapperHeight < containerHight) {$contentContainer.animate({height:wrapperHeight}, 600);
			} else {$contentContainer.css({'height':wrapperHeight});
			}

			if (containerHight < navHeight) {
				$('#content_list').removeClass('else');
				$('#content_list').addClass('if');
			} else {
				$('#content_list').removeClass('if');
				$('#content_list').addClass('else');
			}
}

//	Used on hcr.group/index.html to record changes to the user's state and segment:
function hcrOnChangeSegment()
{
	var state = $('#state option:selected');
	var segment = $('#employer option:selected');

	_gaq.push(['_trackEvent', 'State', 'selected', state.text()]);
	_gaq.push(['_trackEvent', 'Segment', 'selected', segment.text()]);

	$.post( '/healthcarereform/index.php/php/_landing_processor', 'state='+state.val()+'&segment='+segment.val(), function(){location.reload();} );
}

$( function(){
	var url = $.trim( window.location.href );
	url = url.toLowerCase( );
	var isSitemapSelector = url.match( "sitemapselector" );

	if( isSitemapSelector )
	{
		$( document ).ready(
			function( )
			{
				var choice = $( "#sitemapselector_channel_selector" ).val( );
				handleVisibility( choice );
			}
		);

		$( "#sitemapselector_channel_selector" ).change(
			function( )
			{
				var choice = $( "#sitemapselector_channel_selector" ).val( );
				handleVisibility( choice );
			}
		);

	}
});

function handleVisibility( choice )
{
	choice = $.trim( choice );
	choice = choice.toLowerCase( );

	var documentChannel = "document";
	if( documentChannel == choice )
	{
		$( "#sitemapselector_topic_location" ).show( );
	}
	else
	{
		$( "#sitemapselector_topic_location" ).hide( );
	}
}

