var infoGBase;
var usedPosts;
var removeCols = 0;
var removeStates = 0;
var isOOO = false;

var setInfoG = function ( set )
{
	fadeBoxes( set );	
		
	set.find( 'a.tip' ).tooltip(
		{
			track: true,
		    delay: 0, 
		    showURL: false, 
		    opacity: 1, 
		    fixPNG: true, 
		    showBody: " - ",
			left: -20,
			top: 20
		}
	);
};

var setSorting = function ()
{
	infoGBase = $( '#info-g ul' ).html();
	
	$( '#info-g dd a.cat-sort' ).click( function ()
		{
			// temporarily disable all sorting
			$( '#info-g dd a.cat-sort, #info-g dd a.org' ).unbind( 'click' ).click( function ()
				{
					return false;		
				}
			);
			
			// add "loading" overlay
			$( '<img id="infog-loading" src="http://unitinteractive.com/blog/wp-content/themes/unit-verse/images/infog-loading.gif" alt="" />' )
				.appendTo( '#info-g' )
				.css( 
					{
						opacity: .5,
						display: 'block',
						position: 'absolute',
						top: '20px',
						left: '285px'	
					}
				);
			
			if ( $( this ).hasClass( 'selected' ) )
			{
				$( this ).removeClass( 'selected' );
				
				sorting( $( this ).attr( 'title' ), false ); 
			}
			else
			{
				$( this ).addClass( 'selected' );
				
				sorting( $( this ).attr( 'title' ), true );
			}	
		}
	);
	
	$( '#info-g dd a.org' ).click( function ()
		{
			if ( $( this ).hasClass( 'selected' ) )
			{
				return false; 
			}
			else
			{
				// temporarily disable all sorting
				$( '#info-g dd a.cat-sort, #info-g dd a.org' ).unbind( 'click' ).click( function ()
					{
						return false;		
					}
				);
				
				// add "loading" overlay
				$( '<img id="infog-loading" src="http://unitinteractive.com/blog/wp-content/themes/unit-verse/images/infog-loading.gif" alt="" />' )
					.appendTo( '#info-g' )
					.css( 
						{
							opacity: .5,
							display: 'block',
							position: 'absolute',
							top: '20px',
							left: '285px'	
						}
					);
					
				$( '#info-g dd a.org.selected' ).removeClass( 'selected' );
				
				$( this ).addClass( 'selected' );
				
				organizing( $( this ).attr( 'title' ) );
			}	
		}
	);
};

var sorting = function ( cat, isAdd )
{
	var all = $( '#info-g dd a.cat-sort.selected' );
	var cont = $( '#info-g ul' );
	var orgBy = $( '#info-g dd a.org.selected' ).attr( 'title' );
	
	if ( isAdd )
	{
		if ( all.length == 1 )
		{
			cont.html( '' );
		}
		
		var count = $( '#info-g ul li' ).length;
		
		$.get( 'http://unitinteractive.com/blog/wp-content/plugins/process-ajax.php', { func: 'boxes', cat: cat, count: count, orgBy: orgBy }, function ( data )
			{
				$( data ).appendTo( '#info-g ul' );
				
				var cat = $( data ).eq( 0 ).attr( 'className' );
				
				setInfoG( $( '#info-g ul li.' + cat ) );
				
				// re-enable sorting
				setSorting();
				
				//remove loading overlay
				$( '#infog-loading' ).remove();
			}
		);
	}
	else
	{
		if ( all.length )
		{
			$( '#info-g ul li.' + cat ).remove();
		}
		else if ( orgBy == 'date' )
		{
			cont.html( infoGBase );
			
			setInfoG( cont.find( 'li' ) );
		}
		else
		{
			organizing( orgBy );
		}
		
		// re-enable sorting
		setSorting();
		
		//remove loading overlay
		$( '#infog-loading' ).remove();
	}
	
	return false;
};

