var video_templates_prepared = false ;
var video_leftcol_prepared = false ;
var video_json_template = { version: 3 , text : "" , video : { kind : "" } , color : 1 , textstyles:{ size : "2" , font : "1" , bold : "false" , center : "false" , italic : "false" } } ;

var video_colordesc =  { "qmc1" : "color" } ;
var video_textdesc = { "qtxt1" : { "size" : "textstyles.size" , "font" : "textstyles.font" ,  "bold" : "textstyles.bold" , "center" : "textstyles.center" , "italic" : "textstyles.italic" }  };

var video_leftcol_description = [
{ what : "notecolor" , tabname : "tabcolor" , visible : true , elems : [ 
	{ handler : "qsn_addqlubcolorblock" , params : { jsonpath : "color" , others : true ,  transparent:true , title:false , menuseparator: false , callback : "qsn_setcolors"  } } 
	] }, 
{ what : "textstyles" , tabname : "tabtext" , visible : false , elems : [ 
	{ handler : " qsn_addfontfsizes"   , params : { jsonpath : "textstyles.size" , ncols : 2  } } ,
	{ handler : " qsn_addfontfamilies"   , params : { jsonpath : "textstyles.font" , ncols : 2  } } ,
	{ handler : "qsn_addtextoptions"     , params : { jsonpath : "textstyles" , ncols : 2 , opts : [ "bold" , "center" , "italic"  ] } }
	] }
] ;

var video_request = {
	"callerid" : "" ,
	"uploadkind" : "video",
	"title" : "" ,
	"prompt" : "" ,
	"multiple" : false,
	"extensions" : [ "avi" , "divx" , "mkv" , "gvi" , "mov" , "mpg" , "mpeg" , "mp4" , "mp3" , "m4p" , "m4v" , "m3u" , "vp6", "vfw" , "wmv" ] ,
	"namingdata" : { "wantname" : true },
	"formats" : "",
	"callback" : "",
	"cancel": { "callback" : "qsn_canceledit", "args" : [] }
}

function video_upgradejson( r ) {
	if ( ( r.version ) && ( r.version == video_json_template.version ) ) return r ;
	if(r.version<2) r.color = 1;
	if(r.version<3) {
		r.textstyles = { size:r.textsize , font:r.textfont, bold : String( r.textbold !=0 ) , center : String( r.textcenter != 0 ) , italic : String( r.textitalic !=0 ) } ;
		r.textsize = null ;
		r.textfont = null ;
		r.textbold = null ;
		r.textcenter = null ;
		r.textitalic = null ;
	}
	r.version = video_json_template.version ;
	return r ;
}

function video_template( id ) {
	if ( video_templates_prepared ) return qsn_template( id , true ) ;
	qsn_installcontainer( "view" , "video" , ge( "video_notecontent" ).innerHTML ) ;
	var div = qsn_installcontainer( "edit" , "video" , ge( "video_notecontent_edit" ).innerHTML ) ;
	video_templates_prepared = true ;
	return qsn_template( id , true ) ;
}

var have_quicktime = 0 ; // ne pas utiliser true/false
function video_quicktime_missing () {
	if (navigator.plugins) {
		for (i=0; i < navigator.plugins.length; i++ ) {
			if (navigator.plugins[i].name.indexOf("QuickTime") >= 0) {
				have_quicktime = 1;
				break;
			}
		}
	}
	if ((navigator.appVersion.indexOf("Mac") > 0) && (navigator.appName.substring(0,9) == "Microsoft") && (parseInt(navigator.appVersion) < 5) ) have_quicktime = 1; 
	return have_quicktime == 0 ;
}

function video_checkaudiofield( noteid ) {
	var r = qsn_thenotes[ noteid ] ;
	if ( r.video && ( r.video.audio == undefined ) ) {
		var k = video_moviekind ( noteid ) ;
		r.video.audio = ( k ==  "audiostream" ||  k == "audio"  ) ;
	}
}

