var width=0; // pixels
var margin=10; //pixels

function initWidth(w){
	width=w-margin;
}

// OnLoad
$(document).ready(function () {
	// Main init
	main_init();
	initOrientation();
});

/** on ajoute le .mt pour ne pas prendre en compte la topnews */
function initOrientation(){
	var orientation = window.orientation;
	switch(orientation){
	case 0:
	case 180:
		$(".titledcontainer .mt").find(".dtt-landscape").css('display', 'none');
		break;
	case 90:
	case -90:
		$(".titledcontainer .mt").find(".dtt").css('display', 'none');
		break;
	default:
		$(".titledcontainer .mt").find(".dtt-landscape").css('display', 'none');
	}
}

window.onorientationchange = function() {
	var orientation = window.orientation;
	switch(orientation){
	case 0:
	case 180:
		$(".titledcontainer .mt").find(".dtt").css('display', 'block');
		$(".titledcontainer .mt").find(".dtt-landscape").css('display', 'none');
		break;
	case 90:
	case -90:
		$(".titledcontainer .mt").find(".dtt").css('display', 'none');
		$(".titledcontainer .mt").find(".dtt-landscape").css('display', 'block');
		break;
	}
}


/**
 * Main init
 */
function main_init() {
	activate_carousel_videos();
}

/**
 * Activate the carousel for videos
 */
function activate_carousel_videos() {
	if($('.video_visiblepart')==null) return;
//	var width = 248;
	var countElement = $('.video_visiblepart').find('li').length;
	var currentPos = 0;
	
	$('#video_all').width(width);
	$('.video_visiblepart').width(width);
	$('.video_visiblepart').find('li').width(width);
	
	// Sizing ul element
	$('.video_visiblepart').find('ul').css('width', width * countElement);
	
	// Initial positionning
	carousel_videos_place(currentPos, width, countElement);
	
	// Navigation left/right
	$('.video_chevron_left').click(function() {
		if (currentPos > 0) { 
			carousel_videos_place(--currentPos, width, countElement);
		}
	});
	$('.video_chevron_right').click(function() {
		if (currentPos < countElement - 1) {
			carousel_videos_place(++currentPos, width, countElement);
		}
	});
}

/**
 * Place the carousel and move icons
 */
function carousel_videos_place(pos, width, countElement) {
	if (pos == 0) {
		$('.topvideo_container').find('.video_chevron_left').css('display', 'none');
	} else {
		$('.topvideo_container').find('.video_chevron_left').css('display', '');
	}
	
	if (pos == countElement - 1) {
		$('.topvideo_container').find('.video_chevron_right').css('display', 'none');
	} else {
		$('.topvideo_container').find('.video_chevron_right').css('display', '');
	}
	
	$('.video_visiblepart')
		.find('ul')
		.animate({ left: "-" + width*pos + "px" }, 300 );
	
	// mise a jour de la pagination
	for(var j=0;j<countElement;j++){
		var img=document.getElementById("pointsimg"+(j+1));
		
		if(img==null){continue;}
	
		if(j==pos){
			img.src='/sfrxphone/images/pagination/select.gif';
		}else{
			img.src='/sfrxphone/images/pagination/unselect.gif';
		}
	}
}

/**
 * Cache/Affiche un div en mettant a jour l'image du bloc du titre si necessaire
 * Les images doivent avoir une class "down" ou "up" pour pouvoir etre prises en compte.
 * @param id
 * @return
 */
function hideshow(id, idtitle){
	var elt=document.getElementById(id);
	
	if(elt!=null){
		if(elt.style.display=="none"){
			elt.style.display="block";
		} else {
			elt.style.display="none";
		}
	}

	if(idtitle==null || idtitle=="") return;
	
	// on modifie l'image du block du titre s'il y en a une
	var title=document.getElementById(idtitle);
	if(title!=null){
		var imgs=title.getElementsByTagName("img");
		if(imgs!=null && imgs.length>0){
			var img=imgs[0];
			if(img.className=="down") {
				img.className="up";
			} else if(img.className=="up"){
				img.className="down";
			}
		}
	}
}


var divarray=new Array('resultats','team2','goal2');

function setSelected(idattr){
	for(var i=0;i<divarray.length;i++){
		var elt=document.getElementById(divarray[i]);
		if(elt!=null){
			if(elt.id==idattr) elt.className='select';
			else elt.className='unselect';
		}
	}
}

