
;(function($,jBox,rootURL,console) {
	// regular expression to detect the login/session cookie
	// declared here as a 'static' variable as it's only eval'd once ;-)
	var re = /SESSION=(.+?)(?:;|$)/;

	var requiresLoginOnlySelector = ".requiresLogin"; 								// class for  which just require login before being followed
	var postLoginSelector = ".postLoginLightBox";									// class for links which require login and content when followed must appear in lightbox
	var selectorRequiresLogin = requiresLoginOnlySelector+","+postLoginSelector; 	// both
	var selector = "a.loginOrRegister";
	var login = {
		isLoggedIn: null,
		login: function () {
			if (!this.isLoggedIn) $(selector).click();
		},
		updateUI: function () {
			if (re.test(document.cookie)) {
				if (!login.isLoggedIn) $(login).trigger("login");
			} else {
				if (login.isLoggedIn) $(login).trigger("logout");
			}
		}
	};
	var settings = {
			type : "iframe",
			id : "login",
			height : 520,
			width : 440,
			buttons : selector,
			root: rootURL
		};

	// this is the thing that requiresLogin
	var target = null;
	var loginHandler = function (event) {
		if (console.debug) console.debug("Click, requires login");
		event.preventDefault();
		target = $(event.target);
		login.login();
	};

	$(login).bind("login",function (event) {
		if (console.debug) console.debug("logged in, continuing...");
		login.isLoggedIn = true;
		$(selector).contents().replaceWith("Update Profile");
		if (console.debug) console.debug("unbinding requiresLogin click");
		$(selectorRequiresLogin).unbind("click",loginHandler);
		if (target) {
			jBox.hideLightBox('login',function() {
				if (console.dir) console.dir(target);
				var anchor = target.parents("a").add(target).filter("a");
				if ( anchor.length > 0 ) {
					anchor.click();
					var anchorlocation = anchor.filter(':not('+postLoginSelector+')');
					if ( anchorlocation.length > 0 && anchorlocation.attr("href")) {
						location = anchorlocation.attr("href");
					}
				}
				// click() apparenty doesn't exist for <a> but does for <input>
				else if (target.attr("type") == "submit") target.parents("form").submit();
				else target.click();
			});
			event.preventDefault();
		}
	}).bind("logout",function (event) {
		if (console.debug) console.debug("waiting for login");
		login.isLoggedIn = false;
		$(selector).contents().replaceWith("Register or Login");
		if (console.debug) console.debug("binding requiresLogin click");
		$(selectorRequiresLogin).click(loginHandler);
	});

	setInterval(login.updateUI,500); // and try again after iframe contents have loaded as cookie may become present

	$(function() {
		var o = $(selector);
		settings.src = o.attr("href");
		if (console.assert) console.assert(o.size(),"No element matched by " + selector);
		if (console.debug) console.debug("Initialising login iframe lightbox");
		jBox.init(settings);

		var postLoginSettings = {
			type : "iframe",
			id : "postLogin",
			src : "about:blank",
			height : 520,
			width : 440,
			// buttons : selector, I'll handle my own
			root: rootURL
		};
		$(postLoginSelector).each(function() {
			$(this).click(function (event) {
				if ( login.isLoggedIn ) {
					postLoginSettings.src = $(this).attr("href");
					jBox.showIFrame('postLogin');
				}
				event.preventDefault();
				return false;
		})});
		if (console.debug) console.debug("Initialising post login lightbox");
		jBox.init(postLoginSettings);

		$(login).trigger(login.isLoggedIn ? "login" : "logout");
	});



})(jQuery,jBox,rootURL,window.console || {});

// video light box stuff
;(function($,jBox,rootURL,console) {
	$(function() {
		var settings = {
			type : "iframe",
			id : "video",
			src : "about:blank",
			height : 503,
			width : 550,
			buttons : "a.liveStream, a.video",
			root: rootURL
		};
		$("a.liveStream, a.video").click(function (event) {
			event.preventDefault();
			settings.src = $(this).attr("href");
		});
		if (console.debug) console.debug("Initialising video iframe lightbox");
		jBox.init(settings);
	});

})(jQuery,jBox,rootURL,window.console || {});

// wallpaper light box stuff
;(function($,jBox,rootURL,console) {
	$(function() {
		var settings = {
			type : "iframe",
			id : "wallpaper",
			src : "about:blank",
			height : 316,
			width : 524,
			buttons : "a.wallpaper",
			root: rootURL
		};
		$("a.wallpaper").click(function (event) {
			event.preventDefault();
			settings.src = $(this).attr("href");
		});
		if (console.debug) console.debug("Initialising wallpaper iframe lightbox");
		jBox.init(settings);
	});

})(jQuery,jBox,rootURL,window.console || {});

// screensaver light box stuff
;(function($,jBox,rootURL,console) {
	$(function() {
		var settings = {
			type : "iframe",
			id : "screensaver",
			src : "about:blank",
			height : 317,
			width : 524,
			buttons : "a.screensaver",
			root: rootURL
		};
		$("a.screensaver").click(function (event) {
			event.preventDefault();
			settings.src = $(this).attr("href");
		});
		if (console.debug) console.debug("Initialising screensaver iframe lightbox");
		jBox.init(settings);
	});

})(jQuery,jBox,rootURL,window.console || {});

// competition light box stuff
;(function($,jBox,rootURL,console) {
	$(function() {
		var settings = {
			type : "iframe",
			id : "competition",
			src : "about:blank",
			height : 450,
			width : 541,
			buttons : "a.competition",
			root: rootURL
		};
		$("a.competition").click(function (event) {
			event.preventDefault();
			settings.src = $(this).attr("href");
		});
		if (console.debug) console.debug("Initialising competition iframe lightbox");
		jBox.init(settings);
	});

})(jQuery,jBox,rootURL,window.console || {});