/*
    ________________________________________________
   |                                                |
   |              FONCTIONS UTILES                  |
   |________________________________________________|
*/

// Confli JQuery Prototype
var $j = jQuery.noConflict();
$j(document).ready(function(){
    $j("#main_content img").each(function(i){ ImgLoad(this);} );
	$j("#main_content p.videoyoutube").each(function(i){ youtubeloader(this);} );
	$j("#main_content p.videoannex").each(function(i){  annexvideoflv(this);} );
	$j("#main_content p.mp3").each(function(i){  mp3minipalyer(this);} );
});

function trim(myString)
{
        return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

/*
    ________________________________________________
   |                                                |
   |            INCLUSION DES VIDEOS                |
   |________________________________________________|

*/

// Fichier son en Annex
function mp3minipalyer(e)
{
	e=$j(e);
	var myUrl=trim(e.text());
	var myHtml = '<p class="mp3">';
	myHtml    += '<object type="application/x-shockwave-flash" data="js/player_mp3_maxi.swf" width="350" height="20">';
	myHtml    += '<param name="movie" value="js/player_mp3_maxi.swf">';
	myHtml    += '<param name="FlashVars" value="mp3=';
	myHtml    += myUrl;
	myHtml    += '&amp;autoload=1&amp;showstop=1&amp;showinfo=1&amp;showvolume=1">';
	myHtml    += '</object>';
	myHtml    += '</p>';
	
	e.before(myHtml);
	$j(e).remove();

}

// Video Annex (flv, mp4)
function annexvideoflv(e)
{
	e=$j(e);
	var myUrl=trim(e.text());
	var myHtml = '<object id="flowplayer" width="420" height="340" data="js/flowplayer.swf"   ';
	myHtml    += 'type="application/x-shockwave-flash"><param name="movie" value="js/flowplayer.swf" />';
	myHtml    += '<param name="allowfullscreen" value="true" />';
	myHtml    += '<param name="bgcolor" value="#191a1b" />';
	myHtml    += ' <param name="flashvars" value=';
	myHtml    += "'";
	myHtml    += 'config={"clip":{"autoPlay":false,"url":"';
	myHtml    += myUrl;
	myHtml    += '"}, "controls":{"url":"js/flowplayer.controls.swf"}}';
	myHtml    += "'";
	myHtml    += ' /></object>';

	e.before(myHtml);
	$j(e).remove();
}

// Videos YouTube
function youtubeloader(e)
{
	//var you_url   = trim($j(e).text());
	e=$j(e);
	// http://www.youtube.com/watch?v=mJViTxIIJ88
	// http://www.youtube.com/v/mJViTxIIJ88	
	var myUrl  =  trim(e.text());
 	myUrl = myUrl.replace('watch?v=','v/');	
	var myHtml  = '<object width="420" height="340"><param name="movie" value="';
	//myHtml     += 'http://www.youtube.com/v/mJViTxIIJ88';
	myHtml     +=  myUrl;
	myHtml     += '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess"';
	myHtml     += ' value="always"></param><embed src="';
	//myHtml     += 'http://www.youtube.com/v/mJViTxIIJ88'
	myHtml     +=  myUrl;
	myHtml     += '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"';
	myHtml     += ' width="425" height="344"></embed></object>';
	e.before(myHtml);

        $j(e).remove();
}

/*  ________________________________________________
   |                                                |
   |  POST-CORRECTIONS & FONCTIONALITEs AJOUTEES    |
   |________________________________________________|
*/

// Resize image after loading
function ImgLoad(myimg)
{	
	if( $j.browser.msie  ){
	   $j(myimg).ready( function(i){ ImgsResize(myimg);} );
 	} 
	else {
	   $j(myimg).load( function(i){ ImgsResize(myimg);} );	
	}
	
}


// Correction tailles images et zoom 
function ImgsResize(e)
{
	e=$j(e);
	if( e.parent().width() < 1) return false;
	if( e.width() < 1 ) return false;
	if( e.height() < 1) return false;
				
	if( e.width() <= e.parent().width() ) return false;
	
	var my_img_ratio = e.height()/e.width();
	var new_width    = e.parent().width();
	var my_offset    =  parseInt( e.offset().left - $j('#main_content').offset().left );	
	//my_offset +=  my_offset;
	if( (my_offset >0 ) && (my_offset<new_width) ) new_width -= my_offset; 

	var new_height   = Math.round( new_width*my_img_ratio );

	e.attr('width',  new_width);
	e.attr('height', new_height);

 	var e_html = $j('<div>').append(e.clone()).remove().html();
	e.replaceWith('<a href="' + e.attr('src') + '" target="_blank">'+ e_html + '</a>');
	return true;	
}

/*  ________________________________________________
   |                                                |
   |  CODE VALIDATION EMAIL                         |
   |________________________________________________|
*/

// Charge l'image de securite
function LoadImage(AntidotUrl)
{
	var params = '';	
	
	try{
		$j('imgValidation').remove();
		$j('imgKey').remove();
	} catch(err) { /* Some error.. */ }
	
	$j.get(AntidotUrl, function(data) {
		//alert('Load was performed.');
  		$j('#ControlImgContent').html(data);  		
	});
							
}

// Envoi les donnees Email
function SendMail(AntidotUrl)
{
	var email_content_user_to         = document.getElementById('email_content_user_to').value; 
	var email_content_user_mailto     = document.getElementById('email_content_user_mailto').value;
	var email_content_user_from       = document.getElementById('email_content_user_from').value;
	var email_content_user_mailfrom   = document.getElementById('email_content_user_mailfrom').value;
	var email_content_user_message    = document.getElementById('email_content_user_message').value;
	var email_content_user_key        = document.getElementById('email_content_user_key').value;
	var email_content_user_controlkey = document.getElementById('imgKey').value;
	var email_content_url             = document.location.href;
	var email_content_id              = document.getElementById('email_content_id').value;		
	
	document.getElementById('email_content_sender').disabled=true;	
	$j('#VerifReturnInfos').hide();
		
	AntidotUrl += "?sendmail";
	$j.post(AntidotUrl, 
			{ 
			  email_content_user_to: email_content_user_to, 
			  email_content_user_mailto: email_content_user_mailto,
			  email_content_user_from: email_content_user_from,
			  email_content_user_mailfrom: email_content_user_mailfrom,
			  email_content_user_message: email_content_user_message,
			  email_content_user_key: email_content_user_key,
			  email_content_user_controlkey: email_content_user_controlkey,
			  email_content_url: email_content_url,
			  email_content_id: email_content_id  
			}, 
			function(data){
				// Traitement de la reponse...
				// alert('Load was performed.');
				if(data.indexOf('OK') < 0) {
					$j('#VerifReturnInfos').show("slow");				
					document.getElementById('email_content_sender').disabled=false;
				} 
				else {
					document.getElementById('email_content_sender').className = "disabled";
					$j('#Message_OK').show("slow");					
				}
			});

	//document.getElementById('contentcontainer').style.display='block';	
	
}


