feeds.read('/en/experience/feeds/chrysler-news-all.xml');

// setupFilterMenu() is called on page load.
// It creates all the various functionality required to power the filter menu.
function setupFilterMenu(divToWriteTo) {
	// Make sure we have valid data before we attempt to do things with it
	if (!feeds.feedLoaded) {
		setTimeout("setupFilterMenu('"+divToWriteTo+"');", 1000);
		return;
	}
	
	// First thing to do is build up the filter menu from the data found in the feed.  Loop over 
	var feedChecksText = "<h3>Story Types</h3>";
	feedChecksText += "<input type='checkbox' value='all' id='all_feedCheck' class='feed' />";
	feedChecksText += "<a href='#'>All Stories</a><br />";
	
	var tagChecksText = "<h3>Automotive Subjects</h3>";
	tagChecksText += "<input type='checkbox' value='all' id='all_tagCheck' class='tag' />";
	tagChecksText += "<a href='#'>All Subjects</a><br />";
	
	var tagTree = feeds.tagTree;
	
	// iterate over sets of tags
	for (branch in tagTree) {
		// Public tag handler - builds the list of tags and their related tag accordions
		if (!tagTree[branch].internal && typeof(tagTree[branch]) == 'object') {
			tagChecksText += "<h4 class='tagSetToggler' id='"+branch.replace(' ', '_')+"_toggler'>"+branch+"</h4>";
			tagChecksText += "<div class='tagSetContent'>";
			
			// Iterate over individual tags
			for (tag in tagTree[branch]['tags']) {
				if (typeof(tagTree[branch]['tags'][tag]) == 'object') {
					tagChecksText += "<input type='checkbox' value='"+tag+"' id='"+tag+"_tagCheck' class='tag' />";
					tagChecksText += "<a href='#'>"+tagTree[branch]['tags'][tag].tagName+"</a><br />";
				}
			}
			
			tagChecksText += "</div>";
		}
		
		// Internal tag handler - builds the list of 'feeds' for display at the top of the filter menu
		else if (tagTree[branch].internal && typeof(tagTree[branch]) == 'object') {
			// Iterate over individual tags
			for (tag in tagTree[branch]['tags']) {
				if (tagTree[branch]['tags'][tag].feedLike) {
					feedChecksText += "<input type='checkbox' value='"+tag+"' id='"+tag+"_feedCheck' class='feed' />";
					feedChecksText += "<a href='#'>"+tagTree[branch]['tags'][tag].tagName+"</a><br />";
				}
			}
		}
	}
	
	// Write out the HTML for the filter menu
	$('filters').setHTML("<form id='filterForm'>"+feedChecksText+tagChecksText+"<a id='filterFormSubmit' name='&lpos=main&lid=comment_submit' href='#'>Apply Filters</a></form>");
	
	// Set up tag accordions
	$$('#filters h4').each(function(elt) {
		global.initAccordion(elt.getText(), elt.getNext(), elt, false, true);
		global.setTogglerEvents('tags', [elt]);
	});
	
	// Display the section of the tag list with the current tag selection.  Vehicles is the default.
	var tag = getParameter('subject');
	var branch = '';
	if (tag) {
		branch = feeds.tagTree.getParent(tag).replace(' ', '_');
	}
	else {
		branch = 'Chrysler_Vehicles';
	}
	setTimeout("$('"+branch+"_toggler').fireEvent('click');", 2000);
	
	
	
	// On page load, query the checkboxes and run any functions that clicking
	// the boxes would've run.  This is important for page reloads.
	var filterForm = $('filterForm');
	filterForm.getElements('input').each(function(checkbox) {		
		checkbox.addEvent('click', function() {
			// 'All' checkbox has been clicked, en/disable all other checkboxes of its class
			if (checkbox.value == 'all') {
				// Get all checkboxes of its class
				$$('input.'+checkbox.getProperty('class')).each(function(otherCheck) {
					// Filter out the 'all' checkbox itself
					if (otherCheck.value != checkbox.value) {
						// Set the other checkboxes to all's value
						otherCheck.checked = $('all_'+checkbox.getProperty('class')+'Check').checked;
					}
				});
			}
			
			// Normal checkbox was clicked, make sure the all checkbox here is disabled
			else {
				$('all_'+checkbox.getProperty('class')+'Check').checked = false;
			}
		});
		
		// Make links activate their neighboring checkboxes as well
		var anchor = checkbox.getNext();
		anchor.addEvent('click', function() {
			anchor.getPrevious().checked = !(anchor.getPrevious().checked);
			anchor.getPrevious().fireEvent('click');
		});
	});
	
	// Wire up submit button to repopulate the page
	$('filterFormSubmit').addEvent('click', function() {
		var feedIDs = new Array();
		var filterIDs = new Array();
		
		filterForm.getElements('input').each(function(input) {
			switch(input.getProperty('class')) {
				case 'feed':
					if (input.checked && input.value != 'all')
						feedIDs.push(input.value);
				break;
				
				case 'tag':
					if (input.checked && input.value != 'all')
						filterIDs.push(input.value);
				break;
				
				default:
				break;
			}
		});
		
		// All form controls have been handled, update page content
		feeds.update(divToWriteTo, feedIDs, filterIDs, true);
		return false;
	});



	// Check for 'subject' parameter on URL and check the related checkbox
	var tag = getParameter('subject');
	if (tag) {
		var tagBox = $(tag+"_tagCheck");
		if (tagBox) {
			tagBox.checked = true;
		}
	}

	// Check for 'filter' parameter on URL and check the related checkbox
	var feed = getParameter('filter');
	if (feed) {
		var feedBox = $(feed+"_feedCheck");
		if (feedBox) {
			feedBox.checked = true;
		}
	}
	// If no feeds are specified, check 'em all
	else {
		$('all_feedCheck').checked = true;
		$('all_feedCheck').fireEvent('click'); // Necessary so that it checks the other feeds as well
	}
	
	// Finally, fire the click event after pulling out all the URL vars to actually load the selected content
	$('filterFormSubmit').fireEvent('click')
}

onload_register("setupFilterMenu('custom_articleListTarget');");
