$(document).ready(function()
{
  delete_type = "";
  
	InitScroll = true;
	Selection_UpdateSpan();
	
	$("a.selection-delete-step1").show();
	$("a.selection-delete-step2").hide();
	$("a.selection-delete-step3").hide();
	
	$("a.selection-delete-step1").click(function(){
		$("a.selection-delete-step1").hide();
  	$("a.selection-delete-step2").show();
  	$("a.selection-delete-step3").hide();
	});
	$("a.selection-delete-step2").click(function(){
	  delete_type = $(this).attr("rel");
	  
		$("a.selection-delete-step1").hide();
  	$("a.selection-delete-step2").hide();
  	$("a.selection-delete-step3").show();
	});
	$("a.selection-delete-step3").click(function(){
		v = $(this).attr("rel");
		if (v == "oui") Selection_EraseAll(delete_type);
		
		$("a.selection-delete-step1").show();
  	$("a.selection-delete-step2").hide();
  	$("a.selection-delete-step3").hide();
	});
});

function Selection_GetUrlFacebook() {
  window.open(POPUP_URL + '/action-facebook.html?content=' + Selection_GetCheckedMedia());
}
function Selection_GetUrlTwitter() {
  window.open(POPUP_URL + '/action-twitter.html?content=' + Selection_GetCheckedMedia());
}
function Selection_GetUrlEmail() {
  location.href = POPUP_URL + '/action-email.html?content=' + Selection_GetCheckedMedia();
}

function Selection_UpdateSpan()
{
	var aDetail, film_id, au_id;
	
	var NbFilms = Selection_GetMediaCount();
	var NbFilmsChecked = Selection_GetMediaCheckedCount();
	
  $("#selection-count").html(NbFilms);
  if ($("#selection-checked-count").length) $("#selection-checked-count").html(NbFilmsChecked);
	
	if (NbFilms == 0)
	{
	  if ($("#alert-selection").length > 0)
	  {
		  $("#cmd-check").hide();
		  $(".cmd-bottom").hide();
		  $("#scroll-selection").hide();
		  if (js_lang == 'fr') $("#alert-selection").html('<div class="left">Vous n\'avez sélectionné aucun film.<br />Pour ajouter un film à votre sélection cliquez<br />sur le bouton <span class="colorSelection">«Ajouter à ma sélection»</span><br />figurant sur les pages de lecture des films<br /><br /><img src="images/alert.png" alt="Selection" /><br /><br />Depuis votre page <span class="colorSelection">Ma sélection</span> vous<br />pourrez partager vos films sélectionnés<br />par e-mail, sur facebook et twitter.<br /><br />De même vous pouvez partager n\'importe<br />qu\'elle page en utilisant le bouton<br /><span class="colorSelection">«Partager cette page»</span></div>');
			else $("#alert-selection").html('<div class="left">You have not selected any movie.<br />To add a movie to your selection click<br />on the button <span class="colorSelection">«Add to my selection»</span><br />contained on the pages of movie playback<br /><br /><img src="images/alert.png" alt="Selection" /><br /><br />From your page <span class="colorSelection">My selection</span> you<br />can share your selected movies<br />by email, on facebook and twitter.<br /><br />Also you can share it any<br />page by using the <br /><span class="colorSelection">«Share this page»</span></div>');
	  }
	}
	else
	{
	  if ($("#selection-count2").length) {
			if (NbFilms == 0) $("#selection-count2").html((js_lang == "fr" ? "Aucun film" : "No movie"));
			else if (NbFilms == 1) $("#selection-count2").html((js_lang == "fr" ? "Mon film" : "My movie"));
			else if (NbFilms > 1) $("#selection-count2").html((js_lang == "fr" ? "Mon " + NbFilms + " films" : "My " + NbFilms + " movies"));
		}

	  if ($("#selection-count2-info").length) {
			if (NbFilms == 0) $("#selection-count2-info").html((js_lang == "fr" ? "Aucun film" : "No movie"));
			else if (NbFilms == 1) $("#selection-count2-info").html((js_lang == "fr" ? "Mon film" : "My movie"));
			else if (NbFilms > 1) $("#selection-count2-info").html((js_lang == "fr" ? "Mon " + NbFilms + " films" : "My " + NbFilms + " movies"));
		}

	  if ($("#selection-checked-count2").length) {
			if (NbFilmsChecked == 0) $("#selection-checked-count2").html((js_lang == "fr" ? "Aucun film coché" : "No movie checked"));
			else if (NbFilmsChecked == 1) $("#selection-checked-count2").html((js_lang == "fr" ? "Partager le film coché" : "Share the checked movie"));
			else if (NbFilmsChecked > 1) $("#selection-checked-count2").html((js_lang == "fr" ? "Partager les " + NbFilmsChecked + " films cochés" : "Share the " + NbFilmsChecked + " checked movies"));
		}
	}

	
  if ($("#selection-combo-ul").length) {
  	var container = document.getElementById("selection-combo-ul");
		while(container.hasChildNodes()) container.removeChild(container.childNodes[0]);
	  var sContent = Selection_GetMedia();
	  if (sContent != "")
	  {
			aContent = sContent.split("|");
			for (var i=aContent.length-1; i>=0; i--)
			{
			  aDetail = Selection_GetMediaDetail(aContent[i]);
				film_id = aDetail[0];
			  au_id = aDetail[2];

				new_element = document.createElement('li');
		    new_element.innerHTML = '<a href="' + POPUP_URL + '/wizz-selection/' + au_id + '/a/' + film_id + '/b.html">' + film_id + '</a>';
				new_element.setAttribute("id", "combo-li-" + film_id);
				container.insertBefore(new_element, container.firstChild);
			}
		}
	}
}

