/*--------------------------------------------------------------------------*
 * 
 * Copyright (C) 2010 Brand Labs LLC
 * 
 *--------------------------------------------------------------------------*/

var WelcomeMessage =  Class.create({
	Settings: {
		getNameUrl: '/v/custom-login/customer-name.asp',
		welcomeTemplate: new Template('welcome#{comma} <strong>#{custName}</strong>'),
		notYouText: '<strong>not you?</strong><br>To log-in with your username and password <a href="/login.asp?logout=yes">click here.</a>',
		topText: 'paws4claws&trade; house membership:',
		topId: 'header_top_right_top',
		bottomId: 'header_top_right_btm',
		containerId: 'header_top_right'
	},
	
	initialize: function(){
		this.topElement = null;
		this.bottomElement = null;
		this.containerElement = null;
		Event.observe(window, 'load', this.load.bindAsEventListener(this));
	},
	
	load: function(){
		try{
			this.topElement = $(this.Settings.topId);
			this.bottomElement = $(this.Settings.bottomId);
			this.containerElement = $(this.Settings.containerId);
			
			new Ajax.Request(this.Settings.getNameUrl, {
				method: 'post',
				encoding: 'windows-1252',
				evalJS: false,
				evalJSON: false,	
				onSuccess: function(transport){
					try{
						var customerName = transport.responseText;
						if(customerName != ''){//Customer is logged in
							var welcomeEle = new Element('div', {id: 'welcome_line'});
							var notYouEle = new Element('div', {id: 'notyou_line'});
							var comma = '';
							if(customerName != ' '){//A name was returned
								comma = ',';								
							}
							var welcomeText = this.Settings.welcomeTemplate.evaluate({custName: customerName, comma: comma});
							welcomeEle.update(welcomeText);
							
							notYouEle.update(this.Settings.notYouText);
							this.topElement.update(this.Settings.topText);
							this.bottomElement.update(welcomeEle);
							this.bottomElement.insert(notYouEle);
						}
						this.showContainer.bind(this)();
					}
					catch(e){/*ignore*/}
					
				}.bind(this),
				onFailure: function(){
					this.showContainer.bind(this)();
				}.bind(this)
			
			});
		}
		catch(e){/*ignore*/}
		
		
	},
	
	showContainer: function(){
		if(this.containerElement == null || this.containerElement == undefined){
			return;
		}
		this.containerElement.setStyle({display: 'block'});
	}
});

try{
	new WelcomeMessage();
}
catch(e){/*ignore*/}