/********************************** AJAX *************************************/
function next_page(url_next_page,block){
	obj=get_ajax_next_page(url_next_page,block);
	return;
}

function get_ajax_next_page(url_next_page,block){
	var obj=null;
	var request = new XMLHttpRequest();
	request.open('GET', url_next_page,false);
	request.send(null);
	request.onreadystatechange = appeler (request,block);
}
function appeler(request,block) {
	if (request.readyState == 4) {
		obj=request.responseText;
		if(obj!=null){
			add_div_on_page(obj, '',block);
			return;
		}else{
			return;
		}
	}
}
function add_div_on_page(obj , id_next_page,block){
	divpagenext=null;
	divpagenext = document.getElementById('page1');
	container = document.getElementById('container');
	if(divpagenext!=null){
		remove_div_on_page('page1');
	}
	divpagenext = document.createElement('div');
	divpagenext.className='page1';
	divpagenext.id='page1';
	container.appendChild(divpagenext);
	divpagenext.innerHTML=obj;
}
function remove_div_on_page(id){
	container = document.getElementById(id);
	container.innerHTML='';
}
/*****************************************************************************/
function buildnextpage(url, value, block){
	url=url.replace('%CODEDAY%', value);
	next_page(url, block);
}

function addParam(url, paramname, paramvalue){
	url+="&"+paramname+"="+paramvalue;
	document.location.href=url;
}

function livedayfilter(intervall) {
	var li=document.getElementById('blocklive');
	var nodes = li.childNodes;
	for (i=0;i<nodes.length;i++){
		var node=nodes[i];
		if(node==null || node.id==null || node.id=='blocklink'){
			continue;
		}
		if((node.id!='livedayappli'+intervall)){
			node.style.display='none';
		}else if(node.style.display == 'none'){
			node.className='down';node.style.display= '';
		}else{
			node.style.display='none';
		}
	}
} 
function setErrorPicture(){  
	var doc=document.getElementById('blocklink'); 
	if(doc!=null){ 
		doc.className='blocklinkerror'; 
	} 
} 

function hideShowoneElt(elt){
	doc=document.getElementById(''+elt);
	if(doc==null){
		return;
	}
	if(doc.style.display=='none'){
		doc.style.display='';
	}else{
		doc.style.display='none';
	}
}

function hideShowCompo(top1,top2){
	var doc1=document.getElementById(''+top1);
	var doc2=document.getElementById(''+top2);
	doc1.style.display='';
	doc2.style.display='none';
}

function pageTableHideShow(hide, show){
	var doc=document.getElementById(''+hide);
	if(doc!=null){
		doc.style.display='none';	
	}
	var doc2=document.getElementById(''+show);
	if(doc2!=null){
		doc2.style.display='';	
	}
}

var nbaarray=new Array('conf_est_btn','conf_ouest_btn');
function setSelectedConf(idattr){
	for(var i=0;i<nbaarray.length;i++){
		var elt=document.getElementById(nbaarray[i]);
		if(elt!=null){
			if(elt.id==idattr) elt.className='selectcell';
			else elt.className='unselectcell';
		}
	}
}

/**
 * regarde l'historique de navigation et cache le bouton retour si c'est vide
 */
function checkHistory(){
	var button=document.getElementById("previous_button");
	if(button!=null){
		if(history.length==1){
			button.style.display="none";
		}else{
			button.style.display="";
		}
	}
}

/**
 * Met a jour le bouton play d'une video lors d'un clic
 * @param divid
 * @return
 */
function updatePlayButton(divid){
	if(divid==null || divid=="") return;
	
	var div=document.getElementById(divid);
	
	if(div!=null){
		var imgs=div.getElementsByTagName("img");
		if(imgs!=null && imgs.length>0){
			var img=imgs[0];
			img.src='/sfrxphone/images/buttons/btn_play_pressed.gif';
		}
	}
}

function preloadCommonImages(){
	piccommon=new Image(88,88);
	piccommon.src='/sfrxphone/images/backgrounds/carousel_loading.gif';
}

/** Prechargement des images du bouton play. Sinon, pas le temps de voir le pressed lors du clic */
function preloadVideoImages(){
	pic=new Image(88,88);
	pic.src='/sfrxphone/images/buttons/btn_play.gif';
	picpressed=new Image(88,88);
	picpressed.src='/sfrxphone/images/buttons/btn_play_pressed.gif';
}

function playVideo(id, url){
	updatePlayButton(id);
	document.location.href=url;
}
