/*
 * CDR.js
 * 
 * Launch the page
 * 
 */

var CDR = new Class({
	
    /*
     * Main object
     * Manage all the page
     */
    initialize: function()
    {
    	if(arguments.length==1 && arguments[0]=='debug')
    		this.bDebug = true;
    	else
    		this.bDebug = false;
    	
    	this.nLoadCounter		= 0;
    	this.bCDRReady			= false;
    	this.oSearchEngine		= false;
    	this.aLoadedFiles 		= ['Config.js',
						    	'Utils.js',
						    	'SearchEngine.js',
						    	'rate.js'
								];
		this.bPreventCache		= false;		//Avoid Cache Problems during loading
    },
    
    /*
     * Load JavaScript files
     */
    DoDynamicLoading: function()
    {	
    	for(var i=0;i< this.aLoadedFiles.length;i++)
    	{
  	   		var sFile = '/Js/'+this.aLoadedFiles[i];
  	   		if(this.bPreventCache)
  	   			sFile +='?t='+$time();
  	   		if(!window.ie)
	  	   		new Asset.javascript(sFile,{onLoad:'oCDR.CheckDynamicLoading(\''+sFile+'\')',onFailure:this.OnDynamicLoadingFailure});
	  	   	else
	  	   	{
				//BUG MOOTOOLS && Classe Asset :  http://forum.mootools.net/viewtopic.php?id=3297 cf post digitarald
		  	   	try
		  	   	{
			  	   	var myAjax = new Ajax(sFile, {method: 'get',onFailure:this.OnDynamicLoadingFailure,evalScripts:true,async: false});
					myAjax.request();
					//Request are synchronous so we are sure that the file is loaded
					oCDR.CheckDynamicLoading(sFile);
		  	   	}
		  	   	catch(e)
		  	   	{
		  	   		alert(e.message);
		  	   	}
	  	   	}
	   	}
    },

    /*
     * Called when a problem occurs while loading
     */
    OnDynamicLoadingFailure: function()
    {
		alert('Unable to complete loading');
		var messageBox 	= new MessageBox();
		messageBox.text = "Le chargement du portail a &eacute;chou&eacute;";
    },
    
    /*
     * Wait for Loading and Run()
     */
    CheckDynamicLoading: function(sFile)
    {
    	this.nLoadCounter++;
    	if(this.nLoadCounter == this.aLoadedFiles.length)
    	{
    		this.bCDRReady=true;
    		this.Run();
    	}	
    },
    
    /*
     * Launch every object on the web page
     */
    Run: function()
    {
		this.oSearchEngine = new SearchEngine();
		//$('engine').style.display='block';
		
    	//new Effect.Appear('engine',{afterFinish:Focus});
    	if($('top_promo'))
	    	this.BuildSlideShow();
    },
    
   
	/*
	 * Clean the web page
	 */
    Close: function()
    {
    },
    
	addFav : function()
	{ 
		sUrl=SITE_URL;
		sTitle=SITE_FAV_TITLE;
		if (navigator.appName != 'Microsoft Internet Explorer')
             window.sidebar.addPanel(sTitle,sUrl,sTitle);
        else
             window.external.AddFavorite(sUrl,sTitle);
        return false;
	},
	
	BuildSlideShow:function()
    {
        this.nSlideCounter=0;
        this.scroll = new Fx.Scroll('top_promo', {
                wait: false,
                duration: 1000,
                offset: { 'x':0,'y': 0},
                transition: Fx.Transitions.Quad.easeInOut
            });
        this.nTimerId=this.SlideShow.periodical(4000,this);
    },
    
    SlideShow : function()
    {
        var sScrollTo = "top_promo_"+this.nSlideCounter;
        if($(sScrollTo))
			this.scroll.toElement($(sScrollTo));
        
        this.nSlideCounter++;
        if(this.nSlideCounter>=4)
            this.nSlideCounter=0;
    }
	

    
});