tabOnLoad = function() {
	var nav_elms = $('#nav div'),
		nav_stat_elms = $('#nav_status div'),
		i = 0,
		i_len = nav_elms.length;

	for (; i < i_len; i = i + 1) {
		$(nav_elms[i]).click(handleNavEvent);
		$(nav_elms[i]).mouseover(handleNavEvent);
		$(nav_elms[i]).mouseout(handleNavEvent);
		$(nav_elms[i])[0]._status = $(nav_stat_elms[i]);
	}

	$('#rss_custom .s_col li').each(function(idx, elm){
		$(elm).click(handleCustomSColEvent);
	});

	$('#rss_custom .create_button:first').click(createCustom);

	$('#rss_static .s_col li').each(function(idx, elm){
		$(elm).click(handleCustomSColEvent);
		$(elm).click(handleStaticFilterEvent);
	});

	$('#rss_static .create_button:first').click(createStatic);

	$('#rss_favorite .s_favorites')[0]._max = 5;
	$('#rss_favorite .s_favorites li').each(function(idx, elm){
		$(elm).click(handleCustomSColEvent);
	});
	$('#rss_favorite .create_button:first').click(createFavorite);

	$('#rss_box form').each(function(idx, elm){
		$(elm).submit(function(evt){
			evt.stopPropagation();
		});
	});

	window._navLastSelected = $('#nav div:first');
};

handleNavEvent = function(evt) {
	var target = $(evt.currentTarget),
		status = null,
		statusTextElm = null;

	switch (evt.type) {
		case 'click':
			if (target == window._navLastSelected) {
				return;
			}

			window._navLastSelected[0]._status.removeClass('a_filled');
			window._navLastSelected.addClass('nd_' + window._navLastSelected.attr('class').substr(2, 3));

			target[0]._status.addClass('a_filled');
			window._navLastSelected = target;
			window._navLastSelected[0]._status.removeClass('a_outlined');
			window._navLastSelected.removeClass('nd_' + target.attr('class').substr(2, 3));

			status = $('#nav_status .status_text:first');

			if (status.hasClass('status_center')) {
				status.removeClass('status_center');
			} else if (status.hasClass('status_right')) {
				status.removeClass('status_right');
			}

			$('#rss_box .rss_ui').each(function(idx, elm){
				$(elm).hide();
			});

			statusTextElm = $('#nav_status span:first');

			if (target.hasClass('n_sta')) {
				status.addClass('status_center');
				statusTextElm.text("Create a static feed from this Contributor's content.");
				$('#rss_static').show();

				if ($('#rss_static_list li').length < 2) {
					refreshStaticList(true);
				}
			} else if (target.hasClass('n_fav')) {
				status.addClass('status_right');
				statusTextElm.text('Create a feed of the content from your favorite Contributors.');
				$('#rss_favorite').show();
			} else {
				statusTextElm.text('Create a filtered feed based on Type, Categories, and Keywords.');
				$('#rss_custom').show();
			}

		case 'mouseover':
			if (target == window._navLastSelected) {
				return;
			}
			target[0]._status.addClass('a_outlined');
			break;

		case 'mouseout':
			if (target == window._navLastSelected) {
				return;
			}

			target[0]._status.removeClass('a_outlined');
			break;

		default:
			return;
	}
};

handleCustomSColEvent = function (evt) {
	var target = $(evt.currentTarget),
		container = null,
		row = null,
		cb = null
		_fav_delta = null;

	switch (evt.type) {
		case 'click':
			if (target.hasClass('limit')) {
				break;
			}

			if (target[0].tagName.toLowerCase() == 'input') {
				container = target[0].parentNode.parentNode.parentNode;
				row = target[0].parentNode;
			} else {
				container = target[0].parentNode.parentNode;
				row = target;
			}

			if (row.hasClass('no_action')) {
				break;
			}

			cb = row.find('input:first');

			if (cb.type == 'radio') {
				container._selected.removeClass('selected');
				container._selected = row;
				container._selected.addClass('selected');

				if (cb.checked) {
					return;
				}
			}

			if (container._max) {
				_fav_delta = (row._selected) ? (-1) : (1);

				if (container._count == undefined || container._count == 0) {
					container._count = 1;
				} else {
					if (false === row._selected && container._count == container._max) {
						evt.stopPropagation();
						break;
					}

					container._count = (container._count + _fav_delta);
				}
			}

			if (row._selected) {
				row.removeClass('selected');
				row._selected = false;
				cb.checked = false;
			} else {
				row.addClass('selected');
				row._selected = true;
				cb.checked = true;
			}

			break;

		default:
			break;
	}

	return true;
};

