// Checks to see if the groups permissions is checked, to show mygroups.
checkVisibility = function(){
	if($('groups')){
		if($('groups').checked){
			$('my_groups').toggle();
		}
		if($('rss_groups')){
			if($('rss_groups').checked){
				$('rss_my_groups').toggle();
			}
		}
	}
}
function checkPerm(box){
	if($(box).checked == false){
		var perm = 'rss_'+box.id;
		if(!$(perm)){
			return false;
		}
		if($(perm).checked == true){
			var decision = confirm("You still have the RSS permissions set to share.  The objects permissions and RSS permissions must be the same.\n\n*-Click Ok if you want to change the permissions of the RSS\n\n*-Click Cancel if you do not want to unshare the object.");
			if(decision)
				$(perm).checked = false;
			else
				$(box).checked = true;
		}
	}
	//checkVisibility();
}
/**
	This will check to see if the RSS permissions match up with the
	permissions of the object.
*/
function checkRSSPerm(box){
	if($(box).checked == true){
		var perm = box.id.split("_");
		if($(perm[1]).checked == false){
			var decision = confirm("To share this feed with " + perm[1] +", the permissions for the object must be the same as the rss permissions.\n\n*-Click Ok if you want to change the permissions and share this object.\n\n*-Click Cancel if you do not want to change the permissions and share this object.");
			if(decision){
				$(perm[1]).checked = true;
			}
			else{
				$(box).checked = false;
			}
		}
	}
	//checkVisibility();
}

function fillIn(e){
	var tags = $('tags').value.split(',');
	tags.pop();
	tags.push(e.innerHTML);
	$('tags').value = tags.join(', ') + ',';
}

// Initialize
function init(){
	new Ajax.Autocompleter('tags', 'autocomplete_choices', 'search_tags.php', {tokens:",",updateElement:fillIn});
	tagToolTip();
	//document.onkeydown = KeyCheck;
}

function permissionsToolTip(){
	if($('help_permissions')){
		new Tip('help_permissions', 'Permissions allow you to determine your level of privacy. Check the appropriate boxes if you want your profile information visible to others.',{title:'Permissions Help'});
	}
}
function tagToolTip(){
	if($('help_tags')){
		new Tip('help_tags', 'Tags are keywords that help you find people or groups with similar interests.  These tags also help people find you and the files or resources you want to share.  Put in any tag you would like and as many as you would like, and change them as often as you want. Tags are separated by a comma; for example, food, fitness, nutrition, sustainability, local food, etc.',{title:'Tags Help'});
	}
}

function replaceYouTube(v){
	if (swfobject.hasFlashPlayerVersion("7.0.0")) {
			swfobject.embedSWF("http://www.youtube.com/v/"+v+"&autoplay=1", 'youtube', '425', '355', '9.0.0', '<?=SITE?>/includes/expressInstall.swf');
	}
	else {
 			$('youtube').innerHTML = '<p class="error">Flash Player needed.</p><p>Please visit <a href="http://www.adobe.com/go/getflashplayer">http://www.adobe.com/go/getflashplayer</a>'+
 								' to get the latest version of Flash</p>';
	}
}
	
// Search based on description.
function searchDesc(){
	// Have to replace plus signs, or it does not perform the search right.
	var desc = $('description').value.replace(/\+/g, "%2b");
	new Ajax.Updater('search_results', 'functions.php', {postBody:'action=searchDesc&description='+desc});
}

function searchName(){
	new Ajax.Updater('search_results', 'functions.php', {postBody:'action=searchName&name='+$F('name')});
}
// After you click on a tag in the search cloud, the values of the search of that tag
// will be placed in search results.
function searchTagCloud(tag){
	new Ajax.Updater('search_results', 'functions.php', {postBody:'action=searchTags&tags='+encodeURIComponent(tag.innerHTML)});
}

// Search tags in the tag box.
function searchTags(){
	new Ajax.Updater('search_results', 'functions.php', {postBody:'action=searchTags&tags='+$('tags').value});
}

// This is used in every place that has permissions.  There is a hidden value with the
// total value of all the permissions.  This sums up all the values of the checked
// permissions and puts them in the hidden field 'permissions'.
function sumPermissions(){
	var permissions = $('permissions_div').getElementsByTagName('input');
	var total = 0;
	for(var i=0; i < permissions.length; i++){
		if(permissions[i].checked)
			total += permissions[i].value * 1;
	}
	$('permissions').value = total;
	if($('rss_permissions')){
		var permissions = $('rss_permissions_div').getElementsByTagName('input');
		var total = 0;
		for(var i=0; i < permissions.length; i++){
			if(permissions[i].checked)
				total += permissions[i].value * 1;
		}
		$('rss_permissions').value = total;
	}
}

function uploadComplete(o){
	var str = o.responseText;
	var temp = new Array();
	temp = str.split(":");
	if(temp[0] == "Problem"){
		switch(temp[1]){ 
			case '1':
				$('pic_status').innerHTML = 'Not a valid image';
				break;
			case '3':
				$('pic_status').innerHTML = 'File too large.  Must be under 4MB';
				break;
			case '4':
				$('pic_status').innerHTML = 'File too large.  Must be under 4MB';
				break;
		}
			
	}
	else{
		$('pic_status').innerHTML = '<div id="pic_status" class="link" onclick="removeImage()">Remove Image</div>';
	}
	ext = temp[1].split('.');
	$('profile_image').src = temp[1] + '?'+Math.random();
	if($('pic_id')){
		fileSplit = temp[1].split('/');
		fileName = fileSplit.pop();
		fileID = fileName.split('_');
		$('pic_id').value = fileID[0];
	}
	
	$('anim').style.display = 'none';
}

// What to do when gets the callback from Ajax.
var callback =
{
	upload: uploadComplete,
	failure: function(o) {alert(o)},
	timeout:0
}

function getMod(){
	if($F('uploadedfile') == '')
		return 0;
	$('anim').style.display = 'inline';
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '../includes/upload_image.php', callback);
	var formObject = document.getElementById('aForm');
	YAHOO.util.Connect.setForm(formObject, true, true); 
}

// Allows you to see the groups in a select box or not.  This is also used with permissions.
// If the groups permission is checked, all the groups that you belong to will be shown.
function viewGroups(what){
	if(what == 0)
		$('my_groups').toggle();
	else
		$('rss_my_groups').toggle();
}