// JavaScript Document
var keyAction = false;
var wait_icon = '<img src=\"/graphics/ajax_ani.gif\" width=\"16\" height=\"16\" />';

// capture key event
document.onkeyup = KeyCheck;       
// check keypresses
function KeyCheck(e){
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if(keyAction == "setstatus" && KeyID == 13) updateStatus(); // do status update on return press
}

// toggle edit field
function toggleStatusEdit(type){
	if(!keyAction){
		if(type == 2){ com_status_field = '_large'; }else{ com_status_field = ''; }
		beginStatusEdit();
	}else{
		updateStatus();
	}
}


// show status edit field
function beginStatusEdit(){
	keyAction = "setstatus";
	//alert(com_status_field);
	showToggle('showstatus'+com_status_field);
	showToggle('editstatus'+com_status_field);
	_id('input_editstatus').focus();
}
// set status and hide status edit field
function updateStatus(){
	keyAction = "";
	var status = _id('input_editstatus'+com_status_field).value+'';
	//alert('ny status: '+status);
	//alert('gammal status: '+current_community_status);
	if(status != current_community_status){
		/*alert('spara ny status: '+status);*/
		current_community_status = status;
		_id('showstatus'+com_status_field).innerHTML = 'Är '+status;
		//if(_id('showstatus_large')) _id('showstatus_large').innerHTML = 'Är '+status;
		_id('input_editstatus').value = status;
		if(_id('input_editstatus_large')) _id('input_editstatus_large').value = status;
		x_updateStatusSajax(user_id, status, updateStatusCallback);
	}else{/*alert('status är inte ny');*/}
	showToggle('editstatus'+com_status_field);
	showToggle('showstatus'+com_status_field);
}

// recieve status php update callback
function updateStatusCallback(data){
	if(com_status_field){
		_id('showstatus').innerHTML = data;
	}else{
		_id('showstatus_large').innerHTML = data;
	}
}

// check user activity for mini log updates
var mouseIsActive = true;
function setMouseInactive(){mouseIsActive = false;/*alert('mouse inactive');*/}
mouseInactiveTimer = setInterval('setMouseInactive()', 120123);
document.onmousemove = function(){
	mouseIsActive = true;
	clearInterval(mouseInactiveTimer);
	mouseInactiveTimer = setInterval('setMouseInactive()', 120123);
}

// init minilogg autoupdate
getMiniloggTimer = setInterval("getMinilogg()", 30000);
function getMinilogg(){
	// only get log if user is active and has user id
	if(mouseIsActive && typeof user_id != 'undefined' && user_id > 0){
		x_getMinilogg(user_id, getMiniloggCallback);
	}
}
function getMiniloggCallback(data){
	if(document.getElementById('minilogg').innerHTML != data) document.getElementById('minilogg').innerHTML = data;
}

// add a comment to a page
function addPageComment(){
	//alert(user_id);
	var comment_text = document.getElementById('textarea_new_comment').value;
	if(comment_text != '' && typeof page_id != 'undefined'){
		//alert('add comment!');
		document.getElementById('add_comment_message').innerHTML = wait_icon+' Lägger till kommentar...';
		x_addPageComment(page_id, user_id, comment_text, addPageCommentCallback);
		comment_page = 1;
	}else if(comment_text == ''){
		alert('Du måste skriva något för att lägga till en kommentar!');
	}
}

// call comment print function
function printComments(amount,page){
	if(page >0 && page<=max_comment_pages){
		comment_page = page;
		document.getElementById('comment_container').innerHTML += '<div style=\"position:absolute;top:50%;left:0px;width:100%;text-align:center;\">'+wait_icon+'</div>';
		x_printCommentsSajax(page_id, amount, comment_page, addPageCommentCallback);
	}
}

// rewrite comment area with new data TODO: refresh page nr, total count, etc
function addPageCommentCallback(data){
	if(data != 'error'){
		if(document.getElementById('textarea_new_comment'))	document.getElementById('textarea_new_comment').value = '';
		document.getElementById('comments_page_nr').innerHTML = comment_page;
		document.getElementById('comment_container').innerHTML = data; //+document.getElementById('comment_container').innerHTML;
		document.getElementById('add_comment_message').innerHTML = '';
	}else{
		document.getElementById('add_comment_message').innerHTML = 'Ett fel uppstod! Ladda om sidan, försök igen.';	
	}
}


