var selectedAvatar = "";
var playlistTab_State = 0;
var videoTab_State = 0;


function addFriend(friend_id) {

	var url = '/ajax/buildAddFriendMenu.html';
	var pars = '&user_id=' + uid +'&friend_user_id='+ fuid;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {
				window.location.href=json.redirect;
			} else {
				var contentHeight = parseInt(json.content_height);
				$('popup_container').style.display = "";
				$('popup_container').style.top = parseInt(posY - 100) + 'px';
				$('popup_background_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.bottom = parseInt(contentHeight + 15) + 'px';
				$('popup_content').innerHTML = json.content;
				$('popup_content_title').innerHTML = json.content_title;	
			}
		}
	});
	
}

function removeFriend(friend_id) {
	var url = '/ajax/removeFriend.html';
	var pars = '&user_id='+ uid +'&friend_user_id='+ fuid;
	
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {
				window.location.href=json.redirect;
			} else {
				var contentHeight = parseInt(json.content_height);
				$('popup_container').style.display = "";
				$('popup_container').style.top = parseInt(posY - 100) + 'px';
				$('popup_background_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.bottom = parseInt(contentHeight + 15) + 'px';
				$('popup_content').innerHTML = json.content;
				$('popup_content_title').innerHTML = json.content_title;	
			}
		}
	});
}

function removeFriendConfirm(friend_id) {
	var url = '/ajax/removeFriendConfirm.html';
	var pars = '&user_id='+ uid +'&friend_user_id='+ fuid;
	
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {
				window.location.href=json.redirect;
			} else {
				var contentHeight = parseInt(json.content_height);
				$('popup_container').style.display = "";
				$('popup_container').style.top = parseInt(posY - 100) + 'px';
				$('popup_background_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.bottom = parseInt(contentHeight + 15) + 'px';
				$('popup_content').innerHTML = json.content;
				$('popup_content_title').innerHTML = json.content_title;	
			}
		}
	});
}

function groupCheck(sel) {
	var selected = sel.options[sel.selectedIndex];
	
	if (selected.value == "new") {
		switchGroupSelect('new');
	} else {
		switchGroupSelect('list');
	}
}

function switchGroupSelect(selgroup) {
	if (selgroup == "new") {
		$('new_friend_group').style.display = "";
		$('friends_group_select').style.display = "none";
	} else {
		$('new_friend_group').style.display = "none";
		$('friends_group_select').style.display = "";
	}
}

function hidePopup() {
	$('popup_container').style.display = 'none';
	$('popup_content').innerHTML = '';
	$('popup_content_title').innerHTML = '';
}

function addFriendSubmit(friend_id) {
	var url = '/ajax/addFriend.html';
	var pars = $('addFriend').serialize(true);
	
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {		
				alert(json.message);
				return;
			} else {
				var contentHeight = parseInt(json.content_height);
				$('popup_container').style.display = "";
				$('popup_container').style.top = parseInt(posY - 100) + 'px';
				$('popup_background_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.bottom = parseInt(contentHeight + 15) + 'px';
				$('popup_content').innerHTML = json.content;
				$('popup_content_title').innerHTML = json.content_title;	
			}

		}
	});
}

function updateAvatarMenu() {
	var url = '/ajax/updateAvatarMenu.html';
	var myAjax = new Ajax.Request(url, {method: 'post', onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {
				window.location.href=json.redirect;
			} else {	
				var contentHeight = parseInt(json.content_height);
				$('popup_container').style.display = "";
				$('popup_container').style.top = parseInt(posY - 100) + 'px';
				$('popup_background_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.height = contentHeight + 'px';
				$('popup_content_container').style.bottom = parseInt(contentHeight + 15) + 'px';
				$('popup_content').innerHTML = json.content;
				$('popup_content_title').innerHTML = json.content_title;
				if ((json.selectedAvatar != undefined) && (json.selectedAvatar != '')) {
					selectedAvatar = json.selectedAvatar;
					$('thumbnail_'+ selectedAvatar).style.border = "2px solid red";
				}
			}
		}
	});
}

