(function() {
	
	var Auth = window.Auth = {
		
		createOverlay: function() {
			
			// Create overlay
			var overlay = document.createElement("div");
			overlay.id = "login-overlay";

			// Append to body
			document.body.appendChild(overlay);
			
		},
		
		removeOverlay: function() {
			
			try {
				document.body.removeChild(document.getElementById("login-overlay"));
			} catch (e) {}
			
		},
		
		createPopup: function(url, w, h) {
			
			// Create overlay
			Auth.createOverlay();
			
			// Get Window dims
			var wx = (document.body.clientWidth / 2) - (w / 2);
			var wy = 160;//(document.body.clientHeight / 2) - ((height / 2));

			if ('screenLeft' in window) {
			
				// IE-compatible variants
				wx += window.screenLeft;
				wy += window.screenTop;
			
			} else if ('screenX' in window) {
			
				// Firefox-compatible
				wx += window.screenX;
				wy += window.screenY;
			
			}
			
			// Create Popup
			var name = 'oauthwin';
			var fun = 'location=1, resizable=0, scrollbars=1, status=1, width=' + w + ',height=' + h + ',left=' + wx + ',top=' + wy;
			
			try {
				var w = window.open(url, name, fun);
			} catch (e) {
				alert("An error occured, please check your popup settings.");
				Auth.removeOverlay();
				return;
			}
			
			// Monitor Window CLose
			var tm = setInterval(function() {
				if (w.closed) {
					Auth.removeOverlay();
					clearInterval(tm);
				}
			}, 100);

			
		}
		
	};
	
	// Google Auth Handler
	Auth.google = function() {
		
		return {
			
			host: '',
			
			open: function() {
				
				Auth.createPopup('/auth/google', 600, 400);
				
			},
			
			tokenLogin: function(host) {
				
				// Goto user site
				window.location = host;

			}
			
		}
		
	}();
	
	// Twitter Auth Handler
	Auth.twitter = {
		
		open: function(signup) {

			var url= '/auth/twitter';
			
			if (typeof (signup) !== 'undefined' && signup) {
				url+= '?signup=1';
			}
			
			Auth.createPopup(url, 995, 370);
			
		}
		
	}
	
	Auth.facebook = {
		
		open: function(signup) {
			
			var url= "/auth/facebook";
			
			if (typeof (signup) !== 'undefined' && signup) {
				url+= '?signup=1';
			}
			
			Auth.createPopup(url, 650, 300);
			
		}
		
	}
	
	Auth.linkedin = {
		
		open: function(signup) {
			
			var url= "/auth/linkedin";
			
			if (typeof (signup) !== 'undefined' && signup) {
				url+= '?signup=1';
			}
			
			Auth.createPopup(url, 650, 400);
			
		}
		
	}
	
})();