var player = null;
var currentState = "NONE"; 
var previousState = "NONE"; 

var html5_version = false;
var css3_font = false;
var safaric = false;



function popupCenter(page) 
{
	var options = "resizable=no, location=no, status=no, scrollbars=no, menubar=no";
	var url = "popup.php?page=" + page;
	var nom = page;
	var largeur = 670;
	var hauteur = 430;
	var haut=(screen.height-hauteur)/2;
	var Gauche=(screen.width-largeur)/2;
	fencent=window.open(url,nom,"top="+haut+",left="+Gauche+",width="+largeur+",height="+hauteur+","+options);
}


function setHtml5Version()
{
	if(supports_video() != false)
	{
		if(supports_h264_baseline_video() != "")
		{
			
			html5_version = true;
			
		}
		else
		{
			
			html5_version = false;
			
		}
	}
	else
	{
		
		html5_version = false;
		
	}
}

function playerReady(thePlayer) {
	player = document.getElementById("player");
	$('div.video_wrapper').css('background','none');
	addListeners();
}


function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
		//navigatePlaylist("suivante",true);
	}
}






function createFallback(file, preview, l_autoplay) 
{
	$('div.video_wrapper').css('background-color','black');
	$('div.video_wrapper').css("background-image","url('./integration/default/img/loading_black.gif')");
	$('div.video_wrapper').css("background-repeat","no-repeat");
	$('div.video_wrapper').css("background-position","center center");
	


	var flashvars = {
		image:preview,
		file:file,
		skin:"./integration/video/swf/nacht.swf",
		frontcolor:"cc0000",
		lightcolor:"cc0000",
		controlbar:"bottom",
		stretching:"uniform",
		streamer: 'lighttpd',
		autostart:false//l_autoplay
	};
	var params = {
		allowfullscreen:'true',
		allowscriptaccess: 'always',
		wmode:"transparent"
	}
	var attributes = {	
		/*id:'player',
		name:'player'*/
	};
	swfobject.embedSWF(
		"./integration/video/swf/player.swf",
		"player",
		"788",
		"449",
		"9.0.0" ,
		"./integration/video/swf/player.swf",
		flashvars,
		params,
		attributes
	);
	
}

function supports_video() 
{
  return !!document.createElement('video').canPlayType;
}

function supports_h264_baseline_video() 
{
  if (!supports_video()) { return false; }
  var v = document.createElement("video");
  return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
}
function html5VideoReady()
{
	$("#player_html5").bind('ended', function(){
        	navigatePlaylist("suivante");
  	});

	/*var splashImage = document.getElementById("splash_html5");
	var videoElem = document.getElementById("player_html5");
	splashImage.addEventListener("click", function () {
		splashImage.style.display = "none";
		videoElem.play();
	 });*/
	
}
function initVideo(file,preview,l_autoplay) 
{	
var autoplay = "";
	if(l_autoplay == true)
	{
		autoplay = "autoplay";
	}
	var html_c = '<video id="player_html5" controls="controls" height=449 width=789 poster="' + preview + '" src="' + file + '" ' + autoplay + '  ></video>';
	$('div.video_wrapper').css('background','none');	
	$('#player').html(html_c);
	$('#player').show();
	html5VideoReady();
	
}
function getIdFromString(id,val)
{
	var mySplitResult = id.split(val);
	return mySplitResult[1];
}
function getIdVideo()
{
	var l_id = $('a.video_id').attr('id');
	return l_id;
}
function setIdVideo(id)
{
	$('a.video_id').attr('id',id);
}
function setIdPage(page)
{
	$('#current_page').text(page.toString());
}
function getIdPage()
{
	var page = $('#current_page').text();
	return parseInt(page);
}

