/* Script associate to the controls of the player */

function trace(){
if(window.console&&console.log){
if(navigator.userAgent.toLowerCase().indexOf("webkit")>=0)
console.log(arguments.length<2?arguments[0]:Array.prototype.slice.call(arguments));
else
console.log.apply(this,arguments);
}}

function hasFlash(keep)
{
	var s=window.swfobject,v=parseInt(window.flashver,10)||0,b;
	if(s&&typeof s.getFlashPlayerVersion==='function'&&v>=0){
		v=s.getFlashPlayerVersion();
		v=v&&parseInt(v.major,10)||0;
		
		// Ignore Flash 9 or older
		if(!keep)
			if(v<10)v=0;
	}

	return v;
} 

/* Method that we call to play the media in the player */
function relayPlay(id){
	id=id||'flashcontent';
	if(document.getElementById(id)&&hasFlash())
	{
		setFlashFunction();
		var flashMovie=swfobject.getObjectById('flashcontent');
		// check that the movie exist
		if(typeof flashMovie!='undefined'&&flashMovie)
		{
			// check the ext interface
			if(typeof flashMovie.relayPlay=='function'){
				flashMovie.relayPlay();
			}else{
				// Create the interface, check that it exist, then call it
				makeCallable(flashMovie,'relayPlay');
				if(typeof flashMovie.relayPlay=='function'){flashMovie.relayPlay();}
			}
		}
	}
}

/* Method that we call to pause the media in the player */
function relayPause(id){
	id=id||'flashcontent';
	if(document.getElementById(id)&&hasFlash())
	{
		setFlashFunction();
		var flashMovie=swfobject.getObjectById('flashcontent');
			if(typeof flashMovie!='undefined'&&flashMovie){
				if(typeof flashMovie.relayPause=='function'){flashMovie.relayPause();}else{makeCallable(flashMovie,'relayPause');
				if(typeof flashMovie.relayPause=='function'){flashMovie.relayPause();}
			}
		}
	}
}

/* Method that we call to change the current media in the player. We need to pass in parameters the uniqueId associate to the media. */
function relayMedia(p_mediaUniqueId,id){
	id=id||'flashcontent';
	if(document.getElementById(id)&&hasFlash())
	{
		setFlashFunction();
		var flashMovie=swfobject.getObjectById('flashcontent');
		if(typeof flashMovie!='undefined'&&flashMovie){
			if(typeof flashMovie.relayMedia=='function'){flashMovie.relayMedia(p_mediaUniqueId);
			}else{
				makeCallable(flashMovie,'relayMedia');
				if(typeof flashMovie.relayMedia=='function'){
					flashMovie.relayMedia(p_mediaUniqueId);
				}
			}
		}
		setUniqueIdInUrl(p_mediaUniqueId);
	}
}
/* Methods that add a new media in the player */
/*
Ex:
	p_mediaConfig.mediaTitle="en,Watercolor;fr,Watercolor";
*/
function appendMedia(p_mediaConfig,id){
	id=id||'flashcontent';
	if(document.getElementById(id)&&hasFlash())
	{
		setFlashFunction();
		var flashMovie=swfobject.getObjectById('flashcontent');
		if(typeof flashMovie!='undefined'&&flashMovie){
			if(typeof flashMovie.appendMedia=='function'){flashMovie.appendMedia(p_mediaConfig);
			}else{
				makeCallable(flashMovie,'appendMedia');
				if(typeof flashMovie.appendMedia=='function'){
					flashMovie.appendMedia(p_mediaConfig);
				}
			}
		}
	}
}

/* Methods call by the onBeforeUnload to track the current media completion */
function closeWindow(){if(mediaCompletion != 1 && hasFlash()) setTrackMediaCompletion();}

/* Script associate to the tracking */

var mediaCompletion=0,
	mediaTitle="",
	mediaType="",
	trackingCategorie="Media - ";

/* This methods notify google analytics than a media is start */
function trackMedia(p_mediaTitle, p_type){
	if(mediaTitle && mediaCompletion!= 1) setTrackMediaCompletion();
	mediaCompletion=0;
	mediaTitle=p_mediaTitle;
	mediaType=p_type;

	if(typeof _gat !== "undefined" && typeof googleTrackerID !== "undefined")
	{
		var pageTracker=_gat._getTracker(googleTrackerID);
		pageTracker._trackEvent(mediaType + " : " + mediaTitle, "Open media" ,"Open");
	}
}
/* This methods notify google analytics the completion of the current media playing in the media player */
function trackMediaCompletion(p_mediaCompletion,p_mediaDuration){
	var complete=false;
	if(mediaCompletion!= 1 && p_mediaCompletion ==1) complete=true;
	mediaCompletion=p_mediaCompletion;
	if(complete) setTrackMediaCompletion();
}

function setTrackMediaCompletion(){
	if(typeof _gat !== "undefined" && typeof googleTrackerID !== "undefined")
	{
		var pourcent=Math.floor(mediaCompletion * 100);
		var pageTracker=_gat._getTracker(googleTrackerID);
		pageTracker._trackEvent(mediaType + " : " + mediaTitle, "Percentage of completion" ,pourcent);
	}
}

/* Script associate to the url */

function setUniqueIdInUrl(p_uniqueId){window.location.hash="video="+p_uniqueId;}

