function addBrand(brand_id) { new Ajax.Request(ROOT+"index.php?user&action=add_brand&brand_id="+brand_id, {asynchronous:true, evalScripts:true}); }
function removeBrand(brand_id) { new Ajax.Request(ROOT+"index.php?user&action=remove_brand&brand_id="+brand_id, {asynchronous:true, evalScripts:true}); }
function refreshBrandList() { new Ajax.Request(ROOT+"index.php?user&action=refresh_brand_list", {asynchronous:true, evalScripts:true}); }
function refreshAvatar() { new Ajax.Request(ROOT+"index.php?user&action=refresh_avatar", {asynchronous:true, evalScripts:true}); }
function uploadAvatar() { new Ajax.Request(ROOT+"index.php?user&action=upload_avatar", {asynchronous:true, evalScripts:true}); }
function removeAvatar() { new Ajax.Request(ROOT+"index.php?user&action=remove_avatar", {asynchronous:true, evalScripts:true}); }

function refreshBrandPeople (brand_id) { new Ajax.Request(ROOT+"index.php?board&action=refresh_brand_people&brand_id="+brand_id, {asynchronous:true, evalScripts:true}); }
function refreshUserPeople (user_id) { new Ajax.Request(ROOT+"index.php?board&action=refresh_user_people&user_id="+user_id, {asynchronous:true, evalScripts:true}); }

function randomEntourage() {
	 new Ajax.Request(ROOT+"index.php?communities&action=random_entourage", {asynchronous:true, evalScripts:true});
}

function openDashboard() {
	new Ajax.Request(ROOT+"index.php?board&action=dashboard", {asynchronous:true, evalScripts:true});	
}


var buzzlist_type = 0;
var buzzlist_id = 0;
var buzzlist_page = 0;
function refreshBuzzListType(type,ele) {
	var elements = $$('#buzz_list_tabs li a');
	elements.each(function(el) {
		el.removeClassName("current");
	});
	ele.addClassName("current");
	
	buzzlist_type = type;
	loadBuzzList();
}
function refreshBuzzListFirst(to_id) { buzzlist_id = to_id; buzzlist_page = 0; loadBuzzList(); }
function refreshBuzzList(to_id, page) { buzzlist_id = to_id; buzzlist_page = page; loadBuzzList(); }

function loadBuzzList() {
	new Ajax.Request(ROOT+"index.php?board&action=refresh_buzz_list&id="+buzzlist_id+"&page="+buzzlist_page+"&for="+buzz_list_for+"&type="+buzzlist_type, {asynchronous:true, evalScripts:true}); 
}

function refreshQuestions(to_id,page) {
	new Ajax.Request(ROOT+"index.php?board&action=refresh_questions&id="+to_id+"&page="+page, {asynchronous:true, evalScripts:true}); 
}
function refreshQuestionsById(to_id,id) {
	new Ajax.Request(ROOT+"index.php?board&action=refresh_questions&id="+to_id+"&question_id="+id, {asynchronous:true, evalScripts:true}); 
}


function reply_to (user_id,name) {
	$('buzz_reply_to').value = user_id;
	$$('#buzz_reply_to_div span')[0].innerHTML = name;
	$('buzz_reply_to_div').style.display = '';
}
function cancel_reply_to() {
	$('buzz_reply_to').value = '';
	$('buzz_reply_to_div').style.display = 'none';
}

function youYube() {
	new Ajax.Request(ROOT+"index.php?user&action=add_youtube", {asynchronous:true, evalScripts:true}); 
}

function youYubeReply(id) {
	new Ajax.Request(ROOT+"index.php?user&action=add_youtube&reply_to="+id, {asynchronous:true, evalScripts:true}); 
}

function attachPhoto() {
	new Ajax.Request(ROOT+"index.php?user&action=attach_photo", {asynchronous:true, evalScripts:true}); 
}

function attachPhotoReply (id) {
	new Ajax.Request(ROOT+"index.php?user&action=attach_photo&reply_to="+id, {asynchronous:true, evalScripts:true}); 
}

function attachPhotoComplete(path,pathcomplete) {
	attachPhotoCompleteReply(path,pathcomplete,-1);
}

