/* ./js/quickAnnoyance.js */
function setTempQuickMenuMessage(string){
	var head = document.getElementById("quick_menu_wrapper").getElementsByTagName("h2")[0];
	var text = head.innerHTML;
	head.innerHTML = string;
	window.setTimeout(function (){
			head.innerHTML = text;
		},
		4000
	);
}
function quickAnnSave(){
	var mus_id;
	var user_id = document.getElementsByName("qa_current_user_id")[0].getAttribute("value");
	var profile_id = document.getElementsByName("qa_current_profile_id")[0].getAttribute("value");
	var content = document.getElementsByName("qa_ann_content")[0].value;
	if(!content.length > 0){
		setTempQuickMenuMessage("Your annoyance was empty!");
		return;
	}
	try{
		var select = document.getElementsByName("qa_mus_id")[0];
		for(i = 0; i < select.options.length; i++){
			if(select.options[i].selected){
				mus_id = select.options[i].value;
				break;
			}
		}
		if(!mus_id){
			mus_id = 0;
		}
	}catch(e){
		var mus_id = 0;
	}
	postString = "content=" + fixPlus(content)
				+ "&mus_id=" + mus_id 
				+ "&user_id=" + user_id 
				+ "&profile_id=" + profile_id;
	request("./interfaces/quickAnnSave.php", function (){
			handleQuickAnnSave(user_id, profile_id);
		}, 
		postString
	);
}
function handleQuickAnnSave(user_id, profile_id){
	document.getElementById("quick_menu_wrapper").innerHTML = request.text;
	setTempQuickMenuMessage("Your changes have been saved.");
	if(profile_id == user_id){
		if(isPage('musings')){
			window.location = PATH_HTTP + '/musings.php?uid=' + user_id;
		}else if(isPage('annoyances')){
			window.location = PATH_HTTP + '/annoyances.php?uid=' + user_id;
		}else if(isPage('comments')){
			//
		}else{
			reloadProfileAnnoyances(user_id, profile_id);
		}
	}
}
function reloadProfileAnnoyances(user_id, profile_id){
	var postString = "content=annoyances&user_id=" + user_id + "&profile_id=" + profile_id
	request("./interfaces/reloadStatic.php", function (){
			document.getElementById("profile_annoyances").innerHTML = request.text;
		},
		postString
	);
}
function reloadProfileMusings(user_id, profile_id){
	request("./interfaces/reloadStatic.php", function (){
			document.getElementById("profile_musings").innerHTML = request.text;
		},
		"content=musings&user_id=" + user_id + "&profile_id=" + profile_id
	);
}
function handleQuickBuddyAdd(){
	var options = document.getElementsByName("quick_buddy_group_id")[0].options;
	var group_id;
	for(i = 0; i < options.length; i++){
		if(options[i].selected){
			group_id = options[i].value;
		}
	}
	if(!group_id){
		group_id = 0;
	}
	var buddy_id = document.getElementsByName("quick_buddy_buddy_id")[0].value;
	var user_id = document.getElementsByName("quick_buddy_user_id")[0].value;
	var getString = "buddy_id=" + buddy_id + "&user_id=" + user_id + "&group_id=" + group_id;
	request("./interfaces/quickBuddyAdd.php?" + getString, returnQuickBuddyAdd, "");
}
function returnQuickBuddyAdd(){
	document.getElementById("quick_menu_wrapper").innerHTML = request.text;
	setTempQuickMenuMessage("Your buddies have been updated.");	
}
function quickMusSave(){
	var ann_id;
	var user_id = document.getElementsByName("qm_current_user_id")[0].getAttribute("value");
	var profile_id = document.getElementsByName("qm_current_profile_id")[0].getAttribute("value");
	var title = document.getElementsByName("qm_mus_title")[0].value;
	var content = document.getElementsByName("qm_mus_content")[0].value;
	if(!content.length > 0){
		setTempQuickMenuMessage("Your musing was empty!");
		return;
	}else if(!title.length > 0){
		setTempQuickMenuMessage("Your musing had no title!");
		return;
	}
	try{
		var select = document.getElementsByName("qm_ann_id")[0];
		for(i = 0; i < select.options.length; i++){
			if(select.options[i].selected){
				ann_id = select.options[i].value;
				break;
			}
		}
		if(!ann_id){
			ann_id = 0;
		}
	}catch(e){
		var ann_id = 0;
	}
	postString = "title=" + fixPlus(title)
				+ "&content=" + fixPlus(content)
				+ "&ann_id=" + ann_id 
				+ "&user_id=" + user_id 
				+ "&profile_id=" + profile_id;
	request("./interfaces/quickMusSave.php", function (){
			handleQuickMusSave(user_id, profile_id);
		}, 
		postString
	);
}
function handleQuickMusSave(user_id, profile_id){
	document.getElementById("quick_menu_wrapper").innerHTML = request.text;
	setTempQuickMenuMessage("Your changes have been saved.");
	if(profile_id == user_id){
		if(isPage('musings')){
			window.location = PATH_HTTP + '/musings.php?uid=' + user_id;
		}else if(isPage('annoyances')){
			window.location = PATH_HTTP + '/annoyances.php?uid=' + user_id;
		}else if(isPage('comments')){
			//
		}else{
			reloadProfileMusings(user_id, profile_id);
		}
	}
}
function saveQ2(){
	var user_id = document.getElementsByName("q2_current_user_id")[0].getAttribute("value");
	var profile_id = document.getElementsByName("q2_current_profile_id")[0].getAttribute("value");
	var title = document.getElementsByName("q2_mus_title")[0].value;
	var mus_content = document.getElementsByName("q2_mus_content")[0].value;
	var ann_content = document.getElementsByName("q2_ann_content")[0].value;
	if(!title.length > 0){
		setTempQuickMenuMessage("Your musing had no title!");
		return;
	}else if(!ann_content.length > 0){
		setTempQuickMenuMessage("Your annoyance was empty!");
		return;
	}else if(!mus_content.length > 0){
		setTempQuickMenuMessage("Your musing was empty!");
		return;
	}
	postString = "user_id=" + user_id
				+ "&profile_id=" + profile_id
				+ "&title=" + fixPlus(title)
				+ "&mus_content=" + fixPlus(mus_content)
				+ "&ann_content=" + fixPlus(ann_content);
	request("./interfaces/quickSaveQ2.php",
		function(){
			document.getElementById("quick_menu_wrapper").innerHTML = request.text;
			setTempQuickMenuMessage("Your changes have been saved.");				
			if(profile_id == user_id){				
				if(isPage('musings')){
					window.location = PATH_HTTP + '/musings.php?uid=' + user_id;
				}else if(isPage('annoyances')){
					window.location = PATH_HTTP + '/annoyances.php?uid=' + user_id;
				}else if(isPage('comments')){
					//
				}else{
					reloadProfileMusings(user_id, profile_id);
					window.setTimeout(function(){
							reloadProfileAnnoyances(user_id, profile_id);
						},
						1000
					)
				}
			}
		},
		postString
	);
}