function video_addnote ( noteid ) {
	var template = qsn_template( noteid ) ;
	var div = qsn_clonetemplate( noteid , template ) ;
	video_checkaudiofield( noteid ) ;
	qsn_renderhtml( noteid ) ;
	return div ;
}

function video_moviekind( noteid ) {
	var r = qsn_thenotes[ noteid ] ;
	if ( ! r.video.duration ) return "unknown" ;
	if ( r.video.duration == 3579139 ) return "audiostream" ;
	return ( ( r.video.width == 150 ) && ( r.video.height == 0 ) ) ? "audio" : "film" ;
}

function video_setnotecolor( x, c ) {
	var r = qsn_thenotes[ qsn_edittarget.id ] ;
	r.color= c ? c : x.style.backgroundColor ;
	qsn_setcolors( r.common.noteid ) ;
}

function video_renderhtml( noteid ) { 
	var div = getelement ( noteid ) ;
	var r = qsn_thenotes[ noteid ] ;
	var edit = false;
	if ( r.video.audio ) {
		if ( qsn_currstate == "edit" ) {
			var noteimg = gebcn( div , "video_noteimage_edit" );
			q_setimgsrc( noteimg , "/services/video/images/radio.png" ) ;
			edit = true;
		} 
	} 

	if ( qsn_currstate != "edit" ) {
		if ( r.video.duration ) {
			var kind = video_moviekind( noteid ) ;
			var s = video_message[ kind ] ;
			if ( kind != "audiostream" ) s += ", " + r.video.duration + video_message[ "seconds" ] ;
			gebcn( div , "video_infos" ).innerHTML = s ;
		} else gebcn( div , "video_infos" ).innerHTML = video_message[ kind ] ;
	}
	video_rendertext( div , r ) ;
	if ( ! edit ) {
		div.toresize = function () {
			var x = [] ;
			x.push( gebcn( this , "video_noteimage" ) ) ;
			x = x.concat( qsn_container_toresize( this ) ) ;
			return x ;
		} ;// vh 09_06_29 : bug sur IE image en % dans une table
		video_renderimg( div , r ) ;
	}
	if ( qsn_currstate == "edit" ) qsn_blurdefault ( gebcn( div , "text" ) ) ;
	qsn_container_renderhtml( div , r ) ;
	qsn_settextproperties( noteid ) ;
	qsn_setcolors( noteid ) ;
	q_changedwidth( div , parseInt( qsn_width( r.common.noteid ) , 10 ) ) ;// vh 09_06_29 : bug sur IE image en % dans une table
}

function video_rendertext( div , r ) {
	if ( r.text == undefined ) return ;
	var txt = gebcn( div , "text" ) ;
	if ( qsn_currstate != "edit" ) txt.innerHTML = wiqi_text2HTML( r.text ) ;
	else txt.value = r.text ;
}

function video_renderimg( div , r ) {
	if ( ! div ) div = qsn_edittarget ;
	if ( ! r ) r = qsn_thenotes[ div.id ] ;
	var noteimg = gebcn( div, "video_noteimage" ) ;
	if ( ( r.video ) && ( r.video.firstimage ) ) {
		if ( r.video.audio && r.video.firstimage == "/services/video/images/radio.png" ) q_setimgsrc( noteimg , r.video.firstimage ) ;
		else q_setimgsrc( noteimg , q_spaceabsoluteurl( r.common.ownerid , r.video.firstimage ) ) ;
	}
	if ( r.video.audio ) {
		noteimg.style.width = "70px" ;
		noteimg.style.top = "-8px" ;
	} else noteimg.style.top = "0px" ;
	// noteimg.style.width = r.video.audio ? "70px" : "100%" ;
	// noteimg.style.top = r.video.audio ? "-8px" : "0px" ;
}

