(function($){
	$.fn.mccdtweetupdate = function(url){
		return this.each(function(){
			var ul = $(this);
			$.getJSON(url, function(data,status){
				if(!data)return;
				data.reverse();
				$.each(data, function(i){
					if( $("#tweet" + this.id).size()>0 ) return;

					$("<li id=\"tweet" + this.id + "\">")
						.addClass("tweet")
						.append( $("<img>").attr('src', this.user.profile_image_url) )
						.append( $("<a>").attr('href', 'http://twitter.com/' + this.user.screen_name).html( this.user.name ) )
						.append(": ")
						.append( this.text.replace(/(\s)(http:\/\/\S+\.[a-z]{2,}(\/\S*)?)(\s|$)/i, '$1<a href="$2">$2</a>$4') )
						.prependTo( ul );
				});
			});
		});
	}
})(jQuery);


(function($){

	$.fn.tuxsudoBubble = function(html){
		return $(this).each(function(){
			$(this).hover(
				function(e){
					var $bubble = $("<div>").css({position:"absolute", 'z-index':99 }).addClass("tuxsudobubble").html( $("<div>").addClass('inner').html(html) ).appendTo(document.body);
					$bubble.css({ top:$(this).parent().offset().top-$bubble.outerHeight(true), left:$(this).parent().offset().left + $(this).parent().width()/2 });
					$(this).data('$tuxsudoBubble', $bubble  )
				},
				function(){ $(this).data('$tuxsudoBubble').remove() }
			)
		})
	}

})(jQuery);



//mccdpaginate
(function($){

	var defaults = {
		auto:false,
		loop:false,
		interval:10000,
		wait_time:5000,
		on_auto_interupt:"pause", 	// (pause|wait|nothing)
		showfn: function(){ $(this).show() },
		hidefn: function(){ $(this).hide() },
		resetfn:function(){ $(this).show() },
		window_size:1,
		step:1
	}


	var methods = {
		init:function( options ){
			return this.each( function(){

				$(this).data('options', $.extend({}, defaults, options) );
				
				if( $(this).data('options').auto)
					$(this).mccdpaginate('start');
					
			})
		},

		next:function(){
			return this.each(function(){
				if( $(this).data('options').loop)
					$(this).mccdpaginate('loop', 1);
				var total = $(this).children(":visible").size();
				var step = $(this).data('options').step;
				var window_size = $(this).data('options').window_size;

				var s = total - step >= window_size ? step : total - window_size
				$(this).data('options').hidefn.call( $(this).children(":visible").slice(0, Math.max(0, s ) ) );
			});
		},

		prev:function(){
			return this.each(function(){
				if( $(this).data('options').loop)
					$(this).mccdpaginate('loop', -1);

				var $jqobj = $(this);
				$jqobj.data('options').showfn.call( $jqobj.children(":hidden").slice( -1*parseInt($jqobj.data('options').step) ) );
			})
		},

		reset:function(){
			return this.each(function(){
				var hidden = $(this).children(":hidden");
					$(this).data('options').resetfn.call( hidden );
			})
		},
		start:function(){
			return this.each(function(){
				var $jqobj = $(this);
				var options = $jqobj.data('options');
				$jqobj.data('timer', window.setInterval(function(){
					if( $jqobj.children(":visible").size() <= $jqobj.data('options').window_size)
						$jqobj.mccdpaginate('reset');
					else
						$jqobj.mccdpaginate('next');

				}, options.interval) );
			})
		},
		stop:function(){
			return this.each(function(){
				window.clearInterval( $(this).data('timer') );
			})
		},

		delay:function(){
			return this.each(function(){
				var $jqobj = $(this);
				if( !$jqobj.data('options').auto )
					return;
				$jqobj.mccdpaginate('stop');
				window.clearTimeout( $jqobj.data('delay_to') );
				$jqobj.data('delay_to', window.setTimeout(function(){
					$jqobj.mccdpaginate('start');
				}, $jqobj.data('options').wait_time));
			})
		},
		loop:function(direction){
			return this.each(function(){
				if(direction>0)
					$(this).data('options').showfn.call( $(this).children(":hidden").appendTo(this) );
				else
					$(this).children(":visible").last().prependTo(this).hide();
			});

		}


	}

	$.fn.mccdpaginate = function(method){
		if ( methods[method] )
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		else if ( typeof method === 'object' || ! method )
			return methods.init.apply( this, arguments );
		else
			$.error( 'Method ' +  method + ' does not exist' );
	}



})(jQuery);