function getChapterId(){
	var url=window.location.href||'',
	rx=/[\?&#](?:.*?\/)?(?:ch[apter]*|vid[eo]*)i?d?[:=]+([0-9]+)/ig,
	m=rx.exec(url),
	uniqueId=parseInt(m?m[1]:-1,10)||-1;
	return uniqueId;
}

function setUniqueIdInUrl(p_uniqueId){
	window.location.hash="video="+p_uniqueId;
}

// http://code.google.com/p/doctype/wiki/ArticleFixingFlashExternalInterface
function makeCallable(instance,name){setFlashFunction();if(instance&&name){instance[name]=function(){if(instance&&typeof instance.CallFunction=='function'&&typeof __flash__argumentsToXML=='function')return eval(instance.CallFunction("<invoke name=\""+name+"\" returntype=\"javascript\">"+__flash__argumentsToXML(arguments,0)+"</invoke>"));}}}

function setFlashFunction(){var u="undefined";
if(typeof(__flash__arrayToXML)==u||!__flash__arrayToXML)window.__flash__arrayToXML=function(obj){var s="<array>";for(var i=0;i<obj.length;i++){s+="<property id=\""+i+"\">"+__flash__toXML(obj[i])+"</property>";}return s+"</array>";};
if(typeof(__flash__argumentsToXML)==u||!__flash__argumentsToXML)window.__flash__argumentsToXML=function(obj,index){var s="<arguments>";for(var i=index;i<obj.length;i++){s+=__flash__toXML(obj[i]);}return s+"</arguments>";};
if(typeof(__flash__objectToXML)==u||!__flash__objectToXML)window.__flash__objectToXML=function(obj){var s="<object>";for(var prop in obj){s+="<property id=\""+prop+"\">"+__flash__toXML(obj[prop])+"</property>";}return s+"</object>";};
if(typeof(__flash__escapeXML)==u||!__flash__escapeXML)window.__flash__escapeXML=function(s){
return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&apos;");
};if(typeof(__flash__toXML)==u||!__flash__toXML)window.__flash__toXML=function(value){var type=typeof(value);if(type=="string"){return "<string>"+__flash__escapeXML(value)+"</string>";}else if(type=="undefined"){return "<undefined/>";}else if(type=="number"){return "<number>"+value+"</number>";}else if(value==null){return "<null/>";}else if(type=="boolean"){return value?"<true/>":"<false/>";}else if(value instanceof Date){return "<date>"+value.getTime()+"</date>";}else if(value instanceof Array){return __flash__arrayToXML(value);}else if(type=="object"){return __flash__objectToXML(value);}else{return "<null/>";}};
if(typeof(__flash__addCallback)==u||!__flash__addCallback)window.__flash__addCallback=function(instance,name){instance[name]=function(){if(instance&&typeof instance.CallFunction=='function'&&typeof __flash__argumentsToXML=='function')return eval(instance.CallFunction("<invoke name=\""+name+"\" returntype=\"javascript\">"+__flash__argumentsToXML(arguments,0)+"</invoke>"));}};
if(typeof(__flash__removeCallback)==u||!__flash__removeCallback)window.__flash__removeCallback=function(instance,name){instance[name]=null;};}

// Harmony Player
function initPlayer(prefix,lang,advisor)
{
	var width=736,
		height=414,
		pwidth=736, // "100%",
		pheight=advisor?610:484,
		cid="flashplayer",
		playerid="flashcontent",
		fv={
			width:width,
			height:height,
			mouseActivatedMode:"none", ///none - disappear - move
			controlsPadding:10,
			separatorType:"none", ///none - scrubBar - all
			language:lang||"en",
			enableHd:false,
			enableFullScreen:true,
			smoothing:true,
			enableExternalInterface:true,
			playlistControler:!!advisor
		},
		params={
			menu:"false",
			wmode:"opaque",
			allowfullscreen:"true",
			allowscriptaccess:"always"
		},
		c=document.getElementById(cid),
		url=window.location.href||"";

	//prefix=prefix||"../";
	prefix=(url.substr(0,5)==="https")?"https://bluerush.hs.llnwd.net/o15/agf/harmony/r01/":"http://bluerush.vo.llnwd.net/o15/agf/harmony/r01/";

	fv.urlPrefix=prefix;
	fv.playerSettingsUrl="xml/player_settings_20.xml";

	fv.tracking=false;
	//window.googleTrackerID='UA-XXXXXXXX-X';
	//if(fv.tracking) window.onbeforeunload=closeWindow;

	if(c&&c.style)
	{
		c.style.width=pwidth+"px";
		//c.style.height=pheight+"px";
		c.style.display="block";
	}

	// Investor clip only
	if(!advisor)
	{
		fv.initMediaUniqueId=6;
		fv.autoPlay=false;
	}
	else
	{
		fv.autoPlay=true;
	}

	if(window.swfobject)
	{
		swfobject.embedSWF(prefix+"swf/blue_player_14.swf",playerid,pwidth,pheight,"10",prefix+"swf/express_install.swf",fv,params,{id:playerid});
	}
}

function toggleMenu(hide,prefix,lang,advisor)
{
	var bid="harmonyvideobox",b=document.getElementById(bid),
		aid="closeplayer",a=document.getElementById(aid),
		cid="flashplayer",c=document.getElementById(cid);

	// Hide menu
	if(b&&b.style)
	{
		b.style.display=hide?"none":"block";
	}
	
	// Show button when we hide menu
	if(a&&a.style)
	{
		a.style.display=hide?"block":"none";
	}
	
	if(c&&c.style)
	{
		c.style.display=hide?"block":"none";
	}

	if(hide)
	{
		initPlayer(prefix,lang,advisor);
	}
	else
	{
		relayPause();
	}
}
