$(document).ready(function()
{
	$('.tabset').each(function(count)
	{
		$('dt', this).each(function(dt_count)
		{
			var newDt = $(this).clone().wrapInner('<a href="#"></a>')
			newDt.addClass('screen-override');
			newDt.removeClass('selected');
			newDt.attr('dtCount', dt_count);
			newDt.click(function()
			{
				if ($(this).parents('.tabset').hasClass('direct')) {
					return true;
				}
				
				$('dt.selected', this.parentNode).removeClass('selected');
				$(this).addClass('selected');
				
				var count = $(this).attr('dtCount');
				
				$('dd', this.parentNode).css({display:'none'});
				$('dd:eq('+count+')', this.parentNode).css({display:'block'});
				
				return false;
			});
			
			$(this).addClass('print-override');
			$('dt.print-override:eq(0)', this.parentNode).before(newDt);
		});
		
		$('dt:first-child', this).addClass('selected');
		$('dd:gt(0)', this).css({display:'none'});
	});
	
	if (window.location.toString().indexOf('searchtype=advanced') >= 0 || window.location.toString().match(/search\/*$/)) {
		$('dt.screen-override:eq(1)').click();
	}
});

$(document).ready(function()
{
	$(".reason-popup").hide();
	
	$('.alt-link').click(function()
	{
		if ($(".reason-popup").is(":hidden"))
		{
			$(".reason-popup").slideDown('normal');
		}
		else
		{
			$(".reason-popup").hide();
		}
		
		return false;
	});
});


// $(document).ready(function() {
// 	$('input[name="q"]').parents('form:eq(0)').submit(function() {
// 		if ($('input[name="q"]').val().toLowerCase() == 'search grist') {
// 			alert('Please enter a search term.');
// 			return false;
// 		}
// 	});
// });


$(document).ready(function()
{
	$('.filled').focus(function()
	{
		if((!this.initialValue || this.value == this.initialValue) && this.value == 'Search Grist')
		{
			this.initialValue = this.value;
			this.value = '';
		}
		else if(this.initialValue && this.value == '')
		{
			this.value = this.initialValue;
		}
	});
	
	$('.filled').blur(function()
	{
		$(this).trigger('focus');
	});
});

$(document).ready(function()
{
	// $('#add-tag').focus(function(){
	// 	$(this).parents('form').submit(function() { return false; });
	// });
	
	// $('#add-tag').blur(function(){
	// 	$(this).parents('form').submit(function() { return true; });
	// });
	
	// $('.delete-list input[type="checkbox"]').replaceWith("<a href='#' onClick='return removeTag(this);'>delete</a>");
	
	// $("#add-tag").keypress(function(e)
	// {
	// 	if(e.which == 13)
	// 	{
	// 		addTag(this.value);
	// 	}
	// });
});

function addTag(tag)
{
	$('.delete-list').append("<dd><span>"+tag+"</span><a href='#' onClick='return removeTag(this);'>delete</a></dd>");
	$('#add-tag').attr('value', '');
}

function removeTag(self)
{
	if(!self) self = this;
	
	$(self).parent('dd').remove();
	return false;
}

$(document).ready(function()
{
	$('ul.actions:not(.alt) .first').removeClass('first');
	$('ul.actions:not(.alt)').prepend('<li class="first print"><a href="#">Print</a></li>');
	$('.print a').click(function() {
		window.print();
		return false;
	});
});

$(document).ready(function()
{
	$('h3.expand-head').after('<a class="collapse" href="#">Hide</a>');
	$('a.collapse').click(collapse_expand);

	function collapse_expand()
	{
		if($(this).nextAll('*').eq(0).css('display') == 'none')
		{
			$(this).nextAll('*').show();
			$(this).removeClass('expand');
			$(this).addClass('collapse');
			$(this).html('Hide');
		}
		else
		{
			$(this).nextAll('*').hide();
			$(this).removeClass('collapse');
			$(this).addClass('expand');
			$(this).html('Show');
		}
		return false;
	}
});

$(document).ready(function()
{
    $('h3.expand-head-member').after('<a class="expand" href="#">Show</a>');
    $('a.expand').click(collapse_expand);
    $('div.alert').hide();

	function collapse_expand()
	{
		if($(this).nextAll('*').eq(0).css('display') == 'none')
		{
			$(this).nextAll('*').show();
			$(this).removeClass('expand');
			$(this).addClass('collapse');
			$(this).html('Hide');
		}
		else
		{
			$(this).nextAll('*').hide();
			$(this).removeClass('collapse');
			$(this).addClass('expand');
			$(this).html('Show');
		}
		return false;
	}

});

$(document).ready(function()
{
	var author_type = '';
	var alpha = '.initials0';
	
	$('#browse-voices h3').after('<ul class="view-options">'+
		'<li class="all-authors first selected"><a href="#">All</a></li>'+
		'<li class="gristers"><a href="#">Grist Staff Only</a></li>'+
		'<li class="pages"><!--<span>View alphabetically: </span>-->'+
			'<ol class="pages">'+
				'<li class="selected a-h"><a href="#">A-H</a></li>'+
				'<li class="i-q"><a href="#">I-Q</a></li>'+
				'<li class="r-z"><a href="#">R-Z</a></li>'+
			'</ol>'+
		'</li>'+
	'</ul>');
	
	$('#browse-voices li.initials0'+author_type).show();
	$('#browse-voices li.initials1'+author_type).hide();
	$('#browse-voices li.initials2'+author_type).hide();
	
	for(var i=0; i<3; i++)
	{
		var group_size = Math.floor($('#browse-voices li.initials'+i).size()/3);
		
		$('#browse-voices .list:eq(0)').append($('#browse-voices li.initials'+i));
		$('#browse-voices .list:eq(1)').append($('#browse-voices li.initials'+i+':gt('+(group_size-1)+')'));
		$('#browse-voices .list:eq(2)').append($('#browse-voices li.initials'+i+':gt('+((group_size*2)-1)+')'));
	}
	
	$('.view-options .a-h a').click(function()
	{
		alpha = '.initials0';
		
		$(this).parents('ol').find('.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		$('#browse-voices li.initials0'+author_type).show();
		$('#browse-voices li.initials1'+author_type).hide();
		$('#browse-voices li.initials2'+author_type).hide();
		return false;
	});
	$('.view-options .i-q a').click(function()
	{
		alpha = '.initials1';
		
		$(this).parents('ol').find('.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		$('#browse-voices li.initials0'+author_type).hide();
		$('#browse-voices li.initials1'+author_type).show();
		$('#browse-voices li.initials2'+author_type).hide();
		return false;
	});
	$('.view-options .r-z a').click(function()
	{
		alpha = '.initials2';
		
		$(this).parents('ol').find('.selected').removeClass('selected');
		$(this).parent().addClass('selected');
		$('#browse-voices li.initials0'+author_type).hide();
		$('#browse-voices li.initials1'+author_type).hide();
		$('#browse-voices li.initials2'+author_type).show();
		return false;
	});
	$('.view-options .all-authors a').click(function()
	{
		author_type = '';
		
		$(this).parents('ul').find('> .selected').removeClass('selected');
		$(this).parent().addClass('selected');
		
		$('#browse-voices .list li').hide();
		$('#browse-voices li'+alpha+author_type).show();
		
		return false;
	});
	$('.view-options .gristers a').click(function()
	{
		author_type = '.group1';
		
		$(this).parents('ul').find('> .selected').removeClass('selected');
		$(this).parent().addClass('selected');
		
		$('#browse-voices .list li').hide();
		$('#browse-voices li'+alpha+author_type).show();
		
		return false;
	});
});

$(document).ready(function()
{
	$('.d1 > div > .permalink, .d2 > div > .permalink').append('<br /><a href="#add-a-comment" class="comment-reply">Reply</a>');
	$('.comment-reply').click(function()
	{
		var comment_id = $(this).parents('li[id^="c"]').attr('id').replace(/^c/, '');
		
		if($('#comment_form input[name="comment_parrent"]').size())
		{
			$('#comment_form input[name="comment_parrent"]').replaceWith('<input type="hidden" name="comment_parrent" value="'+comment_id+'" />');
		}
		else
		{
			$('#comment_form').append('<input type="hidden" name="comment_parrent" value="'+comment_id+'" />');
		}
		
		var author_name = $(this).parents('li').eq(0).find('.comment-author:eq(0) a').text();
		if (!author_name) {
			author_name = $(this).parents('li').eq(0).find('.comment-author:eq(0) span').text();
		}
		
		if (!$('#im-replying-to').size()) {
			$('#comment_form').before('<p id="im-replying-to">You are replying to '+author_name+'&rsquo;s comment.</p>');
		} else {
			$('#im-replying-to').text('You are replying to '+author_name+'&rsquo;s comment.');
		}
	});
	
	// $('li:has(> .comment-nested li:visible) > .comment-data > .permalink').after('<a href="#" class="comment-toggle">Show Replies/Hide Replies</a>');
	$('#comments ol > li').each(function() {
		if ($(this).find('> .comment-nested > li:visible').size()) {
			// $(this).find('> .comment-nested > li:visible').each(function() {
			// 	alert($(this).html());
			// 	alert($(this).width());
			// });
			
			$(this).find(' > .comment-data > .permalink').after('<a href="#" class="comment-toggle">Show Replies/Hide Replies</a>');
		}
	});
	
	$('.comment-toggle').click(function()
	{
		$(this).parents('li').eq(0).find('> .comment-nested').toggle();
		return false;
	});
	
	$('.comment-nested').each(function()
	{
		if($(this).find('> li').size() > 10)
		{
			$(this).hide();
		}
	});
});

$(document).ready(function()
{
	$('.spot-nav').prepend('<li class="prev"><a href="#" class="arrow">Previous</a></li><li class="next"><a href="#" class="arrow">Next</a></li>');
	$('.spot-nav li').prepend('<span class="bg"></span>');
	$('.spot-nav .arrow').click(function()
	{
		var action = $(this).parent().hasClass('next')?'next':'prev';
		
		if(action == 'next' && $('.spot-nav .selected').next().size())
		{
			$('.spot-nav .selected').next().find('a').click();
		} else if(action == 'next') {
			$('.spot-nav .first').find('a').click();
		}
		
		if(action == 'prev' && !$('.spot-nav .selected').hasClass('first'))
		{
			$('.spot-nav .selected').prev().find('a').click();
		} else if(action == 'prev') {
			$('.spot-nav *:last-child').find('a').click();
		}
		
		return false;
	})
	
	$('.widget-link a').click(function()
	{
		var kingdom = $(this).attr('class')?$(this).attr('class').toString().substring(7):'null';
		var is_short = (window.location.pathname.replace('/', ''))?'/short':'';
		$('.spot-articles').load('/site/header-articles/category/'+kingdom+is_short);
		
		$('.spot-nav .selected').removeClass('selected');
		$(this).parent().addClass('selected');
		
		return false;
	});
});
/** Disabled -- now using direct navigation to tag cloud
$(document).ready(function() {
	$('#kingdoms ul').append('<li class="more"><a href="#">More</a></li>');
	$('#kingdoms .more a').click(function() {
		if (!$('.popup-top').size()) {
			$.get('/site/kingdom-list/all', {}, function(data) {
				$(document.body).append('<div id="kingdom-list-more" class="popup-top"><div class="popup-middle"><div class="popup-bottom"><ul class="more-kingdoms"></ul></div></div></div>');
				$('.more-kingdoms').append(data);
				
				$('#kingdom-list-more').css('top', $('#kingdoms .more').offset().top);
				$('#kingdom-list-more').css('left', $('#kingdoms .more').offset().left - 20);
				
				$('.popup-bottom').prepend('<a href="#" class="popup-close" onclick="$(this).parents(\'.popup-top\').hide(); return false;" style="position:absolute; right:90px; top:30px;">CLOSE</a>');
			});
		} else {
			$('.popup-top').remove();
		}
		
		return false;
	});
});
**/

// http://www.komodomedia.com/blog/2006/01/css-star-rating-part-deux/
$(document).ready(function()
{
	str = '<ul class="star-rating">';
	str+= '<li class="current-rating">Currently 0/5 Stars.</li>';
	str+= '<li><a href="#" title="1 star out of 5" class="one-star" value="1">1</a></li>';
	str+= '<li><a href="#" title="2 stars out of 5" class="two-stars" value="2">2</a></li>';
	str+= '<li><a href="#" title="3 stars out of 5" class="three-stars" value="3">3</a></li>';
	str+= '<li><a href="#" title="4 stars out of 5" class="four-stars" value="4">4</a></li>';
	str+= '<li><a href="#" title="5 stars out of 5" class="five-stars" value="5">5</a></li>';
	str+= '</ul>';
	
	$('.rating-form').after(str);
	
	$('.star-rating a').click(function()
	{
		var average = $(this).attr('value');
		$('#rating').val(average);
		
		var post = $('.rating-form').serialize();
		$.post('/', post, function()
		{
			$('.current-rating').css({width:average*30});
			$('.current-rating').html('Currently '+average+'/5 Stars.');
			$('.star-rating').addClass('rating-duplicate');
		});
		return false;
	});
	
	var average = parseFloat($('#rating-average').val());
	average = average?average:0;
	
	$('.current-rating').css({width:average*30});
	$('.current-rating').html('Currently '+average+'/5 Stars.');
	$('.star-rating').addClass($('.rating-duplicate').size()?'rating-duplicate':'');
	
	$('.rating-form').css({position:'absolute', left:'-9999px'});
});

var arrows =
{
	count:0,
	opts:[],
	generate:function(opts)
	{
		this.opts[this.count] = opts;
		document.write('<a href="#" id="arrow-bck-'+this.count+'" class="back">back</a>');
		document.write('<a href="#" id="arrow-fwd-'+this.count+'" class="forward">forward</a>');
		
		this.count++;
	}
}

$(document).ready(function()
{
	$('.arrows a.back, .arrows a.forward').click(function()
	{
		var direction = $(this).attr('id').substring(6,9);
		var id = $(this).attr('id').substring(10);
		
		if(!arrows.opts[id]) return;
		
		if (!arrows.opts[id].page) {
			arrows.opts[id].page = 0;
		}
		
		if (direction == 'bck') {
			arrows.opts[id].page--;
		} else {
			arrows.opts[id].page++;
		}
		
		var offset = arrows.opts[id].page*arrows.opts[id].per_page;
		if (offset < 0) offset = 0;
		$.get(arrows.opts[id].load+offset, function(data) {
				if ( data.length > 0 )
				{
					$(arrows.opts[id].container).eq(0).replaceWith(data);
				}
				else
				{
					// load the last page of data
					arrows.opts[id].page--;
					offset = arrows.opts[id].page*arrows.opts[id].per_page;
					$(arrows.opts[id].container).eq(0).load(arrows.opts[id].load+offset);
				}
			});
		return false;
	});
});

/**
 *
 * share links
 *
 */

$(document).ready(function()
{
	$('.actions .share').each(function()
	{
		var ul = $(this).find('ul').remove();
		$(this).wrapInner('<a href="#"></a>');
		$('#primary').append(ul);
		
		$(this).find('a').eq(0).click(function() {
			if ($('#share-links:visible').size()) {
				$('#share-links:visible').hide();
			} else {
				$('#share-links:hidden').show();
			}
			
			return false;
		});
	});
	
	$('.popup').wrap('<div id="share-links" class="popup-top"></div>');
	$('.popup').wrap('<div class="popup-middle"></div>');
	$('.popup').wrap('<div class="popup-bottom"></div>');
	
	$('.popup-bottom').prepend('<a href="#" class="popup-close" onclick="$(this).parents(\'.popup-top\').hide(); return false;" style="position:absolute; right:90px; top:30px;">CLOSE</a>');
	
	$('#primary .popup-top').css('top', $(this).find('a').eq(0).offset().top+($.browser.msie?150:0));
	$('#primary .popup-top').css('left', $(this).find('a').eq(0).offset().left+($.browser.msie?100:0));
	$('#primary .popup-top').hide();
});

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

$(document).ready(function() {
	$('input[name="tag_browse_f"]').keyup(function() {
		$('#tag_browse_results').show();
		$('#tag_browse_results').load('/member/tag-browse/'+urlencode($(this).val()), function() {
			$(this).show();
		});
	});
});

function add_tag(tag) {
	$('#tag_browse_results').hide();
	
	$('.delete-list').append('<dd><input type="hidden" name="tag_f[]" value="'+tag+'" /><span>'+tag+'</span><a href="#" onclick="return remove_tag(this);">Remove</a></dd>');
}

function remove_tag(anchor) {
	$(anchor).parent().remove();
	return false;
}

$(document).ready(function() {
	$('#add-tag-btn').click(function() {
		add_tag($('input[name="tag_browse_f"]').val());
		return false;
	});
});

$(document).ready(function()
{
	$('.contest form').submit(function() {
		if ($('#photo-caption').val() == 'Type your Gristacular caption here! You must be a registered Grist user to participate.') {
			$('#photo-caption').val('');
		}
	});
});