// Global use javascript functions
// Copyright 2007 Penn State Snowboard Club. All Rights Reserved.
// Author: Jason Wagner


/*****************************************************************************
* Function: redirectMe(url,sec)
* Input: Takes the url to redirect to. And the time before redirect, in seconds.
* Returns: N/A
******************************************************************************/
	function redirectMe(url,sec) {
	//	if(empty(sec)) sec = .5;
		window.setTimeout("window.location.href = '" + url + "';",sec * 1000);
		return true;
	}
	
/*****************************************************************************
* Function: saveOrder(listclass,hiddenfield)
* Input: Call this to serialize and store the order of sortable elements.
* Returns: N/A
******************************************************************************/
	function saveOrder(listclass,hiddenfield) {
		var order = Sortable.serialize(listclass);
		document.getElementById(hiddenfield).value = order;
		document.position.submit();
		return false;
	}

/*****************************************************************************
* Function: fadeBlock(myElement,command)
* Input: Takes the element to open/close (usually a div).  Takes "open" or 
* 		"close" as an action.
* Returns: N/A
******************************************************************************/
	function fadeBlock(myElement,command) {
		if(command == "close")
			new Effect.Fade($(myElement));
		if(command == "open")
			new Effect.Appear($(myElement));	 
		return true;   
	}
	

