var count_new_records=0;


$(function(){
    if (top.location != location) {
    top.location.replace(self.location);
    }
    init_video();
	/*
	 * Init de flash
	 */
	is_flash=DetectFlashVer(8, 0, 0);
	if(is_flash) {
	swfobject.embedSWF("/swf/pastille.swf", "pastille_newsletter", "110", "110", "9.0.0", "expressInstall.swf",flashvars,params,attributes);
	swfobject.embedSWF("/swf/player.swf", "flash_player", "0", "0", "9.0.0", "expressInstall.swf");
	}
else if(first==1){
	if(confirm(flash_alert)) window.open('http://get.adobe.com/fr/flashplayer/');
};
	
	if(document.getElementById('logo'))
		Logo_init();
	
	$('#cache').css('display','none');
	
	init_select();
	init_navigator();
	init_page();

});

function slide(id){
	init_video();
	$('.slide').each(function(){
		if($(this).attr('id')!='form_'+id && $(this).css('display')!='none') $(this).slideUp(300);
	});
	if($('#form_'+id).css('display')!='none') $('#form_'+id).slideUp(300);
	else $('#form_'+id).slideDown(300);
	return false;
}

function add_records(nmb, nom_annexe, total){
	for (i=0;i<nmb;i++){
		html=$('#'+nom_annexe+'_blank_line').html().replace(/\[\]/gi, '['+(total+count_new_records)+']');
		$('#'+nom_annexe+'_new_records').append(html);
		count_new_records++;
	}
	$('.date-pick').datePicker({clickInput:true,startDate:'01/01/1990'});
	return false;
}

function testProgress() {
    var idProgress=document.getElementById("id_progress").value;
    
    var xhr=new XMLHttpRequest();
    xhr.onload=tcb; //la fonction de rappel qui gère la réponse du serveur
      xhr.open("GET","load/upload_status_.php?progress_key="+idProgress,true);
      xhr.send(null);
setTimeout('testProgress();', 500)
}

function tcb() {
    var repXhr=this.responseText; //récupération de la réponse du serveur via l'objet xmlHttpRequest (this)
    
    /*
    La réponse envoyé par le serveur étant au format texte il faut utiliser eval() pour la manipuler
    La réponse au format json ne peut être exploité directement par eval,
    il faut l'entourer de parenthèses via une concaténation pour éviter un bug
    */
    var objRep=eval("("+repXhr+")");
    //affichage des informations
if(objRep.current!=objRep.total)
   $('.progressbar').html("Progression: " + Math.round(objRep.current/objRep.total*100)+"%");
else $('.progressbar').html('Veuillez patienter...');
    
    //tant que l'upload est en cours le serveur est réinterrogé
   // if (objRep.done==0) {  }
} 

function ajax_update(id){
	url=$('#'+id).attr('action');
	data='';
	$(':input','#'+id).each(function(){
		data+=$(this).attr('name')+"="+$(this).val()+'&';
		this.disabled=true;
	});
	data+="id="+id;
	 $.ajax({
		   type: "POST",
		   url: url,
		   data: data,
		   success: function(){
	 		$(':input','#'+id).each(function(){
				this.disabled=false;
	 		});
		   }
		 });
	return false;
}

function init_navigator() {
	if (navigator.userAgent.indexOf('Firefox/3',0)!=-1) attribute='firefox3';
	if (navigator.userAgent.indexOf('Iceweasel',0)!=-1) attribute='linux';
	if (navigator.userAgent.indexOf('Safari',0)!=-1) attribute='safari';
	if (navigator.userAgent.indexOf('MSIE 6',0)!=-1) attribute='explorer6';
	if (navigator.userAgent.indexOf('Opera',0)!=-1) attribute='opera';
	if (attribute!='explorer6'&& navigator.userAgent.indexOf('MSIE',0)!=-1) attribute='explorer';
	feuilles=document.getElementsByTagName('link');
	for (var i=0; i<feuilles.length;i++) {
	if((feuilles[i].getAttribute('rev')!=attribute)&&(feuilles[i].getAttribute('rev'))) feuilles[i].disabled='true'; 
	}
}