// send friend request functions
function sendFriendRequest(){
	if(typeof user_id != 'undefined' && typeof add_friend_user_id != 'undefined'){
		showToggle('addfriend_content');
		document.getElementById('addfriend_status').innerHTML = '<div style=\"width:380px;text-align:center;\">'+wait_icon+' Skickar...</div>';
		showToggle('addfriend_status');
		var comment_text = document.getElementById('textarea_add_friend').value;
		x_sendFriendRequestSajax(user_id, add_friend_user_id, comment_text, sendFriendRequestCallback);
	}
}
function sendFriendRequestCallback(data){
	var close_btn = '<div class=\"btn_pink_50\" style=\"float:right;\" onclick=\"closeAddFriend();\">Stäng</div><div class=\"space\"></div>';
	var message = 'meddelande';
	// return messages
	if(data.substr(0,6)=='error:' && data.substr(6) == 'same user') message = 'Fel: Du kan inte bli vän med dig själv!';
	if(data.substr(0,6)=='error:' && data.substr(6) == 'connection exists') message = 'Fel: Ni är redan vänner eller så har någon av er redan försökt bli vän med varandra!';
	if(data.substr(0,6)=='error:' && data.substr(6) == 'mail not sent') message = 'Vänförfrågan har skickats men mailet kunde inte skickas';
	if(data.substr(0,6)!='error:') message = 'Vänförfrågan har skickats!';
	// update status area
	document.getElementById('addfriend_status').innerHTML = message+close_btn;
}

// report content functions
// report_vars must be correctly set before calling
function fileReport(){
	showToggle('addreport_content');
	_id('addreport_status').innerHTML = '<div style=\"width:380px;text-align:center;\">'+wait_icon+' Skickar anmälan...</div>';
	showToggle('addreport_status');
	//alert('send report! type: '+report_type+' obj id: '+report_object_id+' user: '+user_id+' text: '+report_text+' image: '+report_image_id+' url: '+report_url);
	x_fileReport(report_type, report_object_id, user_id, report_text, report_image_id, report_url, fileReportCallback);
	
}
function fileReportCallback(data){
	var close_btn = '<div class=\"btn_pink_50\" style=\"float:right;\" onclick=\"closeAddReport();\">Stäng</div><div class=\"space\"></div>';
	var message = '';
	if(data != 'error'){ message = 'Rapporten skickad! Tack för din hjälp!'; }else{ message = 'Rapport ej skickad. Antingen uppstod ett fel eller så har du redan rapporterat innehållet.'; }
	_id('addreport_status').innerHTML = message+close_btn;
}

// mood update functions
function changeMood(newMood, newTitle){
	if(newMood != currentMood){
		currentMood = newMood;
		document.getElementById('mood_title').innerHTML = newTitle;
		document.getElementById('mood_icon').src = '/graphics/moodicon_'+newMood+'.gif';
		document.getElementById('mood_icon_small').src = '/graphics/moodicon_'+newMood+'_small.gif';
		closeMoodIconMenu();
		x_changeMoodSajax(user_id, newMood, changeMoodCallback);
	}
}
function changeMoodCallback(){
	// do nothing here..
}

// add a blog comment
function addBlogComment(blog_entry_id){
	var comment_headline = '';
	var comment_text = document.getElementById('textarea_blog_comment').value;
	if(comment_text != '' && typeof blog_entry_id != 'undefined'){
		document.getElementById('comment_status_message').innerHTML = wait_icon+' Lägger till kommentar...';
		x_addBlogComment(blog_entry_id, user_id, comment_headline, comment_text, addBlogCommentCallback);
		comment_page = 1;
	}else if(comment_text == ''){
		alert('Du måste skriva något för att lägga till en kommentar!');
	}
}
function addBlogCommentCallback(data){
	if(data != 'error'){
		if(document.getElementById('textarea_blog_comment')) document.getElementById('textarea_blog_comment').value = '';
		document.getElementById('comments_page_nr').innerHTML = comment_page;
		document.getElementById('blog_comment_container').innerHTML = data;
		document.getElementById('comment_status_message').innerHTML = '';
	}else{
		document.getElementById('comment_status_message').innerHTML = 'Ett fel uppstod! Ladda om sidan, försök igen.';	
	}
}
// blog comment print function
function printBlogComments(amount,page){
	if(page >0 && page<=max_comment_pages){
		comment_page = page;
		document.getElementById('blog_comment_container').innerHTML += '<div style=\"position:absolute;top:50%;left:0px;width:100%;text-align:center;\">'+wait_icon+'</div>';
		x_printBlogCommentsSajax(blog_entry_id, amount, comment_page, addBlogCommentCallback);
	}
}