/*****************************************************************************
* Function: addComment
* Input: Adds the comment to page.
* Returns: N/A
******************************************************************************/
function addComment(sid) {
		
		document.getElementById("loader").style.display = "inline";
		//Javascript conflicts with "ID" name w/ calendar. Too many functions
		//and such. Built in the capability to pass an ID if need be.
		if(sid == null) 
			var id = document.getElementById("ID").value;
		else 
			var id = sid;
	
		var type = document.getElementById("Type").value;
		var comment = document.getElementById("Comment").value;
		comment = encodeURIComponent(comment);
		
		var bingArgs = {
			method: 'post',
			postBody: "ID=" + id + "&Type=" + type + "&Comment=" + comment,
			onSuccess: function(data) {
				document.getElementById("loader").style.display = "none";					
				var response = data.responseText;
				if(response.indexOf("OK") != -1) {
					refreshComments(id,type);
					tinyMCE.getInstanceById('Comment').getBody().innerHTML=""; 
				} else {
					//Display error message from the server.
					alert(response);
				}
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		}
		
		new Ajax.Request('/PHP/post_comment.php', bingArgs);
}

/*****************************************************************************
* Function: refreshComments(id,type)
* Input: Refreshes the comment box with the newest comment
* Returns: N/A
******************************************************************************/
function refreshComments(id,type) {		
		var bingArgs = {
			method: 'post',
			postBody: 'id=' + id + '&Type=' + type,
			onSuccess: function(data) {
				var response = data.responseText;
				document.getElementById("comment_box").innerHTML = response;
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		}
		
		new Ajax.Request('/PHP/getComments.php', bingArgs);
}

/*****************************************************************************
* Function: editComment(id,type)
* Input: Uses inline editing to edit comment.
* Returns: N/A
******************************************************************************/
function editComment(id,type) {		
		alert("Mouse over comment to edit.");
		var bindArgs = {
			rows:5,
				cols:60,
			externalControl: 'ctrl-edit-' + id,
			highlightendcolor: '#EEEEEE',
			callback: function(form, value) { 
				return 'comment=' + escape(value) 
			}
		}
		new Ajax.InPlaceEditor('comment-p-'+id, '/PHP/moderate_comments.php?id=' + id + '&type=' + type + '&action=edit', bindArgs);

}

/*****************************************************************************
* Function: deleteComment(id,type)
* Input: Deletes the selected comment.
* Returns: N/A
******************************************************************************/
function deleteComment(id,type) {		
		var bingArgs = {
			method: 'get',
			onSuccess: function(data) {
				fadeBlock('comment-'+id,"close");
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		}
		
		new Ajax.Request('/PHP/moderate_comments.php?id=' + id + '&type=' + type + '&action=delete', bingArgs);
}

/*****************************************************************************
* Function: deleteBlog(id)
* Input: Deletes selected blog
* Returns: N/A
******************************************************************************/
function deleteBlog(id) {		
		
		var bingArgs = {
			method: 'post',
			postBody: 'bid=' + id + "&action=deleteBlog",
			onSuccess: function(data) {
				fadeBlock('blog-'+id,"close");
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		};
		
		new Ajax.Request('/PHP/trigger.php', bingArgs);
}
/*****************************************************************************
* Function: deletePhoto(id)
* Input: Deletes selected photo
* Returns: N/A
******************************************************************************/
function deletePhoto(id) {		
		
		var bingArgs = {
			method: 'post',
			postBody: 'pid=' + id + "&action=deletePhoto",
			onSuccess: function(data) {
				fadeBlock('photo-'+id,"close");
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		};
		
		new Ajax.Request('/PHP/trigger.php', bingArgs);
}
/*****************************************************************************
* Function: deleteEvent(id)
* Input: Deletes selected event
* Returns: N/A
******************************************************************************/
function deleteEvent(id) {		
		
		var bingArgs = {
			method: 'post',
			postBody: 'eid=' + id + "&action=deleteEvent",
			onSuccess: function(data) {
				fadeBlock('event-'+id,"close");
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		};
		
		new Ajax.Request('/PHP/trigger.php', bingArgs);
}
/*****************************************************************************
* Function: deleteAlert(id)
* Input: Deletes selected alert
* Returns: N/A
******************************************************************************/
function deleteAlert(id) {		
		
		var bingArgs = {
			method: 'post',
			postBody: 'bid=' + id + "&action=deleteAlert",
			onSuccess: function(data) {
				fadeBlock('alert-'+id,"close");
			},
			on404: function(data) {
				alert('Error 404: location "' + data.statusText + '" was not found.');
			},
			onFailure: function(data) {
				alert('Error ' + data.status + ' -- ' + data.statusText);
			}
		};
		
		new Ajax.Request('/PHP/trigger.php', bingArgs);
}

/*****************************************************************************
* Function: fcenterDiv(Xwidth,Yheight,divid)
* Input: Center div on the center of the browser window
* Returns: N/A
******************************************************************************/
function fcenterDiv(Xwidth,Yheight,divid) {
			var d = document;
			var de = document.documentElement;
			
			// Find how much the visitor has scrolled
			var scrolledX, scrolledY;
			if( self.pageYoffset ) {
				scrolledX = self.pageXoffset;
				scrolledY = self.pageYoffset;
			} else if( de && de.scrollTop ) {
				scrolledX = de.scrollLeft;
				scrolledY = de.scrollTop;
			} else if( d.body ) {
				scrolledX = d.body.scrollLeft;
				scrolledY = d.body.scrollTop;
			}
			
			// Get coordinates of the center of browser window
			var centerX, centerY;
			if( self.innerHeight ) {
				centerX = self.innerWidth;
				centerY = self.innerHeight;
			} else if( de && de.clientHeight ) {
				centerX = de.clientWidth;
				centerY = de.clientHeight;
			} else if( d.body ) {
				centerX = d.body.clientWidth;
				centerY = d.body.clientHeight;
			}
		
			// Xwidth is the width of the div, Yheight is the height of the
			// div passed as arguments to the function:
			var leftoffset = scrolledX + (centerX - Xwidth) / 2;
			var topoffset = scrolledY + (centerY - Yheight) / 2;
			// The initial width and height of the div can be set in the
			// style sheet with display:none; divid is passed as an argument to // the function
			$(divid).setStyle({position: 'absolute',top :topoffset + 'px',left: leftoffset + 'px',display: 'block' });
		
		
		} 
		
		
		function searchClicked(element) {
			if (element.value == 'Search') element.value = "";
			element.style.width = "140px";
			element.style.textAlign = "left";
			document.getElementById("search-submit").style.display = "inline";
		}
		function searchBlur(element) {
			element.value='Search'
			element.style.width = "70px";
			element.style.textAlign = "center";
			document.getElementById("search-submit").style.display = "none";
		}    
	
	
	//Timeout variables
	var min_msg = 1; // Number of minutes to refresh messages
	var min_online = 10; // Number of minutes to refresh online list
	
	function refreshOnlineList() {		
			var bingArgs = {
				method: 'post',
				postBody: 'action=getActiveUsers',
				onSuccess: function(data) {
					var response = data.responseText;
					document.getElementById("online_container").innerHTML = response;
				},
				on404: function(data) {
					alert('Error 404: location "' + data.statusText + '" was not found.');
				}
			}
			new Ajax.Request('/PHP/trigger.php', bingArgs);
	}

	//For Calendar
	function viewEvent(id) {
		redirectMe("/calendar/" + id,.1);
	}
	
	function clearTbody(tbody) {
			var table = tbody.ancestors()[0];
			var tid = tbody.identify();
			$(tbody).remove();	
			var t = new Element("tbody",{id: tid});
			$(table).appendChild(t);
		}
		
	function expandAlert(){
		var expandedHeight = "90px";
		var elem = $('alerts');
		if(elem.style.height == expandedHeight) {
			//close
			$('alerts').morph('height:20px;');
			$('details-link').update("More details...");
		} else {
			//open
			$('alerts').morph('height:' + expandedHeight + ';');
			$('details-link').update("Less details...");
		}
	
	}

			