function video_launch_upload() {
	video_request.callback = "video_new_video_done";
	var id = qsn_edittarget.id ;
	video_request.callerid = id ;
	video_request.prompt = video_message[ "selectavideo" ] ;
	video_request.title = video_message[ "uploadtitle" ] ;
	qdyn_qorgload('qu_reveal',[video_request],'qu');
}

function video_displaysettings() {
	qsn_initdisplaysetting() ;
	var r = qsn_thenotes[ qsn_edittarget.id ] ;
	if (( r.video.src == undefined ) || ( r.video.src == "" )) { // pas de vidéo
		hideblock( qsn_edittarget.id ) ;
		video_launch_upload() ;
//		hideblock ( ["video_eb_textsize", "video_eb_textstyle", "video_eb_textoptions"] ) ;
	} 
	var s = q_spacetype() ;
	if ( ( s == "qlub" ) || ( s == "fede" ) ) showblock( "video_helplink" ) ;
}

function video_showvideoinfos( id ) {
	var div = getelement( id ) ;
	var r = qsn_thenotes[ id ] ;
	gebcn ( div , "video_source" ).innerHTML = r.video.src.replace( /.*\/(.*)$/ , "$1" ) ;
	var audio = ( r.video.width == 150 ) && ( r.video.height == 0 ) ;
	visibleblock( gebcn ( div , "video_dimensions" ).parentNode , ( ! audio ) ) ;
	visibleblock( gebcn ( div , "video_duration" ).parentNode , ( ! audio ) ) ;
	if ( ! audio ) gebcn ( div , "video_dimensions" ).innerHTML = r.video.width + " x " + r.video.height + " pixels" ;
	if ( ! audio ) gebcn ( div , "video_duration" ).innerHTML = r.video.duration + video_message[ "seconds" ] ;
}

function video_saveedit() {
	var r = qsn_thenotes[ qsn_edittarget.id ] ;
	var x = gebcn( qsn_edittarget, "video_textarea" ) ;
	r.text = ( x.value ) ? x.value : "" ;
	if ( r.video.kind == "pc_local" ) {
		if ( ! confirm( video_message["pclocalnotvalidate"] ) ) return false ;
		r.img = q_cloneObject( r.imgbk ) ;
		r.imgbk = null ;
	}
	if ( ( qsn_edittarget ) && ( gebcn( qsn_edittarget , "text" ) ) ) {
		if ( r.text == video_message[ "newnoteprompt" ] ) r.text = '' ;
	}
	qsn_saveedit( true ) ;
}

function video_newnote( ) {
	var id = qsn_makenewjson( "video" ) ;
	qsn_buildnewPosition( qsn_thenotes[ id ] ) ;
	qsn_thenotes[ id ] = q_cloneObject( video_json_template , qsn_thenotes[ id ] ) ;
	qsn_currstate = "edit" ;
	qsn_thenotes[ id ].color = qsn_defaultcolor () ;
	qsn_addnote( id ) ;
	qsn_entereditmode( id ) ;
}

function video_new_video_done( x ) {
	var r = qsn_thenotes[ qsn_edittarget.id ] ;
	r.imgbk = null ;
	r.video.src = x.files[ 0 ].filename ;
	r.video.firstimage = x.files[ 0 ].firstimagefilename ;
	r.video.width = x.files[ 0 ].videowidth ;
	r.video.height = x.files[ 0 ].videoheight ;
	r.video.duration = x.files[ 0 ].videoduration ;
	r.video.size = x.files[ 0 ].filesize ;
	r.video.kind = "pc" ;
	var k = video_moviekind ( qsn_edittarget.id ) ;
	r.video.audio = ( k ==  "audiostream" ||  k == "audio"  ) ;
	if ( r.video.audio ) r.video.firstimage = "/services/video/images/radio.png" ;
	showblock( qsn_edittarget.id ) ;
	video_renderimg( qsn_edittarget , r ) ;
}

