ITV.profile = function() {

	if(!window.location.hash.length) {
		if (!window.history.replaceState) {
			window.location.hash = "feed"
		} else {
			$("li.feed").addClass("selected")
			window.history.replaceState("", "", "#feed");
		} 
	} 
	             
	//load behavior
	$("li." + window.location.hash.replace("#","")).addClass("selected")
	$("article." + window.location.hash.replace("#","")).addClass("selected")

	var $follow = $("a.follow")
	$follow.live("click", follow)
	
	//selector caching
	var $post = $("#wallPost")
	var $news = $("ul.news")
	var $recs = $("ul.recommendations")
	var $nav = $("section.nav nav ul li")
	var $content = $("section.content")
	var $contentArticles = $("section.content article")
	var $messages = $("span.message")
	var $followers = $('article.followers ul')
	var $following = $('article.following ul')

	var $permissions = $('ul.permissions input')


	var linkreg = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/gi

	// do stuff	
	followers()
	following()

	$messages.each(function(i){
		var $message = $($messages[i])
		var str = $message.html()
		//evaluate the string for a link
		var matches = str.match(linkreg)
		if(matches) {
			$.embedly(matches[0], {maxWidth: 400, key: ITV.embedly}, function(oembed, dict) {
				$message.append($(oembed.code))
			})
		}
	})

	

	$nav.click(function(e){

		var $currentTarget = $(e.currentTarget)

		if(!$currentTarget.hasClass("selected")) {
			var $currentContent = $("article." + $currentTarget.attr("class"))  //brittle

			//hide em all
			$nav.toggleClass("selected", false) // DRY UP
			$contentArticles.toggleClass("selected", false)

			//show the one
			$currentTarget.toggleClass("selected", true)
			$currentContent.toggleClass("selected", true)

		}
		
	})

	$permissions.click(function(e){
		var url = ITV.urls.permission.replace(":permission", e.currentTarget.name).replace(":grantOrRevoke", (e.currentTarget.checked) ? "grant" : "revoke") //because true and false get turned into strings
		$.ajax({
			url: url,
			type: "PUT",
			data: {
				userId: ITV.user.uuid
			}
		})
	})

	if (!ITV.isMe) return
	$recs.fetchUserRecs()
	$news.fetchNewsForUser()
	$post.setupWallPost()

	function follow(e) {
		
		e.preventDefault()

		if (!ITV.loggedIn) return
		
		var isFollowed = $(this).hasClass("isFollowed")
		var $self = $(this)
		
		$(this).toggleClass("isFollowed")

		$.ajax({
			url: ITV.urls.follow,
			type: isFollowed ? "DELETE" : "PUT",
			data: {
				userId: ITV.profileUserId
			},
			success: function() {
				$self.text(isFollowed ? "Follow" : "Unfollow")
			},
			error: function(e) {
				// alert("Could not modify favorites!")
			}
		})
	}

	function followers() {
		var url = ITV.urls.followers.replace(":url/", (ITV.isMe) ? "" : ITV.url+"/")
		$.get(url, function(data){
			$followers.html(data)
		})
	}

	function following() {
		var url = ITV.urls.following.replace(":url/", (ITV.isMe) ? "" : ITV.url+"/")
		$.get(url, function(data){
			$following.html(data)
		})
	}
}

// the website settings stuff
// used to reside in settings.js
// now they're in the trashcan
// because they didn't work
