// drawplugin.js

/*************************************** drawFlash ************************************/
function drawPluginFlash(url,height,width) {
	var html = "<!--[if IE]><style type=\"text/css\">div {position: absolute;top: 0px;left: 0px;}</style><![endif]-->";
    html += "<div><object  classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\" height=\""+height+"\" width=\""+width+"\" style=\"position:absolute; background-color:blue; left:0px; top:0px;\">";
    html += "<param name=\"movie\" value=\""+url+"?r=" + new Date().getTime()+"\" id=\"myFlash\">";
    html += "<embed id=\"flashEmbed\" src=\""+url+"?r=" + new Date().getTime()+"\" type=\"application/x-shockwave-flash\" height=\""+height+"\" width=\""+width+"\" pluginspace=\"http://www.macromedia.com/go/getflashplayer\">";
    html += "</embed></object></div>";
	return html;
}

/*************************************** drawVideoFlash ************************************/
function drawPluginAudioMP3(url,height,width) {
	// url du player Flash
	var swf = "../../../../ahp/flash/mp3_player.swf?r=" + new Date().getTime();
	var widthPlayer = 430;
	var heightPlayer = 80;
	
	var html = "<object id=\"mp3-player\" type=\"application/x-shockwave-flash\" data=\""+swf+"\" width=\""+widthPlayer+"\" height=\""+heightPlayer+"\">";
    html = html + "<param name=\"movie\" value=\""+swf+"\" />";
	html = html + "<param name=\"FlashVars\" value=\"mp3Url=" + escape(url) + "?r=" + new Date().getTime() + "\"/>";
	html = html + "<param name=\"bgcolor\" value=\"#ffffff\" />";
	html += "</object>";
	
	return html;

}

