// Adds in the extra "Signup for outline" messaging at the top based on Outline membership.
// Ads code that checks for login status, if not it disables all of the outline gallery links.

(function($) {
	
	$(document).ready(function() {
		var isLoggedIn = document.cookie.indexOf('UserName') > -1,
			gallery_pattern = /gallery/
			
		// This only activates the scroll down box if you're in English US.
		if (!isOutlineUser && $('#ctl00_globalNav_selectedLanguageText').html() === 'English (US)') {
			$('#Header').prepend('<div id="outline_member_push" style="display:none;"><p><span>Not an Outline member?</span> <a href="/Browse/Outlinesignup.aspx" target="_blank" rel="780_600_modal">Request membership</a> to get full access to this exclusive collection of celebrity portraiture.</p></div>')
			$('#Header').css('margin-top', 0)
			$('#outline_member_push').slideDown('slow')
		}
		
		if (!isLoggedIn) {

			// Disable all of the links if you aren't logged in
			$('.Links a').each(function(index, value) {
				if (String(value).match(/ImageGroup/g, "i")) $(this).parent('li').html('<p class="pls_login">Please <a href="'+location.href.replace(location.pathname, '/sign-in')+'?redirect='+encodeURIComponent(location.href)+'" id="signIn">sign in</a> or <a href="javascript:redirectToRegister();">register</a> to view this gallery.</p>')
				
				// Hackity hack hack (this should be deleted at some point)
				if (String(value).match(/Search/g, "i")) $(this).parent('li').html('<p class="pls_login">Please <a href="javascript:void(0)" id="signIn">sign in</a> or <a href="javascript:redirectToRegister();">register</a> to view this gallery.</p>')
			});
			$('a:has(img)').each(function(index, value) {
				if (String(value).match(/ImageGroup/g, "i")) $(this).attr('href', 'javascript:void(0)')
				if (String(value).match(/Search/g, "i")) $(this).attr('href', 'javascript:void(0)')
			});
			
			$('.Caption a').each(function(index) {
				$(this).parent('div').prepend($(this).html()+" ")
				$(this).remove()
			});
			

		
		};
		
		// Pop the signup modal signup=true is passed as a GET variable
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			vars[key] = value;
		});
		if (vars.signup) {
			OpenNewIModal("/Browse/outlinesignup.aspx", 780, 600, "Popped")
			// $('#outline_member_push a').click();
		};
		
	});
})(jQuery);