function flash_detection(){
	if( navigator.mimeTypes.length > 0 )
	{
		return navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin != null;
	}
	else if( window.ActiveXObject )
	{
		try
		{
			new ActiveXObject
( "ShockwaveFlash.ShockwaveFlash" );
			return true;
		}
		catch( oError )
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function init_page(){	

	/*
	 * Transparence des png pour ie6
	 */
	if(attribute=='explorer6') fix_ie();
}

function init_navigator() {
	if (navigator.userAgent.indexOf('Firefox/3',0)!=-1) attribute='firefox3';
	if (navigator.userAgent.indexOf('Iceweasel',0)!=-1) attribute='linux';
	if (navigator.userAgent.indexOf('Safari',0)!=-1) attribute='safari';
	if (navigator.userAgent.indexOf('MSIE 6',0)!=-1) attribute='explorer6';
	if (navigator.userAgent.indexOf('Opera',0)!=-1) attribute='opera';
	if (attribute!='explorer6'&& navigator.userAgent.indexOf('MSIE',0)!=-1) attribute='explorer';
	feuilles=document.getElementsByTagName('link');
	for (var i=0; i<feuilles.length;i++) {
	if((feuilles[i].getAttribute('rev')!=attribute)&&(feuilles[i].getAttribute('rev'))) feuilles[i].disabled='true'; 
	}
}

function Logo_init() {
	Logo=document.getElementById('logo').getElementsByTagName('img')[0];;
//	Logo.img=document.getElementById('logo').getElementsByTagName('img')[0];
	Logo.imageOut=new Image();
	Logo.imageOut.src=Logo.src;
	Logo.imageIn=new Image();
	Logo.imageIn.src="/design_new/logo_1.png";
	Logo.onmouseout=Logo_out;
	Logo.onmouseover=Logo_over;
}

function Logo_over() {this.src=this.imageIn.src;}
function Logo_out() {this.src=this.imageOut.src;}

function init_select()
{
  /* Find all friendly UL's */
  $('ul.SearchableSelect').each(function()
  {
    /* Hide list element */
    $(this).css("display", "none");
  
    /* Create new ID for the select, use existent Id of UL but add the _Select prefix */
    var ElementId = $(this).attr("id") + "_Select";
    
    /* Create new Select-element */
    $(this).after("<select id='" + ElementId + "' class='SearchableSelect'></select>");
    
    /* Loop-through list and add children to select */
    $(this).find("li").each(function()
    {
      /* Setup values */
      var Anchor = $(this).find("a").eq(0), Value = (Anchor.length > 0 ? $(Anchor).attr("href") : '');
      var SelectedElement = ($(this).hasClass("SelectedItem") ? ' selected' : '');
      
      /* Add child to select */
      $("#"+ElementId).append("<option value='"+Value+"'"+SelectedElement+">"+$(this).html()+"</option>");
    });
  });
  
  /* Hook selects */
  $("select.SearchableSelect").change(function()
  {
    if(this.value.length > 0) return ajax(this.value);
  });
};

function fix_ie(){
	var images;
	images=document.getElementsByTagName('img');
	   for(i=0; i<images.length; i++)
	   {
	      var img = images[i];
	      var imgName = img.src.toUpperCase();
	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	      {
	         var imgID = (img.id) ? "id='" + img.id + "' " : "";
	         var imgClass = (img.className) ? "class='" + img.className + "' " : "";
	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
	         var imgStyle = img.style.cssText ;
		if(parseInt($(img).css('width'))>0)width=parseInt($(img).css('width'));
	else width=img.width;
		if(parseInt($(img).css('height'))>0)height=parseInt($(img).css('height'));
	else height=img.height;
	         if (img.align == "left") imgStyle = "float:left;" + imgStyle;
	         if (img.align == "right") imgStyle = "float:right;" + imgStyle;
	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
	         + " style=\"display:block; " + "width:" + width + "px; height:" + height + "px;" + imgStyle + ";\""
		+ "><span class='progid' style=\"display:inline-block; " + "width:" + width + "px; height:" + height + "px;" + imgStyle + ";"
	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	         + "(src=\'" + img.src + "\', sizingMethod='crop');\">&nbsp;</span></span>" ; 
	     
	img.outerHTML = strNewHTML;
	         i = i-1;
	      }
	   }
	}

function init_video(){
	$('.backoffice_video').each(function(){
		flowplayer($(this).attr('id'), {src:"http://disquaire-online/swf/flowplayer-3.1.5.swf",wmode:'opaque'}, {
			plugins: {
				controls: {
			      backgroundColor: '#373535',
			      backgroundGradient: 'none',
			      durationColor: '#88a423',
			      buttonOverColor: '#53Cdf3',
			      buttonColor: '#209DE8',
			      bufferGradient: 'none',
			      sliderColor: '#444444',
			      tooltipColor: '#5F747C',
			      progressColor: '#209DE8',
			      bufferColor: '#AAAAAA',
			      progressGradient: 'medium',
			      timeBgColor: '#555555',
			      volumeSliderColor: '#000000',
			      volumeSliderGradient: 'none',
			      borderRadius: '0px',
			      sliderGradient: 'none',
			      tooltipTextColor: '#ffffff',
			      timeColor: '#b8bca9',
			      height: 10,
			      opacity: 1.0,
			      volume:false,
			      time:false,
			      fullscreen:false,
			      mute:false
			   }
			},
			clip : {          
		        autoPlay: false, 
		        autoBuffering: true,
		        url: $(this).attr('rel')
			}
		});
	});
	return;
}