/**
 * this file is part of SILEX v0-7-2 release
 * SILEX : RIA developement tool - see https://sourceforge.net/projects/silex
 *
 * SILEX is (c) 2007 Alexandre Hoyau and is released under the GPL License:

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License (GPL)
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	To read the license please visit http://www.gnu.org/copyleft/gpl.html

**/

cSilex.prototype._timerID;
cSilex.prototype._oldUrlHash;
function cSilex()
{
// called after initialization of its attributes :	this.init();
}
// popup function
cSilex.prototype.pop = function($url,$width,$height)
{
	window.open ($url, '', config='height='+$height+', width='+$width+', toolbar=no, menubar=no, resizable=no, location=no, directories=no, status=no'); //scrollbars=no, 
}

// *************
// functions for Deeplinking
cSilex.prototype.init = function($initialSectionName)
{
	cSilex.prototype._oldUrlHash=getUrlHash();//top.location.hash.toString().substring(1);
	// ? this._oldUrlHash=top.location.hash.toString().substring(1);
	cSilex.prototype._timerID = setInterval("cSilex.prototype.checkUrlHash()", 50);
	//alert("init : "+cSilex.prototype._oldUrlHash);


	// case of a page linked to a specific page (<=> as if there was a hash value)
	if (cSilex.prototype._oldUrlHash=="" && $initialSectionName) cSilex.prototype._oldUrlHash=$initialSectionName;

	if (cSilex.prototype._oldUrlHash=="") this.tagSection("index");
	else this.tagSection(cSilex.prototype._oldUrlHash);

	cSilex.prototype.phpmyvisitesSite=""; // for phpMyVisites
	cSilex.prototype._uacct = ""; // for google analytics
}
cSilex.prototype.checkUrlHash = function()
{
	// removes the # from hash
	$hashValue=getUrlHash();//top.location.hash.toString().substring(1);

	if (this._oldUrlHash!=$hashValue && $hashValue)
	{
		//alert(this._oldUrlHash+" - "+$hashValue);

		// sends the new path to silex
		document.getElementById('ROOTS').SetVariable('silex_exec_str','immediat set_path:'+$hashValue);
		
		/*
		// Debug
		alert("checkUrlHash : "+this._oldUrlHash+"!="+$hashValue+"    -     "+document.getElementById('ROOTS'));
		//document.getElementById("debug").value=$hashValue;
		/**/

		this._oldUrlHash=$hashValue;
		
		// opens history.swf
		//document.getElementById("ascom").location='history.php?targetPath_str='+$hashValue;
		//document.getElementById("ascom").innerHTML="<IFRAME SRC='history.php?targetPath_str="+$hashValue+"' width=100% height=100% />";
	}
}
cSilex.prototype.changeSection = function($sectionName)
{
	//alert("changeSection "+$sectionName);
	if (this._oldUrlHash!=$sectionName)
	{
		/*
		// Debug
		alert("changeSection "+this._oldUrlHash+"!="+$sectionName);
		/**/

		//unFocus.History.addHistory($sectionName);

		// Modify the url
		setUrlHash($sectionName);

		/* workaround IE bug :
		$hashLength=top.location.hash.toString().length;
		$urlLength=top.location.href.toString().length;
		$urlNoHash=top.location.href.toString().substring(0,$urlLength-$hashLength);
		//alert($urlNoHash);
		top.location.href=$urlNoHash+"#"+$sectionName;
		*/
		this.tagSection($sectionName);
	}
}
// called by silex when an icone is pressed
// unused :
cSilex.prototype.initiateChangeSection=function()
{
//	setUrlHash("");
}
cSilex.prototype.tagSection = function($sectionName)
{
	$sectionName=$sectionName.replace(/-/g,"");

	//alert("fenetre de debugage - tagSection "+$sectionName);
	// loads stats frame
	if (this.phpmyvisitesSite && this.phpmyvisitesSite!="")
	{
		//alert("tag with phpMyVisites : "+this.phpmyvisitesSite+" - "+cSilex.prototype.phpmyvisitesSite);
		trace("tag with phpMyVisites : "+this.phpmyvisitesSite+" - "+cSilex.prototype.phpmyvisitesSite);
		pmv_log(this.phpmyvisitesSite, this.phpmyvisitesURL, $sectionName, Array());
	}
	// call google analytics script
	if (this._uacct!="")
	{
		//alert("urchinTracker("+$sectionName+")");
		trace("urchinTracker("+$sectionName+")");
		_uacct = this._uacct;
		urchinTracker($sectionName);
		/*
		<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">_uacct="UA-xxxx-x";urchinTracker();</script>
		
				WriteLayer("googleAnalFrame","","");
				tmp_str='<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">_uacct="'+_uacct+'";urchinTracker();</script>';
				WriteLayer("googleAnalFrame","",tmp_str);
		*/
	}
	// CyberEstat
	if (this.CM_CLIENT)
	{
		this.logCyberEstat($sectionName);
	}
}
cSilex.prototype.tagCurrentSection = function()
{
//	alert("tesxxt "+this._oldUrlHash);
//	alert("tesxxt "+top.location.hash.toString().substring(1));
//	this.tagSection(this._oldUrlHash);
	this.tagSection(getUrlHash());//top.location.hash.toString().substring(1));
}

