//A CHANGER LORSQU'ON COPIE DE LA VERISON FRANCAISE: LE ID DU GOOGLE ANALYTICS DANS CHAQUE FONCTION OUVRANT UNE CONTENU


function getDirBin()
{
	
	// SI LA PAGE D'APPEL EST DANS HTTP DOC (Niveau 0)
	var dir_bin = "./";

	var path = (document.location).toString() ;
	// SI LA PAGE D'APPEL EST AU NIVEAU 1 
	if ( path.indexOf("/moteur") > -1 ) { dir_bin = "./../"; }	
	
	return dir_bin;
}
function mainfunc (func, par1, par2){
    window[func](par1, par2);
}

function calledfunc(par1, par2){
    alert("par1="+par1+" par2="+par2);
}

function popSource(target,idSource,src,idBD,searchLan,closeVisible)
{
  if (closeVisible==undefined) 
  {
      closeVisible = 1;
  }

	// HIDE VIRTUAL WORLD !!!!!!
	elCMV = document.getElementById('gamecontent');
	
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	
	if (!isIE ) 
	{
	   //alert("on est pas sur IE");
	   elCMV.style.visibility="hidden";
	}
	
	
	// GET DIR BIN FROM PATH
	var dir_bin = getDirBin();
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var paramList = ajaxRequest.responseText;
			//alert(paramList);
			var paramArray  = paramList.split("$;$");
			
			window["open_divpop"](  target,
									searchLan,
									paramArray[0],//urlSource,
									paramArray[1],//widthSource,
									paramArray[2],//heightSource,
									paramArray[3],//analyticSource,
									paramArray[4],//urlPart,
									paramArray[5],//analyticPart,
									paramArray[6],//tooltipPart,
									paramArray[7],//logoPart
									idSource,
									src,
									idBD,
									paramArray[8], // isRated
									paramArray[9], // rating
									closeVisible // is close visible ?
								);

		}
	}
	var d = new Date();
	var time=d.getTime();
	ajaxRequest.open("GET", dir_bin + "services/getSourceInfo.php?id="+idSource+"&src="+src+"&idBD="+idBD+"&lan="+searchLan+"&rand=" + time , true);
	ajaxRequest.send(null); 
	
}

/**function utiliser lorsque le contenu doit communiquer avec SEJ
*/
function popSourceCom(target,idSource,src,idBD,searchLan,keyUser,keyTrans,noExercise,closeVisible,sendData)
{
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	
	if (closeVisible==undefined) 
	{
		closeVisible = 1;
	}
   
	// HIDE VIRTUAL WORLD !!!!!!
	elCMV = document.getElementById('gamecontent');
	
	if( !isIE )
	{
		elCMV.style.visibility="hidden";
	}
	
	// GET DIR BIN FROM PATH
	var dir_bin = getDirBin();
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var paramList = ajaxRequest.responseText;
			//alert(paramList);
			var paramArray  = paramList.split("$;$");
            
            keyUser,keyTrans,noExercise
            
            if( keyTrans != "" && idBD != 0 )
            {
                if( paramArray[0].indexOf( "?" ) == -1 )
                {
                    paramArray[0] += "?"
                }
                else
                {
                    paramArray[0] += "&"
                }
                
                paramArray[0] += "sj_trans=" + keyTrans + "&sj_user=" + keyUser;
                
                if( noExercise != "" )
                {
                    paramArray[0] += "&no_exercise=" + noExercise;
                }
                
                
            }
            
            if( sendData != null )
            {
                paramArray[0] += ( paramArray[0].indexOf( "?" ) == -1 ) ? "?" : "&";
                
                paramArray[0] += "sj_data=" + escape( sendData );
            }
            
            
            
			window["open_divpop"](  target,
									searchLan,
									paramArray[0],//urlSource,
									paramArray[1],//widthSource,
									paramArray[2],//heightSource,
									paramArray[3],//analyticSource,
									paramArray[4],//urlPart,
									paramArray[5],//analyticPart,
									paramArray[6],//tooltipPart,
									paramArray[7],//logoPart
									idSource,
									src,
									idBD,
									paramArray[8], // isRated
									paramArray[9], // rating
									closeVisible // is close visible ?
								);

		}
	}
	var d = new Date();
	var time=d.getTime();
	ajaxRequest.open("GET", dir_bin + "services/getSourceInfo.php?id="+idSource+"&src="+src+"&idBD="+idBD+"&lan="+searchLan+"&rand=" + time , true);
	ajaxRequest.send(null); 
	
}