function updateUserAvatar() {
	var url = '/ajax/updateAvatar.html';
	var pars = $('update_profile_avatar').serialize(true);
	
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == false) {		
				alert(json.message);
				return;
			} else {
				
				$('profile_avatar').src = json.avatar_url;
				hidePopup();	
			}

		}
	});
}


function setSelectedAvatar(imgID) {
	if (imgID == "" || imgID == "undefined") {
		return (0);
	}
	if (selectedAvatar != "") {
		$('thumbnail_'+ selectedAvatar).style.border = "0px";
	} 
	if (selectedAvatar != imgID) {
		selectedAvatar = imgID;
		$('thumbnail_'+ selectedAvatar).style.border = "2px solid red";
		$('avatar_photo_id').value = imgID;
	}
}

function initUserVideos() {
	var url = '/ajax/initProfileVideos.html';
	var pars = 'user_id='+ fuid;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function (obj, json) {
			var json = obj.responseText.evalJSON(true);
			$('profileVideosContainer').innerHTML = json.content;
			// Get a list of users video
			for(i=0; i<json.videos.length; i++) {
				videoList.vid[json.videos[i].video_id] = json.videos[i].video_id;
				videoList.pid[json.videos[i].video_id] = json.videos[i].video_platform_id;
				videoList.title[json.videos[i].video_id] = json.videos[i].title;
				videoList.rating[json.videos[i].video_id] = json.videos[i].stats.video_rating;
				videoList.num_rated[json.videos[i].video_id] = json.videos[i].stats.number_rated;
				videoList.video_owner[json.videos[i].video_id] = json.videos[i].username;
				videoList.profile_link[json.videos[i].video_id] = null;
				videoList.category[json.videos[i].video_id] = null;
			}
			for (i=0; i<json.playlist.length; i++) {
				Playlist.pid[json.playlist[i].video_id] = json.playlist[i].video_platform_id;
				Playlist.title[json.playlist[i].video_id] = json.playlist[i].title;
				Playlist.rating[json.playlist[i].video_id] = json.playlist[i].stats.video_rating;
				Playlist.num_rated[json.playlist[i].video_id] = json.playlist[i].stats.number_rated;
				Playlist.vid[json.playlist[i].video_id] = json.playlist[i].video_id;
			//	Playlist.profile_link[json.playlist[i].video_id] = json.playlist[i].userprofile_link;
				Playlist.video_owner[json.playlist[i].video_id] = json.playlist[i].username;
				Playlist.positions[i] = json.playlist[i].video_id;
			}
			
			if (json.playlist.length > 0) {
				Playlist.Id = json.playlist[0].playlist_id;	
				if (uid == fuid) {
					Sortable.create('playlistItemContainer', {overlap: 'vertical', constraint: false,  onUpdate: function () {
							var CN = $('playlistItemContainer').childNodes;
							for (i=0; i<CN.length; i++) {
								var itemId = CN[i].id;
								var videoId = itemId.replace(/^playlistItems_/g,"");
								Playlist.positions[i] = videoId;
								//Playlist.positions[i] = CN[i].id;
							}
						}
					});	
				}
			}
			
			if (json.currentView == "videos") {
				if (json.videos.length > 0) {
					video.totalRating = json.videos[0].stats.video_rating;
					video.numRatings = json.videos[0].stats.number_rated;
					//video.SwfUrl = '/coastPlayer/vid_'+ json.playlist[0].video_platform_id;
					video.SwfUrl = '/coastPlayer/vid_'+ json.videos[0].video_platform_id;
					video.Id = json.videos[0].video_id;
					videoList.current = json.videos[0].video_id;
					$('videoTitle').innerHTML = videoList.title[video.Id];
					// clear video_owner_link 
					$('video_owner_link').innerHTML = "";
					var profileLink = document.createElement("a");
					profileLink.href = videoList.profile_link[video.Id];
					profileLink.appendChild(document.createTextNode(videoList.video_owner[video.Id]));
					$('video_owner_link').appendChild(profileLink);
				}
			} else {
				if (json.playlist.length > 0) {
					video.totalRating = json.playlist[0].stats.video_rating;
					video.numRatings = json.playlist[0].stats.number_rated;
					//video.SwfUrl = '/coastPlayer/vid_'+ json.playlist[0].video_platform_id;
					video.SwfUrl = '/coastPlayer/vid_'+ json.playlist[0].video_platform_id;
					video.Id = json.playlist[0].video_id;
					Playlist.Id = json.playlist[0].playlist_id;
					Playlist.current = json.playlist[0].video_id;
					$('videoTitle').innerHTML = Playlist.title[video.Id];
					// clear video_owner_link 
					$('video_owner_link').innerHTML = "";
					var profileLink = document.createElement("a");
					profileLink.href = Playlist.profile_link[video.Id];
					profileLink.appendChild(document.createTextNode(Playlist.video_owner[video.Id]));
					$('video_owner_link').appendChild(profileLink);	
					
					
				}
			}
			currentView = json.currentView;
			loadPlayer();
			initVideoRating();
			Event.observe('userVideosTab', 'click', selectVideolist);
			Event.observe('profilePlaylistTab', 'click', selectPlaylist);
			animateVideoTabs();
			//setTimeout("thisPlayer.addViewListener('LOAD', 'onLoaded')", 3000);	
			
		}
	});
}

