/**
* @SCRIPTS FOR GOSSIP 
* @Version - 07/05/2008 
* @package - Prototype and Script-aculo-us
* @author  - Martin Prado � timbasoftware.com
*/

/**
 * class Loader
 * @param {Element} elementID
 */
var Loader = {
	show: function(elementID, animation) {
		if (animation) {
			var dimensions = $(elementID).getDimensions();
		 	var loadId = 'id_'+ elementID;
		 	if(!$(loadId)){
			 	var div = new Element('div', { 'class': 'myLoader', 'id': loadId});
			 	div.setStyle({
					  width: dimensions.width+'px',
					  height: dimensions.height+'px'
					});
				div.setOpacity(0.90);
			 	$(elementID).setStyle({
					  position: 'relative'
					});
				$(elementID).insert(div, 'top');
		 	}
		 	else {
		 		$(loadId).show();
		 	}
		} else {
			$(elementID).setStyle({
				height: '30px'
			});
			$(elementID).update('Loading...');
		}			
	},
	hide: function(elementID, animation) {
		if (animation) {
			var loadId = 'id_'+ elementID;
		 	if($(loadId)) {
				Effect.Fade($(loadId));
			}
		} else {
			$(elementID).setStyle({
				height: 'auto'
			});
			$(elementID).update('');
		}			
	}
};

function elementAppear(toHide, toShow) {
	Effect.SlideUp(toHide, {
		afterFinish: function() {
			Effect.Appear(toShow);
		},
		duration: 0.4
	});
}

/**
* loginGossip
* Loguea al usuario 
*/
function loginGossip() {
	if(validateLogin()){
		new Ajax.Request('../includes/services/security/Login.php', {
			parameters: {loginMail: $F('loginMail'), loginPass: $F('loginPass')},
			onLoading: function() {
				Loader.show('menu', true);
			},
			onComplete: function(transport) {
				if (transport.responseText != false) {
					Loader.hide('menu', true);
					if ($('login_notify').getStyle('display') != 'none') {
						Effect.Puff('login_notify', {duration: 1});
					}
					$('menu').update(transport.responseText);
					matrix();
				} else {
					Loader.hide('menu', true);
					$('login_notify').addClassName('full');						
					errorLogin('Umm .. that\'s not entirely correct <a href="javascript:;" onclick="forgotPassword();">Forgot Password?</a>');
				}
			},
			onFailure: function() {
			Loader.hide('menu', true);
			$('login_notify').removeClassName('full');
				errorLogin('Sorry there\'s an error with the server');
			}});
	}
}

/**
 * validateLogin
 * Valida el formulario de login y muestra el error cuando los campos son vacios
 * @return {bool} if valid 
 */
function validateLogin() {
	if($('loginMail').value == '' || $('loginPass').value == '' ){
		$('login_notify').removeClassName('alert');
		$('login_notify').addClassName('full');
		$('notify_text').update('I think...you are forgetting something.. <a href="javascript:;" onclick="forgotPassword();">Forgot Password?</a>');
		Effect.Appear('login_notify', {
			afterFinish: function(){
			Effect.Puff('login_notify', {delay: 3, duration: 1 });
		}, duration: 0.4
		});	
		return false;
	}
	else{ 
		return true;
	}
}

/**
 * forgotPassword
 * @param {Bool} state
 */
function forgotPassword(state) {
	if (state) {
		Effect.Puff('login_notify', {
			afterFinish: function() {
				new Ajax.Request('../includes/services/user/RenewPasswordMail.php', {
					parameters: {email: $F('loginMail')},
					onSuccess: function(transport) {
						if (transport.responseText) {
							$('login_notify').removeClassName('password');
							if (transport.responseText == 0) {
								$('login_notify').addClassName('full');
								errorLogin('Umm .. that\'s not entirely correct <a href="javascript:;" onclick="forgotPassword();">Forgot Password?</a>');
							} else {
								$('login_notify').addClassName('full');
								$('login_notify').addClassName('success');
								$('notify_text').update('An email have been send to '+transport.responseText);
								Effect.Appear('login_notify', {
									afterFinish: function(){
										Effect.Puff('login_notify', {delay: 3, duration: 1 });
									}, duration: 0.4
								});	
							}
						}
					}
				})
			}, delay: 3, duration: 1});
	} else {
		Effect.Puff('login_notify', {
			afterFinish: function() {
				$('login_notify').removeClassName('full');
				$('login_notify').removeClassName('alert');
				$('login_notify').addClassName('password');
				$('notify_text').update('Enter your email and <a href="javascript:;" onclick="forgotPassword(true);">Click here!</a>');
				Effect.Appear('login_notify', {duration: 0.4});
			}, delay: 3, duration: 1});
	} 	
}
 
 
/**
* errorLogin
* @param {string} text 
*/
function errorLogin(text) {	
	$('login_notify').addClassName('alert');
	$('notify_text').update(text);
	Effect.Appear('login_notify', {
		afterFinish: function(){
		Effect.Puff('login_notify', {delay: 3, duration: 1});
	}, duration: 0.4
	});	
}
/**
 * viewReply
 * @param {Element} elem
 * @param {Integer} countReply
 * @param {Element} child
 */
