﻿function TwlPlaylist()
{

	this.NewPlayList;
	this.IdPlayList;
	this.IdPlaylistCurrent;
	this.NomPlayList;
	this.CommentVideo;
	this.CallGetPlaylists = false;
	this.WidthPlayer;
	this.HeightPlayer;
	

	var AxAddVideoToPlayList = {
		delay: 200,
		prepare: function() { 
			InputVar = new Array(myTwlAuth.SecurityKey,myTwlPlaylist.NewPlayList,myTwlPlaylist.IdPlayList,myTwlVideo.IdVideo,myTwlAuth.MemberId,myTwlPlaylist.NomPlayList,myTwlPlaylist.CommentVideo);
		},
		call: proxies.AjaxFunc.AddVideoToPlaylist,
		finish: function (p) 
		{ 
			
			if (p != "KO")
			{
				alert("La vidéo a été rajoutée à votre playlist");
				if (myTwlPlaylist.NewPlayList == "TRUE")
					appendToList(document.getElementById("twl_lst_playlist"),myTwlPlaylist.NomPlayList,p)
			}
			else
				alert("La vidéo existe déjà dans la playlist sélectionnée");
				
				
		},
		onException: ajax.alertException
	}
	
	var AxGetInfoVideoFromPlaylist = {
		delay: 200,
		prepare: function() { 
			InputVar = new Array(myTwlPlaylist.IdPlaylistCurrent,myTwlVideo.IdVideo);
		},
		call: proxies.AjaxFunc.GetInfoVideoFromPlaylist,
		finish: function (p) 
		{ 
			myTwlPlaylist.ExecLaunchVideoById(p);
		},
		onException: ajax.alertException
	}


	var AxGetListePlaylistByUsager = {
		delay: 200,
		prepare: function() { 
			InputVar = new Array(myTwlAuth.MemberId);
		},
		call: proxies.AjaxFunc.GetListePlaylistByUsager,
		finish: function (p) 
		{ 
			
			document.getElementById("TwlPlaylistList").style.display = "";
			
			myTwlPlaylist.CallGetPlaylists = true;
			var ComboDest = document.getElementById("twl_lst_playlist");
			ComboDest.options.length = 0;
			var Item = new Option("Choisissez une playlist", "", false, false);
			ComboDest.options[0] = Item;
			
			if (p != "NO")
			{
				var LstPl = p.split("|MPMR|");
				
				for (var i=0; i<LstPl.length; i++) {
					
					if (LstPl[i] != "")
					{
						var APlayList = LstPl[i].split("|MPMF|"); 
						
						if (APlayList[0] != "")
						{				
							var j = i +1;
							Item = new Option(APlayList[1], APlayList[0], false, false);
							ComboDest.options[j] = Item;	
						}
					}
				}
			}
				
		},
		onException: ajax.alertException
	}
	
	var AxAddPlaylistToFavoris = {
		delay: 200,
		prepare: function() { 
			InputVar = new Array(myTwlAuth.SecurityKey,myTwlPlaylist.IdPlaylistCurrent,myTwlAuth.MemberId);
		},
		call: proxies.AjaxFunc.AddPlaylistToFavoris,
		finish: function (p) 
		{ 
			document.getElementById("TwlPlaylistFavoris").className = "favoris on";
			document.getElementById("TwlPlaylistFavoris").href = "javascript:myTwlPlaylist.AlreadyInFavoris()";
		},
		onException: ajax.alertException
	}



	this.AddToPlayList = function ()
	{
		if (myTwlAuth.NoMember)
		{
			
			document.getElementById("TwlPlaylistList").style.display = "none";
			document.getElementById("TwlLoginFixed").style.display = "";
			myTwlAuth.CallBack = function () { myTwlPlaylist.GetListePlaylistByUsager(); };
			
			
		}
		else
		{
			
			if (!myTwlPlaylist.CallGetPlaylists)
				myTwlPlaylist.GetListePlaylistByUsager();
			else
				myTwlVideo.DisplayTwlContent('TwlPlaylistList','playlist',document.getElementById("TwlPlaylistListLink"));
		}
	}
	
	this.GetListePlaylistByUsager = function ()
	{

		myTwlVideo.DisplayTwlContent('TwlPlaylistList','playlist',document.getElementById("TwlPlaylistListLink"));
		SoapMethod = "POST";
		ajax.Start(AxGetListePlaylistByUsager);
		
	}
	
	this.ExecAddToPlayList = function ()
	{
		myTwlPlaylist.NewPlayList = "FALSE";
		myTwlPlaylist.NomPlayList = document.getElementById("twl_nom_playlist").value;
		myTwlPlaylist.CommentVideo = "";
		myTwlPlaylist.IdPlayList = document.getElementById("twl_lst_playlist").options[document.getElementById("twl_lst_playlist").selectedIndex].value;
		if (myTwlPlaylist.NomPlayList == "" && myTwlPlaylist.IdPlayList == "")
		{
			alert("Vous devez choisir une playlist ou en créer une nouvelle");
			return false;
		}
		if (myTwlPlaylist.NomPlayList != "")
			myTwlPlaylist.NewPlayList = "TRUE";
		SoapMethod = "POST";
		ajax.Start(AxAddVideoToPlayList);
	}
	
	this.LaunchVideoById = function (aIdVideo)
	{
		document.getElementById("TwlVideo" + myTwlVideo.IdVideo).className = "item";
		myTwlVideo.IdVideo = aIdVideo ;
		SoapMethod = "POST";
		ajax.Start(AxGetInfoVideoFromPlaylist);
	}
	
	this.ExecLaunchVideoById = function (resp)
	{
		var tableau = resp.split("||");
		var VideoTitre = tableau[0];
		var VideoFlv = tableau[2];
		var VideoUrl = tableau[3];
		var VideoDescr= tableau[4];
		var NbDiggsM= tableau[5];
		var NbDiggsF= tableau[6];
		var so = new SWFObject('/flash/pv5.1/player-licensed.swf','mpl',myTwlPlaylist.WidthPlayer,myTwlPlaylist.HeightPlayer,'0');
		so.addParam('allowfullscreen','true');
		so.addParam('wmode','transparent');
		so.addParam('quality','high');
		so.addVariable("autostart", "true");
		so.addVariable("controlbar", "bottom");
		so.addVariable("logo.hide", "true");
		so.addVariable("displayclick", "false");
		so.addVariable("abouttext", "zeoVid Player");
		so.addVariable("aboutlink", "http://www.zeovid.com");
		so.addVariable("file", VideoFlv);
		so.addVariable("skin", "/flash/skins/modieus/modieus.swf");
		so.write('TwlPlayer');
		document.getElementById("TwlVideo" + myTwlVideo.IdVideo).className = "item selected";
		document.getElementById("TwlTitreVideo").innerHTML = "<b>Titre  : </b>" + VideoTitre;
		document.getElementById("TwlDescrVideo").innerHTML = "<b>Description  : </b>" + VideoDescr;
		document.getElementById("TwlUrlVideo").innerHTML = '<b>Lien permanent : </b><a href="' + myTwl.url_base + '/video/' + VideoUrl + '">' + myTwl.url_base + '/video/' + VideoUrl + '</a>';
		document.getElementById("TwlTdDiggM").innerHTML =  '<a class="digg_m" id="TwlDiggM" href="javascript:myTwlDigg.AddDiggVideo(\'M\')">' + NbDiggsM + '<br>vote(s)</a>';
		document.getElementById("TwlTdDiggF").innerHTML =  '<a class="digg_f" id="TwlDiggF" href="javascript:myTwlDigg.AddDiggVideo(\'F\')">' + NbDiggsF + '<br>vote(s)</a>';
	}
	
	this.AddToPlaylistToFavoris = function ()
	{
		if (myTwlAuth.NoMember)
		{
			document.getElementById("TwlPlaylistList").style.display = "none";
			document.getElementById("TwlLoginFixed").style.display = "";
			myTwlAuth.CallBack = function () { myTwlPlaylist.AddToPlaylistToFavoris(); };
			
			
		}
		else
		{
			SoapMethod = "POST";
			ajax.Start(AxAddPlaylistToFavoris);
		}
	}
	
	this.AlreadyInFavoris = function ()
	{
		alert("Vous êtes déjà abonné à cette playlist.");
	}
	
	
	function appendToList(theSel, newText, newValue)
	{
	  if (theSel.length == 0) {
		var newOpt1 = new Option(newText, newValue);
		theSel.options[0] = newOpt1;
		theSel.selectedIndex = 0;
	  } else if (theSel.selectedIndex != -1) {
		var selText = new Array();
		var selValues = new Array();
		var selIsSel = new Array();
		var newCount = -1;
		var newSelected = -1;
		var i;
		for(i=0; i<theSel.length; i++)
		{
		  newCount++;
		  selText[newCount] = theSel.options[i].text;
		  selValues[newCount] = theSel.options[i].value;
		  selIsSel[newCount] = theSel.options[i].selected;
	      
		  if (newCount == theSel.selectedIndex) {
			newCount++;
			selText[newCount] = newText;
			selValues[newCount] = newValue;
			selIsSel[newCount] = false;
			newSelected = newCount - 1;
		  }
		}
		for(i=0; i<=newCount; i++)
		{
		  var newOpt = new Option(selText[i], selValues[i]);
		  theSel.options[i] = newOpt;
		  theSel.options[i].selected = selIsSel[i];
		}
	  }
	}


	
}

var myTwlPlaylist = new TwlPlaylist();







