/* __________________________ openauth object */ 

openAuth = {
	init : function() {
		this.token = null;
		this.loggedIn = false;
		this.userId = null;
		this.referer = "";
		
		this.getToken();
	},
	createTokenCookie : function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readTokenCookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	eraseTokenCookie : function(name) {
		this.createTokenCookie(name,"",-1);
	},
	getToken : function() {	
		if (this.readTokenCookie('__s03t19m01_')) {
			this.token = this.readTokenCookie('__s03t19m01_');
			if (this.token) {
				this.getInfo();
			}
		}
		else {
			requestUrl = "https://api.screenname.aol.com/auth/getToken?devId="+openAuth.devId+"&f=json&c=openAuth.gotToken";
			tokenJSONScript = new scriptJSON(requestUrl);
			tokenJSONScript.buildScriptTag();
			tokenJSONScript.addScriptTag();	
		}	
	},
	gotToken : function(objJSON) {
		if (objJSON.response.statusCode != 200) {
			this.loggedIn = false;
			tokenJSONScript.removeScriptTag();
			this.makeLoginLink();
		} else {
			this.loggedIn = true;
			this.token = objJSON.response.data.token.a;
			tokenJSONScript.removeScriptTag();			
    		if (this.token) {
				this.getInfo();
			}
		}
	},
	getInfo : function() {
		requestUrl = "https://api.screenname.aol.com/auth/getInfo?a="+openAuth.token+"&devId="+openAuth.devId+"&referer="+openAuth.referer+"&f=json&c=openAuth.gotInfo";
		infoJSONScript = new scriptJSON(requestUrl);
		infoJSONScript.buildScriptTag();
		infoJSONScript.addScriptTag();	
	},
	gotInfo : function(objJSONB){
		if (objJSONB.response.statusCode !== 200) {
			this.eraseTokenCookie('__s03t19m01_');
			this.makeLoginLink();
		} else {
			if (!this.readTokenCookie('__s03t19m01_')) {
				this.createTokenCookie('__s03t19m01_', this.token, 1);	
			}	
			/*sitesocial*/
			siteSocialIntegration.init();
			if(window.AIM) {
				AIM.params.token = this.token;
				AIM.params.authenticationMode = 0;
				AIM.params.callbacks.endSession.push("siteSocialIntegration.logoutUser")
				if(!AIM.params.sessionId) AIM.transactions.startSession();
			}	
			this.userId = objJSONB.response.data.userData.loginId;
			
			this.makeLogOutLink();
		}
	},
	makeLoginLink : function() {
		  var self = this;
	jQuery("#GH_").trigger('setAuthState',{
        	doAuth : true,
        	authenticated: false,
        	unauthState: '<li class="GH_hat_LI"><a class="jSignIn" title="Sign In / Register">Sign In / Register</a></li>'
   	 	});
	
	jQuery('#GH_ .jSignIn').live('click', function(e) { 
       		 e.preventDefault(); 
        	self.openAuthWindow(e); 
   		 });
	},
	openAuthWindow : function (){
			var loginUrl = "http://api.screenname.aol.com/auth/login?devId="+openAuth.devId+"&f=qs&succUrl="+openAuth.loginSuccURL;	
			var x = (screen.width - 528)/2; var y = (screen.height -530)/2;								 
			window.open(loginUrl,'name','resizable=yes,width=528,height=530,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=yes,left='+x+',top='+y);
	},
	makeLogOutLink : function() {
		 
        if (this.userId && (!(document.getElementById("logoutLink")))) {
            
             jQuery("#GH_").trigger('setAuthState',{
            doAuth : true,
            authenticated: true,
            authState: "<li class='GH_hat_LI'><b class='GH_hat_links_A'>Hi, " + this.userId + "</b></li><li class='GH_hat_LI'><a class='jSignOut GH_hat_more' title='Sign Out'>Sign Out</a></li>"
        });
        
        jQuery('#GH_ .jSignOut').live('click', function(e) { 
            e.preventDefault(); 
            
            var logoutUrl = "https://api.screenname.aol.com/auth/logout?a="+openAuth.token+"&devId="+openAuth.devId+"&doSNSLogout=1&f=qs&succUrl="+openAuth.logoutSuccURL;
           
             
            var x = (screen.width - 528)/2; var y = (screen.height -530)/2;                                 
            window.open(logoutUrl,'name','resizable=yes,width=528,height=530,directories=no,titlebar=no,status=no,menubar=no,toolbar=no,location=yes,left='+x+',top='+y);    
        });
		}
	},
	
	/*this function calls Blogsmith's sns javasvript to populate Blogsmith's comment's module fields*/
	sns : function () {
		if (document.getElementById("sns")) {
			sns.calls.getToken();
		}
	},
	/*The function de-populates Blogsmith's comment's module fields*/
	clearForm: function(){
		if (document.getElementById('cmtuinfo_sns')){
			var snsInput = getElementsByClassName(document, "input", "formtext");
			for(a=0; a<snsInput.length; a++) {
				snsInput[a].value = '';
			}
		}
	}

}

/*sitesocial */
siteIM_params_launchMethod = function() {
	if(!AIM.params.token) {
		openAuth.openAuthWindow();
	} else {
		AIM.transactions.startSession();
	}
}
siteSocialIntegration = {
	init : function () {
	},
	logoutUser:function() {
				openAuth.removeCurrentLink();
			openAuth.makeLoginLink();
			openAuth.clearForm();
			openAuth.loggedIn = false;
			openAuth.eraseTokenCookie('blogToken');
		}
}


/*end sitesocial*/
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}



/* end openauth */