/*tuxsudoDropnav*/
(function($){

	function isActivated($obj){
		return $obj.hasClass("activated");
	}
	
	function activate($obj){
		return $obj.addClass("activated").children("ul:hidden, div:hidden").slideDown('fast');
	}
	
	function deactivate($obj){
		return $obj.removeClass("activated").children("ul:visible, div:visible").hide();
	}
	
	function toggle($obj){
		return isActivated($obj) ? deactivate($obj) : activate($obj);
	}
		

	var methods = {
		init:function(args){
			var options = $.extend({}, { 'separate':true }, args );

			$("body").click( $.fn.tuxsudoDropnav.reset );		

			return $(this).each( function(){
				var $list = $(this).addClass("tuxsudo_dropdown");
				var $parent_nodes = $list.find("li > ul, li > div").parent().addClass("parent");
				
				$parent_nodes.children("a").each( function(){
					
					var $indicator = $("<span>").html('▼').addClass("dropdown");
					
					$(this).append( $indicator );
					
					(options.separate ? $indicator : $(this) ).click( function(){
							deactivate( $(this).closest("li").siblings("li") );
							toggle( $(this).closest("li") );
							return false;
					})
					
				});

				$(this).find("input").click( function(){ return false} );

			});
		},

		reset:function(){
			$(".tuxsudo_dropdown li.activated").tuxsudoDropnav("deactivate");
		}

	};


	$.fn.tuxsudoDropnav = function(method){
		if ( methods[method] )
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		else if ( typeof method === 'object' || ! method )
			return methods.init.apply( this, arguments );
		else
			$.error( 'Method ' +  method + ' does not exist' );
	}
	
	$.fn.tuxsudoDropnav.reset = function(){
		deactivate( $(".tuxsudo_dropdown li.activated") );
	}



})(jQuery);


(function($) {


	$(function(){


		$('#header .nav > ul').tuxsudoDropnav({'separate':false});
		$('#quicklinks ul').tuxsudoDropnav({'separate':false});

		$('#happenings .social .tweets ul').mccdtweetupdate('http://api.twitter.com/1/mcccd/lists/all/statuses.json?callback=?').mccdpaginate({
			auto:true,
			loop:true,
			showfn:function(){ $(this).slideDown() },
			hidefn:function(){ $(this).slideUp() }
		});


//		window.setInterval( function(){$('#happenings .tweets ul').mccdtweetupdate('http://api.twitter.com/1/mcccd/lists/all/statuses.json?callback=?')}, (1000 * 60 * 5) );

		$("#happenings .promos ul").mccdpaginate({
			showfn:function(){
				$(this).fadeIn('fast');
			},
			hidefn:function(){
				$(this).fadeOut('fast');
			},
			resetfn:function(){
				$(this).width($(this).data('oldwidth')).show();

			},
			loop:true,
			window_size:4,
			step:1
		});

		$('#happenings .social, #happenings .promos ')
			.mouseover(function(){ $(this).find(".nav").show(); })
			.mouseout(function(){ $(this).find(".nav").hide() })

		$('#happenings .social .tweets a.nav.next').click( function(){ $('#happenings .tweets ul').mccdpaginate('next').mccdpaginate('delay') });
		$('#happenings .social .tweets a.nav.prev').click( function(){ $('#happenings .tweets ul').mccdpaginate('prev').mccdpaginate('delay') });
		$('#happenings .promos a.nav.next').click( function(){ $('#happenings .promos ul').mccdpaginate('next').mccdpaginate('delay')  });
		$('#happenings .promos a.nav.prev').click( function(){ $('#happenings .promos ul').mccdpaginate('prev').mccdpaginate('delay')  });

	})






})(jQuery);





