/****************************************************************/
/**********              GENERAL               ******************/
/****************************************************************/
YAHOO.namespace("ajaxpopup");

function init(){
	//Initialize the blurry background
	YAHOO.ajaxpopup.bgpanel = new YAHOO.widget.Panel("background-panel", 
		{ 
			height: document.body.clientHeight + "px",
			width: document.body.clientWidth + "px",
			visible:false, 
			constraintoviewport:true,
			zIndex:5000
		} 
	);
	YAHOO.ajaxpopup.bgpanel.render();
	
	//Call specific initialization
	if (document.getElementById('login-panel')){
		initLoginPopup();
	}
	if (document.getElementById('bookmark-panel')){
		initBookmarkPopup();
	}
}

function getLeftMargin(width){
	return parseInt((document.body.clientWidth/2) - (width/2));
}

YAHOO.util.Event.addListener(window, "load", init);

/****************************************************************/
/**********              LOGIN BOX               ****************/
/****************************************************************/

YAHOO.namespace("ajaxpopup.login");

var loginSuccess=false;
var loginBody='';
var forgotPswBody='';
var loginTitle='';
var errorPlaceHolder;

function initLoginPopup() {
	//Initialize the login popup
	YAHOO.ajaxpopup.login.panel = new YAHOO.widget.Panel("login-panel", 
		{ 
			height: document.body.clientHeight + "px",
			width: document.body.clientWidth + "px",
			visible:false, 
			constraintoviewport:true,
			zIndex:5005
		} 
	);
	YAHOO.ajaxpopup.login.panel.render();
	
	YAHOO.util.Event.addListener("show-login", "click", showLoginPanel);
	YAHOO.util.Event.addListener("hide-login", "click", hideLoginPanel);	
}

/**
 * determine if we are on the homepage
 */
function onHomepage(){
	if (document.getElementById("login-box-homepage")){
		return true;
	}
	return false;
}

function initErrorPlaceHolder(){
	if (onHomepage()){
		errorPlaceHolder=document.getElementById('login-hp-error-placeholder');
	}
	else{
		errorPlaceHolder=document.getElementById('login-error-placeholder');
	}
}

function showLoginPanel(e){
	if (onHomepage()){
		//We are on the homepage
		document.getElementById("emailfield").style.borderColor='black';
		document.getElementById("passwordfield").style.borderColor='black';
		return false;
	}
	if (loginBody!=''){
		document.getElementById("login-placeholder").innerHTML=loginBody;
	}
	if (loginTitle!=''){
		document.getElementById('popup-title').innerHTML=loginTitle;
	}
	displayLoginPanel(); 
}

function hideLoginPanel(e){
	YAHOO.ajaxpopup.bgpanel.hide();
	YAHOO.ajaxpopup.login.panel.hide();
}

function showForgotPassword(title){
	if (loginBody==''){
		loginBody=document.getElementById("login-placeholder").innerHTML;
	}
	if (forgotPswBody==''){
		YAHOO.util.Connect.asyncRequest('GET', '/profile/forgotpassword-ajax.html', 
			{
				success:function(o){
					if(o.responseText !== undefined){
						forgotPswBody = o.responseText;
						displayForgotPassword(title);
					}
			 	},
				failure: function(o){}
			}
		);
	}
	else{
		displayForgotPassword(title);
	}
}

function hideForgotPassword(){
	showLoginPanel();
}

function displayForgotPassword(title){
	var div=document.getElementById('login-placeholder');
	div.innerHTML = forgotPswBody;
	showHideCancelButton();
	if (loginTitle==''){
		loginTitle=document.getElementById('popup-title').innerHTML;
	}
	document.getElementById('popup-title').innerHTML=title;
	displayLoginPanel();
}

function showHideCancelButton(){
	if (onHomepage()){
		document.getElementById('forgotpsw-cancel').style.display='none';
	}
	else{
		document.getElementById('forgotpsw-cancel').style.display='inline';
	}
}

function displayLoginPanel(){
	initErrorPlaceHolder();
	errorPlaceHolder.style.display='none';
	YAHOO.ajaxpopup.bgpanel.show();
	YAHOO.ajaxpopup.login.panel.show();
}

function sendLoginRequest(oForm){
	if (loginSuccess) return true;
	var postData=YAHOO.util.Connect.setForm(oForm) + '&ajax=true';		
	var callback={
		success:function(o){
			if(o.responseText.indexOf('failure')>1){
				//Failure
				var map=eval(o.responseText);
				initErrorPlaceHolder();
				errorPlaceHolder.style.display='block';
				errorPlaceHolder.innerHTML="<ul class='error'><li>" + map.failure + "</li></ul>";				
			}
			else{
				//Success
				loginSuccess=true;
				oForm.submit();
			}
		}		
	};
	YAHOO.util.Connect.asyncRequest('POST', '/ajax_security_check', callback, postData);	
}

function sendForgotPasswordRequest(oForm){
	var postData=YAHOO.util.Connect.setForm(oForm);		
	var callback={
		success:function(o){
			if(o.responseText.indexOf('success')>1){
				//Success
				var map=eval(o.responseText);
				var div=document.getElementById('login-placeholder');
				div.innerHTML=map.success;
			}
			else{
				//Failure
				var div=document.getElementById('login-placeholder');
				div.innerHTML=o.responseText;
				showHideCancelButton();
			}
		}
	};
	YAHOO.util.Connect.asyncRequest('POST', oForm.action, callback, postData);	
}

/****************************************************************/
/**********              BOOKMARK BOX               *************/
/****************************************************************/

YAHOO.namespace("ajaxpopup.bookmark");

var bookmarkListDirty=false;

function initBookmarkPopup() {
	//Initialize the bookmark popup
	YAHOO.ajaxpopup.bookmark.panel = new YAHOO.widget.Panel("bookmark-panel", 
		{ 
			height: document.body.clientHeight + "px",
			width: document.body.clientWidth + "px",
			visible:false, 
			constraintoviewport:true,
			zIndex:5005
		} 
	);
	YAHOO.ajaxpopup.bookmark.panel.render();
	
	YAHOO.util.Event.addListener("show-bookmark", "click", showBookmarkPanel);
	YAHOO.util.Event.addListener("hide-bookmark", "click", hideBookmarkPanel);
}

function showBookmarkPanel(e){	
	YAHOO.ajaxpopup.bgpanel.show();
	YAHOO.ajaxpopup.bookmark.panel.show();
	getBookmarkList();
} 

function hideBookmarkPanel(e){
	YAHOO.ajaxpopup.bgpanel.hide();
	YAHOO.ajaxpopup.bookmark.panel.hide();
	if (bookmarkListDirty){
		//refresh
		window.location.reload(true);
	}
}

function getBookmarkList(){
	YAHOO.util.Connect.asyncRequest('GET', '/bookmarks-ajax.html', 
		{
			success:function(o){
				if(o.responseText !== undefined){
					var div=document.getElementById('bookmark-placeholder');
					div.innerHTML = o.responseText;					
				}
		 	},
			failure: function(o){}
		}
	);
}

function sendBookmarkRequest(oForm){
	var postData=YAHOO.util.Connect.setForm(oForm);	
	var callback={
		success:function(o){
			bookmarkListDirty=true;
			getBookmarkList();
		}
	};
	YAHOO.util.Connect.asyncRequest('POST', '/bookmark.html', callback, postData); 
}