var organizing = function ( orgBy )
{
	var all = $( '#info-g dd a.cat-sort.selected' );
	var cats = [];
	
	if ( all.length )
	{
		$( '#info-g ul li' ).each( function ()
			{
				if ( cats[ cats.length - 1 ] != $( this ).attr( 'className' ) )
				{
					cats[ cats.length ] = $( this ).attr( 'className' );
				}
			}
		);
		
		for ( var i in cats )
		{	
			$.get( 'http://unitinteractive.com/blog/wp-content/plugins/process-ajax.php', { func: 'orgBoxes', cat: cats[i], orgBy: orgBy }, function ( data )
				{
					var thisClass = $( data ).eq(0).attr( 'className' );
					
					$( '<li id="replace-me"></li>' ).insertBefore( $('#info-g li.' + thisClass ).eq(0) );
					
					$('#info-g li.' + thisClass ).remove();
					
					$( '#replace-me' ).replaceWith( data );
					
					setInfoG( $( '#info-g ul li.' + thisClass ) );
					
					if ( i == cats.length - 1)
					{
						// re-enable sorting
						setSorting();
						
						//remove loading overlay
						$( '#infog-loading' ).remove();
					}
				}
			);
		}	
	}
	else
	{
		$.get( 'http://unitinteractive.com/blog/wp-content/plugins/process-ajax.php', { func: 'orgBoxes', orgBy: orgBy }, function ( data )
			{
				$( '#info-g ul' ).html( data );
				
				setInfoG( $( '#info-g ul li' ) );
				
				// re-enable sorting
				setSorting();
				
				//remove loading overlay
				$( '#infog-loading' ).remove();
			}
		);
	}
};

var fadeBoxes = function ( boxes )
{
	var boxCount = boxes.length;
	
	boxes.each( function ( i )
		{
			$( this ).css( { opacity: ( ( -0.6 / boxCount) * i ) + 1 } );
		}
	);
	
	return
};

var setShowPost = function ()
{
	$( '#content div.post, #content div.article ')
		.unbind( 'click' )
		.css (
			{
				cursor: 'pointer'
			}
		)
		.click ( function ( e )
			{
				if ( e.target.tagName == 'A' && !$( e.target ).parent().hasClass( 'expand' ) && e.target.parentNode.tagName != 'H2' )
				{
					return true;
				}
				else
				{
					var id = $( this ).attr( 'id' );
					id = id.replace( /ID/, '' );

					showPost( $( this ), id );
					
					return false;
				}	
			}
		);	
};

var showPost = function ( box, postID )
{
	var boxPos = box.position();
	
	if ( boxPos.left > 560 )
	{
		boxPos.left = 560;
	}
	
	var contPos = box.parent().offset();
	box.fadeTo( 500, 0 );
	
	$.get( 'http://unitinteractive.com/blog/wp-content/plugins/process-ajax.php', { func: 'fullpost', uvPostID: postID }, function ( data )
		{
    		var article = $( data );
			
			article.css (
					{
						opacity: 0,
						top: boxPos.top + 72,
						left: boxPos.left	
					}
				)
				.appendTo ( '#main' );
									
			setTimeout( function ()
				{
					/*if ( window.console )
					{
						console.log( article.height() );
					}*/
					
					var curHeight = $( '#content' ).height();
					var newHeight = article.height() + boxPos.top + 15;
					
					if ( newHeight > curHeight )
					{
						curHeight = newHeight;
						
						$( '#content' ).css( { height: curHeight } );	
					}
					
					article
						.fadeTo( 500, 1, function ()
							{
								if ( $.browser.msie ) $( this )[0].style.removeAttribute( 'filter' );
							}
						)
						.find( 'li.expand, div.topper a' )
						.show()
						.click( function ()
							{
								article.fadeTo( 500, 0, function ()
									{
										$( '#content' ).css( { height: 'auto' } );
										
										$( this ).remove();	
										
										box.fadeTo( 500, 1, function ()
											{
												$( '#content div.tweet, #content div.del, #content div.post, #content div.article' )
													.css( 
														{
															opacity: 1	
														} 
													)
													.unbind( 'click' )
													.each( function ()
														{
															if ( $.browser.msie ) $( this )[0].style.removeAttribute( 'filter' );
														}
													);
												
												setShowPost();
											} 
										);
									} 
								);
								
								return false;
							}
						);
				}, 200
			);
  		}
	);
	
	$( '#content div.tweet, #content div.del, #content div.post, #content div.article' )
		.css( 
			{
				opacity: 0.5	
			} 
		)
		.unbind( 'click' )
		.click( function ()
			{
				return false;	
			}
		);
};

var setMorePosts = function ()
{
	var totalPosts = parseInt( $( '#total-posts' ).text() );
	var shownPosts = parseInt( $( '#shown-posts' ).text() );
	
	if ( totalPosts > shownPosts )
	{
		$( 'a.more' )
			.removeClass( 'no-more' )
			.click( function ()
				{
					getMorePosts( totalPosts );
					
					return false;	
				}
			);	
	}
	else
	{
		$( 'a.more, a.fewer' ).click( function ()
			{
				return false;
			}
		);
	}
};