function viewReply(elem, countReply, child){
	var dependable = $(child);
	var a = elem;
	var miDiv = '';
	miDiv = a.parentNode;
	var close = false;//default Close
	close = (dependable.style.display == 'none');
	if(close) {
		status = false;
		a.innerHTML = 'Hide ' +countReply +' reply';
		miDiv.className = miDiv.className + ' open';
		Effect.BlindDown(dependable, {
			afterFinish: function() {
				status = true;
			}
		});
	} else if (status) {
		status = false;		
		a.innerHTML = 'View ' +countReply +' reply';
		miDiv.className = 'reply';
		Effect.BlindUp(dependable, {
			afterFinish: function() {
				status = true;
			}
		});
	}
}

/**
 * replyComment
 * @param {Element} elementID
 */
function replyComment(elementID) {
	var textareaDiv = $(elementID).parentNode;
	var elem = textareaDiv.previous('a');
	var close = textareaDiv.style.display == 'none';
	if(close){
		status = false;
		Effect.BlindDown(textareaDiv, {
			afterFinish: function() {
				status = true;
				$(elementID).focus();
			}
		});
		elem.update('Cancel Reply');
	}
	else if (status) {
		status = false;
		Effect.BlindUp(textareaDiv, {
			afterFinish: function(){
				status = true;
			}
		});
		elem.update('Reply');
	} 
}

/**
 * renewCaptcha
 * Renueva la imagen del Captcha
 */
function renewCaptcha(){
	var img = $('captcha_img');
	img.src = img.src+'?'+Math.random(1,10);

}

/**
 * Selecciona el index de una lista
 * @param {Element} id
 * @param {String} value
 */
function selectIndex(id, value){
	var options = $$('#'+id+' option');
	for(var i = 0; i<options.size(); i++)
	{
		if(options[i].readAttribute('value') == value )
		{
			options[i].selected = "selected";
		}
                
	}
}

/**
 * myScrollTo
 * @param {Element} ancla
 */
function miScrollTo(ancla){
	new Effect.ScrollTo(ancla);
	if ($(ancla).tagName == 'TEXTAREA') {
		$(ancla).focus();
	} 
	return false;        
}

/**
 * changeTab
 * @param {Element} id
 * @param {Element} divDisplay
 */
function  changeTab(id, divDisplay){
	var tab1 = $('tabPhoto');
	var tab2 = $('tabFriends');
	var div1 = $('photos');
	var div2 = $('friends');
	var tabClick = $(id);
	if(tabClick.hasClassName('current')){
		
	}
	else {
		tabClick.addClassName('current');
		if(tab1.id == tabClick.id)
		{
			tab2.removeClassName('current');
		}
		else {
			tab1.removeClassName('current');
		}
		if($(divDisplay).id == div1.id ) {
			div2.hide();
		}
		else {
			div1.hide();
		}
		$(divDisplay).show();
	}
	
}

/**
 * loadDesc
 */
function loadDesc() {
	$('desc').value = 'Enter a description';
}

/**
 * clearDesc
 */
function clearDesc() {
	if($('desc').value == 'Enter a description'){
		$('desc').value = '';
	}
}


/**
 * vote
 * @param {String} type
 * @param {Integer} id
 */
function vote(type, id) {
	new Ajax.Updater('gossip'+id, '../includes/services/gossip/Vote.php', {
		parameters: {"type": type, "id": id}
	});
}

/**
 * showVote
 * @param {Integer} id
 */
function showVote(id) {
	new Ajax.Updater('gossip'+id, '../includes/services/gossip/ShowVote.php', {
		parameters: {"id": id}
	});
}

/**
 * popular
 */
function popular() {
	new Ajax.Updater('most-popular', '../includes/services/widgets/Popular.php');
}

