/**

 * @copyright 2007 NFL

 * @package nfl.auth

 * @version 1.0

 * @requires prototype

 * @requires nfl

 */

 

if(typeof nfl.util.authentication=="undefined"){

	function userLogIn() 

	{

		new Ajax.Updater('headlines_myteam_tabs_1', '/ajax/home', { 

				method: 'post',

				evalScripts: true,

				parameters: { userId: $('userName').value, password: $('password').value,

				loginCommand: 'true'

			}, 

			onCreate: function(transport) { 

				$('loginSubmit').disabled="disabled"; 

			}, 

			onLoading: function(transport) { 

		 		$('loginSubmit').disabled="disabled";

			}, 

		 	onSuccess: function(transport) {

	 			// analytics: login event

	 			setAjaxLoginEvent('event11');

		 	},

		 	onComplete: function(transport) {

			 	if($('myTeamNotLogged') !== null) {

			 		loginCount++;

				 	if(loginCount >= 3) {

				 		window.location = '/registration/login';

				 	}

			 	}

			 	else {

			 		loginCount = 0;

			 	}

			}

			

		});

	}



	nfl.util.authentication = Class.create();

	nfl.util.authentication.prototype = {

		initialize: function(){

			Object.Event.extend(this);

			//try{ this.checkcookies(); }catch(e){}

			nfl.events.observe("contentloaded",function(){

				try{ this.setHeaderOptions(); }catch(e){ throw "contentnotready"; }

				/* try{ this.setFooterContent(); }catch(e){ throw "contentnotready"; } */

				if(nfl.util.Server.Path == "/" || nfl.util.Server.Path == "/home"){try{ Event.observe($('myteam_tab'),'click', this.setLoginContent.bindAsEventListener($('myteam_tab')));}catch(e){ throw "contentnotready"; }}

			}.bind(this))

			this.observe('authenticated',this.onauthenticated.bind(this));

			this.notify('initialized',{name:'nfl.util.authentication.initialize',obj:this.base});

		},

		checkcookies: function(){

			nfl.log("nfl.util.authentication.checkcookies called..");

			var userId = nfl.util.isNull(nfl.util.cookies.getCookie('userId'));

			if(userId != '') { nfl.log("nfl.util.authentication: user has cookie") };

			/*

			var userId = nfl.util.isNull(nfl.util.cookies.getCookie('userId'));

			new Ajax.Updater('headlines_myteam_tabs_1', '/ajax/home', { 

					method: 'post',

					evalScripts: true,

					parameters: { userId: userId, loginCommand: 'true'

				}  

			});

			*/

		},

		login: function(submitEle){

			var params	= $(submitEle).up('form.login-form').serialize(true);

			var wrapper	= $(submitEle).up('div.login-widget').up(0);

			

			/* construct ajax request */	

			this._loginrequest	= new Ajax.Updater(wrapper,'/ajax/home', { 

				method: 'post',

				evalScripts: true,

				onsuccess: function(transport){

					

				},

				parameters: params

			});

		},

		onauthenticated: function(args){

			nfl.log('nfl.auth.user.id = \''+ this.user.id +'\'');

			nfl.log('nfl.auth.user.favoriteteam = \''+ this.user.favoriteteam +'\'');

			if(this.user.id !== '') {

				nfl.util.cookies.setCookie('userId', this.user.id, getExpDate(365 * 20, 0, 0), '/');

				this.setHeaderOptions();

			}

			if(this.user.favoriteteam !== '') {

				nfl.util.cookies.setCookie('favoriteTeam', this.user.favoriteteam, getExpDate(365 * 20, 0, 0), '/');

				var nofavteammessages	= document.getElementsByClassName(".no-team-message");

				nofavteammessages.each(function(element){ element.setStyle({'display':'block'});});

			}

		},

		setLoginContent: function(){

			$('headlines_myteam_tabs_1').update('');

			var userId = nfl.util.isNull(nfl.util.cookies.getCookie('userId'));

			var params = userId!==''? { userId: nfl.util.cookies.getCookie('userId'),loginCommand: 'false'} : {userId:'',loginCommand: 'false'};

			

			new Ajax.Updater('headlines_myteam_tabs_1', '/ajax/home', { 

				method: 'post',

				evalScripts: true,

				parameters: params

			});

		},

		setFooterContent: function(){

			$('ftrLogin').update('');

			var userId = nfl.util.isNull(nfl.util.cookies.getCookie('userId'));

			var params = userId!==''? { userId: nfl.util.cookies.getCookie('userId'),loginCommand: 'false',zoneOrigin:'footer'} : {userId:'',loginCommand: 'false',zoneOrigin:'footer'};



			new Ajax.Updater('ftrLogin', '/ajax/home', { 

				method: 'post',

				evalScripts: true,

				parameters: params

			});

		},

		clearcookies: function(){

			nfl.util.cookies.deleteCookie('userId','/');

			nfl.util.cookies.deleteCookie('favoriteTeam','/');

		},

		isAuthenticated: function(){

		   var userId = nfl.util.isNull(nfl.util.cookies.getCookie('userId'));

		   if(userId != '') { nfl.log("nfl.util.authentication.isAuthenticated = true"); return true; }else{ nfl.log("nfl.util.authentication.isAuthenticated = false"); return false; }

		},

		setHeaderOptions: function(){

			if($('loginButtons')){

				var hasLoginButtons		= false;

				var hasLogoutButtons	= false;

				if($('loginButtons').hasClassName('authenticated')){ hasLogoutButtons = true; }else{ hasLoginButtons = true;}

				

				if(this.isAuthenticated()){

   					$('loginButtons').innerHTML	= '<li class="first"><a href="/">NFL.com Home</a></li><li><a href="javascript: document.forms[\'logoutForm\'].submit();">Sign Out</a></li>' +

   												  '<li><a href="javascript: document.forms[\'loginForm\'].submit();">Profile</a></li>';  

				}else{

					$('loginButtons').innerHTML	= '<li class="first"><a href="/">NFL.com Home</a></li><li><a href="javascript: document.forms[\'loginForm\'].submit();">Sign In</a></li>' +

												  '<li><a href="javascript: document.forms[\'registrationForm\'].submit();">Register</a></li>';   

				}

				nfl.log("nfl.util.authentication.setHeaderOptions: authenticated = '"+ (this.isAuthenticated()) +"'");

			}else{

				nfl.log("nfl.util.authentication.setHeaderOptions could not locate loginButtons");

			}

		}

	}

}



function setLoggedInButtons() {

	try{ return nfl.auth.setHeaderOptions();}catch(e){}

}



nfl.auth = new nfl.util.authentication();

if(nfl.global.eventModel != "prototype"){

	nfl.events.notify('libAuthenticationLoaded');

}else{

	document.fire('libAuthenticationLoaded');

}