function video_entereditmode( id ) {
	var x = getelement( id ) ;
	if ( ! qsn_isnewnote ) {
		var img ;
		img = x.getElementsByTagName( "img" )[ 0 ] ;
		if ( img ) img.style.display = "none" ;
		var h = gebcn ( x , "text" ).offsetHeight ;
		if ( img ) img.style.display = "inline" ;
		x.style.display = "none" ;
		x.id = id + "_bk" ;
		qsn_thenotes[ id + "_bk" ] = q_cloneObject( qsn_thenotes[ id ] ) ;
		qsn_addnote( id ) ;
		qsn_edittarget = getelement( id ) ;
		if ( h < 32 ) h = 32 ;
		gebcn ( qsn_edittarget , "text" ).style.height = "" + h + "px" ;
	} else qsn_edittarget = x ;
	
	qsn_showlinks( qsn_edittarget , false ) ;
	qsn_setselected( qsn_edittarget , true ) ;
	qsn_showsettings( id ) ;
	qsn_dimothers( id ) ;
	qsn_puttofront ( id ) ;
	initsky() ;
}

function video_showwin(URLStr, name, left, top, width, height) {
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	return window.open(URLStr, name, styleStr, true);
}

function video_myshowwin(aurl,name,larg_wind,haut_wind, positionner, wantmini) {
	if ( wantmini == undefined ) wantmini = false;
	var haut_max = window.screen.availHeight - 40;
	var haut_screen = window.screen.availHeight;
	var larg_max = window.screen.availWidth;
	var larg_screen = window.screen.availWidth;
	var pos_left = 0;
	if (haut_wind > haut_max) haut_wind = haut_max;
	if (larg_wind > larg_max) larg_wind = larg_max;
	if ( wantmini && haut_wind < 100) haut_wind = 400;
	if ( wantmini && larg_wind < 100) larg_wind = 400;
	pos_top = 0;
	pos_left = 0;
	if (positionner) {
		pos_left = larg_screen - larg_wind;
		if (pos_left <= 0) pos_left = 50; else pos_left = pos_left/2;
		pos_top = haut_screen - haut_wind;
		if (pos_top <= 0) pos_left = 50; else pos_top = pos_top/2;
	}
	return video_showwin(aurl, name, pos_left, pos_top, larg_wind, haut_wind);
}

// ccc globales pour afficheur
// ccc v 24/04/08
var video_kind; // vidéo ou audio
var video_autostart;
var video_loop;
var video_play_src;
var video_play_title;
var video_play_w;
var video_play_h;
var video_play_movieid;
var video_play_backcolor;
var video_play_color;
var video_play_txt;
var video_r ;

function video_get_quicktime () {
	var url = "/services/video/video_get_quicktime_" + window.opener.q_lang + ".html";
	document.title = video_message[ "errqt" ] ;
	document.body.style.background = "#FFFFFF";
	window.location.href = url ;
	window.resizeTo (400,200);
}
function video_showvideo( x, v , newwin ) {
	var div = getContainer( x ) ;
	var r = qsn_thenotes[ div.id ] ;
//	var k = video_moviekind ( div.id ) ;
	// couleur de fond de la win
	var c = parseInt( r.color, 10 ) ;
	if ( c == -1 ) c = "#000";
	else c = ( c < 10 ) ? getsncontextualbackgroundcolors( 7 )[ c - 1 ] : r.color ;
	video_play_backcolor = c ;
	// couleur text audio
	if (  c =="#000" ) video_play_color = "#CCC";
	else video_play_color = rgbcontrastcolor150( c ) ;
	if ( r.video.audio  ) {
		video_kind = "audio" ; 
		video_autostart = "true";
		video_loop = "false" ;
		video_play_w = 180 ;
		video_play_h = 200;
		video_play_txt = r.text ;
		video_r = r;
		
	} else {
		video_kind = "video" ; 
		video_autostart = "true";
		video_loop = "false";
		video_play_w = r.video.width ;
		video_play_h = r.video.height ;
		
	}
	video_play_src = q_spacebaseurl( r.common.ownerid ) + encodeURI( r.video.src );
	var rr = r.video.src ;
	video_play_title = rr.substr ( rr.lastIndexOf("/") + 1 );
	video_play_movieid = "movie_" + div.id ;
	
	if ( /\.wmv$/i.test( rr ) ) video_myshowwin( video_play_src , "", video_play_w + 40, video_play_h + 55, 1) ;
	else video_myshowwin("/services/video/video_play.html", "", video_play_w + 40, video_play_h + 55, 1) ; 
	return false;	
}