function rate_source(idSource,src,idBD,rate,search_lan)
{
	// GET DIR BIN FROM PATH
	var dir_bin = getDirBin();
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				//alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ret = ajaxRequest.responseText;
			
			//alert(ret);

			// une fois le vote effectue, les fleches de vote disparaissent
			var iFrame = document.getElementById("iFrameHeader");
			
			urlIframe = iFrame.src;
			urlIframe = urlIframe.replace("israt=0", "israt=1");
			
			// LOAD THE IFRAME WITH THE PARAMETER isRatet = 1
			iFrame.src = urlIframe;
			
		}
	}
	ajaxRequest.open("GET", dir_bin + "services/rateSource.php?id="+idSource+"&src="+src+"&idBD="+idBD+"&rt="+rate+"&lan="+search_lan, true);
	ajaxRequest.send(null); 
}
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function open_divpop(target,
					 searchLan,
					 urlSource,
					 widthSource,
					 heightSource,
					 analyticSource,
					 urlPart,
					 analyticPart,
					 tooltipPart,
					 logoPart,
					 idSource,
					 src,
					 idBD,
					 isRated,
					 rating,
					 closeVisible
					 )
{
	var ieVersion = getInternetExplorerVersion();
	var openInNewWindow=false;
	// PAGES HTML DU BIN
	var dir_bin = getDirBin();	
	
	var widFLASH = 966;

	
	var titlePart    = "";
	var heiBan = 83;

	var widthFrame  =  widthSource;
	var heightFrame = heightSource;
	// 800 = taille minimum pour afficher les logos et les fleches de vote
	if (widthFrame < widFLASH) { widthFrame = widFLASH; }

	var elbody;
	var elback;
	var elsource;

	
	
	elbody = document.getElementsByTagName("body")[0];
	
	// Affiche un filtre foncé autour du popup
	//grayOut(true,{'zindex':'50', 'bgcolor':'#00FF00', 'opacity':'70'});
	grayOut(true);

	// Taille de la fenetre
	var bodyH ;
	var bodyW ;
	
	if(elbody.offsetHeight)          {bodyH=elbody.offsetHeight;}
	else if(elbody.style.pixelHeight){bodyH=elbody.style.pixelHeight;}
	
	if(elbody.offsetWidth)          {bodyW=elbody.offsetWidth;}
	else if(elbody.style.pixelWidth){bodyW=elbody.style.pixelWidth;}
	
	//bodyH = 1500;
	bodyH = $(document).height();
	elback = document.getElementById('divpop');
	 
	elback.style.width = bodyW + "px";
	elback.style.height = bodyH + "px";
	elback.style.left = "0px"; 
	elback.style.top = "0px";
	
	elback.style.visibility = "visible";
	
	elsource = document.getElementById('divpop_source');
	var sourceT = 0;
	var sourceL = 50;
	// POUR LES VIDEOS ON DOIT SAVOIR SI L'URL EST EXTERNE OU INTERNE
	if ( src == 'VIDEO' )
	{
		// SI LA VIDEO EST HEBERGEE CHEZ NOUS
		if ( urlSource.substring(0,4) != "http" )
		{
			urlSource = dir_bin + "services/video/videoplayer.php?url=" + urlSource + "&wid=" + widthSource + "&hei=" + heightSource ;
			heightFrame = parseInt(heightFrame) + 20 ;
		}
	}
	if ( src == 'JEU' )
	{
		// SI LE JEU EST HEBERGE CHEZ NOUS
		if ( urlSource.substring(0,4) != "http" )
		{
			urlSource = dir_bin + "jeux/" + urlSource;
		}
	}
	if ( src == 'FICHE' )
	{           
    
		// SI LA FICHE EST HEBERGE CHEZ NOUS
		if ( urlSource.substring(0,4) != "http" )
		{
		  var pos = urlSource.indexOf(".",0)+1 ;
		  var ext = urlSource.substring(pos,pos+3);
		  ext = ext.toLowerCase();
		  var phpUrl = "";
		  if ( ext=="jpg" || ext=="png" || ext=="gif" || ext=="bmp") 
		  {
			phpUrl = "openimage.php" + "?url=" + urlSource ;
		  }
		  else if(ext=="pdf")
		  {
			  if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
				  openInNewWindow=true
		  }
		  	urlSource = dir_bin + "fiches/" + urlSource ;
			heightFrame = parseInt(heightFrame) + 20 ;
		}
	}
	
	if(openInNewWindow==false)
	{
		if ( widthFrame < (bodyW-100) )
		{
			sourceL = parseInt( (bodyW-widthFrame)/2 );
		}
		
		//alert(sourceT+"   "+sourceL); // sdfgsdfgdfg
		
		if (target == "null") 
		{
			sourceT = 86;
			sourceL = parseInt( (bodyW-widthFrame)/2 );
		}
		else
		{
			var pos = findPos(target);
			
			sourceT = pos[1] - parseInt(heightFrame*2/3) ;
			
		}
		
		var hScreen = $(window).height();
		

		if ( (sourceT + parseInt(heightFrame) + heiBan ) > bodyH ) { sourceT = bodyH - parseInt(heightFrame) - heiBan - 20; }

		if (sourceT < 50) { sourceT = 50; }
		if (sourceL < 20) { sourceL = 20; }
		
		var heightBack = parseInt(heightFrame) + 83 ;	
		
		elsource.style.top = sourceT + "px";
		elsource.style.left = sourceL + "px";
			
		elsource.style.width = widthFrame + "px";
		elsource.style.height = heightBack + "px";	 
				
		var isdisp = 0;
		var pos1 = urlSource.indexOf("sj_trans");
		var pos2 = urlSource.indexOf("sj_user");
		
		if ( pos1 > -1 && pos2 > -1 ) { isdisp = 1; }
		
		var url_header = dir_bin + "pop_header?dbin=" + dir_bin;
		url_header    += "&lan=" + searchLan ;
		url_header    += "&upar=" + urlPart ;
		url_header    += "&lpar=" + logoPart ;
		url_header    += "&tlpar=" + tooltipPart ;
		url_header    += "&tpar=" + titlePart ;
		url_header    += "&apar=" + analyticPart ;
		url_header    += "&israt=" + isRated ;
		url_header    += "&rate=" + rating ;
		url_header    += "&isrc=" + idSource ;
		url_header    += "&tsrc=" + src ;
		url_header    += "&ibd=" + idBD ;
		url_header    += "&wid=" + parseInt(widthFrame-5) ;
		url_header    += "&close=" + closeVisible   ;
		url_header    += "&isdisp=" + isdisp   ;
		
		var contentHTML = '';
		contentHTML += '<table border="0" cellpadding="0" cellspacing="0" width="'+widthFrame+'px" height="100%" >';
		contentHTML += '  <tr><td>';
		contentHTML += '	<iframe id="iFrameHeader" name="iFrameHeader"  src="'+ url_header + '" width='+ widthFrame +'px height="83px" scrolling=no frameborder=0 marginheight="0px" marginwidth="0px" hspace="0px" vspace="0px"></iframe>';
		contentHTML += '    </td></tr>';
		contentHTML += '  <tr bgcolor="#000000">';
		contentHTML += '  <td colspan=6 height='+heightSource+'px>';
		contentHTML += '	<iframe id="frameSource" name="frameSource" type="text/html" src="'+urlSource+'" width='+widthFrame+'px height='+heightFrame+'px scrolling=auto frameborder=0></iframe>';
		contentHTML += '  </td>';
		contentHTML += '  </tr>';
		contentHTML += '</table>';
		contentHTML += '<script type="text/javascript">try { var pageTracker = _gat._getTracker("UA-8973460-1"); pageTracker._trackPageview("' + analyticSource + '");} catch(err) {}</script>';
		
		//alert(contentHTML);
		elsource.innerHTML = contentHTML;
		
		elsource.style.visibility = "visible";
		
		removeStickyElement();
	}
	else
	{//ouvre dans une nouvelle fenetre lorsque c'est un pdf et que firefox est utiliser
		close_popup();
		openURL(urlSource);
	}
}
function removeStickyElement() 
{
	/*
	var d = document.getElementById("searchResult");
	var olddiv = document.getElementById(divId);
	d.removeChild(olddiv);
	*/
	el = document.getElementById("divSticky");
	el.style.visibility = "hidden";
	el.innerHTML = "";
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		do 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		
		return [curleft,curtop];
	}
}
function imprimerSource()
{

  frames["frameSource"].focus();  
  frames["frameSource"].print();  
}
function close_popup(isDispatch) 
{
	var elsource = document.getElementById('divpop_source');
	// TEST
	grayOut(false);
	
	if (isDispatch == undefined) 
	{
		isDispatch = 1 ;
	}
	
 	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var elback = document.getElementById('divpop');
	
	elback.innerHTML = null;
	elsource.innerHTML = null;
	
	elback.style.visibility = "hidden";
	elsource.style.visibility = "hidden";

	// UN HIDE VIRTUAL WORLD !!!!!!
	elCMV = document.getElementById('gamecontent');
	elCMV.style.visibility="visible";
	
	
	if(fullscreen){
		// version fullscreen
		var swf = document.getElementById('gamecontent');
	}else{		
		//page qui n'utilise pas le fullscreen (IE9, résolution plus petite que 900 pixels) 	
		var swf = document.getElementById('swf_cmv');
	}
  
 
 if( swf != null /*&& isDispatch == 1*/ )
  {
      swf.onPopupClose();
  }
  
}




function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    var pageWidth = $(document).width()+"px";
    var pageHeight = $(document).height()+"px";

    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}
