$(function(){
	// initialize thickbox/pager stuff
	$('#surveyContainer ul.pages li').hide();
	$('#surveyContainer ul.pages li:first').show();
	$('#surveyContainer ul.nav a').click(function(){
		$(this).parents('ul.nav').find('li a').show();
		if ($(this).hasClass('prev')) {
			var current = $('#TB_ajaxContent ul.pages li:visible');
			var next = current.prev('li');
			if (next.size() == 1) {
				current.hide();
				next.show();
			}
		} else if ($(this).hasClass('next')) {
			var current = $('#TB_ajaxContent ul.pages li:visible');
			var next = current.next('li');
			if (next.size() == 1) {
				current.hide();
				next.show();
			}
		}
		// show/hide nav links as needed
		navLinkCheck.call(this, next);
		return false;
	});
	navLinkCheck.call($('#surveyContainer ul.nav a:first'), $('#TB_ajaxContent ul.pages li:visible'));
	$('#TB_ajaxContent a.close').live('click', function(){
		if ($(this).hasClass('submit-survey')) {
			// fix "other" inputs
			$('input.user-survey-other').each(function(){
				$(this).parents('li').find('input[value*=Other]').val($(this).parents('li').find('input[value*=Other]').val()+'~|~'+$(this).val());
			});
			var form = $('#TB_ajaxContent form');
			$.post(form.attr('action'), form.serialize(), function(){
			}, 'json');
		}
		tb_remove();
		return false;
	});
	
	// open text fields for "other" options
	$('input[type=radio]').each(function(){
		if (this.value.indexOf('Other') != -1) {
			$(this).next('label').after('<div class="input text"><input type="text" class="user-survey-other" /></div>');
		}
	});
});

function navLinkCheck(current) {
	if (current.prev('li').size() == 0) {
		$(this).hide();
	} else if (current.next('li').size() == 0) {
		$(this).hide();
	}
}
$(window).load(function(){
	if ($('#surveyContainer').size() == 1) {
            // 2 minutes in future
            setTimeout('openSurvey()', 120000);		
	}
});
function openSurvey(){
    // apparently this is the only way to programmatically open a thickbox
    $('a.surveyLink').trigger('click');
}