function Selection_SetValue(sValue, UpdateAffichage)
{
  var expDate = new Date();
  expDate.setTime(expDate.getTime() + (360 * 24 * 3600 * 1000));
	document.cookie = "WizzSelection=" + sValue + ";path=/films/;domain=wizz.fr;expires=" + expDate.toGMTString();
	//document.cookie = "WizzSelection=" + sValue + ";expires=" + expDate.toGMTString();

	Selection_UpdateSpan();
	if (UpdateAffichage) Selection_HtmlList();
}

function Selection_GetMediaCount()
{
	var sReturn = 0;
  var sContent = Selection_GetMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		sReturn = aContent.length;
	}
  return(sReturn);
}
function Selection_GetMediaCheckedCount()
{
	var sReturn = 0;
  var sContent = Selection_GetCheckedMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		sReturn = aContent.length;
	}
  return(sReturn);
}

function Selection_AddMedia(film_id, au_id)
{
  var debut = document.cookie.indexOf("WizzSelection=");

  if (debut >= 0)
	{
    var sContent = Selection_GetMedia();
    if (sContent.indexOf(String("[" + film_id + "]")) == -1)
		{
      sContent += (sContent == "" ? "" : "|") + "[" + film_id + "][C][" + au_id + "]";
      Selection_SetValue(sContent, true);
    }
		else
		{
      alert("deja la ...");
    }
  }
	else
	{
		sContent = "[" + film_id + "][C][" + au_id + "]";
    Selection_SetValue(sContent, true);
  }
}

function Selection_GetMedia()
{
	var sReturn = '';
	var fin, debut = document.cookie.indexOf("WizzSelection=");

  if (debut >= 0)
	{
    debut += String("WizzSelection").length + 1;
    fin = document.cookie.indexOf(";", debut);
    if (fin < 0) fin = document.cookie.length;
    sReturn = document.cookie.substring(debut, fin);
  }
  return(sReturn);
}

