jQuery(document).ready(function($) {

var cpt = {
	state : $('.catNav .catTabs .allTab a'),
	
	clickCount : 0,
									
	imagePath : '/wp-content/plugins/category-post-tabs/images/',
	
	fadeSpeed : 500,
	
	contentHide :  function(state) {
					$('.catNav .pageBottom li a').hide();
					$('.articleWrap').fadeOut(cpt.fadeSpeed, function() {
						$(this).empty()
						.append('<img alt="" class="catTabsLoadImg" src="' + cpt.imagePath + 'ajax-loader-' + state.attr('href').slice(1) + '.gif" />')
						.show();
					});
					$('.catNav .pagetop li a')/*.filter(function() {return $(this).css('display') != 'none';})*/.fadeOut(Math.round(cpt.fadeSpeed / 2));
				},
	
	tabNav : function(event) {
				var t = $(event.target);
				$('.catTabs a').unbind('click').click(function() {return false;});
				cpt.clickCount = 0;
				cpt.state = t;
				cpt.contentHide(t);
				// apply active style and send request for posts
				switch(t.attr('href')) {
					case '#orig':
						$.post('/wp-admin/admin-ajax.php', { tab: '#orig', cat: cpt.currentCategory, action: 'cpt' }, cpt.handlePosts, 'json' );
						cpt.allTabStyle();
						cpt.synTabStyle();
						$('ul.catTabs li a').removeClass('active');
						cpt.activeStyle(t);
						t.addClass('active');
						break;
					case '#syn':
						$.post('/wp-admin/admin-ajax.php', { tab: '#syn', cat: cpt.currentCategory, action: 'cpt' }, cpt.handlePosts, 'json' );
						cpt.allTabStyle();
						cpt.origTabStyle();
						$('ul.catTabs li a').removeClass('active');
						cpt.activeStyle(t);
						t.addClass('active');
						break;
					default:
						$.post('/wp-admin/admin-ajax.php', { tab: '#all', cat: cpt.currentCategory, action: 'cpt' }, cpt.handlePosts, 'json' );
						cpt.origTabStyle();
						cpt.synTabStyle();
						$('ul.catTabs li a').removeClass('active');
						cpt.activeStyle(t);
						t.addClass('active');
				}
				event.preventDefault();
	},
	
	prevNextNav : function(event) {
					var t = $(event.target);
					// change current page of posts
					cpt.contentHide(cpt.state);
					$('.catNav .pagenav li a').unbind('click').click(function() {return false;});
					var clicked = t.attr('href');
					if( clicked == '#prev' ) {
						cpt.clickCount--;
					} else {
						cpt.clickCount++;
					}
					$.post('/wp-admin/admin-ajax.php', { 	tab: 'prevNext',
															cat: cpt.currentCategory, 
															clickCount: cpt.clickCount,
															state: cpt.state.attr('href'), 
															action: 'cpt' 
														}, cpt.handlePosts, 'json' );
					event.preventDefault();
				},
	
	showHideNew : function() {
					if(cpt.clickCount) {
						$('#column1and2 .pagenav .prev a').fadeIn(cpt.fadeSpeed);
					} else {
						$('#column1and2 .pagenav .prev a').fadeOut(cpt.fadeSpeed);
					}
				},
				
	showHideOld : function(foundPosts, postsPerPage) {
					if( foundPosts - ( postsPerPage * cpt.clickCount ) <= postsPerPage ) {
						$('.pagenav .next a').fadeOut(cpt.fadeSpeed);
					} else {
						$('.pagenav .next a').fadeIn(cpt.fadeSpeed);
					}
				},
									
	activeStyle : function(t) {
					t.prev().attr('src', this.imagePath + 'bkgdL1.gif');
					t.next().attr('src', this.imagePath + 'bkgdR1.gif');
				},
	
	allTabStyle : function() {
					$('.allTab a').prev().attr('src', this.imagePath + 'allBkgdL0.gif');
					$('.allTab a').next().attr('src', this.imagePath + 'allBkgdR0.gif');	
				},
	
	origTabStyle : function() {
					$('.origTab a').prev().attr('src', this.imagePath + 'origBkgdL0.gif');
					$('.origTab a').next().attr('src', this.imagePath + 'origBkgdR0.gif');	
				},
	
	synTabStyle : function() {
					$('.synTab a').prev().attr('src', this.imagePath + 'synBkgdL0.gif');
					$('.synTab a').next().attr('src', this.imagePath + 'synBkgdR0.gif');	
				},
				
	handlePosts : function(response, status) {
					// ajax callback, receive posts and print them
					$('.articleWrap').hide()
									 .empty()
									 .append(response['posts'])
									 .fadeIn(cpt.fadeSpeed, function() {
																		cpt.showHideNew();
																		cpt.showHideOld(response['foundPosts'], response['postsPerPage']);
																		$('.catTabs a').unbind('click').bind('click', cpt.tabNav);
																		$('.catNav .pagenav li a').unbind('click').bind('click', cpt.prevNextNav);
																	   });
				},
				
	getCategory : function(event) {
					var t = $(this);
					t.unbind('click').click(function() {return false;});
					// get the category from hidden input
					cpt.currentCategory = $('#currentCategory').val();
					// make sure we're not on a paginated category archive
					var loc = window.location;
					var pathName = loc.pathname.split('/');
					var potentialCatSlug = pathName[pathName.length - 2];
					if(!isNaN(potentialCatSlug)) {
						// redirect to base category page
						var catUrl = document.location.href.split('/');
						for(var $i = 0; $i < 3; $i++) {
							catUrl.pop();	
						}
						catUrl = catUrl.join('/');
						loc.replace(catUrl);
					} // end if
					// pass event to tab functions
					var clicked = t.attr('href');
					if( clicked == '#prev' || clicked == '#next' ) {
						cpt.prevNextNav(event);
					} else {
						cpt.tabNav(event);
					}
					event.preventDefault();
				}
} // end cpt

$('.catTabs a').hover(
	function() {
		var t = $(this);
		cpt.activeStyle(t);
	}, function() {
		cpt.allTabStyle();
		cpt.origTabStyle();
		cpt.synTabStyle();
		cpt.activeStyle(cpt.state);
	}
);

$('.catTabs a, .catNav .pagenav li a').click(cpt.getCategory);

}); //document ready
