

// DISPLAY AJAX PAGE CONTENT IN A MODAL POPUP
function popupAjaxContent(title,width,height,url){
	YAHOO.namespace("container");
	// SHORTCUT 
	Y = YAHOO.container;
	
	// DEFAULT WIDTH
	width = intVal(width);
	if(width < 20){
		width = 300;
	}
	// DEFAULT HEIGHT
	if(height != 'auto'){		
		height = intVal(height);
		if(height < 20){
			height = 100+'px';
		}else{
			height = height+'px';
		}
	}
	
	// UNIQUE NAME
	var uniqueID = uniqid();
	
	// Instantiate a Panel from script
	Y[uniqueID] = new YAHOO.widget.Panel("ajaxPanel"+uniqueID, { 
						width:width+"px",
						visible:false, 
						draggable:true, 
						close:true, 
						iframe:true, 
						modal:true,
						dragOnly:true, // improves cpu performance
						fixedcenter: "contained",
						autofillheight: "body"
						} );
	// shortcut
	Y[uniqueID].setHeader(title);
	Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'"><img src="/images/ajax-loader.gif">Loading...</div>');
	//Y.ajaxPanel.setFooter("End of Panel #2");
	Y[uniqueID].render("container");
	
	
	YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'height', height);  
	YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'overflow', 'auto');  
	Y[uniqueID].show();
	
	var callback = {
		success: function(o) {
			if(o.responseText == ''){
				Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'">An error was encountered while retrieving this information.</div>');
			}else{				
				Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'">'+o.responseText+'</div>');
				YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'height', height);  
				YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'overflow', 'auto');  
			}
			
		},
		
		failure: function(o) {
			Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'">An error was encountered while contacting theFOAT.com.</div>');
		}
		
	};
	
	// FILL BODY WITH CONTENT
	YAHOO.util.Connect.asyncRequest('POST', url, callback );
	
	
}

// DISPLAY AJAX PAGE CONTENT IN A MODAL POPUP
function popupAjaxFormContent(title,width,height,formID){
	var myForm = YAHOO.util.Dom.get(formID);
	if(!myForm){
		return false;
	}
	YAHOO.namespace("container");
	// SHORTCUT 
	Y = YAHOO.container;
	
	// DEFAULT WIDTH
	width = intVal(width);
	if(width < 20){
		width = 300;
	}
	// DEFAULT HEIGHT
	if(height != 'auto'){		
		height = intVal(height);
		if(height < 20){
			height = 100+'px';
		}else{
			height = height+'px';
		}
	}
	
	// UNIQUE NAME
	var uniqueID = uniqid();
	
	// Instantiate a Panel from script
	Y[uniqueID] = new YAHOO.widget.Panel("ajaxPanel"+uniqueID, { 
						width:width+"px",
						visible:false, 
						draggable:true, 
						close:true, 
						iframe:true, 
						modal:true,
						dragOnly:true, // improves cpu performance
						fixedcenter: "contained",
						autofillheight: "body"
						} );
	// shortcut
	Y[uniqueID].setHeader(title);
	Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'"><img src="/images/ajax-loader.gif">Loading...</div>');
	//Y.ajaxPanel.setFooter("End of Panel #2");
	Y[uniqueID].render("container");
	
	
	YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'height', height);  
	YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'overflow', 'auto');  
	Y[uniqueID].show();
	
	YAHOO.util.Connect.setForm(formID, true);
	var callback = {
		upload: function(o) {
			if(o.responseText == ''){
				Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'">An error was encountered while retrieving this information.</div>');
			}else{				
				Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'">'+o.responseText+'</div>');
				YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'height', height);  
				YAHOO.util.Dom.setStyle('ajaxPanelBody'+uniqueID+'', 'overflow', 'auto');  
			}
			
		},
		
		failure: function(o) {
			Y[uniqueID].setBody('<div id="ajaxPanelBody'+uniqueID+'">An error was encountered while contacting theFOAT.com.</div>');
		}
		
	};
	
	// FILL BODY WITH CONTENT
	YAHOO.util.Connect.asyncRequest('POST', '/index.php', callback );
	
	
}