// ************
function WriteLayer(ID,parentID,sText) 
{// marche pour div like : 		<div id="googleAnalFrame" name="googleAnalFrame" />
	 if (document.layers) {
	   var oLayer;
	   if(parentID){
	     oLayer = eval('document.' + parentID + '.document.' + ID + '.document');
	   }else{
	     oLayer = document.layers[ID].document;
	   }
	   oLayer.open();
	   oLayer.write(sText);
	   oLayer.close();
	 }
	 else if (parseInt(navigator.appVersion)>=5&&navigator.
	appName=="Netscape") {
	   document.getElementById(ID).innerHTML = sText;
	 }
	 else if (document.all) document.all[ID].innerHTML = sText
}
// include function does not work in IE
function include(fileName) 
{
    if (document.getElementsByTagName) {
        Script = document.createElement("script");
        Script.type = "text/javascript";
        Script.src = fileName;
        Body = document.getElementsByTagName("BODY");
        if (Body) {
            Body[0].appendChild(Script);
        }
    }
}
// *******************
// code for google analytics
cSilex.prototype.setTrackingCode=function(uacct)
{
	// 		<script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script>
	// 		<div id="googleAnalFrame" name="googleAnalFrame"></div>
	// does not work in IE : include("http://www.google-analytics.com/urchin.js");
	// WriteLayer("googleAnalFrame","",'<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>');
	this._uacct=uacct;
	_uacct=uacct;
}
//********************
// functions for phpMyVisite
cSilex.prototype.setPhpmyvisites = function($phpmyvisitesURL,$phpmyvisitesSite)
{
//	include($phpmyvisitesURL+"silex_phpmyvisites.js");
	this.phpmyvisitesSite=$phpmyvisitesSite;
	this.phpmyvisitesURL=$phpmyvisitesURL;
	//cSilex.prototype.phpmyvisitesSite=$phpmyvisitesSite;
}
// http://www.phpmyvisites.net/ 
// License GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
function pmv_log(pmv_site, pmv_url, pmv_pname, pmv_vars)
{
	function plugMoz(pmv_pl) {
		if (pmv_tm.indexOf(pmv_pl) != -1 && (navigator.mimeTypes[pmv_pl].enabledPlugin != null))
			return '1';
		return '0';
	}
	function plugIE(pmv_plug){
		pmv_find = false;
		//document.write('<SCR' + 'IPT LANGUAGE=VBScript>\n on error resume next \n pmv_find = IsObject(CreateObject("' + pmv_plug + '"))</SCR' + 'IPT>\n');
		WriteLayer("stats","",'<SCR' + 'IPT LANGUAGE=VBScript>\n on error resume next \n pmv_find = IsObject(CreateObject("' + pmv_plug + '"))</SCR' + 'IPT>\n');
		if (pmv_find) return '1';
		return '0';
	}
	var pmv_jav='0'; if(navigator.javaEnabled()) pmv_jav='1';
	var pmv_agent = navigator.userAgent.toLowerCase();
	var pmv_moz = (navigator.appName.indexOf("Netscape") != -1);
	var pmv_ie= (pmv_agent.indexOf("msie") != -1);
	var pmv_win = ((pmv_agent.indexOf("win") != -1) || (pmv_agent.indexOf("32bit") != -1));
	
	if (!pmv_win || pmv_moz){
		pmv_tm = '';
		for (var i=0; i < navigator.mimeTypes.length; i++)
			pmv_tm += navigator.mimeTypes[i].type.toLowerCase();
		var pmv_dir = plugMoz("application/x-director");
		var pmv_fla = plugMoz("application/x-shockwave-flash");
		var pmv_pdf = plugMoz("application/pdf");
		var pmv_qt = plugMoz("video/quicktime");
		var pmv_rea = plugMoz("audio/x-pn-realaudio-plugin");
		var pmv_wma = plugMoz("application/x-mplayer2");
	} else if (pmv_win && pmv_ie){
		var pmv_dir = plugIE("SWCtl.SWCtl.1");
		var pmv_fla = plugIE("ShockwaveFlash.ShockwaveFlash.1");
		var pmv_pdf = '0'; 
		if (plugIE("PDF.PdfCtrl.1") == '1' || plugIE('PDF.PdfCtrl.5') == '1' || plugIE('PDF.PdfCtrl.6') == '1') 
			pmv_pdf = '1';
		var pmv_qt = plugIE("QuickTimeCheckObject.QuickTimeCheck.1");
		var pmv_rea = plugIE("rmocx.RealPlayer G2 Control.1");
		var pmv_wma = plugIE("MediaPlayer.MediaPlayer.1");
	}
	
	var getvars='';
	for (var i in pmv_vars){
		if (!Array.prototype[i]){
			getvars = getvars + '&a_vars['+ escape(i) + ']' + "=" + escape(pmv_vars[i]);
		}
	}
	pmv_do = document; 
	pmv_da = new Date();
	try {rtu = top.pmv_do.referrer;} catch(e) {
		try {rtu = pmv_do.referrer;} catch(E) {rtu = '';}
	}
	
	src = pmv_url;
	src += '?url='+escape(pmv_do.location)+'&pagename='+escape(pmv_pname)+getvars;
	src += '&id='+pmv_site+'&res='+screen.width+'x'+screen.height+'&col='+screen.colorDepth;
	src += '&h='+pmv_da.getHours()+'&m='+pmv_da.getMinutes()+'&s='+pmv_da.getSeconds();
	src += '&flash='+pmv_fla+'&director='+pmv_dir+'&quicktime='+pmv_qt+'&realplayer='+pmv_rea;
	src += '&pdf='+pmv_pdf+'&windowsmedia='+pmv_wma+'&java='+pmv_jav+'&ref='+escape(rtu);

	//pmv_do.writeln('<img src="'+src+'" alt="phpMyVisites" style="border:0" />');
	WriteLayer("stats","",'<img src="'+src+'" alt="phpMyVisites" style="border:0" />');
}
// pmv_log(phpmyvisitesSite, phpmyvisitesURL, pagename, a_vars);