function Selection_GetCheckedMedia()
{
  var film_id, checked, aDetail, sChecked = '';
  var sContent = Selection_GetMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		for (var i=0; i<aContent.length; i++) {
		  aDetail = Selection_GetMediaDetail(aContent[i]);
			film_id = aDetail[0];
		  checked = aDetail[1];
		  if (checked == "C") sChecked += (sChecked == "" ? "" : "|") + film_id;
		}
	}
  return(sChecked);
}
function Selection_GetUncheckedMedia()
{
  var film_id, checked, aDetail, sChecked = '';
  var sContent = Selection_GetMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		for (var i=0; i<aContent.length; i++) {
		  aDetail = Selection_GetMediaDetail(aContent[i]);
			film_id = aDetail[0];
		  checked = aDetail[1];
		  if (checked != "C") sChecked += (sChecked == "" ? "" : "|") + film_id;
		}
	}
  return(sChecked);
}

function Selection_ExistMedia(id)
{
	var film_id, aDetail, bExist = false;
  var sContent = Selection_GetMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		for (var i=0; i<aContent.length; i++) {
		  aDetail = Selection_GetMediaDetail(aContent[i]);
			film_id = aDetail[0];
		  if (film_id == id) {
				bExist = true;
				break;
			}
		}
	}
	return(bExist);
}

function Selection_EraseMedia(id, isInSelection)
{
  //if (confirm("Confirmer ?"))
  //{
    var film_id, aDetail;
		var sContent = Selection_GetMedia();
    if (sContent != "")
    {
			aContent = sContent.split("|");
			for (var i=0; i<aContent.length; i++) {
			  aDetail = Selection_GetMediaDetail(aContent[i]);
				film_id = aDetail[0];
			  if (film_id == id) {
					aContent.splice(i, 1);
					break;
				}
			}
			Selection_SetValue(aContent.join("|"), true);
		}
		
		if (isInSelection) Selection_CheckScrollbar();
  //}
}

function Selection_EraseAll(type)
{
	var sList = "";
	
  if (type == "t") Selection_SetValue("", true);
  else if (type == "c") {
		sList = Selection_GetCheckedMedia();
		sList = sList.split("|");
		for (var i=0; i<sList.length; i++) {
			film_id = sList[i];
			Selection_EraseMedia(sList[i], true);
		}
	}
  else if (type == "nc") {
		sList = Selection_GetUncheckedMedia();
		sList = sList.split("|");
		for (var i=0; i<sList.length; i++) {
			film_id = sList[i];
			Selection_EraseMedia(sList[i], true);
		}
	}

	Selection_CheckScrollbar();
}

function Selection_CheckScrollbar()
{
	var nb = 0;
	nb = Selection_GetMediaCount();
	if (nb <= 3) $(".jScrollPaneTrack").hide();
}

function Selection_CheckAll()
{
  var film_id, au_id, aDetail;
	var sContent = Selection_GetMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		for (var i=0; i<aContent.length; i++) {
		  aDetail = Selection_GetMediaDetail(aContent[i]);
			film_id = aDetail[0];
		  au_id = aDetail[2];

			aContent[i] = "[" + film_id + "][C][" + au_id + "]";
			
			document.getElementById("checkbox-" + film_id).checked = true;
			icbImgUpdate(document.getElementById("icbcheckbox-" + film_id));
		}
		Selection_SetValue(aContent.join("|"), false);
	}
}
function Selection_UncheckAll()
{
  var film_id, au_id, aDetail;
	var sContent = Selection_GetMedia();
  if (sContent != "")
  {
		aContent = sContent.split("|");
		for (var i=0; i<aContent.length; i++) {
		  aDetail = Selection_GetMediaDetail(aContent[i]);
			film_id = aDetail[0];
		  au_id = aDetail[2];
		  
			aContent[i] = "[" + film_id + "][U][" + au_id + "]";
			
			document.getElementById("checkbox-" + film_id).checked = false;
			icbImgUpdate(document.getElementById("icbcheckbox-" + film_id));
		}
		Selection_SetValue(aContent.join("|"), false);
	}
}

