var bcExp;
var modVP;
var modExp;
var modCon;


var launchFirstVideo = true;
var currentVideo;
var currentName = "WebTv Roubaix";

var currentPlaylistId;

//var tok = "Sth8DmaKyg1yWP34g_rAvBEG6iRzEM84FzRHekEDNeMQu39ZSBuCJw..";
//var defPlID = 798351813001;

var tok = "yllKURhA0yZy_FqumZEU31CoJxyThuRkbywCaPxbXk-aKPP3PA5b-g..";
var defPlID = 908704365001;

function onTemplateLoaded(experienceID){
	
    bcExp = brightcove.getExperience(experienceID);
    modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
    modExp = bcExp.getModule(APIModules.EXPERIENCE);
    modCon = bcExp.getModule(APIModules.CONTENT);
    modMenu = bcExp.getModule(APIModules.MENU);
	social 	= bcExp.getModule(APIModules.SOCIAL);
    modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    
	modVP.addEventListener(BCMediaEvent.CHANGE,handleMediaChange);
}

function onTemplateReady(evt){
	
	//bcExp.debug("------------------------------onTemplateReady");
  
	var defaultPlaylistId  = extractUrlParams()['bclid'];
	var defaultVideoId = extractUrlParams()['bctid'];
	
	if(defaultPlaylistId != undefined && defaultVideoId != undefined){
		//launchFirstVideo=false;
		//getPlaylist(defaultPlaylistId);
		updateLink(defaultVideoId,defaultPlaylistId);
	}else if(defaultVideoId != undefined){
		//launchFirstVideo=false;
		//getPlaylist(defPlID);
	
	}else{
		//getPlaylist(defPlID);
	}
	
}

function updateLink(videoId, playlistId) {	
	
		/*var playlistKey = "bclid"; 
		var videoKey = "bctid";
		var currentLink = location.host + location.pathname ;
		
		var newLink = currentLink + "?" + playlistKey + "=" + playlistId + "&" + videoKey + "=" + videoId;*/
		var newLink = window.location.href;
		social.setLink(newLink);
		social.setEmbedCode('<object id="flashObj" width="480" height="270" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0"><param name="movie" value="http://c.brightcove.com/services/viewer/federated_f9?isVid=1&isUI=1" /><param name="bgcolor" value="#FFFFFF" /><param name="flashVars" value="@videoPlayer='+videoId+'&playerID=986323797001&playerKey=AQ~~,AAAAsLrfgbE~,DkcmrvAaUBE5vZ31onotZM5FRQamkam-&domain=embed&dynamicStreaming=true" /><param name="base" value="http://admin.brightcove.com" /><param name="seamlesstabbing" value="false" /><param name="allowFullScreen" value="true" /><param name="swLiveConnect" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://c.brightcove.com/services/viewer/federated_f9?isVid=1&isUI=1" bgcolor="#FFFFFF" flashVars="@videoPlayer='+videoId+'&playerID=986323797001&playerKey=AQ~~,AAAAsLrfgbE~,DkcmrvAaUBE5vZ31onotZM5FRQamkam-&domain=embed&dynamicStreaming=true" base="http://admin.brightcove.com" name="flashObj" width="480" height="270" seamlesstabbing="false" type="application/x-shockwave-flash" allowFullScreen="true" allowScriptAccess="always" swLiveConnect="true" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>');
								
}


function handleMediaChange(evt){
	
	currentVideo = modVP.getCurrentVideo();
	currentName = currentVideo.displayName;
	
	refreshVideoInfo(currentVideo);
	
	updateLink(currentVideo.id,currentPlaylistId);
	
}


/**
 * Fonction de récupération des paramètres GET de la page
 * @return Array Tableau associatif contenant les paramètres GET
 */
function extractUrlParams(){	

	var t = location.search.substring(1).split('&');
	var f = [];

	for (var i=0; i<t.length; i++){
		var x = t[ i ].split('=');
		f[x[0]]=x[1];
	}
	return f;
}


function getPlaylist(id){
	
	currentPlaylistId=id;
	
	// addScriptTag("refreshPlaylist", "http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id="+id+"&playlist_fields=name,videos,videoIds&customFields=etat&token="+tok, "refreshPlaylist");
	//addScriptTag("refreshPlaylist","http://api.brightcove.com/services/library?command=find_playlist_by_id&token=yllKURhA0yZy_FqumZEU31CoJxyThuRkbywCaPxbXk-aKPP3PA5b-g..&playlist_id=908704365001&video_fields=customFields&fields=id,name,shortDescription,videoIds,videos,thumbnailURL", "refreshPlaylist");
}



function init(){
	//active coverflow
	getMostViewed();
	//getNewest()

}


function loadVideoById(id){
    //modVP.loadVideo(id);
}

function addScriptTag(id, url, callback){
    var scriptTag = document.createElement("script");
    scriptTag.setAttribute("type", "text/javascript");
    scriptTag.setAttribute("charset", "utf-8");
    scriptTag.setAttribute("src", url + "&callback=" + callback);
    scriptTag.setAttribute("id", id);
    var head = document.getElementsByTagName("head").item(0);
    head.appendChild(scriptTag);
}


function showMenuPage(pmenu){
	modMenu.showMenuPage(pmenu);	
}
function shareFacebook(){
	var str = "http://www.facebook.com/sharer/sharer.php?u="+encodeURIComponent(window.location.href)+"&t="+encodeURIComponent(currentName);
	//var str = "http://www.facebook.com/sharer/sharer.php?u="+encodeURIComponent('http://roubaix-v2.novastream.fr/plugin/fb.php?bctid='+currentVideo.id)+"&t="+encodeURIComponent(currentName);
	window.open(str);
}
function shareTwitter(){
	window.open("http://twitter.com/share?url="+escape(window.location.href));
}
function getMostViewed(){
   
  // addScriptTag("topVideos", "http://api.brightcove.com/services/library?command=find_all_videos&token="+tok+"&video_fields=shortDescription,id,name,thumbnailURL,videoStillURL,playsTrailingWeek,longDescription&sort_by=plays_total&page_size=5", "refreshCoverflow");
   
   addScriptTag("newVideos", "http://api.brightcove.com/services/library?command=search_videos&token="+tok+"&video_fields=shortDescription,id,name,thumbnailURL,videoStillURL,playsTrailingWeek,longDescription&sort_by=plays_total:desc&page_size=10&none=Live Video Stream&all=etat:prod&exact=true", "refreshCoverflow");


}
function getNewest(){
  
	addScriptTag("newVideos", "http://api.brightcove.com/services/library?command=search_videos&token="+tok+"&video_fields=shortDescription,id,name,thumbnailURL,videoStillURL,playsTrailingWeek,longDescription&sort_by=publish_date:desc&page_size=10&none=Live Video Stream&all=etat:prod&exact=true", "refreshCoverflow");

}