// *********************
// CyberEstat
cSilex.prototype.logCyberEstat=function($sectionName)
{
	/*
	<img src="http://stat3.cybermonitor.com/france5_v?R=france5_education_section1_rubrique1&S=total;france5_education_section1">
	*/
//	var _str='<script language="javascript">CM_CLIENT = "'+this.CM_CLIENT+'";CM_SECTION1 = "'+this.CM_SECTION1+'";CM_RUBRIQUE = "'+this.CM_SECTION1+'_'+$sectionName+'";alert("CyberEstat in the frame");</script><script language="javascript" src="http://js.cybermonitor.com/france5.js"></script>';
//	var _str='<img src="http://forum.telecharger.01net.com/data/globaldata/avatars/119178.gif" />';
	var _str='<img src="http://stat3.cybermonitor.com/'+this.CM_CLIENT+'_v?R='+this.CM_SECTION1+'_'+$sectionName+'&S=total;'+this.CM_SECTION1+'">';
	trace("CyberEstat : "+_str);
	WriteLayer("CyberEstat","",_str);
}

// get/set hash value
// in safari it does not contains the #
function setUrlHash($sectionName)
{
	// detect if browser = safari then don't add the '#'
	if (navigator.userAgent.indexOf('Safari') != -1)
		top.location.hash=$sectionName;
	else
		top.location.hash="#"+$sectionName;
	//alert(top.location);
}
function getUrlHash()
{
	// detect if browser = safari then don't add the '#'
/*	if (navigator.userAgent.indexOf('Safari') != -1)
		return top.location.hash;
	else*/
		return top.location.hash.toString().substring(1);
	//alert(top.location);
}

// *******************
function trace(aMessage) 
{
//  var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
//  consoleService.logStringMessage("SILEX - " + aMessage);
//	Components.utils.reportError("SILEX - " + aMessage);
	// window.dump("SILEX - " + aMessage);
	// toJavaScriptConsole();
	//alert("SILEX - " + aMessage);
}