function attachPhotoCompleteReply(path,pathcomplete,reply_to) {
	Notice.hide();
	if(reply_to == -1) {
		$('buzz_photo').value = path;
		$$('#attached_photo img')[0].src = pathcomplete;
		$('attached_photo').style.display = '';		
	}
	else {
		$('buzz_photo_'+reply_to).value = path;
		$$('#attached_photo_'+reply_to+' img')[0].src = pathcomplete;
		$('attached_photo_'+reply_to).style.display = '';
	}
}

function showSubCategories(category, id) {
    if(!$('subcategories_'+id)) return;
	if($('subcategories_'+id).childNodes.length == 1) {
		$('subcategories').style.display = "none";
	}
	else {
		$('subcategories').firstChild.firstChild.innerHTML = $('subcategories_'+id).innerHTML;
		var tmp = Position.cumulativeOffset(category);
		$('subcategories').style.left = (tmp[0] + 100)+"px";
		$('subcategories').style.top = (tmp[1] - 18)+"px";
		$('subcategories').style.display = "";
		
	}
}
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

/* friends */
function addUser(user_id) { new Ajax.Request(ROOT+"index.php?user&action=add_user&user_id="+user_id, {asynchronous:true, evalScripts:true}); }
function removeUser(user_id) { new Ajax.Request(ROOT+"index.php?user&action=remove_user&user_id="+user_id, {asynchronous:true, evalScripts:true}); }


/* create */
function start_upload() {
	$('upload_image').appendChild($('another_photo'));
	$('upload_image').insertBefore($('another_photo'),$('upload_image').firstChild);
	
	var form = new IFrameForm('upload_image',null);
	form.submit();
	
	$('input_image').innerHTML = 'Uploading...';
}
function create_upload_finish(filename) {
	$('create_image_preview').innerHTML = '<img src="'+filename+'" alt="" />';
	$('upload_image').innerHTML = '';
	$('create_image_uploaded').checked = true;
	$('input_image').innerHTML = '<input onchange="javascript:start_upload();" type="file" name="another_photo" id="another_photo" />';
}


/* upload dialog box */
function start_upload_form () {
	var upload_dialog = new IFrameForm('upload_dialog_form',null);
	upload_dialog.submit();
	$('upload_dialog_form').style.display = "none";
	$('upload_dialog_form_loading').style.display = "";
	upload_dialog.onSubmit = function() {
		new Effect.Fade("upload_form", {duration: 0.5, afterFinish:function() {
			new Effect.Appear("upload_progress", {duration: 0.5});
		}}); 
	}
	upload_dialog_complete = function() {
		Notice.hide();
	}

}



/* brand / join */
function brand_check_join(brand_id) {
	var loc = window.location.toString();
	var pos = loc.strpos('#') + 1;
	switch(loc.substring(pos)) {
		case 'join':
			new Ajax.Request(ROOT+"index.php?user&action=popup_join_brand&brand_id="+brand_id, {asynchronous:true, evalScripts:true});
			break;
	}
}


/* load widget */
function open_widget(query_string) {
	new Ajax.Request(ROOT+"index.php?board&action=load_widget&query_string="+query_string.encode(),{asynchronous:true, evalScripts:true});
}

/* load video */
function open_video(url) {
	new Ajax.Request(ROOT+"index.php?board&action=load_video&url="+url.encode(),{asynchronous:true, evalScripts:true});
}

function trimLength(textarea, maxChars)
{
	if(textarea.value.length <= maxChars) return;

	textarea.value = textarea.value.substr(0, maxChars)
}

function canAddCharacter(textarea, maxChars)
{
	if(typeof(textarea.onkeypress.arguments[0]) != 'undefined')
		var keyCode = textarea.onkeypress.arguments[0].keyCode;
	else
	{
		if(document.selection.createRange().text.length != 0) return true;
		var keyCode = event.keyCode;
	}

	var allowedChars = new Array(8, 37, 38, 39, 40, 46);	//Backspace, delete and arrow keys
	for(var x=0; x<allowedChars.length; x++) if(allowedChars[x] == keyCode) return true;

	if(textarea.value.length < maxChars) return true;

	return false;
}