/**
 * showRecentComments
 */
function showRecentComments() {
	new Ajax.Updater('recent-comments', '../includes/services/widgets/RecentComments.php');
}

/**
 * showMyRecentActivities
 */
function showMyRecentActivities() {
	new Ajax.Updater('my-recent', '../includes/services/activities/Gossip.php');
}

/**
 * showRecentActivities
 * @param {Integer} id
 */
function showRecentActivities(id) {
	new Ajax.Updater('recent-activities', '../includes/services/activities/Recent.php', {
		parameters: {"id": id}
	});
}

/**
 * addUrl
 */
function addUrl() {
	var network = checkNetworkType();
	
	if (validateNetwork()) {
		if(network){
			new Ajax.Request('../includes/services/networks/Save.php', {
				parameters: {description: $F('desc'), url: $F('url'), networkclass: network[0], networktitle: network[1]},
				onLoading: Loader.show('myNetwork', true),
				onSuccess: function(transport) {
					if (transport.responseText != false) {
						if (transport.responseText.isJSON()) {
							Loader.hide('myNetwork', true);
							createNetworkLink(transport.responseText.evalJSON());
							$('desc').value  = 'Enter a description';
							$('url').value   = 'http://';
							$('title').value = '';
							Effect.BlindUp('dependableInput', {
								afterFinish: function() {
									$('dependableInput').setStyle({display: 'none'});
								}
							});
							
						}
					}
				}
			});
		}
		
	}
}

/**
 * deleteUrl
 * @param {Integer} id
 */
function deleteUrl(id) {
	new Ajax.Request('../includes/services/networks/Delete.php', {
		parameters: {record: id},
		onLoading: Loader.show('myNetwork', true),
		onSuccess: function(transport) {
			if (transport.responseText != false) {
				Loader.hide('myNetwork', true);
				var elementID = 'net'+id;
				Effect.Fade(elementID, {
				afterFinish: function() {
					$(elementID).remove();
				}});				
			}
		}
	});
}

/**
 * validateNetwork
 */
function validateNetwork(){
	
	var valueDesc = $('desc').value;
	var valueUrl = $('url').value;
	var url = new RegExp(/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i);
	var notError1 = false;
	var notError2 = false;
	
	
	if(valueDesc == '' || valueDesc == 'Enter a description' ){
		Effect.Appear($('desc_info'), { duration: 2.0 });
		notError1 = false;
	}
	else {
		$('desc_info').hide();
		notError1 = true;
	}
	
	
	if(valueUrl == '' || url.exec(valueUrl) == null){
		Effect.Appear($('url_info'), { duration: 2.0 });
		notError2 = false;
	}
	else {
		$('url_info').hide();	
		notError2 = true;
	}
	
	if(notError1 && notError2){return true}
	else{return false}
	
} 


/**
* createNetworkLink
* Crea el link de network con su clase, su url, y su name
* @return string de 'a'
*/
function createNetworkLink(response) {
	var link = "<li id='net"+response.id+"' style='display: none;'><a href='" +response.url+ "' class='"+response.classname+"' target='_blank' title='"+response.description+"' >" +response.title+ "</a><a href='javascript:;' onclick='deleteUrl("+response.id+");'>Remove</a></li>";
	var list = $('miNetwork');
	list.insert(link);
	Effect.Appear('net'+response.id, {
		afterFinish: function() {
			new Effect.Highlight('net'+response.id);
		}
	});		
}

/**
* checkNetworkType
* 
* @return array de title y class
*/
function checkNetworkType() {
	var valueUrl = ($('url').value).toLowerCase();
	var networkClass;
	var networkName;
	var typeKnow = true;
	var network = new Array();
	
	if (valueUrl.include('facebook')) {
			networkClass = 'facebook';
			networkName = 'Facebook';
	}
	else if (valueUrl.include('flickr')) {
			networkClass = 'flickr';
			networkName = 'Flickr';
	}
	else if (valueUrl.include('last.fm')) {
			networkClass = 'last';
			networkName = 'Last.fm';
	}
	else if (valueUrl.include('twitter')) {
			networkClass = 'twiter';
			networkName = 'Twitter';
	}
	else if (valueUrl.include('reddit')) {
			networkClass = 'reddit';
			networkName = 'Reddit';
	}
	else {
		if ($('dependableInput').getStyle('display') != 'block') {
			Effect.BlindDown('dependableInput', {
				afterFinish: function() {
					$('dependableInput').setStyle({
						display: 'block'
					});
				}
			});
		}
		networkClass = 'star';
		typeKnow = false;
		if($('title').value != ''){
			networkName = $('title').value;
			typeKnow = true;
		}			
	}
	
	if (typeKnow) {
		network[0] = networkClass;
		network[1] = networkName;
		return network;
	}
	
}
/**
* showNetworkLinks
* Muestra los links de las Network
* @param {Boolean} flag
*/
function showNetworkLinks(flag) {
	new Ajax.Request('../includes/services/networks/Show.php', {
		parameters: {bool: flag},
		onLoading: Loader.show('myNetwork', true),
		onComplete: function(transport) {
			if (transport.responseText != false) {
				Loader.hide('myNetwork', true);
				$('myNetwork').update(transport.responseText);
				Effect.Appear('miNetwork');				
			}
		}
	});
}

