window.hubsites = window.hubsites || {};
hubsites.load = hubsites.load || [];
hubsites.load.push(function ($) {
	$('#page-content .module-link2').live('click', function (ev) {
		var moduleUid = parseInt($(this).attr('href').substr(1)),
		i = 0,
		modules = hubsites.pageData.modules, $w = $(window);
		$('#page-content .module-link2').removeClass('selected');
		$(this).addClass('selected');
		for (; i < modules.length; i++) {
			if (modules[i].moduleUid === moduleUid) {
				if ($('.overlay').length === 0) {
					$('body').append($('#overlay-template').html());
				}
				$('.overlay .content .blah').html($('#module-content-template').tmpl(modules[i]));
				adjustModal();
				$('.overlay').fadeIn('fast');
				adjustModal();
				break;
			}
		}
	});

	var adjustModal = function (hide) {
		var $w = $(window),
		appleDevices, i, userAgent, cs, ch;
		$('.overlay').css({
			width: $w.width(),
			height: $w.height(),
			left: 0,
			top: 0,
			position: 'fixed'
		}).find('.transparent').css({
			opacity: 0.75,
			width: $w.width(),
			height: $w.height(),
			left: 0,
			top: 0
		});
		cw = $('.overlay .content').width();
		ch = $('.overlay .content').height();
		$('.overlay .content').css({
			left: (($w.width() / 2) - (cw / 2)),
			top: (($w.height() / 2) - (ch / 2))
		});
		appleDevices = ['iphone', 'ipad', 'ipod'];
		userAgent = window.navigator.userAgent.toLowerCase();
		for (i = 0; i < appleDevices.length; i++) {
			if (userAgent.indexOf(appleDevices[i]) > -1) {
				$('.overlay .content').css({ top: 30 });
				break;
			}
		}
	};
	$(window).bind('resize scroll', function () {
		if ($('.overlay:visible').length > 0) { adjustModal(); }
	});
	$('.overlay .close, .overlay .transparent').live('click', function (ev) {
		ev.preventDefault();
		$('#page-content .module-content').html('');
		$('.overlay').fadeOut('fast', function () {
			$('.overlay').find('.blah').html('');
		});
	});
	$('.overlay .content').live('click', function (ev) {
		ev.preventDefault();
	});
	/*$('.overlay').live('click', function () {
		$('#page-content .module-content').html('');
		$('.overlay').fadeOut('fast', function () {
			$('.overlay').find('.blah').html('');
		});
	}); */
	window.setTimeout(function () {
		if (window.location.hash !== '') {
			$('#page-content .module-link[href="' + window.location.hash + '"]').trigger('click');
		}
	}, 1000);
});