function initPlaylist()
{

	$('div.jaquette_row.inactive').hide();
	$('div.jaquette.inactive div').children('img').show();		
	$('div.jaquette.active div').children('img').show();
	$('div.jaquette.active:not(.rubrique) div div.active').show();
	$('div.jaquette.inactive div div.active').hide();
	$('div.jaquette_row.active').hide();

	
	$('div.jaquette_row:not(.rubrique)').click(function(){
		var l_id = getIdFromString($(this).attr('id'),"row_");
		readVideo(l_id);
	});

	$('div.jaquette:not(.rubrique)').each(function(i,val){
		var posL;
		if(i == 0)
		{
			posL = 40;
		}
		else
		{
			posL = ( i * 157 ) + (40 + i );
		}
		$(this).find('div.active').css('left',posL + "px");
	});

	


	$('div.jaquette.active:not(.rubrique)').mouseover(function(){
		$(this).find('div.active').hide();
	});

	$('div.jaquette_row.rubrique').click(function(e){
		
		var l_id = getIdFromString($(this).attr('id'),"row_rubrique_");
		if(l_id > 0)
		{
			readVideo(l_id);
			$(this).unbind("click");
			return false;
		}
		else
		{
			return false;
		}
	});
	$('a.read_video:not(.rubrique)').click(function(e){
		e.preventDefault();
		var l_id = getIdFromString($(this).attr('id'),"v_");
		readVideo(l_id);
	});
	$('a.read_video.rubrique').click(function(e){
		e.preventDefault();
		var l_id = getIdFromString($(this).attr('id'),"v_rubrique_");
		if(parseInt(l_id) > 0)
		{
			readVideo(l_id);
			$(this).unbind("click");			
		}
	});


	/*on désactive les rubriques*/
	var l_id = getIdVideo();
	$('#row_rubrique_' + l_id).hide();
	$('#jaq_rubrique_' + l_id + ' div img').show();
	$('#jaq_rubrique_' + l_id).show();



}

function initListingVideo()
{
	$('div.black.w2.tle.ro').hide();
	$('div.black.w2.tle:not(.ro)').show();

	$('div.v_ligne:not(.admin)').click(function(e){
		var l_id = getIdFromString($(this).attr('id'),"ligne_");
		readVideo(l_id);
		$(this).unbind("click");
	});
	


}
function sifrMe()
{

	
	if(css3_font == false)
	{
			var fedra = {
			      src: 'http://www.esec.edu/integration/default/swf/fedra_light.swf'
			    };

			sIFR.activate(fedra);

			sIFR.replace(
					fedra,
					{
						selector: 'h1',
						css: [
							'.sIFR-root{color: #ffffff;font-size: 20px}, .sIFR-root a{text-decoration: none;cursor:pointer;},.sIFR-root a:link{color: #ffffff;},.sIFR-root a:hover{color: #ffffff;}, .sIFR-root .pdf{color: #ffffff;font-size: 16px;}'
						],
						filters: {
							knockout: true
						},
						wmode: 'transparent'
					}
			);


	
		
	}
	else
	{
		$('div#video h1').addClass('csst');
	}

}

function setPlayList()
{
	$('div.jaquette:not(.rubrique) div').mouseover(function(){
		//init
		$('div.jaquette_row:not(.rubrique)').hide();
		$('div.jaquette:not(.rubrique) div').children('img').show();
		


		code_rubrique = $(this).attr('id');
		$('div#jaq_' + code_rubrique + ':not(.rubrique) div').children('img').hide();
		$('div#row_' + code_rubrique ).show();
		if(!$(this).is('.active'))
		{
			$('div.jaquette.active:not(.rubrique) div div.active').show();
		}
		else
		{
			$('div.jaquette.active div div.active').hide();
		}
		
	});
	
	$('div.jaquette.rubrique div').mouseover(function(){
		//init
		$('div.jaquette_row.rubrique').hide();
		$('div.jaquette.rubrique div').children('img').show();


		code_rubrique = $(this).attr('id');
		$('div#jaq_rubrique_' + code_rubrique + '.rubrique div').children('img').hide();
		$('div#row_rubrique_' + code_rubrique ).show();
	});
	
	$('div.jaquettew').mouseleave(function(){
		initPlaylist();
	});

	$('a.read_video').click(function(e){
		e.preventDefault();
	});

	initPlaylist();
	

}
function setListingVideo()
{
	
		
	$('div.v_ligne').mouseover(function(){
		
		$('div.black.w2.tle.ro').hide();
		$('div.black.w2.tle:not(.ro)').show();
		
		var id = getIdFromString($(this).attr('id'),'ligne_');
		$('div#v_liste_' + id).hide();		
		$('div#v_liste_ro_' + id).show();		
	});
	
	$('div.listingVideo').mouseleave(function(){
		initListingVideo();
	});
	initListingVideo();
}