/**
* adjustCols
* @Ajusta las columnas del formulario de envio de gossip
*/
function adjustCols() {
	var dim = $$('.right_content')[0].getDimensions();
	var dim2 = $$('.white.top')[0].getDimensions();
	var alto = ""+(dim.height - 78)+"px";
	if(dim2.height < dim.height){
		$$('.white.top')[0].style.height = alto;
	}
}

/**
 * matrix
 */
function matrix() {
	var id = $$('body')[0].readAttribute('id');
	if (id == 'index') {
		showGossips();
	} else if (id == 'show') {
		showVote($('gid').value);
		showPostComment($('gid').value);
		new PeriodicalExecuter(function(pe) {
		  miScrollTo('postComment');
		  pe.stop();
		}, 2);
		$('scroller').writeAttribute('onclick', "miScrollTo('postComment');");
	}
}

/**
 * deletePhoto
 * @param {Integer} id
 */
function deletePhoto(id) {
	new Ajax.Request('../includes/services/photos/Delete.php', {
		parameters: {record: id},
		onLoading: Loader.show('your_photos', true),
		onSuccess: function(transport) {
			if (transport.responseText != false) {
				Loader.hide('your_photos', true);
				var elementID = 'photo'+id;
				Effect.Fade(elementID, {
				afterFinish: function() {
					$(elementID).remove();
				}});				
			}
		}
	});
}

/**
 * show
 * Ajax request 
 * @param {Element} elementID
 * @param {String} url
 * @param {String} message
 * @param {Element} elemClass
 * @param {Array} params
 * @param {Boolean} pagination
 * @param {JSON} options
 */
function show(elementID, url, message, elemClass, params, pagination, options) {
	var params = (Object.isArray(params)) ? params : null;
	var options = (pagination) ? options : null;
	var elemClass = elemClass;
	var messageText = message;
	var elementID = elementID;
	var url = '../includes/services/'+url;
	var parameters = '{';
	if (params != null) {
		for (var i = 0; i < params.size(); i++) {
			parameters += '"'+params[i][0]+'": '+params[i][1]+', ';
		}
	}
	if (options != null) {
		parameters += '"limit": '+options.limit+', "page": '+options.page+', "links": '+options.links;
	}
	parameters += '}';
	new Ajax.Request(url, {
		parameters: parameters.evalJSON(),
		onLoading: Loader.show(elementID, false),
		onComplete: function (transport) {
			Loader.hide(elementID, false);
			if (transport.responseText != '') {
				if (transport.responseText.isJSON()) {
					var photos = $$(elemClass);
					for (var i = 0; i < photos.size(); i++) {
						photos[i].remove();
					}
					if ($('message')) {
						$('message').remove();
					}
					var responseJSON = transport.responseText.evalJSON();
					var newParams  = {"page": options.page, "result": responseJSON.result, "limit": options.limit, "links": options.links, "func": options.func};
					pagination = new Paginator(elementID, newParams, options.id);
					if (responseJSON.result == 0) {
						if (!$('message')) {
							$(elementID).insert({top: '<div id="message" class="message">'+messageText+'</div>'});
						} else {
							$('message').remove();
							$(elementID).insert({top: '<div id="message" class="message">'+messageText+'</div>'});
						}
					} else {
						$(elementID).insert({top: responseJSON.response});
					}					
				}
			} else {
				var message = 'An Error Has Occured';
				if (!$('message')) {
					$(elementID).insert({top: '<div id="message" class="message">'+message+'</div>'});
				} else {
					$('message').remove();
					$(elementID).insert({top: '<div id="message" class="message">'+message+'</div>'});
				}
			}
		}
	});	
}

