/*------------------------------------------------------------------------------

MASTER.JS -- CUIRE - ZL.COM - V3

------------------------------------------------------------------------------*/

$(document).ready(function() {
	
// --> FUNCTIONS
	
	// LOAD TAGGED POSTS: shared function to retrive the newly 
	//					  requested ON-PAGE TAG LINKS
	function loadTaggedPosts() {
		
		var selected_tag = $(this).text();
		
		var c_selected_element = $('section.tag-list').find('.selected');
		$(c_selected_element).removeClass('selected');
		var tag_list_link = $('section.tag-list').find('a[href*="' + selected_tag + '"]').parent().addClass('selected');
		
		var the_right_links = $('#main').find('a[href*="' + selected_tag + '"]');
		var the_right_posts = $(the_right_links).parent().parent();
		
		$('section:visible').not('section.tag-list').fadeOut(function() {
			
			$(the_right_posts).fadeIn();
			
		});
		
	}
	
	
// --> TAG PARSING
	
	// show the tags
	$('.tags').show();
	
	// create an array of the tag sets
	var tags_on_page = $('.tags');
	
	// loop through that array of tag sets
	for (var i = 0; i < tags_on_page.length; i++) {
		
		// extract the raw tags
		var tags_list = tags_on_page[i].innerHTML;
		
		// create an array of those tags
		var tags_array = tags_list.split(', ');
		
		// wipe the raw tag data from its parent element
		tags_on_page[i].innerHTML = "";
		
		// loop through the tag array
		for (var j = 0; j < tags_array.length; j++) {
			
			// store the current tag in a variable
			var tag = tags_array[j];
			// wrap the tag in an anchor element and assign its URL
			var wrapped_tag = '<a href="/tag/' + tag + '">' + tag + '</a>, ';
			
			// append the new wrapped tag to its parent element
			$(tags_on_page[i]).append(wrapped_tag);
		}
		
		// trim the last space and comma characters
		var with_comma = $(tags_on_page[i]).html();
		var sans_comma = with_comma.substring(0, with_comma.length-2);
		$(tags_on_page[i]).html(sans_comma);
		
	}
	
	
// --> ARCHIVES PAGE
	
	// target just the 'Archives' page 
	if(window.location.pathname == '/tag' || '/tag/') {
		
		// CHANGE PAGE SUMMARY: because we have JavaScript, we have to adjust the 
		// 						summary so it informs the user about Tags
		
		// grab the current summary
		var c_summary = $('#tags h3.summary').text();
		
		// append the new summary information
		var n_summary = c_summary + ' I like to use a tag-based archive system. <a href="#">Read this</a> to find out why.';
		
		// put the new summary onto the page
		$('#tags h3.summary').html(n_summary);
		
		// add in my tag list header
		$('#tags h3.summary').after('<section class="tag-list"><h4><em>my assortment of </em> Tags</h4><ul></ul></section>');
		
		// generate a dynamic tag list
		var tag_list = $('#main').find('a[href*="/tag"]');
		
		// REMOVE DUPLICATES: an important little section to remove dupes from appearing
		// 					  in my tag list. [http://bit.ly/ou00SY]
		
		// create empty set to store values if they're seen
		var seen = {};
		
		// go through each item in the tag list
		$(tag_list).each(function() {
			
			// store the tag value in a variable
			var txt = $(this).text();
			
			// check to see if the value is in the set
			if (seen[txt]) {
				// get the tags index in the array
				var index = $.inArray(this, tag_list);
				// pull it out of the array
				tag_list.splice(index, 1);
			} else {
				// add the link to the set so we can look for it later
				seen[txt] = true;
			}
				
		});
		
		// loop through the freshly edited array again
		for (var k = 0; k < tag_list.length; k++) {
			
			// store the current tag in a variable
			var c_tag = tag_list[k].innerHTML;
			// wrap the tag in an anchor element and assign its URL
			var c_wrapped_tag = '<li><a href="' + c_tag + '">' + c_tag + '</a></li>';
			
			// append the new wrapped tag to its parent element
			$('section.tag-list ul').append(c_wrapped_tag);
		}		
		
		
		// IN-BOUD LINKS: show the corresponding articles for the requested 
		// 				  tags via the URL
		
		// if there's a hash ('#') attached to the URL, start working
		if(window.location.hash !== '') {
			
			// hide all the sections
			$('section').hide();
			
			// store the hash in a variable
			var selected_tag = window.location.hash;
			
			// find the link in the tag-list that corresponds to the hash
			$('section.tag-list').find('a[href*="' + selected_tag + '"]').parent().addClass('selected');
			
			// target the right posts via their tags
			var the_right_links = $('#main').find('a[href*="' + selected_tag + '"]');
			var the_right_posts = $(the_right_links).parent().parent();
			
			// show the tag-list section, followed by the right posts
			$('section.tag-list').show();
			$(the_right_posts).show();
		}
		
		// ON-PAGE TAG LINKS: the two potential link lists that would require
		// 					  dynamically changing the tags on the page
		
		// .tag-list links
		$('section.tag-list a').click(loadTaggedPosts);
		// tag links from the posts currently visible on the page
		$('.tags a').click(loadTaggedPosts);
			
	}
	
	

	
	
});
