$(function() {
	IndvidualFeeds.init();
	Other.init();
});


var IndvidualFeeds = {
	
	Items: null,
	Checkboxes: null,
	
	init: function() {
		var cc = this;
		cc.Items = $('#IndividualFeeds li');
		cc.Checkboxes = $('#IndividualFeeds li input');
		cc.SelectLink = $('#SelectLink');
		cc.SubscribeButton = $('#SubscribeIndividual');
		
		cc.events();
		cc.render();
	},
	
	events: function() {
		var cc = this;
		cc.Checkboxes.bind('click', function(){
			var item = $(this).parent();
			if($(this).is(':checked')) {
				$(item).fadeTo('fast', 1);
			}
			else 
			{
				item.fadeTo('fast', .5);
			}
			
			var checked = false;
			cc.Checkboxes.each(function(){
				if($(this).attr('checked')) {
					checked = true;
				}
			})
			if(checked) {
				cc.SelectLink.addClass('clear');
				cc.SelectLink.text('Clear All');
				cc.SubscribeButton.css({opacity: 1});
			}
			else
			{
				cc.SelectLink.removeClass('clear');
				cc.SelectLink.text('Select All');
				cc.SubscribeButton.css({opacity: .5});
			}
		})
		
		cc.SelectLink.click(function(){
			
			var item = $(this);
			
			if(item.hasClass('clear')) {
				cc.Checkboxes.attr('checked', false);
				cc.Items.fadeTo('fast', .5);
				cc.SubscribeButton.css({opacity: .5});
				item.removeClass('clear');
				item.text('Select All');
			} 
			else {
				cc.Checkboxes.attr('checked', true);
				cc.Items.fadeTo('fast', 1);
				cc.SubscribeButton.css({opacity: 1});
				item.addClass('clear')
				item.text('Clear All');
			}
			
			return false;
		});
		
		cc.SubscribeButton.click(function() {
			var url = '';
			cc.Checkboxes.each(function(){
				var checkbox = $(this);
				if(checkbox.attr('checked')) {
					if(url!='') {
						url += '-'+checkbox.attr('name');
					}
					else {
						url = checkbox.attr('name');
					}
				}
			})
			if(url!='') {
				document.location = '/rss/'+url;
			}
			return false;	
		});
	},
	render: function() {
		var cc = this;
		cc.SubscribeButton.css({opacity: .5});
		cc.Items.css({opacity: .5});

	}
}

var Other = {
	
	Items: null,
	Links: null,
	
	init: function() {
		var cc = this;
		cc.Items = $('#Other li');
		cc.Links = $('#Other li a');
		
		cc.events();
		cc.render();
	},
	
	events: function() {
		var cc = this;
		cc.Links.bind('mouseenter', function(){
			var item = $(this).parent();
			item.stop();
			$(item).fadeTo('fast', 1);
		})
		
		cc.Links.bind('mouseleave', function(){
			var item = $(this).parent();
			item.stop();
			$(item).fadeTo('fast', .5);
		})
	
	},
	
	render: function() {
		var cc = this;
		
		cc.Items.css({opacity: .5});
	}
}