var Popup = function(){
	var self = this;
	self.attr = {};

	self.login=function(){
		$.ajax({
			url: self.attr.form_url,
			cache: false,
			success: function(data){
				self.fader.css({
					width: $(document).width(),
					height: $(document).height()
				});
				
				var containerLeft = ($(document).width() / 2) 
				- (self.container.width() / 2);
				var containerTop = ($(window).height() / 2) 
				- (self.container.height() / 2) 
				+ getScrollY();
				
				function getScrollY(){
					var scrollY = 0;
					if (typeof window.pageYOffset == "number") {
					scrollY = window.pageYOffset;
					} else if (document.documentElement && document.documentElement.scrollTop) {
					scrollY = document.documentElement.scrollTop;
					} else if (document.body && document.body.scrollTop) {
					scrollY = document.body.scrollTop; 
					} else if (window.scrollY) {
					scrollY = window.scrollY;
					}
					return scrollY;
				}
				
				self.container.css({
					left: containerLeft,
					top: containerTop
				});
				
				self.hideBanners();
				self.fader.show();
				self.container.show().html(data);
				$(self.container).find("input:first").focus();
				
				self.close_link = $('#' + self.attr.close_link_id);
				$(self.close_link).click(function(){
					self.showBanners();
					self.fader.hide();
					self.container.hide();
					return false;
				});
			},
			error: function(){
				location = self.link.attr("href");
			}
		});
		return false;
	};

	self.init = function(){
		self.hideBanners = function(){
			$('object, iframe').css('visibility', 'hidden');
		}
		self.showBanners = function(){
			$('object, iframe').css('visibility', 'visible');
		}
	
		self.fader = $('#' + self.attr.fader_id);
		if(!self.fader.length){
			$("body").append("<div id=" + self.attr.fader_id + "></div>");
			self.fader = $('#' + self.attr.fader_id);
		}
		
		self.fader.css({
			position: "absolute",
			left: 0,
			top: 0,
			width: $(document).width(),
			height: $(document).height(),
			background: "#000",
			opacity: 0.5,
			zIndex: 9999
		});
		
		self.fader.hide();
		$(self.fader).click(function(){
			self.showBanners();
			self.fader.hide();
			self.container.hide();
		});
		$(document).keypress(function(e){
			var code = (e.keyCode ? e.keyCode : e.which);
			if (code == 27 ){
				self.showBanners();
				self.fader.hide();
				self.container.hide();
			}
		});
		$(window).resize(function(){
			self.fader.hide();
			self.container.hide();
		});

		self.container = $('#' + self.attr.container_id);
		if(!self.container.length){
			$("body").append("<div id=" + self.attr.container_id + "></div>");
			self.container = $('#' + self.attr.container_id);
		}
		
		self.container.css(self.attr.container_css);
		self.container.css({
			position: 'absolute',
			left: 0,
			top: 0,
			zIndex: 9999
		})
		self.container.hide();

		self.link = $('a.' + self.attr.link_class);
		self.link.click(function(){
			return self.login();
		});

		self.form = $('form.'+ self.attr.form_class);
		self.form.submit(function(){
			return self.login();
		});
	}
}

$(document).ready(function(){
	login = new Popup;
	login.attr = {
		fader_id: 'fader',
		container_id: 'popup',
		container_css: {
			width: '400px',
			height: '292px',
			fontSize: '1.2em',
			lineHeight: '1.3em',
			background: '#fff'
		},
		link_class: 'login-link',
		form_class: 'login-form',
		form_url: '/user/login/?ajax',
		close_link_id: 'close-popup'
	}
	login.init();
});