var gArgCountErr =	'The "%%" function requires an even number of arguments.' +	'\nArguments should be in the form "attributeName", "attributeValue", ...';
var gTagAttrs = null ;
var gQTGeneratorVersion = 1.0 ;

function AC_QuickTimeVersion() {
	return gQTGeneratorVersion;
}

function _QTComplain(callingFcnName, errMsg) {
	errMsg = errMsg.replace("%%", callingFcnName);
	adminalert(errMsg);
}

function _QTAddAttribute(prefix, slotName, tagName) {
	var value;
	value = gTagAttrs[prefix + slotName];
	if ( null == value ) value = gTagAttrs[slotName];
	if ( null != value ) {
		if ( 0 == slotName.indexOf(prefix) && (null == tagName) ) tagName = slotName.substring(prefix.length); 
		if ( null == tagName ) tagName = slotName;
		return '' + tagName + '="' + value + '"';
	} else return "";
}

function _QTAddObjectAttr(slotName, tagName) {
	// don't bother if it is only for the embed tag
	if ( 0 == slotName.indexOf("emb#") ) return "";
	if ( 0 == slotName.indexOf("obj#") && (null == tagName) ) tagName = slotName.substring(4); 
	return _QTAddAttribute("obj#", slotName, tagName);
}

function _QTAddEmbedAttr(slotName, tagName) {
	// don't bother if it is only for the object tag
	if ( 0 == slotName.indexOf("obj#") ) return "";
	if ( 0 == slotName.indexOf("emb#") && (null == tagName) ) tagName = slotName.substring(4); 
	return _QTAddAttribute("emb#", slotName, tagName);
}

function _QTAddObjectParam(slotName, generateXHTML) {
	var paramValue;
	var paramStr = "";
	var endTagChar = (generateXHTML) ? ' />' : '>';
	if ( -1 == slotName.indexOf("emb#") ) {
		// look for the OBJECT-only param first. if there is none, look for a generic one
		paramValue = gTagAttrs["obj#" + slotName];
		if ( null == paramValue ) paramValue = gTagAttrs[slotName];
		if ( 0 == slotName.indexOf("obj#") ) slotName = slotName.substring(4); 
		if ( null != paramValue ) paramStr = '<param name="' + slotName + '" value="' + paramValue + '"' + endTagChar;
	}
	return paramStr;
}

function _QTDeleteTagAttrs() {
	for ( var ndx = 0; ndx < arguments.length; ndx++ ) {
		var attrName = arguments[ndx];
		delete gTagAttrs[attrName];
		delete gTagAttrs["emb#" + attrName];
		delete gTagAttrs["obj#" + attrName];
	}
}

