
if (window.disqus === undefined) {
	var disqus = {};
}

(function () {
	disqus.showSelectForumBox = function(el) {
		var i = el.href.indexOf('/comments/');
		var targetNav = el.href.substring(i).replace('/comments/', '').slice(0,-1);

		if(targetNav && $(el).attr('rel') == 'selectforum') {
			// Set title
			$('.select-forum h3').html('Select a Site for ' + utils.capitalize(targetNav));
			// Set navigation message
			$('.select-forum p span').html('Select one to view its ' + targetNav + '.');
		}

		// Show lightbox
		$.facebox($('#select-forum').html());

		// Set selection action
		$('.select-action a').click(function () {
			disqus.selectForum(this.rel, el.href);
			return false;
		});
	};

	disqus.selectForum = function(forum, target) {
		if(target.slice(-1) == '#') { // if the href was "#"
			target = window.location.toString().replace('/' + context.forumUrl + '/', '/' + forum + '/');
			window.location = target;
		} else {
			window.location = target + forum + '/';
		}
	};

	disqus.makeFBApp = function() {
		var form_name = 'settings-form';
		if (window.location.href.indexOf('comments/setup') !== -1) {
			form_name = 'setup-form';
		}

    FB.init(context.facebook_api_key, '/facebook-connect/xd_receiver.htm');
		FB.ensureInit(function() {
			FB.Connect.requireSession(function() {
				FB.Connect.createApplication('DISQUS Comment System', function(obj) {
					if (obj && obj.new_api_key) { // Guard because we get 'undefined' obj sometimes
						$('#facebook_api_key').attr('value', obj.new_api_key);
						var input = document.createElement('input');
						input.type = 'hidden';
						input.name = 'made_fb_app';
						input.value = '1';
						document[form_name].appendChild(input);
					}
				});
			});
		});
	};
})();

$(document).ready(function () {

	// Close the /comments/login lightbox
	$('#lightbox-login a[rel=signup]').click(function () {
		$('#lightbox-login, #facebox_overlay').remove();
		return false;
	});


	// Site-select
	if(!window.XMLHttpRequest) {
		// IE6
		$('#site-select').click(function () {
			if(!$(this).hasClass('hover')) {
				$(this).addClass('hover');
			} else {
				$(this).removeClass('hover');
			}

			return false;
		});
	} else {
		$('#site-select a.site-select-link').click (function () {
			return false;
		});
	}

	// Set the select-forum for navigation with multiple forums
	$('#navigation li a[rel=selectforum]').click(function () {
		disqus.showSelectForumBox(this);
		return false;
	});

	// Set the select-forum for switch-link links
	$('a[rel=switchsite]').click(function() {
		disqus.showSelectForumBox(this);
		return false;
	});


});
