$(document).ready(function() {

	//Remove twitter message if it's empty
	if(jQuery.trim($('div#twitterContainer').text()) == 'No public Twitter messages.') {
		$('div#twitterContainer').html('<a href="http://twitter.com/davist11" class="twitter-link">Follow Me on Twitter</a>');
		//$('body').addClass('noTwitter');
	}

	//Recent Work Section
	$('div#recentWorkContainer').after('<a href="#" id="recentWorkPrev" title="geri">geri</a><a href="#" id="recentWorkNext" title="ileri">ileri</a>');
	
	var currentWorkPosition = 0;
	var goTo = 0;
	var bodyId = $('body').attr('id');
	
	if(bodyId == 'home') {
		var scrollAmount = 480;
	} else {
		var scrollAmount = 360;
	}
	
	$('a#recentWorkPrev, a#recentWorkNext').click(function() {
		$(this).css('outline','none');
		var action = $(this).text();
		if(action == 'geri') {
			if(currentWorkPosition == 0) {
				if(scrollAmount == 480) {
					currentWorkPosition = 960;
				} else {
					currentWorkPosition = 1080;
				}
			} else {
				currentWorkPosition = currentWorkPosition - scrollAmount; 
			}
		} else if( action == 'ileri') {
			if((scrollAmount == 480 && currentWorkPosition == 960) || (scrollAmount == 360 && currentWorkPosition == 1080)) {
				currentWorkPosition = 0;	
			} else {
				currentWorkPosition = currentWorkPosition + scrollAmount; 
			}
		}
		goTo = '-'+currentWorkPosition+'px';
		$('ul#recentWorkDisplay').animate({'left':goTo}, 1500);
		return false;
	});
	
	//Recent posts Tabs
	$('ul#tabNav a').click(function() {
		$(this).css('outline','none');
		var curChildIndex = $(this).parent().prevAll().length + 1;
		$(this).parent().parent().children('.current').removeClass('current');
		$(this).parent().addClass('current');
		$(this).parent().parent().next('.tabContainer').children('.current').slideUp('fast',function() {
			$(this).removeClass('current');
			$(this).parent().children('div:nth-child('+curChildIndex+')').slideDown('normal',function() {
				$(this).addClass('current');
			});
		});
		return false;
	});
	
	//RSS Feeds
	$('a#rssHeader').click(function() {
		$(this).css('outline','none');
		$(this).siblings('ul').slideToggle()
		return false;
	});

	//Search form
	if($('form#searchForm input#s').val() == '') {
		$('form#searchForm input#s').val($('form#searchForm input#s').attr('title'));
	}
	
	$('form#searchForm input#s').focus(function() {
		if($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	});
	
	$('form#searchForm input#s').blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	//Subpage accordian
	$('div.recent h3 a').click(function() {
		$(this).css('outline','none');
		if($(this).parent().parent().hasClass('current')) {
			$(this).parent().siblings('ul').slideUp('slow',function() {
				$(this).parent().removeClass('current');
				$.scrollTo('#recentPosts',1000);
			});
		} else {
			$('div.recent.current ul').slideUp('slow',function() {
				$(this).parent().removeClass('current');
			});
			$(this).parent().siblings('ul').slideToggle('slow',function() {
				$(this).parent().toggleClass('current');
			});
			$.scrollTo('#recentPosts',1000);
		}
		return false;
	});
	
	$('div#secondaryContent div.module.asides ul li p:last-child').addClass('last');
	
	//Comments & Trackbacks Tabs
	$('div#comments ul#commentsNav a').click(function() {
		var commentsNavId = $(this).parent().attr('id');
		
		if(!$(this).parent().hasClass('current')) {
			
			$('div#comments ul#commentsNav li.current').removeClass('current');
			$(this).parent().addClass('current');
			
			if(commentsNavId == 'numComments') {
				$('div#comments ol#trackbackListing').slideUp('slow', function() {
					$('div#comments ol#commentListing').slideDown('fast');
				});
			} else if(commentsNavId == 'numTrackbacks') {
				$('div#comments ol#commentListing').slideUp('slow', function() {
					$('div#comments ol#trackbackListing').slideDown('fast');
				});
			}
			$.scrollTo('#commentsNav',1000);	
		}
		return false;
	});
	
	//Comments reply
	$('ol#commentListing li.reply a').click(function() {
		var commentor = $(this).parent().siblings('.commentor').text();
		var currentComment = $(this).parent().parent().parent().attr('id');
		var commentVal = $('form#commentform textarea#comment').val();
		
		var newVal = '@<a href="#'+currentComment+'">'+commentor+'</a>-'+"\n";
		
		if(commentVal === '') {
			$('form#commentform textarea#comment').val(newVal);
		} else {
			$('form#commentform textarea#comment').val(newVal+commentVal);
		}
		
		$.scrollTo('form#commentform',1000);
		$('form#commentform textarea#comment').focus();
		return false;

	});
	
	//Flickr alt class
	$('div#flickr ul li:nth-child(4n)').addClass('alt');
	
	$('#flickr ul a').each(function() {
		var imgSrc = $(this).children().attr('src');
		$(this).attr('href',imgSrc.replace('_s.jpg','.jpg'));
	});
	//$('a[rel*=lightbox]').slimbox();
	
	//Grid link
	$('a.viewGrid').click(function() {
		$('body').prepend('<div id="grid" style="display: none; height: 0;"></div>');
		
		if($.browser.msie && $.browser.version <= 6.0) {
			var windowHeight = $(document).height();
			windowHeight = windowHeight + 'px';
		} else {
			var windowHeight = '100%';
		}
		
		
		$('div#grid').animate({ height: windowHeight}, 2500);
		$('div#grid').click(function() {
			$(this).animate({ height: "0"}, 1000, function() {
				$(this).remove();
			});
		});
		return false;
	});
	
	
	//Tooltips
	var title = '';
	$('ul#portfolio a').hover(function() {
		title = $(this).attr('title');
		$(this).attr('title','');
		$(this).children('img').attr('alt','');
		var elOff = $(this).offset();
		
		$('<div id="tooltip" />')
			.appendTo('body')
			.text(title)
			.hide()
			.css({top: elOff.top + 10,left: elOff.left + 100})
			.fadeIn('normal');
			
	}, function() {
		$(this).attr('title',title);
		$('#tooltip').remove();
	});	
});