$(document).ready(function(){
	// Set Vars //
	var color = [
		{color : 'purple', value : '#812869'}, 
		{color : 'blue', value : '#4e75b6'}, 
		{color : 'orange', value : '#d47831'}, 
		{color : 'green', value : '#3f8022'}, 
		{color : 'teal', value : '#59a1a5'}, 
		{color : 'red', value : '#a44728'}
	];
	
	var cur   = 0;
	
	
	// Menu Functions //
	var menu = {
		// Initialize //
		init : function(){
			$(document).click(function(){
				menu.hideMenu();
			});
			
			$('.newListSelected').hover(function(){
				$(this).addClass('newListSelHover');
			}, function(){
				$(this).removeClass('newListSelHover');
			});
			
			$('.newListSelected').click(function(){
				$(this).focus();
				menu.showMenu();
				return false;
			});
			
			$('.newList a').click(function(){
				$('.newListSelected .selectedTxt').text($(this).text());
				$(this).parent().focus();
				menu.hideMenu();
				menu.getURL($(this));
				return false;
			});
		},
		// Hide Submenu //
		hideMenu : function(){
			$('.newList').css('display', 'none');
		},
		// Show Submenu //
		showMenu : function(){
			$('.newList').css('display', 'block');
		},
		// Get URL //
		getURL   : function(obj){
			// Get Page URL //
			var url = obj.attr('href');
			$.ajax({
				url      : url,
				dataType : 'html',
				success  : function(response){
					// Change Content //
					$('#tweets').fadeOut('slow', function(){
						// Change Color //
						var next = cur < color.length - 1? cur + 1 : 0;
						
						$('.container').removeClass(color[cur].color);
						$('.container').addClass(color[next].color);
						$('.container').animate({
							backgroundColor : color[next].value
						}, "slow");
						cur = next;
						
						$(this).html(jQuery(response).find('#tweets').html());
						$('#pager').html(jQuery(response).find('#pager').html());
						$('#pager a').each(function(){
							old = $(this).attr('href');
							$(this).attr('href', (url + old))
						});
						$('.quote').html(jQuery(response).find('.quote').html());
						$(this).fadeIn('slow');	
					});
				}
			});
		}
	};
	
	menu.init();
	
	$('#pager a').live('click', function(){
		// Get Page URL //
		var url = $(this).attr('href');
		$.ajax({
			url      : url,
			dataType : 'html',
			success  : function(response){
				surl = url.replace(/\?.*/,'');
				// Change Content //
				$('#tweets').fadeOut('slow', function(){
					$(this).html(jQuery(response).find('#tweets').html());
					$('#pager').html(jQuery(response).find('#pager').html());
					
					$('#pager a').each(function(){
						old = $(this).attr('href');
						//alert(surl + old);
						$(this).attr('href', (surl + old))
					});
					$(this).fadeIn('slow');
					
				});
			}
		});
		
		return false;
	});
});