// FUNCTION TO PERFORM AJAX STATUS UPDATE COMMENT
function statusComment(status_id){
	
	Dom = YAHOO.util.Dom; // shortcut
	//the second argument of setForm is crucial,
	// (the "true" tells Connection Manager this is a file upload form)
	YAHOO.util.Connect.setForm('fAR'+status_id, true);			
	var callback = {		  
		upload: function(o) {
			if(o.responseText == ''){
				alert('Comment Failed!');
			}else{
				// MUST BE JSON RESPONSE
				try {
					var resp = YAHOO.lang.JSON.parse(o.responseText);  
				}
				catch(e) {
					waitingPanel.hide();
					alert('Invalid Response from Server');
					alert(o.responseText);
					return false;
				}
				// If failure, show error, otherwise, get new areas.
				if(resp.success == 0){
					waitingPanel.hide();
					alertVar(resp.errors);
					return false;
				}else{
					waitingPanel.hide();
					var c = resp.customObj;
					// add comment to top of comments
					var el = document.createElement('div');
					el.id = 'status-update-comment-'+resp.customObj.rec_id;
					Dom.insertAfter(el, Dom.get('comments-anchor-'+status_id));
					yui_el = Dom.get(el.id);
					
					// build html to insert
					var htmlSnip = '<div onMouseOver="showLayer(\'delete-comment-btn-'+c.rec_id+'\')" ';
					htmlSnip += 'onMouseOut="hideLayer(\'delete-comment-btn-'+c.rec_id+'\')">';
					htmlSnip += '<input type="button" class="submit floatr hidden" id="delete-comment-btn-'+c.rec_id+'" ';
					htmlSnip += 'onclick="deleteStatusComment(\''+c.rec_id+'\');" ';
					htmlSnip += 'style="position:relative; z-index:1000;width:25px; height:20px; font-weight:bold; color:#A00; padding:0; font-size:10px;" ';
					htmlSnip += 'value="X" title="Remove Comment" />';
					htmlSnip += '<div class="avatar-div"><a href="'+c.user_href+'"><img src="/avatar_images/'+c.avatar_file+'"></a></div>';
					htmlSnip += '<div class="details-div">';
					htmlSnip += '<a href="'+c.user_href+'">'+c.login+'</a> ';
					htmlSnip += c.description;
					htmlSnip += '<div class="when">'+c.when+'</div>';
					htmlSnip += '</div>';
					htmlSnip += '<div class="clear"></div>';
					htmlSnip += '</div>';
					
					// insert html
					yui_el.innerHTML = htmlSnip;
					Dom.addClass(yui_el,'status-update-comment');
					
					// close comment box
					hideLayer('ar-'+status_id);
					
					return false;
				}

			}
		},
		failure: function(o){
			alert('Unapprove Failed');
		}
	};
	
	// initiate a new waiting panel (use "window.xxx" so we can hide it from outside of this function);
	window.waitingPanel = new panelLoading('Adding Comment.  Please wait...<br />[<a href="#" onClick="stopIt();waitingPanel.hide()">Stop Submit</a>]');
	// Show the Panel
	waitingPanel.show();
	
	// Perform Upload Call
	window.oConnect = YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback);
	

};

// DELETE A COMMENT ON A STATUS ENTRY
function deleteStatusComment(id){
	var poststr = "sg=user.ajax_remove_status_comment&comment_id=" + intVal(id);
	var callback = {
		success: function(o) {
			if(o.responseText == ''){
				alert('Remove Failed!');
			}else{
				// MUST BE JSON RESPONSE
				try {
					var resp = YAHOO.lang.JSON.parse(o.responseText);  
				}
				catch(e) {				
					//waitingPanel.hide();
					//alert('Invalid Response from Server');
					//alert(o.responseText);
					return false;
				}
				// If server said failure
				if(resp.success == 0){
					waitingPanel.hide();
					alert('Remove Failed');
					return false;
					
				}else{			
					waitingPanel.hide();
					disableField('status-update-comment-'+id);
					//var btnID = getID('mmvbutton'+id);
					//btnID.innerHTML = 'Deleted';
				}
			}
			
		},
		
		failure: function(o) {
			alert('DELETE failed');
		}
		
	};
	// initiate a new waiting panel (use "window.xxx" so we can hide it from outside of this function);
	window.waitingPanel = new panelLoading('Deleting.  Please wait...<br />[<a href="#" onClick="stopIt();waitingPanel.hide()">Stop</a>]');
	// Show the Panel
	waitingPanel.show();
	// start ajax request
	YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback, poststr );
}

// DELETE A STATUS ENTRY
function deleteStatus(id){
	var poststr = "sg=user.ajax_remove_status&status_id=" + intVal(id);
	var callback = {
		success: function(o) {
			if(o.responseText == ''){
				alert('Remove Failed!');
			}else{
				// MUST BE JSON RESPONSE
				try {
					var resp = YAHOO.lang.JSON.parse(o.responseText);  
				}
				catch(e) {				
					//waitingPanel.hide();
					//alert('Invalid Response from Server');
					//alert(o.responseText);
					return false;
				}
				// If server said failure
				if(resp.success == 0){
					waitingPanel.hide();
					alert('Remove Failed');
					return false;
					
				}else{			
					waitingPanel.hide();
					disableField('sur-'+id);
				}
			}
			
		},
		
		failure: function(o) {
			alert('DELETE failed');
		}
		
	};
	// initiate a new waiting panel (use "window.xxx" so we can hide it from outside of this function);
	window.waitingPanel = new panelLoading('Deleting.  Please wait...<br />[<a href="#" onClick="stopIt();waitingPanel.hide()">Stop</a>]');
	// Show the Panel
	waitingPanel.show();
	// start ajax request
	YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback, poststr );
}

