
function init(){
$('shoutText').scrollTop = $('shoutText').scrollHeight;

}

window.addEvent('domready', function(){

//Reply links (show and move reply box)
	var replyLinks = $$('a.replyButton');
	
	for (var x = 0; x < replyLinks.length; x++) {
		replyLinks[x].addEvent('click', function(event) {
			event.stop();
			
			
			//var containingTable = this.parentNode.parentNode.parentNode.parentNode.parentNode;
			//var commentID = containingTable.getProperty('id').replace(/comment/, '');
			var commentID = this.get('id').replace(/replyLink/, '')
			var commentBox = $('addComment');
			
			var containingTable = $('commentBounds'+commentID);
			
			commentBox.inject(containingTable, 'after');
			
			var commentIDField = document.getElement('input[name=reply_comment_id]');
			var commentUserIDField = document.getElement('input[name=reply_user_id]');
			
			if(commentID=='end'){
				commentIDField.setProperty('value', '0');
				commentUserIDField.setProperty('value', this.get('userid'));
				$('addCommentHead').set('html', "Add new comment");}
			else{
				commentIDField.setProperty('value', commentID);
				commentUserIDField.setProperty('value', this.get('userid'));
				$('addCommentHead').set('html', "Reply to above comment");}
			
			
			commentBox.set('style', 'display:block');
			$('form_comment').focus();
			
			} );
	}
	
//Invite list
	var userItems = $$('a.userSelectItem');
	var filter = $('form_t_filter');
	if(filter != null){filter.set('value', '')}; //Must be cleared on refresh.
	var userTotal = userItems.length;
	var userCount = 0;
	
	for (var x = 0; x < userItems.length; x++) {
		userItems[x].addEvent('click', function(event) {
			event.stop();
			var filter = $('form_t_filter');
			var id = this.get('id').replace(/u_/,  '')
			
			//userSelectYes
			if(this.get('state') == 'no'){
				if(userCount > userTotal * 0.75){
					alert("You can only invite 75% of the avaliable furs. This is to prevent potential abuse.\n\nFeel free to make a thread to discuss wether this limit should be here or not. =^.^=");
				}else{
					this.set('state', 'yes');
					this.inject($('userSelectYes'), 'before');
					//add to filter
					filter.set('value', filter.get('value') + id + ',');
					userCount += 1;
				}
			}else{
				this.set('state', 'no');
				this.inject($('userSelectNo'), 'before');
				//remove from filter
				filter.set('value', filter.get('value').replace(id+',', ''));
				userCount -= 1;
			}
			
			
		});
		
	};
	
//quotes (expand mini quote)
	var replyQuotes = $$ ('a.quoteButton');
	for (var x = 0; x < replyQuotes.length; x++) {
		replyQuotes[x].addEvent('click', function(event) {
			event.stop();
			var quoteDiv = this.parentNode.parentNode;
			var cID = this.getProperty('href').replace(/#c_/,  '');
			//alert(cID);
			var text = $('commentText'+cID).get('html');
			var head = $('commentHead'+cID).get('html');

			var out = head.replace(/<\/div>/, ' (<a href="#c_'+cID+'">link</a>)</div>') + '<br>' + text;
			quoteDiv.set('html', out);
			} );
	}
	
	
	
	

	
//+/- Rating links
	var ratingLinks = $$('a.ratingLink');
	
	for (var x = 0; x < ratingLinks.length; x++) {
		ratingLinks[x].addEvent('click', function(event) {
			event.stop();
			
			//alert(this.get('pid')+this.get('pty')+this.get('pre'));
			var pid = this.get('pid');
			var pty = this.get('pty');
			var pre = this.get('pre');
			
			var req = new Request.HTML({
				url:'?', 
				data: {'function':'setVote', 'pid':pid, 'pty':pty, 'pre':pre},
				onSuccess: function(html) {
					$('rating'+pid).set('text', '');
					$('rating'+pid).adopt(html);
				},
				onFailure: function() {
					$('rating'+pid).set('text', 'The request failed.');
				}
			});
			req.send();
		});
			
	}
	
//MIGS rollovers
	var migslinks = $$('img.rollover');
	
	for (var x = 0; x < migslinks.length; x++) {
		migslinks[x].addEvent('mouseover', function(event) {
			event.stop();
			var type = this.get('roll');
			if(type == 'hm'){
				this.set('src', 'res/ico_happymigs.gif');
			}else if(type == 'wm'){
				this.set('src', 'res/ico_whinymigs.gif');
			
			};
		});
		
		migslinks[x].addEvent('mouseout', function(event) {
			event.stop();
			var type = this.get('roll');
			if(type == 'hm'){
				this.set('src', 'res/ico_happymigs_static.gif');
			}else if(type == 'wm'){
				this.set('src', 'res/ico_whinymigs_static.gif');
			
			};
		});
	};



} );


//Shoutbox submit
//shoutText
//shoutFooter
var shoutHasFocus = false;
var shoutTimerId = 0;
var shoutPingTime = 10000;
var shoutPingCount = 0;
var shoutAJAXActive = false;
var shoutUserListCount = 0;
//alert(dateTime + "\n" + (dateTime+1));
	function shoutSubmit(sendPost){
		if(!shoutAJAXActive){
		shoutAJAXActive = true;
		var post = "";
		if(sendPost==true){post = $("shoutTextEntry").get('value');}
		pingRunning = false;
		var pingOffset = 0;
		
		if(shoutHasFocus = true){pingOffset = 10}//Offset in minuites to user ping recorded on server.
		else{pingOffset=5};
		if(pingOffset<0){pingOffset=0};//So we can tell how idle users are. Normal pageloads have no offset so are determined as more idle.
			
			if(sendPost==true){$('shoutTextEntry').set('value', '');}
			
			var req = new Request.HTML({
				url:'?', 
				//id, post
				data: {'function':'shoutBox', 'pingOffset':pingOffset, 'lastID':$("shoutLastID").get('value'), 'post':post},
				onSuccess: function(html) {
					
					if(html.length>0){
						$("shoutLastID").set('value', (html[html.length-2].getAttribute('pid')));
						$("shoutLastTS").set('text', "Last post at " + (html[html.length-2].getAttribute('ts')));
						//$("shoutLastTS").set('text', html[html.length-2].data);
						//html[html.length-2].data = "";
						//dissect(html[html.length-2]);
						html[html.length-2].dispose()
						$('shoutText').adopt(html);
						$('shoutText').scrollTop = $('shoutText').scrollHeight;
						
						shoutPingTime=5000;//new posts, speed up check for other new posts.
						shoutPingCount=0;
					}
				},
				onFailure: function() {
					//$('rating'+pid).set('text', 'The request failed.');
					//alert("fail");
				}
			});
			req.send();
			
			//get online user list
			shoutUserListCount += 1
			if(sendPost==false && shoutUserListCount == 10){
				shoutUserListCount = 0;
				var req = new Request({
					url:'?', 
					//id, post
					data: {'function':'onlineList'},
					onSuccess: function(result) {
						
						if(result.length>0){
						//
							$('shoutOnlineList').set('html', result);
						}
					},
					onFailure: function() {
					}
				});
				req.send();
			}
			if(shoutHasFocus==true){shoutPingTime=10*1000}else{shoutPingTime=30*1000}
			if(shoutPingCount>50){shoutPingTime=10*60*1000}
			clearTimeout(shoutTimerId);
			shoutTimerId = setTimeout(shoutSubmit, shoutPingTime, false);
			shoutPingCount += 1;
			
						
			//dissect($('shoutTextEntry'));
		shoutAJAXActive = false;
		return false;
		}
	}
	
	
	function dissect(object){
		var t = "";
		c=0
		for(att in object){
			t += (att + ": " + object[att] + " \n");
			c+=1
			if(c>10){
				alert(t);
				t= "";
				c= 0;
			}
		}
		if(t != ""){alert(t);}
		
	}