function selectPlaylist(e) {

	currentView = "playlist";
	animateVideoTabs();

}

function selectVideolist(e) {
	currentView = "videos";
	animateVideoTabs();

}

function animateVideoTabs() {
//	currentView = (currentView != undefined) ? currentView : "videos";
//	if (currentView == "videos") {
		if (videoTab_State == 1) {
			Effect.BlindUp('userVideosItemsContainer', { duration : 0.4 });
			Effect.BlindDown('profilePlaylistItemsContainer', { duration : 0.4 });
			videoTab_State = 0;
			playlistTab_State = 1;
		
		} else {	
			playlistTab_State = 0;
			videoTab_State = 1;
			Effect.BlindDown('userVideosItemsContainer', { duration : 0.4});
			Effect.BlindUp('profilePlaylistItemsContainer', { duration : 0.4 });
		} 
/*
	} else {
		if (playlistTab_State == 1) {
			Effect.BlindUp('profilePlaylistItemsContainer', { duration : 0.4 });
			Effect.BlindDown('userVideosItemsContainer', { duration : 0.4});

			videoTab_State = 1;
			playlistTab_State = 0;
			currentView = 'videos';		
		} else {
			Effect.BlindUp('userVideosItemsContainer', { duration : 0.4 });
			Effect.BlindDown('profilePlaylistItemsContainer', { duration : 0.4 });

			videoTab_State = 0;
			playlistTab_State = 1;		
		}
*/
//	}

}

function onPlay(obj) {
	
	if (previousView != "") {
		switch(previousView) {
			case 'playlist':
				$('playlistItems_'+ Playlist.previous).removeClassName('selectedVideo');
				break;
			default:
				
				$('videoItems_'+ videoList.previous).removeClassName('selectedVideo');
				break;	
		}
	}
	
	if (currentView != 'videos') {
		$('playlistItems_'+ Playlist.current).addClassName('selectedVideo');
		Playlist.previous = Playlist.current;	
	} else {
		$('videoItems_'+ videoList.current).addClassName('selectedVideo');	
		videoList.previous = videoList.current;
	}
	
	previousView = currentView;
}

function paginationUserMedia(currPage, sortCol, sortDir, mediaType, theDiv) {
	var url = '/ajax/mediaPagination';
	var pars = 'mediaType='+ mediaType + '&userId='+ fuid +'&categoryId=&currPage='+ currPage +'&sortCol='+ sortCol +'&sortDir='+ sortDir +'&divCont='+ theDiv;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);

			$('pageLink_'+ theDiv).innerHTML = json.pagination_links;
			$(theDiv).innerHTML = json.media;
		}
	});
}