var getMorePosts = function ( totalPosts )
{
	var shownPosts = parseInt( $( '#shown-posts' ).text() );
	var args;
	
	if ( isOOO )
	{
		var items = $( '#content div.article, #content div.post, #content div.tweet, #content div.del' );
		var exclude = items.eq( items.length - 1 ).attr( 'id' );
		exclude = exclude.replace( /ID/, '' );
		
		args = 
			{
				func: 'moreRows',
				offset: shownPosts - 1,
				exclude: exclude
			};
	}
	else
	{
		args = 
			{
				func: 'moreRows',
				offset: shownPosts
			};
	}
	
	$.get( 'http://unitinteractive.com/blog/wp-content/plugins/process-ajax.php', args, function ( data )
		{
			data = $( data );
			
			var shownUpdate = data.filter( '#shown-posts' );
			
			if ( shownUpdate.hasClass( 'out-of-order' ) )
			{
				isOOO = true;
			}
			else 
			{
				isOOO = false;
			}
			
			$( '#shown-posts' ).replaceWith( shownUpdate );
			
			removeCols = 0;
			removeStates++;
			
			data.each( function ( i ) 
				{ 
					if ( i+1 < data.length )
					{
						var post = $( this ).css( 
										{ 
											display: 'none' 
										} 
									)
									.appendTo( '#content' );
									
						setTimeout( function() 
							{ 
								post.fadeIn( 500 );	
							}, i * 100 
						);	
						
						removeCols++;
					}
					else
					{
						if ( !$.browser.msie )
						{
							removeCols = removeCols / 2;	
						}
						
						setShowPost();
						
						if ( totalPosts == parseInt( $( '#shown-posts' ).text() ) )
						{
							$( 'a.more' )
								.addClass( 'no-more' )
								.unbind( 'click' )
								.click( function ()
									{
										return false;	
									}
								);
						}
						
						$( 'a.fewer' )
							.removeClass( 'no-fewer' )
							.unbind( 'click' )
							.click( function ()
								{
									removePosts( totalPosts );
									
									return false;	
								}
							);
					}
				} 
			);
			
			$( '.req-js' ).show();
		}
	);
};

var removePosts = function ( totalPosts )
{
	$.fn.reverse = [].reverse;
	
	var shownPosts = parseInt( $( '#shown-posts' ).text() );
	
	if ( removeCols )
	{
		$( '#content div.article, #content div.post, #content div.cont-4' ).reverse().each( function ( i )
			{
				removeCols--;
				shownPosts--;
				
				if ( $( this ).hasClass( 'cont-4' ) && $( this ).children().length == 2 )
				{
					shownPosts--;
				}
				
				var post = $( this );
			
				setTimeout( function() 
					{ 
						post.fadeOut( 500, function () 
							{
								post.remove();
							}
						);	
					}, i * 100 
				);
				
				if ( !removeCols )
				{
					$( '#shown-posts' ).text( shownPosts );
					
					removeStates--;
					
					if ( !removeStates )
					{
						$( 'a.fewer' )
							.addClass( 'no-fewer' )
							.unbind( 'click' )
							.click( function ()
								{
									return false;	
								}
							);
					}
					
					return false;
				}
			}
		);
	}
	else
	{
		var count = 0;
		
		removeCols = 20;
		
		$( '#content div.article, #content div.post, #content div.cont-4' ).reverse().each( function ( i )
			{
				if ( count == removeCols )
				{
					$( '#shown-posts' ).text( shownPosts );
					
					removeStates--;
					removeCols = 0;
					
					if ( !removeStates )
					{
						$( 'a.fewer' )
							.addClass( 'no-fewer' )
							.unbind( 'click' )
							.click( function ()
								{
									return false;	
								}
							);
					}
					
					return false;
				}
				else
				{
					var post = $( this );
					
					shownPosts--;
				
					if ( post.hasClass( 'cont-4' ) && post.children().length == 2 )
					{
						shownPosts--;
					}
				
					if ( post.hasClass( 'article' ) ) count += 2;
					else count++;
				
					setTimeout( function() 
						{ 
							post.fadeOut( 500, function () 
								{
									post.remove();
								}
							);	
						}, i * 100 
					);
				}
			}
		);
	}
	
	$( 'a.more' )
		.removeClass( 'no-more' )
		.unbind( 'click' )
		.click( function ()
			{
				getMorePosts( totalPosts );
				
				return false;	
			}
		);
};

$( document ).ready( function ()
	{
		$( '.req-js' ).show();
		
		setSorting();
		
		setInfoG( $( '#info-g ul li' ) );
			
		$( 'div.tag-box' ).each( function ()
			{
				setInfoG( $( this ).find( 'li' ) );	
			}
		);
		
		setShowPost();
		
		setMorePosts();
	}
);
