var previousView = "";
var currentPlaylist = null;

function Playlist() {
	this.vid = new Array();
	this.pid = new Array();
	this.title = new Array();
	this.rating = new Array();
	this.video_owner = new Array();
	this.category = new Array();
	this.profile_link = new Array();
	this.num_rated = new Array();
	this.current = 0;
	this.previous = 0;
	this.positions = new Array();
	this.Id = 0;
	
}

function addToPlaylist(videoId) {
	var url = '/ajax/editPlaylist.html';
	var pars = 'video_id='+ videoId;

	
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj,json) {
			var json = obj.responseText.evalJSON(true);
			
			Effect.BlindDown('playListBox', { duration : 0.4 });
			eval("$('addToPlaylist').onclick = function () {};");
			endColor = (json.success == true) ? '#D9E129' : '#E41B23';
			new Effect.Highlight('playListBox', { startcolor: '#CACACA', endcolor: endColor });
			$('playListBoxMessage').innerHTML = json.content;
			setTimeout("resetPlaylistBox('"+ videoId +"')", 6000);
		}
	});
}

function resetPlaylistBox(videoId) {
	Effect.BlindUp('playListBox', { duration : 2.0});
	eval("$('addToPlaylist').onclick = function () { addToPlaylist('"+ videoId +"'); };");
}

function initPlaylist() {
	Playlist.current = video.Id;
	loadPlayer();
	initVideoRating();
	getPlaylist(Playlist.Id);
}

function removeItemFromPlaylist(videoId) {
	if (videoId == undefined) { return; }
	var url = '/ajax/removeVideoFromPlaylist.html';
	var pars = 'playlist_id='+ Playlist.Id +'&videoId='+ videoId;
	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);			
			} else {
				Effect.Fade('playlistItems_'+ videoId, {duration: 1.0});
				setTimeout('$("playlistItems_'+ videoId +'").remove()', 1000);
				var pvIndex = Playlist.position.indexOf(videoId);
				Playlist.position.splice(pvIndex, 1);
	
			}
		}
	});
}

function sortPlaylist() {
	var url = '/ajax/sortPlaylistItems.html';
	var pars = 'playlistId='+ Playlist.Id + '&';
	pars += Sortable.serialize('playlistItemContainer', {name: 'videoId'});
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj, json) {
			var json = obj.responseText.evalJSON(true);
			if (json.success == true) {
				
				Playlist.vid = new Array();
				Playlist.pid = new Array();
				Playlist.title = new Array();
				Playlist.rating = new Array();
				Playlist.video_owner = new Array();
				Playlist.category = new Array();
				Playlist.profile_link = new Array();
				Playlist.num_rated = new Array();
				Playlist.current = 0;
				Playlist.previous = 0;
				Playlist.positions = new Array();
				Playlist.Id = 0;
				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;
				}
				new Effect.Highlight('watch', { startcolor: '#DFCC00', endcolor: '#000000' });
			}
		}
	});	
}

function getPlaylist(playlistId) {
	var url = '/ajax/getPlaylist.html';
	var pars = 'playlist_id='+ Playlist.Id;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj, json) {
			var json = obj.responseText.evalJSON(true);
			
			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;
				
			}
		}
	});
	
	
}

function viewPlaylist(playlistId) {
	var url = '/ajax/lightPlaylistPlayer.html';
	var pars = 'playlist_id='+ playlistId;
	var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onComplete: function(obj, json) {
			var json = obj.responseText.evalJSON(true);
			var playerLoaded = null;
			$('lightViewContainer').innerHTML = json.content;
			
			
			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;
				
			}
			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 = playlistId;
			Playlist.current = json.playlist[0].video_id;
			loadPlayer();
			initVideoRating();
			Lightview.show({
				href: '#lightViewContainer',
				rel: 'inline',
	  			title: '',
				options: {
					autosize: true,
					topclose: true
				}
	
			});
			setTimeout('playSelection('+ json.playlist[0].video_id +');',5000);
			
			
		}
	});	
}

function playSelection(pIndex) {
	vidList = (currentView == 'playlist') ? Playlist : videoList;
	vidList.previous = vidList.current;
	if (vidList.current != pIndex) {
		thisPlayer.sendEvent('STOP');
		swfobject.removeSWF('movie1');
		video.SwfUrl = '/coastPlayer/vid_'+ vidList.pid[pIndex];
		loadPlayer();
	} 
	vidList.current = pIndex;	
	
	
	$('videoTitle').innerHTML = vidList.title[pIndex];
	// clear video_owner_link 
	$('video_owner_link').innerHTML = "";
	var profileLink = document.createElement("a");
	profileLink.href = vidList.profile_link[pIndex];
	profileLink.appendChild(document.createTextNode(vidList.video_owner[pIndex]));
	$('video_owner_link').appendChild(profileLink);

	video.totalRating = vidList.rating[pIndex];
	video.numRatings = vidList.num_rated[pIndex];
	video.Id = pIndex;
	$('videoRater').reloadRating();
	setTimeout('thisPlayer.sendEvent("PLAY")', 3000);
	
	if (currentView == "playlist") {
		Playlist = vidList;	
	} else {
		videoList = vidList;	
	}
	

}

function labelSelection(pIndex) {
	$('playlistSelectionTitle').innerHTML = 'Click to watch '+ Playlist.title[pIndex];	
}

function clearLabel() {
	$('playlistSelectionTitle').innerHTML = '&nbsp;';
}

function getPlaylistData() { 
	var plst = null;
	plst = thisPlayer.getPlaylist();

	if (plst) { 
		currentPlaylist = plst; 

	}	
}