function movePlaylist(id)
{
	
	/*inactive*/
	$('div.jaquette.active').addClass('encours');
	$('div.jaquette.active').removeClass('active');
	$('div.jaquette.encours').addClass('inactive');
	$('div.jaquette.active').removeClass('encours');

	$('div.jaquette_row.active').addClass('encours');
	$('div.jaquette_row.active').removeClass('active');
	$('div.jaquette_row.encours').addClass('inactive');
	$('div.jaquette_row.active').removeClass('encours');

	/*active*/
	$('div#jaq_' + id).removeClass('inactive');
	$('div#jaq_' + id).addClass('active');	
	$('div#row_' + id ).removeClass('inactive');
	$('div#row_' + id ).addClass('active');
	$('div#row_rubrique_' + id ).removeClass('inactive');
	$('div#row_rubrique_' + id ).addClass('active');
	$('div#jaq_rubrique_' + id ).removeClass('inactive');
	$('div#jaq_rubrique_' + id ).addClass('active');

	/*image*/
	
	initPicture($('div.jaquette img'),id);
}
function initPicture(obj,id)
{
	var reg_active=new RegExp("(active)", "g");
	var reg_nb=new RegExp("(nb)", "g");
	var src_to_replace = "";
	obj.each(function(i, val){
		var src_img = $(this).attr('src');		
		/*INIT*/
		src_to_replace = src_img.replace(reg_active,"nb");
		$(this).attr('src',src_to_replace);
	});
	if($('div#jaq_' + id).length > 0)
	{
		var src_img_active = $('div#jaq_' + id).find('img').attr('src');
		if(src_img_active != "")
		{
			src_to_replace = src_img_active.replace(reg_nb,"active");
			$('div#jaq_' + id).find('img').attr('src',src_to_replace);
		}
	}
}
function getCurrentPage(id,type)
{
	var page = getIdFromString(id,type + '_');
	if(type == "fd")
	{
		page = parseInt(page) + 1;
	}
	else if(type == "fg")
	{
		page = parseInt(page) - 1;
	}
	else
	{
		page = parseInt(page);
	}
	return page;
}

function readPlaylist(id)
{
	var old_id = getIdVideo();
	if( id != old_id )
	{
		readVideo(id,false,"",true,true);
	}
}
function loadPlaylist(id,text,class_name,page)
{
	
		if(!page)
		{
			var page = getIdPage();	
		}
		
		$('div#main').load(
				'./aja/playlist.php?p=' + page,
				function(){
					sifrMe();
					setPlayList();
					movePlaylist(id);
					initPlaylist();
					setPager();
					showNavigation();
				}
		);
	
		if( text != "" )
		{
			$("a#link").text(text);
			$("a#link").attr("class",class_name);
		}
	

}

function setPager()
{

	var id = getIdVideo();

	$('a.fd:not(.end)').click(function(){
			var page = getCurrentPage($(this).attr('id'),'fd');
	
			if ( page >= 0 )
			{
				loadPlaylist(id,"","",page);
				setIdPage(page);
			}
			return false;
		});
	$('a.fd.end').click(function(){
		return false;
	});
	$('a.fg').click(function(){
			var page = getCurrentPage($(this).attr('id'),'fg');
			if ( page >= 0 )
			{
				loadPlaylist(id,"","",page);
				setIdPage(page);
			}
			return false;
		});
		$('a.pager_go_to').click(function(){
			var page = getCurrentPage($(this).attr('id'),'p');
	
			if ( page >= 0 )
			{
				loadPlaylist(id,"","",page);
				setIdPage(page);
			}
			return false;
		});
}
function setPermalien(id)
{
	$('input#partager').val('http://www.esec.tv/index.php?id=' + id);
}
function loadTitle(id)
{
	$('div.video_title').load(
				'./aja/title.php?id=' + id,
				function(){sifrMe(); }
	);	
}
function loadProgramme(id,text,class_name)
{
	$('div#main').load(
				'./aja/programme.php?jaquette=' + 'jaq_' + id,
				function(){
					sifrMe();
					setListingVideo();
					setHeightTexteProgramme();
				}
	);
}