// UPDATE STATUS LIKES
function countLikes(status_id){
	var poststr = "sg=user.ajax_count_status_likes&status_id=" + intVal(status_id);
	var callback = {
		success: function(o) {
			if(o.responseText == ''){
				alert('Update Failed!');
			}else{
				// MUST BE JSON RESPONSE
				try {
					var resp = YAHOO.lang.JSON.parse(o.responseText);  
				}
				catch(e) {				
					waitingPanel.hide();
					//alert('Invalid Response from Server');
					//alert(o.responseText);
					return false;
				}
				// If server said failure
				if(resp.success == 0){
					waitingPanel.hide();
					alert('Update Failed');
					return false;
					
				}else{			
					waitingPanel.hide();
					var likes = Dom.get('status-likes-'+status_id);
					if(resp.customObj.num_likes == 0){
						likes.innerHTML = "";
					}else{
						if(resp.customObj.num_likes == 1){
							plural = 'person likes';
						}else{
							plural = 'people like';
						}
						likes.innerHTML = '<img src="/images/icons/thumbs_up.png" class="vmiddle" style="text-decoration:none;"> ';
						likes.innerHTML += '<a href="javascript:getLikes('+status_id+');">'+resp.customObj.num_likes+' '+plural+' this.</a>';
					}
					
				}
			}
			
		},
		
		failure: function(o) {
			alert('DELETE failed');
		}
		
	};
	// initiate a new waiting panel (use "window.xxx" so we can hide it from outside of this function);
	window.waitingPanel = new panelLoading('Counting people who liked this...  Please wait...<br />[<a href="#" onClick="stopIt();waitingPanel.hide()">Stop</a>]');
	// Show the Panel
	waitingPanel.show();
	// start ajax request
	YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback, poststr );
}

// ADD STATUS LIKE
function addLike(status_id){
	var poststr = "sg=user.ajax_like_status&status_id=" + intVal(status_id);
	var callback = {
		success: function(o) {
			if(o.responseText == ''){
				alert('Update Failed!');
			}else{
				// MUST BE JSON RESPONSE
				try {
					var resp = YAHOO.lang.JSON.parse(o.responseText);  
				}
				catch(e) {				
					waitingPanel.hide();
					//alert('Invalid Response from Server');
					//alert(o.responseText);
					return false;
				}
				// If server said failure
				if(resp.success == 0){
					waitingPanel.hide();
					alert('Update Failed');
					return false;
					
				}else{			
					waitingPanel.hide();
					countLikes(status_id);	
					var like_unlike = Dom.get('like-unlike-'+status_id);
					if(like_unlike){
						like_unlike.innerHTML = '<a href="javascript:unLike(\''+status_id+'\');">unlike</a>';
					}				
				}
			}
			
		},
		
		failure: function(o) {
			alert('Like failed');
		}
		
	};
	// initiate a new waiting panel (use "window.xxx" so we can hide it from outside of this function);
	window.waitingPanel = new panelLoading('Liking...  Please wait...<br />[<a href="#" onClick="stopIt();waitingPanel.hide()">Stop</a>]');
	// Show the Panel
	waitingPanel.show();
	// start ajax request
	YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback, poststr );
}

// REMOVE STATUS LIKE
function unLike(status_id){
	var poststr = "sg=user.ajax_unlike_status&status_id=" + intVal(status_id);
	var callback2 = {
		success: function(o) {
			if(o.responseText == ''){
				alert('Unlike Failed!');
			}else{
				// MUST BE JSON RESPONSE
				try {
					var resp = YAHOO.lang.JSON.parse(o.responseText);  
				}
				catch(e) {				
					waitingPanel.hide();
					//alert('Invalid Response from Server');
					//alert(o.responseText);
					return false;
				}
				// If server said failure
				if(resp.success == 0){
					waitingPanel.hide();
					alert('Update Failed');
					return false;
					
				}else{			
					waitingPanel.hide();
					countLikes(status_id);
					var like_unlike = Dom.get('like-unlike-'+status_id);
					if(like_unlike){
						like_unlike.innerHTML = '<a href="javascript:addLike(\''+status_id+'\');">like</a>';
					}						
				}
			}
			
		},
		
		failure: function(o) {
			alert('Unlike failed');
		}
		
	};
	// initiate a new waiting panel (use "window.xxx" so we can hide it from outside of this function);
	window.waitingPanel = new panelLoading('Unliking...  Please wait...<br />[<a href="#" onClick="stopIt();waitingPanel.hide()">Stop</a>]');
	// Show the Panel
	waitingPanel.show();
	// start ajax request
	YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback2, poststr );
}