/**
 * Enables the rss box checkboxes to alter the subscription link
 *
 * Copyright 2011 dkd Internet Service GmbH
 *
 * @author Grigori Prokhorov <grigori.prokhorov@dkd.de>
 * @since 2011-11-02
 */
$(document).ready(function() {
	$(".page_row_right_portlet .rsscheckbox").click(function() {
		/**
		 * Initialize
		 * - the concatenation prefix
		 * - the current link value
		 * - the full parameter string
		 *
		 * - the currently selected categories
		 * - the category array
		 * - the category string which is appended later
		 * - the position at which to append the category string
		 */
		var prefix = '?';
		var rssLink = $('#rss_feed_link').attr('href');
		var catParam = 'tx_ttnews[cat]=';
		
		var currentlySelected = '';
		var catArray = new Array();
		var categoryString = '';
		var appendPos = rssLink.length;

			//determine which prefix to use for glueing the string later
		if (rssLink.indexOf('?') >= 0) {
			prefix = '&';
		}

		/**
		 * Analyze the link string and extract the currently selected categories:
		 * 
		 * if the category parameter is already set,
		 *		get the currently selected categories by extracting the string starting from the end of the category parameter
		 *		to the link string's end
		 *
		 *		if the value string actually contains any characters (which is not neccesserily the case since the category
		 *		parameter might be set without carrying any value)
		 *			split the string by comma and store each part as an array item
		 */
		if (rssLink.indexOf(catParam) >= 0) {
			currentlySelected = rssLink.substr(
				rssLink.indexOf(catParam) + catParam.length,
				rssLink.length
			);

			if (currentlySelected.length > 0) {
				catArray = currentlySelected.split(',');
			}
		}

		/**
		 * React to state change of clicked checkbox:
		 *
		 * If the click activated the checkbox (i.e. the user selected it) 
		 *		append the checkbox' value to the category array
		 * else 
		 *		cycle through the category array and
		 *			if the current checkbox' value is found within the category array
		 *				remove the current checkbox' value.
		 */
		if ($(this).is(':checked')) {
			catArray.push($(this).val());
		} else {
			for (var catIndex in catArray) {
				if (parseInt(catArray[catIndex]) === parseInt($(this).val())) {
					catArray.splice(catIndex, 1);
				}
			}
		}

		/**
		 * Clean up the category array:
		 *
		 * Cycle through the category array and
		 *		if the current value is already present
		 *			ignore it
		 *		else
		 *			add it to the category string postfixed with a comma as delitimiter
		 */
		for (var catIndex = 0; catIndex < catArray.length; catIndex++) {
			if (parseInt(catArray[catIndex]+1) > 0 && parseInt(catArray[catIndex]) === parseInt(catArray[catIndex+1])) {
				continue;
			} else {
				categoryString += parseInt(catArray[catIndex]) + ',';
			}
		}

			// remove the last comma from the string to avoid creating an empty array element in the next call
		categoryString = categoryString.substr(0, categoryString.length-1);

		/**
		 * Determine the position at which the category parameter is to be appended:
		 *
		 * if the category parameter is already present in the link string
		 *		the append position is the position of the first character of the category parameter in the link string, 
		 *		minus one character which is the paremeter prefix (either ? or &).
		 */
		if (parseInt(rssLink.indexOf(catParam)) >= 0) {
			appendPos = parseInt(rssLink.indexOf(catParam) - 1);
		}

		/**
		 * Update the link string with the new category parameter by concatinating
		 * - the link string from beginning to the determined append position
		 * - plus the prefix (either ? or &)
		 * - plus the category parameter
		 * - plus the category string
		 */
		$('#rss_feed_link').attr('href', rssLink.substr(0, appendPos) + prefix + catParam + categoryString);
	});
});