/**
* @author jgaraza
* Paginator
* Clase paginadora de datos
* @param {Object} elementID
* @param {JSON} params
* @param {Array} options
*/

var Paginator = Class.create({
	initialize: function(elementID, params, id) {
		this.elementID = elementID+'_p';
		this.config(params);
		this.checkParams(id);
		this.construct();
	},
	config: function(params) {
		this.page = params.page;
		this.result = params.result;
		this.limit = params.limit;
		this.func = params.func;
		this.linksMaxNum = params.links;
		this.totalPagination = Math.ceil(this.result/this.limit);
		this.totalPages = Math.ceil(this.totalPagination/this.linksMaxNum);
		this.hack = 1/this.linksMaxNum;
		this.helper = [];
		this.multi = [];
		this.links = '';
	},
	checkParams: function(id) {
		this.id = (id) ? id : null;
	},
	construct: function() {
		if (this.totalPagination == 0) {
			this.links += '<div class="prev"><a href="javascript:;" class="inactive">Prev</a></div><div class="next"><a href="javascript:;" class="inactive">Next</a></div><div class="number"></div>';
			this.display(this.links);
			return false;
		}
		
		for (var j = 1; j <= this.totalPages; j++) {
			this.helper[j] = ((j-1)*this.linksMaxNum)+2;
			this.multi[j]  = j;
		}
		
		if (this.page == 1) {
			this.links += '<div class="prev"><a href="javascript:;" class="inactive">Prev</a></div>';
		} else {
			this.links += '<div class="prev"><a href="javascript:;" onclick="'+this.createFunction(this.page-1)+'">Prev</a></div>';
		}
		
		if (this.page == this.totalPagination) {
			this.links += '<div class="next"><a href="javascript:;" class="inactive">Next</a></div>';
		} else {
			this.links += '<div class="next"><a href="javascript:;" onclick="'+this.createFunction(this.page+1)+'">Next</a></div>';
		}
		
		this.links += '<div class="number">';
		
		var h = (this.page == 1) ? 1 : Math.ceil((this.page/this.linksMaxNum)-this.hack);
		var i = this.helper[h];
		var k = this.multi[h];
		if (i == 2) {
			var current = (this.page == 1) ? true : false;
			this.links += this.createLink(current, '1', 1);
		} else {
			this.links += this.createLink(false, '...', (i-this.linksMaxNum));
		}
		for (i; i < (this.linksMaxNum*k)+2; i++) {
			if (i > this.totalPagination) {
				break;
			}
			this.links += (i == this.page) ? this.createLink(true, i, i) : this.createLink(false, i, i);
		}
		
		if ((i-1) == this.totalPagination) {
			this.links += '';
		} else {
			this.links += this.createLink(false, '...', i);
		}
		
		this.links += '</div>';
		this.display(this.links);
	},
	createLink: function(current, innerText, newPage) {
		var pageNum = (newPage) ? newPage : false;
		var className = (current) ? 'class="current" ' : '';
		var a = '<a href="javascript:;" '+className+'onclick="'+this.createFunction(newPage)+'">'+innerText+'</a>';
		return a;
	},
	createFunction: function(newPage) {
		var pageNum = (newPage) ? newPage : this.page;
		var id = (Object.isNumber(this.id)) ? this.id : '\''+this.id+'\'';
		var f = this.func+'(';		
		f += pageNum;
		f += (this.id != null) ? ', '+id : '';
		f += ');';
		return f;
	},
	display: function() {
		$(this.elementID).update(this.links);
	}
});

/**
 * showPostComment
 * @param {Object} id
 */
function showPostComment(id) {
	new Ajax.Updater('post-comment', '../includes/services/gossip/ShowPost.php', {
		parameters: {"id": id}
	});
}

/**
 * showPhotos
 * @param {Integer} id
 * @param {Integer} page
 */
function showPhotos(page, id) {
	var page = (page) ? page : 1;
 	var params = [["type", true], ["id", id]];
	show('your_photos', 'photos/Show.php', 'You don\'t have any photo yet!', '.uploader div.picture', params, true, {"page": page, "limit": 28, "links": 4, "func": "showPhotos", "id": id});
}

/**
 * showGossips function 
 * @param {Integer} page
 * @param {String} type
 * @param {String} value
 * @param {String} searchType
 */