/*************************************** drawVideoFlash ************************************/
function drawPluginVideoFlash(url,height,width,startImg) 
{
	// traitement url
	var sep = 0;
	var rtmpUrl = "";
	var flvUrl = url;
	
	// Prise en compte de tous les protocoles rtmp
	if ((url.indexOf('rtmp://') != -1) ||           
		(url.indexOf('rtmpt://') != -1) ||           
		(url.indexOf('rtmpe://') != -1) || 
		(url.indexOf('rtmpte://') != -1) ||	
		(url.indexOf('rtmps://') != -1))
	{
		// rtmp
		sep = url.indexOf('/',url.indexOf('/',url.indexOf('//')+2)+1)+1;
		rtmpUrl = url.substring(0,sep-1);
		flvUrl = url.substring(sep, url.length);
		// Format flash
		if ((flvUrl.lastIndexOf(".flv") + 4) == flvUrl.length)
			flvUrl = url.substring(sep, url.length-4);
	}
	
	var swf = "../../../../ahp/flash/simple_player.swf?r=" + new Date().getTime();
	//var html = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" height=\""+height+"\" width=\""+width+"\">";
	var html = "<object id=\"flash-player\" type=\"application/x-shockwave-flash\" data=\""+swf+"\" width=\""+width+"\" height=\""+height+"\">";
    html = html + "<param name=\"movie\" value=\""+swf+"\" />";
	if (rtmpUrl != "")
		html = html + "<param name=\"FlashVars\" value=\"netconnection=" + escape(rtmpUrl) + "&amp;flv=" + escape(flvUrl) + "?r=" + new Date().getTime()+"&amp;width="+width+"&amp;height="+height+"&amp;showstop=1&amp;autoplay=0&amp;autoload=1&amp;showiconplay=1&amp;showvolume=1&amp;showtime=1&amp;showfullscreen=1&amp;buttoncolor=3CAFEA&amp;buttonovercolor=096FB1&amp;loadingcolor=3CAFEA&amp;slidercolor1=3CAFEA&amp;slidercolor2=096FB1&amp;sliderovercolor=096FB1&amp;loadingcolor=3CAFEA&amp;playeralpha=50&amp;margin=0&amp;buffercolor=3CAFEA&amp;buffershowbg=0&amp;ondoubleclick=fullscreen";
	else
		html = html + "<param name=\"FlashVars\" value=\"flv="+escape(flvUrl)+"?r="+new Date().getTime()+"&amp;width="+width+"&amp;height="+height+"&amp;showstop=1&amp;autoplay=0&amp;autoload=1&amp;showiconplay=1&amp;showvolume=1&amp;showtime=1&amp;showfullscreen=1&amp;buttoncolor=3CAFEA&amp;buttonovercolor=096FB1&amp;loadingcolor=3CAFEA&amp;slidercolor1=3CAFEA&amp;slidercolor2=096FB1&amp;sliderovercolor=096FB1&amp;loadingcolor=3CAFEA&amp;playeralpha=50&amp;margin=0&amp;buffercolor=3CAFEA&amp;buffershowbg=0&amp;ondoubleclick=fullscreen";

	// image de depart avant lancement video
	if ((startImg!=null) && (startImg!=""))
		html = html + "&amp;startimage=" + startImg;
	html = html + "\"/>";
	html += "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
	html += "<param name=\"allowFullScreen\" value=\"true\" />";
	//html += "<embed src=\""+swf+"\" quality=\"high\" bgcolor=\"#ffffff\" width=\""+width+"\" height=\""+height+"\" name=\"simple_player\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" FlashVars=\"flv="+escape(url)+"&amp;width="+width+"&amp;height="+height+"&amp;showstop=1&amp;autoplay=1&amp;showvolume=1&amp;showtime=1&amp;showfullscreen=1&amp;buttoncolor=3CAFEA&amp;buttonovercolor=096FB1&amp;loadingcolor=3CAFEA&amp;slidercolor1=3CAFEA&amp;slidercolor2=096FB1&amp;sliderovercolor=096FB1&amp;loadingcolor=3CAFEA&amp;playeralpha=50&amp;margin=0&amp;buffercolor=3CAFEA&amp;buffershowbg=0&amp;ondoubleclick=fullscreen\"/>";
	html += "</object>";
	
	return html;
}
/*************************************** drawReal ************************************/
function drawPluginReal(url,height,width,autostart,showcontrols) {
    var html;
	if (showcontrols)
	{
	    html = drawObjectReal(url,height-20,width,autostart,false);
	    html +=drawObjectReal(url,20,width,autostart,true);
	}
	else
	    html = drawObjectReal(url,height,width,autostart,false);
    return html;
}
function drawObjectReal(url,height,width,autostart,controls) {
	var show = controls?"controlpanel":"ImageWindow";
	var id = controls?"ControlPanel":"MediaPlayer";
  	var html = "<object classid=\"clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA\" id=\""+id+"\" ";
  	html += "width=\""+width+"\" ";
  	html += "height=\""+height+"\">";
  	html += "<param name=\"src\" value=\""+url+"?r=" + new Date().getTime()+"\"></param>";
    html += "<param name=\"controls\" value=\""+show+"\"/>";
  	html += "<param value=\"Clip1\" name=\"console\"/>";
  	html += "<param value=\""+autostart+"\" name=\"autostart\"/>";
  	html += "<param value=\"true\" name=\"maintainaspect\"/>";
  	html += "<embed name=\""+id+"\" type=\"audio/x-pn-realaudio-plugin\" autostart=\""+autostart+"\" ";
  	html += "console=\"Clip1\" maintainaspect=\"true\" ";
  	html += "controls=\""+show+"\" ";
  	html += "src=\""+url+"?r="+new Date().getTime()+"\" ";
  	html += "width=\""+width+"\" ";
  	html += "height=\""+height+"\">";
  	html += "</embed></object>";
  	return html;
}
/*************************************** drawWms ************************************/
function drawPluginWms(url,height,width,autostart,showcontrols) {
	var auto = autostart?1:0;
	var show = showcontrols?1:0;
  	var html = "<object data=\"\" id=\"MediaPlayer\" ";
  	if (navigator.userAgent.indexOf('MSIE') != -1)
  	    html += "codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" classid=\"clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6\" standby=\"Loading Microsoft Windows Media 	Player components...\" type=\"application/x-oleobject\" ";
  	else
  	    html += "type=\"video/x-ms-wmv\" ";
  	html += "width=\""+width+"\" ";
  	html += "height=\""+height+"\">";
  	html += "<param name=\"url\" value=\""+url+"?r=" + new Date().getTime()+"\">";
  	if (!showcontrols)
  	    html += "<param name=\"UIMode\" value=\"none\">";
  	html += "<param name=\"ShowStatusBar\" value=\""+show+"\">";
  	html += "<param name=\"ShowControls\" value=\""+show+"\">";
  	html += "<param name=\"src\" value=\""+url+"?r=" + new Date().getTime()+"\">";
  	html += "<param name=\"stretchToFit\" value=\"1\">";
  	html += "<param name=\"autostart\" value=\""+auto+"\">";
  	html += "</object>";
  	return html;
}
/************************************** drawImage *********************************/
function drawImage(url,maxHeight,maxWidth,resize) {
	var img = new Image();
    img.src = url;
    if (resize && !img.complete) {
    	// Image is not loaded
    	// Load it before !
    	return null;
    }
    var html = "<img src=\"";
    html += url+"?r=" + new Date().getTime();
    html += "\" ";
    if (resize) {
    	var size = resizeImage(img,maxHeight,maxWidth);
	    if (size[0]>0) {
	    	html += "height=\""+size[0]+"\" ";
	    }
	    if (size[1]>0) {
	    	html += "width=\""+size[1]+"\" ";
	    }
	}
    html += "/>";
    return html;
}
function resizeImage(img,maxHeight,maxWidth) {
    var realHeight = img.height;
    var realWidth = img.width;
  	var drawHeight = realHeight;
  	var drawWidth = realWidth;
  	if (drawWidth == 0) {
  		drawWidth = maxWidth;
  	}
  	if (drawHeight == 0) {
  		drawHeight = maxHeight;
  	}
    // Si la largeur ou la hauteur depasse la taille maximale
    if ((realHeight > maxHeight) || (realWidth > maxWidth)) {
      // Si la largeur et la hauteur depasse la taille maximale
      if ((realHeight > maxHeight) && (realWidth > maxWidth)) {
        // On positionne la hauteur à max
        drawHeight = maxHeight;
        // On recalcule la taille proportionnellement
        drawWidth = parseInt((realWidth * drawHeight) / realHeight, 10);
        if (drawWidth > maxWidth) {
          // C'est pas bon. On positionne la largeur à max.
          drawWidth = maxWidth;
          // On recalcule la taille proportionnellement
          drawHeight = parseInt((realHeight * drawWidth) / realWidth, 10);
        }
      } else if ((realHeight > maxHeight) && (realWidth <= maxWidth)) {
        // Si la hauteur depasse la taille maximale
        drawHeight = maxHeight;
        // On recalcule la taille proportionnellement
        drawWidth = parseInt((realWidth * drawHeight) / realHeight, 10);
      } else if ((realHeight <= maxHeight) && (realWidth > maxWidth)) {
        // Si la largeur depasse la taille maximale
        drawWidth = maxWidth;
        // On recalcule la taille proportionnellement
        drawHeight = parseInt((realHeight * drawWidth) / realWidth, 10);
      }
    }
	var size = new Array(2);
	size[0] = drawHeight;
	size[1] = drawWidth;
	return size;
}