handleStaticFilterEvent = function(evt) {
	var target = $(evt.currentTarget);

	if (evt.type != 'click') {
		return;
	}

	if (target.hasClass('no_action') || target[0].parentNode.hasClass('no_action')) {
		return;
	}

	if (window._staticXHR) {
		staticRefreshCancel();
	}

	$('#rss_static .s_list').hide();
	$('#rss_static_none').hide();
	$('#rss_static_loader').show();

	if (window._staticRefreshTimeout) {
		clearTimeout(window._staticRefreshTimeout);
	}

	$('#rss_static_list li').each(function(idx, elm){
		$(elm).remove();
	});

	window._staticRefreshTimeout = setTimeout(refreshStaticList, 1200);

	return;
};

handleStaticRefresh = function(responseText, textStatus, xhr) {
	$('#rss_static_loader').hide();

	if ($('#rss_static_list li').length > 0) {
		$('#rss_static .s_list').show();
	} else {
		$('#rss_static_none').show();
	}

	window._staticXHR = null;

	return true;
};

staticRefreshCancel = function(evt) {
	if (false === window._staticXHR) {
		return false;
	}

	window._staticXHR.transport.abort();
	window._staticXHR = null;

	return true;
};

refreshStaticList = function() {
	var param = {user_id: window._userID};

	if (window._staticRefreshTimeout) {
		param.push($('#rss_static'));
	}

	 $('#rss_static_list').load('/action_rsslist_pop.shtml', param, handleStaticRefresh);

	return true;
};

showFeedLink = function (elm_id, feed_url) {
	var link = $('#' + elm_id + ' a span:first');

	link.parent().attr('href', feed_url);

	if ($('body').text() != undefined) {
		link.text((feed_url.length > 95) ? (feed_url.substr(0, 92) + ' ...') : (feed_url));
	} else {
		link[0].textContent = (feed_url.length > 95) ? (feed_url.substr(0, 92) + ' ...') : (feed_url);
	}

 	$(elm_id.toString()).removeClass(elm_id.toString()).addClass(elm_id + '_link');
	$('#' + elm_id + ' .rss_link').show();
};

createCustom = function (evt) {
	var args = ($('#rss_custom').serialize(true));

	if ($('#rss_custom .interior input[name="title"]').val() == '') {
		alert('You must specify a feed title');
		$('input[name="title"]').focus();
		return false;
	}

	$.ajax({
		url: window._rssBaseURL + '/action_rss_getlink.shtml',
		async: false,
		type: 'post',
		data: args,
		success: function(data, status, xhr) {
			showFeedLink('rss_custom', data);
		}
	});
};

createStatic = function(e) {
	var args = ($('#rss_static').serialize(true));

	if ($('#rss_static .create input[name="title"]').val() == '') {
		alert('You must specify a feed title');
		$('input[name="title"]').focus();
		return false;
	}

	$.ajax({
		url: window._rssBaseURL + '/action_rss_getlink.shtml',
		async: false,
		type: 'post',
		data: args,
		success: function(data, status, xhr) {
			showFeedLink('rss_static', data);
		}
	});
};

createFavorite = function(e) {
	var args = ($('#rss_favorite').serialize(true));

	if ($('#rss_favorite .create input[name="title"]').val() == '') {
		alert('You must specify a feed title');
		$('input[name="title"]').focus();
		return false;
	}

	$.ajax({
		url: window._rssBaseURL + '/action_rss_getlink.shtml',
		async: false,
		type: 'post',
		data: args,
		success: function(data, status, xhr) {
			showFeedLink('rss_favorite', data);
		}
	});
};