function showGossips(page, type) {
	var page = (page) ? page : 1;
	var params = null;
	var func = "showGossips";
	var value = $('search_str').value;
	var searchType = $('type').value; 
	var message = 'Sorry, theres no gossip yet!';
	if (type == "popular") {
		params = (value == '') ? [["type", '\"'+type+'\"']] : [["type", '\"'+type+'\"'], ["value", '\"'+value+'\"'], ["searchType", '\"'+searchType+'\"']];
		$('popular').className = "selected";
		$('recent').className = "";
	} else if (type == "search") { 
		params = [["type", '\"'+type+'\"'], ["value", '\"'+value+'\"'], ["searchType", '\"'+searchType+'\"']];
		$('popular').className = "";
		$('recent').className = "selected";
		message = 'Sorry, nothing found matching your search terms!';
	} else {
		params = (value != '') ? [["value", '\"'+value+'\"'], ["searchType", '\"'+searchType+'\"']] : null;
		$('popular').className = "";
		$('recent').className = "selected";
	}
 	show('gossips', 'gossip/Show.php', message, 'div.item', params, true, {"page": page, "limit": 4, "links": 10, "func": func, "id": type});
}

/**
 * saveComment
 * Save a comment 
 * @param {Integer} parentId, the id of the parent comment
 * @param {Integer} level,	the level in the tree
 * @param {String} commentTextId, the id of the comment textarea
 */

function saveComment(parentId,level, commentTextId){
	var text = $(commentTextId).value;
	$(commentTextId).value = '';
	var gossipId = $('idGossip').value;
	var theParams = 'gid=' + gossipId + '&parentId=' + parentId + '&level=' + level + '&comment=' + text;
	new Ajax.Request('../includes/services/gossip/SaveGossipComment.php',
		{
			parameters: theParams,
			onComplete: function(transport){
				if(parentId > 0){
					replyComment(commentTextId);
					if ($('childs'+parentId)) {
						$('childs'+parentId).insert({top: transport.responseText});
					}else{
						var temp = $(commentTextId).parentNode.parentNode;
						temp.insert({after: transport.responseText});
					}
				}else{
					$('itemComment').insert({top: transport.responseText});
				}
				miScrollTo('itemComment');
			}
		});
	
}

/**
 * getComments
 * @param {Object} id
 */
function getComments(id) {
	var gossipId = (id) ? id : $('idGossip').value;
	var params = 'gid=' + gossipId;
	new Ajax.Updater('comments','../includes/services/gossip/GetGossipComments.php',
		{
			parameters: params
		});
}

/**
 * limitChars
 * @param {Object} maxchars
 * @param {Object} elementID
 */
function limitChars(maxchars, elementID) {
	var size = $(elementID).value.length;
	var value = $(elementID).value;
	if (size > maxchars) {
		$(elementID).value = value.substring(0, maxchars);
		size = $(elementID).value.length;
		alert('You have reached the entry limit!');
	} 
	$('counter').update(maxchars - size);
}

/**
 * checkForPaste
 * @param {Object} event
 */
function checkForPaste(event) {
	var e = event.element();
	if ((e.previousValue && e.value.length > e.previousValue.length + 1) || (!e.previousValue && e.value.length > 1)) { 
		if (e.onpaste) {
			e.onpaste(e)
		} else if (e.readAttribute("onpaste")) {
			eval(e.readAttribute("onpaste"));
		}
	}
	e.previousValue = e.value;
}
/**
 * firefoxOnPaste
 */
function firefoxOnPaste() {
	$$('textarea').each(function(e) { 
		if (e.onpaste || e.readAttribute("onpaste")) {
			Event.observe(e,'input',checkForPaste);
		}
	});
}

if (Prototype.Browser.Gecko) {
	document.observe('dom:loaded', firefoxOnPaste);
}

/**
 * changeInputType
 * @param {Object} element
 */
function changeInputType(element) {
	var value = element.value;
	var type = (value == 1) ? 'file' : 'text';
	var input = new Element('input', {'type': type, 'name': 'upload', 'id':'upload'});
	$('type-upload').update(input);
}

/**
 * showHelp
 * @param {Object} field
 */
function showHelp(field, elementID){
	var offset = $(elementID).positionedOffset();
	$$('#helper h4')[0].update(help.fields[field].title);
	$$('#helper p')[0].update(help.fields[field].text);
	var height = ($('helper').getHeight() / 2) - 5;
	$('helper').setStyle({
		display: 'block',
		top: (offset.top - height) + 'px',
		left: (offset.left + 30) + 'px'
	});
}

function closeAlert(){
	Effect.Fade('gossipAlert');
}