function Selection_GetAjaxTitle(film_id, au_id)
{
	$.ajax({ type: "POST", url: POPUP_URL + "/ajax-info.php", data: "type=selection&lang=fr&md_id=" + film_id + "&au_id=" + au_id, success: function(sHtmlReturn) {
		sHtmlReturn = sHtmlReturn.split("[|]");
		$("#infos-selection-p-" + film_id).html('<div class="gris">' + sHtmlReturn[0] + '</div>' + sHtmlReturn[1]);
		$("#a-selection-" + film_id).attr("href", POPUP_URL + '/wizz-player-selection/' + au_id + '/' + sHtmlReturn[2] + '/' + film_id + '/' + sHtmlReturn[3] + '.html');
	}});
}

function Selection_HtmlList()
{
	var film_id, checked, au_id, aDetail;
	var container = document.getElementById("ul-niveau2-selection");
	
	if (container)
	{
		while(container.hasChildNodes()) container.removeChild(container.childNodes[0]);

	  var sContent = Selection_GetMedia();
	  if (sContent != "")
	  {
			aContent = sContent.split("|");
			for (var i=aContent.length-1; i>=0; i--)
			{
			  aDetail = Selection_GetMediaDetail(aContent[i]);
				film_id = aDetail[0];
			  checked = aDetail[1];
			  au_id = aDetail[2];
			  
			  s = '';
			  s += '<a id="a-selection-' + film_id + '" href="#" class="li-link">';
			  s += '<div class="corner-272-153"><div class="picto-play"></div></div><img src="/documents/medias169/' + film_id + '/' + film_id + '-272-153.jpg" class="img-over" width="272" height="153" />';
				s += '</a>';
				s += '<p id="infos-selection-p-' + film_id + '"></p>';
				s += '<div class="checkbox"><input type="checkbox" name="checkbox-' + film_id + '" id="checkbox-' + film_id + '" /></div>';
				if (js_lang == "fr") s += '<div class="delete"><a href="#" id="link-delete-' + film_id + '" rel="' + film_id + '" class="link-delete">Supp</a><a href="#" onClick="Selection_EraseMedia(' + film_id + ', true);" id="confirm-' + film_id + '">OUI&nbsp;&nbsp;</a><a href="#" onClick="Selection_NoConfirm(' + film_id + ');" id="noconfirm-' + film_id + '">&nbsp;&nbsp;NON</a></div>';
				else s += '<div class="delete"><a href="#" id="link-delete-' + film_id + '" rel="' + film_id + '" class="link-delete">Del</a><a href="#" onClick="Selection_EraseMedia(' + film_id + ', true);" id="confirm-' + film_id + '">YES&nbsp;&nbsp;</a><a href="#" onClick="Selection_NoConfirm(' + film_id + ');" id="noconfirm-' + film_id + '">&nbsp;&nbsp;NO</a></div>';

				new_element = document.createElement('li');
		    new_element.innerHTML = s;
				new_element.setAttribute("id", "li-" + film_id);
				//new_element.setAttribute("class", "scroll-interval2" + (i == (aContent.length-1) ? " last" : ""));
				new_element.setAttribute("class", "scroll-interval2" + (i == 0 ? " first" : ""));
				container.insertBefore(new_element, container.firstChild);

				icbCBoxtoImgCB('checkbox-' + film_id, (checked == "C" ? true : false), 'images/check.png', 'images/uncheck.png', 17, 17, true);
				
				Selection_NoConfirm(film_id);
				Selection_GetAjaxTitle(film_id, au_id);
			}
			
			$("a.link-delete").hover(
		   	function() { $(this).html((js_lang == "fr" ? "Supprimer" : "Delete")); },
		   	function() { $(this).html((js_lang == "fr" ? "Supp" : "Del")); }
			);

			$('a.link-delete').click(function(){
				$(this).hide();
				$("#confirm-" + $(this).attr("rel")).show();
				$("#noconfirm-" + $(this).attr("rel")).show();
			});
		}

		$("#ul-niveau2-selection").width((($("#ul-niveau2-selection li").length * (272 + 15))-15) + "px");
		
		if (InitScroll) {
			$('#scroll-selection').jScrollHorizontalPane({resize:true, scrollbarHeight:16, scrollbarMargin:0, wheelSpeed:30, animateStep:3, animateInterval:10, animateTo:true});
	  	InitScroll = false;
	 	} else {
	 		$('#scroll-selection').jScrollHorizontalPane.reset();
	 	}
	}
}

