// profile class

var profile = {
	init: function() {
		profile.setUserStars();
		profile.setStatusEditor();
		profile.setTabs();
		profile.setBioExpand();
		profile.setWishStars();
		profile.setFriendFinder();
	},
	
	// init setters
	setUserStars: function() {
		if (!document.getElementsByClassName('user-rank')[0]) { return; }
		var target = document.getElementsByClassName('user-rank')[0];
		new Starbox(target, target.id.split('-')[1],{
			overlay: 'big.png',
			locked: true
		});
	},
	setStatusEditor: function() {
		new Ajax.Request('/profile/ajax/owner_check', {
			method: 'post',
			parameters: {
				userName: window.location.pathname.split('/')[3]
			},
			tempStatus: null,
			onSuccess: function(t) {
				var returnCode = $H(t.responseText.evalJSON())._object;
				if (returnCode.success) {
					new Tip('status_update','Click to edit status',{
						style: 'darkgrey',
						width: 110,
						stem: 'topLeft'
					});
					new Ajax.InPlaceEditor( 'status_update', '/profile/ajax/status_update/',{
						okButton: false,
						onEnterEditMode: function(form) {
							profile.tempStatus = $('status_update').innerHTML.strip();
						},
						onComplete: function(t) {
							if (!t) {return;}
							var response = $H(t.responseText.evalJSON()).toObject();
							if (response.success) {
								$('status_update').update(response.mssg);
								$('status_date').update('Updated: ' + response.date);
							} else {
								$('status_update').update(profile.tempStatus);
								util.talkbox(t.responseText);
							}
						},
						onFailure: function(o,t) {
							_tools.ajaxBug(t);
						},
						savingText: 'Updating status...'
					});
				}
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	},
	setStatusDate: function() {
		alert('setting date');
	},
	setWishStars: function() {
		$A($$('.wish-score')).each(function(wishAt){
			new Starbox(wishAt, wishAt.id.split("_")[1],{
				locked: true,
				rated: true
			});
		})
	},
	
	// friend stuff
	addFriend: function(userID) {
		util.setSaving();
		new Ajax.Request('/profile/ajax/add_friend/', {
			method: 'post',
			parameters: {
				friendID: userID
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				$('wrapper').update(t.responseText);
			}
		});
	},
	friendSubmit: function() {
		new Ajax.Request('/profile/ajax/friend_submit/', {
			method: 'post',
			parameters: $('friendForm').serialize(),
			onSuccess: function(t) {
				if (t.responseText.indexOf('"header":') != -1) {
					util.talkbox(t.responseText);
				} else {
					$('talkTarget').update(t.responseText);
				}
				new Effect.Highlight('talkTarget');
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	},
	setFriendFinder: function() {
		if (!$('find_friends')) { return;}
		new Ajax.Autocompleter('find_friends', 'completer', '/profile/ajax/friend_finder/',{
			paramName: 'userName',
			minChars: 4,
			afterUpdateElement: profile.jumpToProfile
		});
	},
	jumpToProfile: function(input) {
		var profileURL = "/profile/view/" + input.value;
		$('friendFinder').setAttribute("action",profileURL);
	},

	//general util stuff
	postComment: function() {
		$('body').focus();
		new Effect.ScrollTo('body', {duration: .5});
	},
	setTabs: function() {
		$A($$('.tab-container')).each(function(contAt){
			$A(contAt.select('.tab')).each(function(tabAt){
				$(tabAt).onclick = function() {
					profile.showTab(tabAt);
				}
			});
		});
	},
	showTab: function(obj) {
		profile.hideTabs(obj.parentNode);
		$(obj).addClassName('on');
		$('content_' + obj.id.split("_")[1]).addClassName('show');
	},
	hideTabs: function(parent) {
		$A($(parent).select('.tab')).each(function(tabAt){
			$(tabAt).removeClassName('on');
		});
		$A(parent.select('.content')).each(function(contAt){
			$(contAt).removeClassName('show');
		});
	},
	setBioExpand: function() {
		if (!$$('.user-bio')[0]) {return;}
		var target = $$('.user-bio')[0];
		var linkCont = Builder.node('p',{className: 'show-bio'},[
			Builder.node('a',{href: '#', onclick: 'profile.toggleBio(this,\'down\'); return false;'}, 'show full bio')
		]);
		profile.bioDefaultHeight = target.getHeight();
		target.parentNode.appendChild(linkCont);
	},
	bioDefaultHeight: null,
	toggleBio: function(trigger,dir) {
		var target = $$('.user-bio')[0];
		switch (dir) {
			case "down":
				trigger.update('collapse bio');
				trigger.onclick = function() {
					profile.toggleBio(trigger,'up'); return false;
				}
				var newHeight = target.scrollHeight + 'px';
			break;
			
			case "up":
				trigger.update('show full bio');
				trigger.onclick = function() {
					profile.toggleBio(trigger,'down'); return false;
				}
				var newHeight = profile.bioDefaultHeight + 'px';
			break;
		}
		target.morph('height:' + newHeight);
	},

	//reporting stuff
	reportUser: function(userID) {
		if (!confirm('Are you sure that you want to report this user for something on their profile page?')) {return;}
		util.setSaving();
		new Ajax.Request('/report/ajax/profile/', {
			method: 'post',
			parameters: {
				userID: userID,
				objectID: '666',
				url: window.location.href
			},
			onSuccess: function(t) {
				util.talkbox(t.responseText);
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	}
}

document.observe("dom:loaded",profile.init);