// add a wall post
function addWallpost(owner_id){
	var wall_text = document.getElementById('textarea_wall').value;
	if(wall_text != '' && typeof owner_id != 'undefined'){
		document.getElementById('wall_status_message').innerHTML = wait_icon+' Lägger till inlägg...';
		x_addWallpost(owner_id, user_id, wall_text, addWallpostCallback);
		wallpost_page = 1;
	}else if(wall_text == ''){
		alert('Du måste skriva något för att lägga till något på väggen!');
	}
}
function deleteWallpost(wallpost_id){
	_id('wall_status_message').innerHTML = wait_icon+' Tar bort inlägget...';
	x_deleteWallpost(wallpost_id, user_id, addWallpostCallback);
	wallpost_page = 1;
}
function addWallpostCallback(data){
	if(data != 'error'){
		if(_id('textarea_wall')) document.getElementById('textarea_wall').value = '';
		//document.getElementById('wallposts_page_nr').innerHTML = comment_page;
		_id('wallpost_container').innerHTML = data;
		_id('wall_status_message').innerHTML = '';
	}else{
		_id('wall_status_message').innerHTML = 'Ett fel uppstod! Ladda om sidan, försök igen.';	
	}
}


// blog comment print function (not in use?)
function printWallposts(amount,page){
	if(page >0 && page<=max_wallpost_pages){
		wallpost_page = page;
		document.getElementById('wallpost_container').innerHTML += '<div style=\"width:100%;text-align:center;\">'+wait_icon+'</div>';
		x_printWallpostsSajax(owner_id, amount, wallpost_page, addWallpostCallback);
	}
}

// check username availability
function checkUsername(){
	if(document.getElementById('input_username').value != ''){
		var username = document.getElementById('input_username').value;
		document.getElementById('check_username_status').innerHTML = wait_icon+' Kontrollerar namn...';
		x_checkUsername(username, checkUsernameCallback);
	}else{
		document.getElementById('check_username_status').innerHTML = 'Du måste skriva ett namn!';
	}
}
function checkUsernameCallback(data){
	if(data == 'available'){
		document.getElementById('check_username_status').innerHTML = 'Jippi! Namnet är ledigt!';
	}else if(data == 'unavailable'){
		document.getElementById('check_username_status').innerHTML = 'Buuuh! Namnet är upptaget, testa ett annat!';
	}else{
		document.getElementById('check_username_status').innerHTML = 'Ett fel uppstod, testa igen!';
	}
}

// vote for image
function imageVote(image_id, grade, votes, newVote){
	_id('grade_number').innerHTML = '?';
	x_imageVote(image_id, grade, votes, newVote, imageVoteCallback);
}
function imageVoteCallback(data){
	if(data != 'error'){
		_id('grade_number').innerHTML = data;
	}else{
		//alert('Rösten registrerade inte');
		_id('grade_number').innerHTML = grade;
	}
}

// image comment print function
function printImageComments(amount,page){
	if(page >0 && page<=max_comment_pages){
		comment_page = page;
		document.getElementById('image_comment_container').innerHTML += '<div style=\"position:absolute;top:50%;left:0px;width:100%;text-align:center;\">'+wait_icon+'</div>';
		x_printImageComments(image_id, amount, comment_page, printImageCommentCallback);
	}
}
function addImageComment(image_id){
	var comment_headline = '';
	var comment_text = document.getElementById('textarea_image_comment').value;
	if(comment_text != '' && typeof(image_id) != 'undefined'){
		document.getElementById('comment_status_message').innerHTML = wait_icon+' Lägger till kommentar...';
		x_addImageComment(image_id, user_id, comment_headline, comment_text, printImageCommentCallback);
		comment_page = 1;
	}else if(comment_text == ''){
		document.getElementById('comment_status_message').innerHTML = 'Du måste skriva något!';
	}
}
function printImageCommentCallback(data){
	if(data != 'error'){
		document.getElementById('comment_status_message').innerHTML = ' ';
		document.getElementById('image_comment_container').innerHTML = data;
		document.getElementById('comments_page_nr').innerHTML = comment_page;
	}else{
		document.getElementById('comment_status_message').innerHTML = data;	
	}
}

// lost password
function sendUserPassword(){
	var email_address = _id('input_lost_password').value;
	if(email_address != ''){
		_id('lost_password_status').innerHTML = wait_icon+' Skickar..';
		//_id('lost_password_message').innerHTML = wait_icon+' Skickar..';
		x_sendUserPassword(email_address, sendUserPasswordCallback);
	}
}
function sendUserPasswordCallback(data){
	_id('lost_password_status').innerHTML = '';
	_id('lost_password_message').innerHTML = data;
}