function Selection_NoConfirm(film_id)
{
	$("#link-delete-" + film_id).show();
	$("#confirm-" + film_id).hide();
	$("#noconfirm-" + film_id).hide();
}

function Selection_Update()
{
	var film_id, isChecked, au_id, aDetail;
	var aContent = Selection_GetMedia().split("|");
	for (var i=0; i<aContent.length; i++)
	{
	  aDetail = Selection_GetMediaDetail(aContent[i]);
		film_id = aDetail[0];
	  au_id = aDetail[2];
		isChecked = document.getElementById("checkbox-" + film_id).checked;
		aContent[i] = "[" + film_id + "][" + (isChecked ? "C" : "U") + "][" + au_id + "]";
	}
	Selection_SetValue(aContent.join("|"), false);
}

function Selection_GetMediaDetail(aContent)
{
	var film_id = aContent.split('][')[0].substring(1);
	var check = aContent.split('][')[1];
	var au_id = aContent.split('][')[2].substring(0, aContent.split('][')[2].length-1);
	return(Array(film_id, check, au_id));
}

function Selection_GetPrevNext(id)
{
	var film_id, au_id, aDetail;
	var aContent = Selection_GetMedia().split("|");
	var ready = false;
	var prev_id = 0, prev_au_id = 0, next_id = 0, next_au_id = 0;
	for (var i=0; i<aContent.length; i++)
	{
	  aDetail = Selection_GetMediaDetail(aContent[i]);
		film_id = aDetail[0];
	  au_id = aDetail[2];
	  if (ready) {
			next_id = film_id;
			next_au_id = au_id;
			break;
		}
	  if (id == film_id) ready = true;
	  else {
			prev_id = film_id;
			prev_au_id = au_id;
		}
	}
	
	$.ajax({ type: "POST", url: POPUP_URL + "/ajax-info.php", data: "type=selection&lang=fr&md_id=" + prev_id + "&au_id=" + prev_au_id, success: function(sHtmlReturn) {
		sHtmlReturn = sHtmlReturn.split("[|]");
		$("a.bt-prev").attr("href", POPUP_URL + "/wizz-selection/" + prev_au_id + "/" + sHtmlReturn[2] + "/" + prev_id + "/" + sHtmlReturn[3] + ".html");
	}});
	
	$.ajax({ type: "POST", url: POPUP_URL + "/ajax-info.php", data: "type=selection&lang=fr&md_id=" + next_id + "&au_id=" + next_au_id, success: function(sHtmlReturn) {
		sHtmlReturn = sHtmlReturn.split("[|]");
		$("a.bt-next").attr("href", POPUP_URL + "/wizz-selection/" + next_au_id + "/" + sHtmlReturn[2] + "/" + next_id + "/" + sHtmlReturn[3] + ".html");
		//$("#QtId").attr("qtnext1", "<" + POPUP_URL + "/wizz-selection/" + prev_au_id + "/" + sHtmlReturn[2] + "/" + prev_id + "/" + sHtmlReturn[3] + ".html> T<_parent>");
	}});
	
	return(Array(prev_id, prev_au_id, next_id, next_au_id));
}

function Selection_GetPlayerList()
{
	var film_id, au_id, aDetail, checked;
	var aContent = Selection_GetMedia().split("|");
	var aContentSend = new Array();
	for (var i=0; i<aContent.length; i++)
	{
	  aDetail = Selection_GetMediaDetail(aContent[i]);
		film_id = aDetail[0];
	  au_id = aDetail[2];
	  checked = aDetail[1];
	  if (checked == 'C') aContentSend.push(new Array(film_id, au_id));
	}
	
	return(aContentSend);
}
