/* Apercu d'un thème */
function apercu_theme(id_theme,id_user,id_photo){
	var tab_pars=new Array();
	tab_pars.push("id_user=" + id_user);
	tab_pars.push("id_theme=" + id_theme);
	tab_pars.push("id_photo=" + id_photo);
	var pars = encodeURI(tab_pars.join('&'));
	var myAjax = new Ajax.Request('/requestfromajax/apercu_theme.php', {method: 'post',postBody: pars, onSuccess: affiche_theme, onFailure: affiche_theme_failure} );
}

function affiche_theme(request){
	var xml = request.responseXML.documentElement;
	var result = xml.getElementsByTagName('photo');
	var name = xml.getElementsByTagName('name');
	name = name[0].firstChild.nodeValue;
	var id_user = xml.getElementsByTagName('id_user');
	id_user = id_user[0].firstChild.nodeValue;
	var id_theme = xml.getElementsByTagName('id_theme');
	id_theme = id_theme[0].firstChild.nodeValue;
	var nb_star = xml.getElementsByTagName('nb_star');
	nb_star = nb_star[0].firstChild.nodeValue;
	var avg_star = xml.getElementsByTagName('avg_star');
	avg_star = avg_star[0].firstChild.nodeValue;
	var nb_vote = xml.getElementsByTagName('nb_vote');
	nb_vote = nb_vote[0].firstChild.nodeValue;
	if(result.length>0){
		nav_photo="";
		nav_photo="<table><tr>";
		for(i=0;i<result.length;i++){
			id_file=result[i].childNodes[1].firstChild.nodeValue;
			id_photo=result[i].childNodes[0].firstChild.nodeValue;
			nav_photo=nav_photo+"<td valign=\"middle\" id=\"photo_"+id_photo+"\"><a href=\"#photo_view\" onclick=\"javascript:apercu_photo("+id_photo+","+id_user+")\"><img src=\"/photo/tn/"+id_file+".jpg\" class=\"nav_photo\"/></a></td>";
		}
		nav_photo=nav_photo+"</tr></table>";
		$('scroll_apercu').innerHTML=nav_photo;
		$('scroll_apercu').scrollLeft=0;
		$('nb_star').innerHTML="<img src=\"/common_img/bio/stars_rouge.gif\" alt=\"nombre d'etoiles\" style=\"display:inline;margin-left:10px;\"/>"+nb_star+"&nbsp;";
		$('avg_star').innerHTML=" ( moy : "+avg_star+" )";
		$('nb_vote').innerHTML=" étoiles sur "+nb_vote+" votes";
		var id_photo_url = xml.getElementsByTagName('id_photo_url');
		id_photo_url = id_photo_url[0].firstChild.nodeValue;
		if(id_photo_url!="0"){
			id_photo=id_photo_url;
		}
		else{
			id_photo=result[0].childNodes[0].firstChild.nodeValue;
		}

		apercu_photo(id_photo,id_user);
		if(type_user!=''){
			x=(screen.availWidth-800)/2;
			y=(screen.availHeight-635)/2;
			$('load_player').innerHTML="<a href=\"#\" onclick=\"javascript:window.open('/photo_player/player.php?&id_user="+id_user+"&id_theme="+id_theme+"','video_player','width=800px,height=635px,top="+y+",left="+x+"');\"><img src=\"/common_img/picto/picto_player_photo.gif\"/></a>";
		}
		else{
			$('load_player').innerHTML="<a href=\"javascript:player_ext_alert();\"><img src=\"/common_img/picto/picto_player_photo.gif\"/></a>";
		}
	}
	$('target_theme_name').innerHTML=html_entities_decode(unescape(name));
	for(i=0;i<id_themes.length;i++){
		$('fond_theme_'+id_themes[i]).style.backgroundColor="#EEE";
	}
	$('fond_theme_'+id_theme).style.backgroundColor="#CCC";
}

function affiche_theme_failure(){
	return false;
}

function apercu_photo(id_photo,id_user){
	$('apercu_photo').innerHTML="<img src=\"/common_img/popUp/searching.gif\" class=\"view_photo_load\"/>";
	var tab_pars=new Array();
	tab_pars.push("id_photo=" + id_photo);
	tab_pars.push("id_user=" + id_user);
	var pars = encodeURI(tab_pars.join('&'));
	var myAjax = new Ajax.Request('/requestfromajax/apercu_photo.php', {method: 'post',postBody: pars, onSuccess: affiche_photo, onFailure: affiche_photo_failure} );
}

