/*************************************************************************************************************************
// File:	Registration.js
// Author: 	Matthew Hammond Mah, CondeNet
// Date: 	4/2/2007
// Summary: This javascript library is intended for the generic registration process that is used across all of CondeNet's
// 			desitnation sites (Style.com,Mens.Style.com,Concierge.com,Epicurious.com)
//
*************************************************************************************************************************/

var agt=navigator.userAgent.toLowerCase();
var is_safari = true;

function ReadCookie(cookieName) {
 	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return "";
 	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length;
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

var currEntryId;

function UserInfo () {
}

UserInfo.USERNAME_COOKIE = 'amg_user_info';

UserInfo.prototype.getUserName =
	function () {
		var cookieVal = ReadCookie(UserInfo.USERNAME_COOKIE);
		if (cookieVal == '') {
			return null;
		}

		return cookieVal;
        };

UserInfo.prototype.isLoggedIn =
	function () {
		return this.getUserName() != null;
	};



function showMouseOverPointer(e) {
	document.getElementById(this.id).style.cursor = 'pointer';
}

function appendReturnto() {
        window.location = '/user/login/retrieve?returnto=' + escape(window.location.pathname + window.location.search);
    }

function doLogoutPost() {
	var opt = {
		method: 'post',
		onSuccess:
			function(response) {
				window.location = window.location;
			}
	}
//        if (confirm("Are you sure that you want to log out?")) {
		new Ajax.Request('/user/logout', opt);
//        }
}

function gotoLogin() {
	window.location = '/user/login?returnto=' + escape(window.location.pathname + window.location.search);
}

// Register Nav Bar events
function registerLoginLink(){
	var login_link = document.getElementById('login_link');
    if(login_link)
	{
		if(login_link.addEventListener)		//IE6 does not support addEventListener
		    login_link.addEventListener("click", gotoLogin, false); 
		else
		    login_link.attachEvent("onclick", gotoLogin);
		
	}
	
}

runOnLoad(showUserStatusInNav);
runOnLoad(registerLoginLink);
/* old yahoo event registration removing because of library conflicts */
/*
YAHOO.util.Event.addListener(window, 'load', showUserStatusInNav);
YAHOO.util.Event.addListener('login_link', 'click', gotoLogin);
*/

function showUserStatusInNav() {
	if (new UserInfo().isLoggedIn()) {
		doShowSignedInNavbar();
	} else {
		doShowSignedOutNavbar();
	}
}

function doShowSignedOutNavbar() {
	if (document.getElementById('logged_out') != null) {
		document.getElementById('logged_out').style.display = 'block';
	}
	if (document.getElementById('logged_in') != null) {
		document.getElementById('logged_in').style.display = 'none';
	}
	if (document.getElementById('userId') != null) {
		document.getElementById('userId').innerHTML = '';
	}
}

function doShowSignedInNavbar() {
	if (document.getElementById('logged_in') != null) {
		document.getElementById('logged_in').style.display = 'block';
	}
	if (document.getElementById('logged_out') != null) {
		document.getElementById('logged_out').style.display = 'none';
	}
	if (document.getElementById('userId') != null) {
		document.getElementById('userId').innerHTML = '<a id="userProfileHome" href="/user/preferences">' + new UserInfo().getUserName() + '</a>';
	}
	
}