function readVideo(id,start,preview,l_autoplay,show_navigation)
{
	
	var old_id = getIdVideo();
	$('img.title.gd').hide();
    $('img.title.pt').show();
    $('div#header').css('height','39px');
	$('div#video div.video_title').show();
	$('div#video div.video_wrapper').show();
	$('div#video div.video_navigation').show();
	if(id != old_id || start == true || document.getElementById('player').style.display == "none")
	{
		
		$.getJSON('aja/video.php?id=' + id, function(data) {
			
			
			if (html5_version && safaric)
			{
				initVideo(data.file,data.preview,l_autoplay); 
			}
			else
			{
				createFallback(data.file, data.preview,l_autoplay);
			}
		});
		
		
		movePlaylist(id);
		initPlaylist();
		setPermalien(id)
		setIdVideo(id);
		loadTitle(id);
		loadProgramme(id,"< Aujourd'hui sur ESEC.tv","playlist");
	}
	else
	{
		loadProgramme(id,"< Aujourd'hui sur ESEC.tv","playlist");
	}

	if(!show_navigation)
	{
		showNavigation();
	}
	
	
	
}
function setSafaric()
{
	
	if ( $.browser.safari && !$.browser.opera && !$.browser.msie && !$.browser.mozilla ) 
	{
		var userAgent = navigator.userAgent.toLowerCase();
		if ( userAgent.indexOf("chrome") === -1 ) 
		{
			if(parseInt($.browser.version) >= 533)
			{
				//safaric = true;
				safaric = false;
			}
		}
	}
	else
	{
		safaric = false;
	}
}		

function setHeightTexteProgramme()
{
	var l_height = parseInt($('div#p_texte_wrapper').height());
	
	if(l_height < 81)
	{
		$('div#p_texte_wrapper').height(81);
	}
	else
	{
		var nbr_ligne = l_height / 46;
		nbr_ligne = Math.ceil(nbr_ligne);
		nbr_ligne = nbr_ligne - 1;		
		var v_height = (nbr_ligne * 46) + 81;
		$('div#p_texte_wrapper').height(v_height);
	}
	return true;
	

}

function navigatePlaylist(sens,l_autoplay)
{
	var l_autoplay = false;
	var l_id = getIdVideo();
	var b_playlist_loader;

	
	b_playlist_loader = false;
	

	$.getJSON('aja/video_playlist.php?id=' + l_id + '&type=' + sens, function(data) {		
			if(parseInt(data.id_video) > 0)
			{			
				readPlaylist(data.id_video,l_autoplay);
				showNavigation(data.first,data.last,data.count)
			}
			else
			{
				location.href='index.php';
			}
			
		});
}
function myCallbackFunction(){
	$(document).ready(function(){
	 	myFavoriteCallbackFunction();
	});
}
function getStatusVideo(id)
{
	$.getJSON('aja/video_playlist.php?id=' + id, function(data) {		
			if(parseInt(data.id_video) > 0)
			{			
				showNavigation(data.first,data.last,data.count)
			}
			else
			{
				location.href='index.php';
			}
			
	});
}
function showNavigation(first, last, count)
{
	var id_video = getIdVideo();
	var a_video = $('a#v_' + id_video);	

	if(first == undefined && last == undefined && count == undefined)
	{
		getStatusVideo(id_video);
	}


	//init
	if (first && count > 1)
	{
		$('div#v_suivante').css('margin-left','238px');
		$('div#v_precedente').hide();
		$('div#v_suivante').show();
	}
	else if(!first && !last)
	{
		$('div#v_suivante').css('margin-left','1px');
		$('div#v_suivante').show();
		$('div#v_precedente').show();
	}
	else if (!first && last)
	{
		$('div#v_suivante').hide();
		$('div#v_precedente').show();
	}
	$('input#partager').hide();
}
function loadProgrammes()
{
    $('img.title.pt').hide();
    $('img.title.gd').show();
    $('div#header').css('height','85px');
    
	$('div#main').load(
				'./aja/programmes.php',
				function(){
					sifrMe();
					setListingVideo();
					setPlayList();
					initPlaylist();
				}
	);
	$('div#video div.video_title').hide();
	$('div#video div.video_wrapper').hide();
	$('div#video div.video_navigation').hide();

}




