﻿var repAnim = "";

Container = function() 
{
		this.control;
		this.currentPage;
		this.oldPage;
		this.classNamePage;
		this.classPage;
		this.tabPage = new Array();
		this.currentIdPage = 0;
}

Container.prototype =
{
	handleLoad: function(control, userContext, rootElement)
	{	
		this.currentPage = null;
		this.oldPage = null;
		this.classNamePage = null;
		this.classPage = null;
		this.currentIdPage = 0;
		
		this.control = control;
		this.container = this.control.content.findName("container");
		
		// preloading
        this._clipChargement = this.control.content.findName("chargement");
		this.control.content.findName("barreChargement").width = 0;
		this._clipChargement.visibility = "Visible";
		
		this.initActionFleches();

		//page à charger par défaut
	    this.getPage(this.tabPage[this.currentIdPage][0], this.tabPage[this.currentIdPage][1]);
	    

	
	},
	//------------------------------------------------------------------
	nextPage: function(){
		this.currentIdPage++;
		
		if(this.currentIdPage == this.tabPage.length-1) this.flecheDroite.visibility = "Collapsed";
		if(this.currentIdPage >= this.tabPage.length){
		 this.currentIdPage = 0;
		 this.flecheGauche.visibility = "Collapsed";
		 this.flecheDroite.visibility = "Visible";
		}
		if(this.currentIdPage > 0 ){
			 this.flecheGauche.visibility = "Visible";
		}
		
	    this.getPage(this.tabPage[this.currentIdPage][0], this.tabPage[this.currentIdPage][1]);
	},
	//------------------------------------------------------------------
	addPage: function(tab){
		this.tabPage.push(tab);
	},
	//------------------------------------------------------------------
	getPage: function(urlToload, className) 
	{
		
		this.urlPageToload = urlToload;
		this.classNamePage = className;
		this.oldPage = this.currentPage;
		
		this.control.content.findName("flechesContainer").visibility = "Collapsed";
			
		// on retire l'ancienne page
		if(this.oldPage != null)
		{
		  if(this.classPage.close != null){
		  	this.classPage.close(  Silverlight.createDelegate(this, this.onClosePageFinish));
		  }else{
	    	this.container.children.remove(this.oldPage);
	    	this.loadPage();
		  }
		}else{
	    	this.loadPage();
		}
			
	},
	//------------------------------------------------------------------
	onClosePageFinish: function(sender, args) 
	{
	   this.container.children.remove(this.oldPage);
	   this.loadPage();
	},
	//------------------------------------------------------------------
	loadPage: function() 
	{
		
		
		//affichage progress chargement
        this._clipChargement = this.control.content.findName("chargement");
		this.control.content.findName("barreChargement").width = 0;
		this._clipChargement.visibility = "Visible";
		
	    var downloader = this.control.createObject("downloader");
	    downloader.addEventListener("completed", Silverlight.createDelegate(this, this.onCompletedPage));
	    downloader.addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, this.onProgressChanged));
	    downloader.addEventListener("DownloadFailed", Silverlight.createDelegate(this, this.onDownloadFailed));
	    downloader.open("GET", repAnim + this.urlPageToload);
	    downloader.send();
	},
	//------------------------------------------------------------------
	onCompletedPage: function(sender, eventArgs) 
	{
		this._clipChargement.visibility = "Collapsed";
		this.control.content.findName("flechesContainer").visibility = "Visible";
		
	    // on récupère le xaml
	    var myXaml = sender.getResponseText("anim.xaml");
	    
	    // creation de l'objet a partir du xaml
	    this.currentPage = this.control.content.createFromXaml(myXaml);
	    
	    //on récupère le code js et on instancie la classe de la page
	   		eval(sender.getResponseText(this.classNamePage + ".js"));
	   		this.classPage = eval("new " + this.classNamePage + "();");
	   		this.classPage.init(sender, this.currentPage);
	    	this.container.children.add(this.currentPage);
	   		this.classPage.display();
	   		
	    this.timer = setTimeout( Silverlight.createDelegate(this, this.nextPage), timerPageValue);
	   		

	},
	//------------------------------------------------------------------
	onImageFailed: function(sender, eventArgs){
		
	},
	//------------------------------------------------------------------
	onDownloadFailed: function(sender, eventArgs){
		
	},
	//------------------------------------------------------------------
	onProgressChanged: function(sender, eventArgs) 
	{
		var pourcentage = Math.round(sender.downloadProgress*100);
		this.control.content.findName("pourcentage").Text = "" + pourcentage + " %";
		this.control.content.findName("barreChargement").width = sender.downloadProgress*this.control.content.findName("contourChargement").width;
	},
		
		
	//------------------------------------------------------------------
	initActionFleches: function() 
	{
		this.flecheDroite = this.control.content.findName("flecheDroite");
		this.flecheGauche = this.control.content.findName("flecheGauche");
		this.flecheGauche.Cursor = "Hand";
		this.flecheDroite.Cursor = "Hand";
		this.flecheGauche.visibility = "Collapsed";
		
		this.flecheDroite.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.flecheDroiteOver));
		this.flecheGauche.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.flecheGaucheOver));
		
		this.flecheDroite.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.flecheDroiteOut));
		this.flecheGauche.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.flecheGaucheOut));
		
		this.flecheDroite.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.flecheDroiteClick));
		this.flecheGauche.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.flecheGaucheClick));
	},
	//------------------------------------------------------------------
	flecheDroiteOver: function(sender, eventArgs) 
	{
		this.control.content.findName("flecheDroiteOver").Begin();
	},
	//------------------------------------------------------------------
	flecheGaucheOver: function(sender, eventArgs) 
	{
		this.control.content.findName("flecheGaucheOver").Begin();
	},
	//------------------------------------------------------------------
	flecheDroiteOut: function(sender, eventArgs) 
	{
		this.control.content.findName("flecheDroiteOut").Begin();
	},
	//------------------------------------------------------------------
	flecheGaucheOut: function(sender, eventArgs) 
	{
		this.control.content.findName("flecheGaucheOut").Begin();
	},
	//------------------------------------------------------------------
	flecheDroiteClick: function(sender, eventArgs) 
	{
		this.flecheDroiteOut(sender, eventArgs);
		clearTimeout(this.timer);
		this.currentIdPage++;
		if(this.currentIdPage >= this.tabPage.length-1) this.flecheDroite.visibility = "Collapsed";
		
		this.flecheGauche.visibility = "Visible";
		
	    this.getPage(this.tabPage[this.currentIdPage][0], this.tabPage[this.currentIdPage][1]);
	},
	//------------------------------------------------------------------
	flecheGaucheClick: function(sender, eventArgs) 
	{
		this.flecheGaucheOut(sender, eventArgs);
		clearTimeout(this.timer);
		this.currentIdPage--;
		if(this.currentIdPage <= 0) this.flecheGauche.visibility = "Collapsed";
		
		this.flecheDroite.visibility = "Visible";
		
	    this.getPage(this.tabPage[this.currentIdPage][0], this.tabPage[this.currentIdPage][1]);
	}
	
	
	
}




