var AjaxLogin = AjaxLogin || function() {
	var pub = {}, listeners = [], content, win, overlay, throbber;
	
	function bind_links_iframe() {
		pub.iframe.$('.register_link').click(function() { pub.load_form('register'); return false; });
	};
	
	function reposition() {
		overlay.css({	height: document.documentElement.scrollHeight + 'px', width: document.body.offsetWidth + 'px' });
    
		var ch = content && content.length ? content.get(0).offsetHeight : 0;
    
		win.css({ marginLeft: -Math.ceil(win.width() / 2) + 'px', top: pub.top(ch) + 'px', height: ch + 'px' });
	};
	
	pub.top = function(ch) {
	  var t = Math.floor((document.documentElement.scrollTop || document.body.scrollTop) + (document.documentElement.clientHeight / 2) - (ch / 2));
		return t < 0 ? 0 : t;
	}
	
	pub.bind_links = function() {
		$('.register_link').click(function() { pub.load_form('register'); return false; });
	};
		
	pub.iframe_loaded = function() {
		pub.iframe = window.frames['ajax_login'] || win.get(0).contentDocument || win.get(0).contentWindow;
		pub.iframe.$('.close_button').click(pub.hide_window);
		
		content = pub.iframe.$('.content');

		if (pub.origin) {
			pub.iframe.$('form').append('<input type="hidden" name="origin" value="' + pub.origin + '" />');
		}

		if (listeners.length) {
			$(listeners).each(function() {
				this('iframe_loaded');
			});
		}

		// Make sure links load in parent window
		pub.iframe.$('body').click(function(e) {
			var t = $(e.target);

			if (t.is('a') && !t.attr('target')) {
				window.location = t.attr('href');
				return false;
			}
		});
		
		win.css({ left: '50%' });
		reposition();
		bind_links_iframe();
	};
	
	pub.add_listener = function(func) {
		listeners.push(func);
	};
	
	pub.complete = function(type, uid) {
		if (listeners.length) {
			pub.hide_window();

			$(listeners).each(function() {
				this(type, uid);
			});
		}
		else {
			type == 'login' ? window.location.reload() : window.location = '/thank-you';
		}
	};
	
	pub.hide_window = function() {
		win.css({ left: '-100%' });
		$(window).unbind('resize', reposition);
		
		if ($(this).is('.close_button')) {
		  throbber.hide();
      overlay.fadeTo('fast', 0, function() {
        overlay.hide();
      });
		}

		return false;
	};
	
	pub.load_form = function(type) {
		if (!win) {
		  throbber = $('<div class="throbber"><img src="/themes/renewal/img/ajax-loader.gif" /></div>');
      
			win = $('<iframe id="ajax_login" name="ajax_login" frameborder="0" border="0"></iframe>').css({ left: '-100%' });
			$(document.body).append(win).append(throbber);
			
			overlay = $('<div id="overlay"></div>').css({
				width: document.body.offsetWidth + 'px',
				height: document.documentElement.scrollHeight + 'px',
				opacity: 0
			});
      
			$(document.body).append(overlay);
		}
		else {
			overlay.show();
			throbber.show();
		}
		throbber.css('top', pub.top(48) + 'px');
		
		overlay.fadeTo('fast', 0.3);

		$(window).bind('resize', reposition);
		
		var str = '';
		for (var i = 0; i < 10; i++) str += String.fromCharCode(Math.floor(Math.random() * 26) + 97); // Prevent Opera cache
		win.attr('src', '/ajax_login/' + type + '?origin=' + (pub.origin ? pub.origin : '') + '&o=' + str);
	};
	
	return pub;
}();

$(AjaxLogin.bind_links);