var nbParPage = 50;
var OVC_delay = false;

//List of current waiting videos
var OVC_listAT = false;
//List of current downloading videos
var OVC_listEC = false;
//List of current available songs
var OVC_listAV = false;

var OVC_matchesURL = /^http:\/\/(www.)?(youtube).com\/watch\?v=([a-zA-Z0-9_]+)/i;

/*
 * Pages
 */
function OVC_add()
{
	if (OVC_delay)
		clearTimeout(OVC_delay);

	$('contenu').innerHTML = "";
	var titre = document.createElement("h2");
	titre.innerHTML = "Ajout d'une vidéo à convertir";
	$('contenu').appendChild(titre);

	OVC_addScreen(false);
}

function OVC_addScreen(modif)
{
	formulaire = document.createElement("form");
    formulaire.id = "add"
    formulaire.onsubmit = function(){ checkAndAdd(modif); return false; }
      lab_link = document.createElement("label");
      lab_link.innerHTML = "Lien :";
      lab_link.setAttribute("for", "link");
      inp_link = document.createElement("input");
      if(modif)
	  	inp_link.value = OVC_listEC[modif-1][1];
      inp_link.type = "text";
      inp_link.size = "34";
      inp_link.maxLength = "250";
      inp_link.id = "link";

      lab_form = document.createElement("label");
      lab_form.innerHTML = "Convertir en :";
      lab_form.setAttribute("for", "format");
      sel_form = document.createElement("select");
      sel_form.id = "format";
        sel_form_1 = document.createElement("option");
        sel_form_1.value = "1";
        sel_form_1.innerHTML = "Musique MP3";
        sel_form_2 = document.createElement("option");
        sel_form_2.value = "2";
        sel_form_2.innerHTML = "Musique OGG";
        sel_form_3 = document.createElement("option");
        sel_form_3.value = "3";
        sel_form_3.innerHTML = "Musique AAC";
        sel_form_4 = document.createElement("option");
        sel_form_4.value = "4";
        sel_form_4.innerHTML = "Vidéo MPEG";
        sel_form_5 = document.createElement("option");
        sel_form_5.value = "5";
        sel_form_5.innerHTML = "Vidéo MP4";
        sel_form_6 = document.createElement("option");
        sel_form_6.value = "6";
        sel_form_6.innerHTML = "Vidéo FLV";

      inp_submit = document.createElement("input");
      inp_submit.type = "submit";
      inp_submit.id = "submit";
      if (modif)
	  	inp_submit.value = "Modifier";
      else
	  	inp_submit.value = "Ajouter";
      inp_submit.style.marginLeft = "165px";

      formulaire.appendChild(lab_link);
      formulaire.appendChild(inp_link);
      formulaire.appendChild(document.createElement("br"));
      formulaire.appendChild(lab_form);
        sel_form.appendChild(sel_form_1);
        sel_form.appendChild(sel_form_2);
        sel_form.appendChild(sel_form_3);
        sel_form.appendChild(sel_form_4);
        sel_form.appendChild(sel_form_5);
        sel_form.appendChild(sel_form_6);
      formulaire.appendChild(sel_form);
      formulaire.appendChild(document.createElement("br"));
      if(modif)
	  	sel_quan.value = GSPC_liste[modif-1][4];
      formulaire.appendChild(document.createElement("br"));
      formulaire.appendChild(inp_submit);
      if (modif)
      {
		  inp_del = document.createElement("input");
		  inp_del.type = "button";
		  inp_del.value = "Supprimer";
		  inp_del.onclick = function() { GSPC_del(modif); };
		  inp_del.style.marginLeft = "15px";
		  formulaire.appendChild(inp_del);
      }
    $('contenu').appendChild(formulaire);
    $('link').focus();

    confirmation = document.createElement("h3");
    confirmation.style.color = "orange";
    confirmation.id = "confirm";
    $('contenu').appendChild(confirmation);
}

function OVC_lists()
{
	if (OVC_delay)
		clearTimeout(OVC_delay);

	window.scrollTo(0,0);

	$('contenu').innerHTML = "";
	var titre = document.createElement("h2");
	titre.innerHTML = "Vidéos en cours de téléchargement";
	$('contenu').appendChild(titre);

	OVC_liste = false;

	OVC_viewlisteEC();

	var titre = document.createElement("h2");
	titre.innerHTML = "Vidéos en attente";
	$('contenu').appendChild(titre);

	OVC_viewlisteAT();
}

function GSPC_viewliste()
{

}

/*
 * Internal functions
 */
function checkAndAdd(modif)
{
    if(modif)
	modif = "&id=" + OVC_listAT[modif-1][0];
    else
	modif = "";

    if (!OVC_matchesURL.test($('link').value))
    {
	$('link').className = "erreur";
	alert('Le lien que vous avez indiqué n\'est pas valide !');
	$('link').focus();
    }
    else
    {
	var compile = OVC_matchesURL.exec($('link').value);
	alert('Yeah !\n\t- Service : ' + compile[2] + "\n\t- ID : " + compile[3]);

	$('link').disabled = true;
	$('format').disabled = true;
	$('submit').disabled = true;

	new Ajax.Request(
	    'ajax.php?d=action&a=OVC' + modif,
	    {
		method: 'post',
		parameters: {service: compile[2], id: compile[3], format: $('format').value},
		onSuccess: function(transport, json)
		{
		    if (json.statut != 1)
		    {
			firstLoad();
			alert("Vous avez été déconnecté. La vidéo n'a pas été ajoutée à la liste.");
		    }
		    else
		    {
			OVC_loadList();
			OVC_lists();
		    }
		},
		onFailure: function() { printEtat(3); }
	    }
	);
    }
}
