CH.ns(function() {with(CH) {
	
	Login = {
		lightbox: null, form: null, button: null, referer: null, hash: null, 
		
		init: function(skipInit) {	
			if(skipInit != true) {
				this.lightbox = new LightBox('login_form', {overlay_class: 'overlay_modal', autoPosition: true});
				
				this.lightbox._smartWait(function() {
					
					this.lightbox.options.offsetTop = function() { return (this.lightbox._getPageSize().windowHeight/3) - (this.lightbox.options.height/2)}.bind(this);
					this.lightbox.options.offsetLeft = function() { return (this.lightbox._getPageSize().windowWidth/2) - (this.lightbox.options.width/2)}.bind(this);
					
					this.form = $('frm_login');
					this.button = $('btn_login_submit');
					this.loader = $('login_hourglass');
					this.errorContainer = $('login_errors');
					this.fieldErrorClass = 'li_error';
					
				}.bind(this));
			} else {
				this.form = $('frm_login');
				this.button = $('btn_login_submit');
				this.loader = $('login_hourglass');
				this.errorContainer = $('login_errors');
				this.fieldErrorClass = 'li_error';
			}
		},
		
		requestHash: function() {
			new Ajax.Request('/ajaxlogin.php?' + Math.round(9999999*Math.random()), {
				onComplete: function(transport) {
					hash = transport.responseText;
					
	  				/*if(hash.length == 32) {
	      				this.hash = hash;
	  				} else {
	  					this.setError('There was an error requesting a secure login. Please refresh the page and try again.');
						this.showErrors();
	  				}*/
				}.bind(this),
				onFailure: function() {
					//this.setError('An unkown error occurred while trying to obtain a secure login session. Please refresh the page and try again.');
					//this.showErrors();
				}.bind(this)
			});
		},
		
		show: function(type, referer) {
			this.lightbox._smartWait(function() {
				
				if(referer!=""){
					this.setReferer(referer);
				}else{
					this.setReferer(Vars.this_url_64);
				}
				
				// Check if there is a URL hash.
				if(window.location.hash) {
					this.urlHash = window.location.hash.substr(1);
				}
				
				if (typeof(SymOnLoad) != 'undefined') {
				    document.location = '/signin/feature:symantec/from:'+this.referer;
				}
				
				var pageSize = this.lightbox._getPageSize();
				var windowScroll = this.lightbox._getWindowScroll();
	
				// override the default onclick hide settings
				var hide = this.hide.bindAsEventListener(this);
				this.lightbox.overlayObj.onclick = function () {hide(); return false;};
	
				// calculate the starting hide position
				var left = (windowScroll['left'] + (pageSize['windowWidth']/2 - this.lightbox.options.width/2));
				var moveTop = windowScroll['top'] + (pageSize['windowHeight'] / 3) - (this.lightbox.options.height/2);
				
				// set the starting position hidden so we can slide it in
				this.lightbox.show(windowScroll['top']-this.lightbox.options.width, left);
				
				// switch the message type

				switch(type) {
					case 'like':
						this.setError('You need to be signed in to like stuff.');
						this.showErrors();
					break;
					
					case 'comment':
						this.setError('You need to be signed in to view and make comments.');
						this.showErrors();
					break;
					
					case 'rrated':
						this.setError('You must be over 18 years old and signed in to view rrated stuff.');
						this.showErrors();
					break;
					
					case 'send_message':
						this.setError('You need to be logged in to send messages.');
						this.showErrors();
					break;
				}
				
				// call the server for a hash seed so we dont need to submit the password in plain text
				this.requestHash();
				
				// move the login box into view
				new Effect.Move (this.lightbox.id,{duration: .8, x: left, y: moveTop, mode: 'absolute', 
					afterFinish: function() {
						$('login_email').focus();
					}});
				
				delete left;
				delete moveTop;
				delete pageSize;
				delete windowScroll;
			}.bind(this));
		},
		
		hide: function() {
			this.lightbox.overlayObj.style.display = 'none';
			new Effect.DropOut (this.lightbox.id, {duration: .8, 
				afterFinish: function() { 
					this.hideLoader();
					this.hideErrors();
				    this.enableLoginBtn();
				    
				    this.lightbox.hide();
				    
				}.bind(this)
			});
	
		},
		
		doLogin: function() {
	
		    var pars = 'login_email='+$('login_email').value;
		    pars += '&login_password='+$('login_password').value;
		    pars += '&referer=' + this.referer;
		    pars += '&hash=' + this.hash;
			pars += '&urlHash=' + this.urlHash;
	
		    this.showLoader();
		    this.disableLoginBtn();
		    
		    new Req.ajax({url:"ajaxlogin.php", options:{parameters:pars, onFailure:function () {alert("An error occurred. Please try again.");}}});
		},
		
		setReferer: function(referer) {
			
			this.referer = referer;
		},
		
		enableLoginBtn: function() {
			this.button.disabled = false;
		},
		
		disableLoginBtn: function() {
			this.button.disabled = true;
		},
		
		
		setError: function(error) {
			this.errorContainer.innerHTML = error;
		},
		
		showErrors: function() {
			this.errorContainer.show();
		},
		
		hideErrors: function() {
			this.errorContainer.hide();
			
			$$(this.fieldErrorClass).each(
				function(elm) {
					elm.className = 'text';
				}.bind(this)
			);
		},
		
		showLoader: function() {
			this.loader.show();
		},
		
		hideLoader: function() {
			this.loader.hide();
		}
	
	};
}});