function affiche_photo(request){
	var xml = request.responseXML.documentElement;
	var name = xml.getElementsByTagName('name');
	name = name[0].firstChild.nodeValue;
	var id_file = xml.getElementsByTagName('id_file');
	id_file = id_file[0].firstChild.nodeValue;
	var id_photo = xml.getElementsByTagName('id_photo');
	id_photo = id_photo[0].firstChild.nodeValue;
	$('name_photo').innerHTML=html_entities_decode(unescape(name));
	$('apercu_photo').innerHTML="<img src=\"/photo/"+id_file+".jpg\" class=\"view_photo\"/>";
	var noeuds = $('scroll_apercu').firstChild.firstChild.firstChild.firstChild;
	while (noeuds!=null) {
	  noeuds.style.border="1px solid #CCC";
	  noeuds = noeuds.nextSibling;
	}
	$('photo_'+id_photo).style.border="2px solid #CC3333";
	
	var is_favourite = xml.getElementsByTagName('is_favourite');
	is_favourite = is_favourite[0].firstChild.nodeValue;
	if(is_favourite=="true"){
		var url_simple = xml.getElementsByTagName('url_simple');
		url_simple = html_entities_decode(unescape(url_simple[0].firstChild.nodeValue));
		favourite="<a href=\""+url_simple+"\">Voir dans ma séléction<img src=\"/common_img/fiche_artiste/etoile_galerie.gif\"/></a>";
	}
	else{
		favourite="<a href=\"javascript:add_playlist("+id_photo+");\"><img src=\"/common_img/picto/picto_ajouter_favoris.gif\"/>Ajouter à ma galerie</a>";
	}
	$('add_favourite').innerHTML=favourite;
	
	var photo_vote="<ul onmouseout=\"stoprate_photo("+id_photo+");\"><a href=\"javascript:photorate("+id_photo+",1);\" onmouseover=\"prerate_photo("+id_photo+",1);\"></a><li><a href=\"javascript:photorate("+id_photo+",1);\" onmouseover=\"prerate_photo("+id_photo+",1);\"><img id=\"rate_1\" src=\"/common_img/bio/stars_grise.gif\" alt=\"Voter pour cet artiste\" title=\"Voter pour cet artiste\"></a></li><a href=\"javascript:photorate("+id_photo+",2);\" onmouseover=\"prerate_photo("+id_photo+",2);\"></a><li><a href=\"javascript:photorate("+id_photo+",2);\" onmouseover=\"prerate_photo("+id_photo+",2);\"><img id=\"rate_2\" src=\"/common_img/bio/stars_grise.gif\" alt=\"Voter pour cet artiste\" title=\"Voter pour cet artiste\"></a></li><a href=\"javascript:photorate("+id_photo+",3);\" onmouseover=\"prerate_photo("+id_photo+",3);\"></a><li><a href=\"javascript:photorate("+id_photo+",3);\" onmouseover=\"prerate_photo("+id_photo+",3);\"><img id=\"rate_3\" src=\"/common_img/bio/stars_grise.gif\" alt=\"Voter pour cet artiste\" title=\"Voter pour cet artiste\"></a></li><a href=\"javascript:photorate("+id_photo+",4);\" onmouseover=\"prerate_photo("+id_photo+",4);\"></a><li><a href=\"javascript:photorate("+id_photo+",4);\" onmouseover=\"prerate_photo("+id_photo+",4);\"><img id=\"rate_4\" src=\"/common_img/bio/stars_grise.gif\" alt=\"Voter pour cet artiste\" title=\"Voter pour cet artiste\"></a></li><a href=\"javascript:photorate("+id_photo+",5);\" onmouseover=\"prerate_photo("+id_photo+",5);\"></a><li><a href=\"javascript:photorate("+id_photo+",5);\" onmouseover=\"prerate_photo("+id_photo+",5);\"><img id=\"rate_5\" src=\"/common_img/bio/stars_grise.gif\" alt=\"Voter pour cet artiste\" title=\"Voter pour cet artiste\"></a></li></ul><img id=\"ratetext_"+id_photo+"\" src=\"/common_img/bio/bt_votez.gif\" alt=\"votez\" style=\"display: inline;\">";
	getphotorate(id_photo);
	$('photo_vote').innerHTML=photo_vote;
	
	var sum_rate = xml.getElementsByTagName('sum_rate');
	sum_rate = sum_rate[0].firstChild.nodeValue;
	var count_rate = xml.getElementsByTagName('count_rate');
	count_rate = count_rate[0].firstChild.nodeValue;
	var average_rate = xml.getElementsByTagName('average_rate');
	average_rate = average_rate[0].firstChild.nodeValue;
	
	$('rate').innerHTML = "<span class=\"textBoldRouge\" id=\"sum_rating\">"+sum_rate+"</span> étoiles sur <span id=\"count_rating\">"+count_rate+"</span> votes (moy : <span id=\"avg_rating\">"+average_rate+"</span>)<br/>";
}

function affiche_photo_failure(){
	$('apercu_photo').innerHTML="<img src=\"/common_img/popUp/searching.gif\"/>";
	return false;
}

function add_playlist(id_media){
		var tab_pars=new Array();
		tab_pars.push("id_media=" + id_media);
		var pars = encodeURI(tab_pars.join('&'));
		var myAjax = new Ajax.Request('/common_rfa/add_playlist.php', {method: 'post',postBody: pars, onSuccess: add_playlist_success, onFailure: add_playlist_failure} );
}
/* Fonction dupliqué en enlevant le is_connect car problème depuis le flash. */
function add_playlist_flash(id_media){
	var tab_pars=new Array();
	tab_pars.push("id_media=" + id_media);
	var pars = encodeURI(tab_pars.join('&'));
	var myAjax = new Ajax.Request('/requestfromajax/add_playlist.php', {method: 'post',postBody: pars, onSuccess: add_playlist_success, onFailure: add_playlist_failure} );
}

function add_playlist_success(request){
	var xml = request.responseXML.documentElement;
	
	var url_simple = xml.getElementsByTagName('url_simple');
  url_simple = html_entities_decode(unescape(url_simple[0].firstChild.nodeValue));
  var id_media = xml.getElementsByTagName('id_media');
  id_media = html_entities_decode(unescape(id_media[0].firstChild.nodeValue));
  if($('add_favourite'))
  {
	  $('add_favourite').innerHTML="<a href=\""+url_simple+"/?tab=community\"><img src=\"/common_img/fiche_artiste/etoile_galerie.gif\"/></a>";
  }
  if($('add_favourite_'+id_media))
	{
  	$('add_favourite_'+id_media).innerHTML="<a href=\""+url_simple+"/?tab=community\"><img src=\"/common_img/fiche_artiste/etoile_galerie.gif\"/></a>";
  }
	return true;
}

function add_playlist_failure(){
	return false;
}