// generate an embed and object tag, return as a string
function _QTGenerate(callingFcnName, generateXHTML, args) {
	if ( args.length < 4 || (0 != (args.length % 2)) ) {
		_QTComplain(callingFcnName, gArgCountErr);
		return "";
	}
	gTagAttrs = new Object();
	gTagAttrs["src"] = args[0];
	gTagAttrs["width"] = args[1];
	gTagAttrs["height"] = args[2];
	gTagAttrs["classid"] = "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B";
		//Impportant note: It is recommended that you use this exact classid in order to ensure a seamless experience for all viewers
	gTagAttrs["pluginspage"] = "http://www.apple.com/quicktime/download/";

	// set up codebase attribute with specified or default version before parsing args so
	//  anything passed in will override
	var activexVers = args[3] ;
	if ( (null == activexVers) || ("" == activexVers) ) activexVers = "6,0,2,0";
	gTagAttrs["codebase"] = "http://www.apple.com/qtactivex/qtplugin.cab#version=" + activexVers;

	var attrName, attrValue;

	for ( var ndx = 4; ndx < args.length; ndx += 2) {
		attrName = args[ndx].toLowerCase();
		attrValue = args[ndx + 1];

		// "name" and "id" should have the same value, the former goes in the embed and the later goes in
		//  the object. use one array slot 
		if ( "name" == attrName || "id" == attrName ) gTagAttrs["name"] = attrValue;
		else gTagAttrs[attrName] = attrValue;
	}

	// init both tags with the required and "special" attributes
	var objTag =  '<object '
					+ _QTAddObjectAttr("classid")
					+ _QTAddObjectAttr("width")
					+ _QTAddObjectAttr("height")
					+ _QTAddObjectAttr("codebase")
					+ _QTAddObjectAttr("name", "id")
					+ _QTAddObjectAttr("tabindex")
					+ _QTAddObjectAttr("hspace")
					+ _QTAddObjectAttr("vspace")
					+ _QTAddObjectAttr("border")
					+ _QTAddObjectAttr("align")
					+ _QTAddObjectAttr("class")
					+ _QTAddObjectAttr("title")
					+ _QTAddObjectAttr("accesskey")
					+ _QTAddObjectAttr("noexternaldata")
					+ '>'
					+ _QTAddObjectParam("src", generateXHTML);
	var embedTag = '<embed '
					+ _QTAddEmbedAttr("src")
					+ _QTAddEmbedAttr("width")
					+ _QTAddEmbedAttr("height")
					+ _QTAddEmbedAttr("pluginspage")
					+ _QTAddEmbedAttr("name")
					+ _QTAddEmbedAttr("align")
					+ _QTAddEmbedAttr("tabindex");

	// delete the attributes/params we have already added
	_QTDeleteTagAttrs("src" , "width" , "height" , "pluginspage" , "classid" , "codebase" , "name" , "tabindex" , "hspace" , "vspace" , "border" , "align" , "noexternaldata" , "class" , "title" , "accesskey");

	// and finally, add all of the remaining attributes to the embed and object
	for ( var attrName in gTagAttrs ) {
		attrValue = gTagAttrs[attrName];
		if ( null != attrValue ) {
			embedTag += _QTAddEmbedAttr(attrName);
			objTag += _QTAddObjectParam(attrName, generateXHTML);
		}
	} 
	return objTag + embedTag + '></em' + 'bed></ob' + 'ject' + '>';
}

// return the object/embed as a string
function QT_GenerateOBJECTText() {
	return _QTGenerate("QT_GenerateOBJECTText", false, arguments);
}

function QT_GenerateOBJECTText_XHTML() {
	return _QTGenerate("QT_GenerateOBJECTText_XHTML", true, arguments);
}

function QT_WriteOBJECT()
{
	document.writeln(_QTGenerate("QT_WriteOBJECT", false, arguments));
}

function QT_WriteOBJECT_XHTML()
{
	document.writeln(_QTGenerate("QT_WriteOBJECT_XHTML", true, arguments));
}

/*
function video_initforweb( id ) {
	video_showbuttons ( id ) ;
}
*/

/*
function -
function TOOLBAR
*/

function video_toolbarversion() {
	return 1 ;
}

function video_helpnote() {
//	window.open( "http://aide.quomodo.com/les_smartnotes-les_smartnotes_video" ) ; 
	window.open( "http://" + qlub_help_pages[ "a_video" ] , "quomodo help" ) ;
}

var video_js_loaded = true ;
if ( window.qdyn_loaded ) qdyn_loaded ( "video_js_loaded" ) ;
